001 /*************************************************************************************************** 002 * MODULE DESCRIPTION 003 **************************************************************************************************** 004 * 005 * NAME: EntityHandler.java 006 * LANGUAGE: Java2 007 * DATE: 27.3.2003 008 * AUTHOR: Miika Nurminen, University of Jyväskylä 009 * 010 **************************************************************************************************** 011 * COPYRIGHT (C) KIURU -PROJECT GROUP 012 * Limited rights granted. Please refer to license 013 **************************************************************************************************** 014 * 015 **************************************************************************************************** 016 * UPDATES 017 **************************************************************************************************** 018 * 019 * 27.3.2003: Initial release / mn 020 * 021 ****************************************************************************************************/ 022 023 package kiurubeans; 024 import javax.servlet.http.HttpServletRequest; 025 026 /** 027 * Representation of a database entity. 028 * @author Miika Nurminen 029 */ 030 public abstract class EntityHandler extends KiuruHandler { 031 032 // Attributes 033 034 /** Updates attributes from database based on current ID information */ 035 public static final int GET_RECORD=1; 036 037 /** Clears attributes. Used when adding a new record */ 038 public static final int RESET_RECORD=2; 039 040 /** Deletes record from database & clears attributes */ 041 public static final int DELETE_RECORD=3; 042 043 /** Saves current record from database, provided that attributes are ok. */ 044 public static final int POST_RECORD=4; 045 046 047 048 // Constructors 049 /** 050 * Dummy empty constructor. 051 */ 052 public EntityHandler() { 053 } 054 055 056 // Access methods 057 058 /** 059 * Returns current max state value. Inherited classes may override this 060 * when new states are introduced. 061 * 062 * @return max state value 063 */ 064 protected int getMaxState() { 065 return 4; 066 } 067 068 // Operations 069 070 /** 071 * Implementation of modify rights checking. 072 * 073 * @return true is user can modify entity. 074 * @see #hasModifyRight() 075 */ 076 protected abstract boolean doCheckModifyRight(); 077 078 /** 079 * Returns true if current user has full rights to modify current entity. 080 * Descendant classes may introduce more detailed modification constraints. 081 * <p> 082 * Note! User must be set when calling this method. 083 * 084 * @return true is user can modify entity 085 * @see #doCheckModifyRight() 086 */ 087 public boolean hasModifyRight() { 088 if (getUser()==null) throw new IllegalStateException("User is not assigned!"); 089 return doCheckModifyRight(); 090 } 091 092 093 /** 094 * Performs action based on actionType. Called from performAction. 095 * Descendant classes should override this and call the same method 096 * in inherited class as default. 097 * 098 * @throws Exception If something went wrong during actual executed action. 099 * @see KiuruHandler#performAction() 100 */ 101 protected void defaultAction() throws java.lang.Exception { 102 switch (getEnumState()) { 103 case GET_RECORD: 104 getRecord(); 105 break; 106 case RESET_RECORD: 107 resetRecord(); 108 break; 109 case DELETE_RECORD: 110 deleteRecord(); 111 break; 112 default: 113 postRecord(); 114 break; 115 } 116 } 117 118 119 /** 120 * Retrieves record from database according to current Id field(s) 121 * value(s). 122 * <p> 123 * Id field(s) are defined in descendant classes. Method implements 124 * also user rights check, eg. if user has no right to retrieve 125 * the record, empty object is returned instead. 126 * 127 * @throws Exception if there is a problem with DB connection 128 */ 129 public abstract void getRecord() throws Exception; 130 131 /** 132 * Clears field values. 133 * <p> 134 * Result should be equivalent to constructing a new object. 135 * Nothing is saved to database. 136 */ 137 public abstract void resetRecord(); 138 139 /** 140 * Deletes current record from database. 141 * <p> 142 * Actually applies deleted=true. Object is cleared after deletion. 143 * 144 * @throws Exception if there is a problem with DB connection 145 */ 146 public abstract void deleteRecord() throws Exception; 147 148 /** 149 * Saves record to database. 150 * <p> 151 * If field values are not valid, sets Ok property to false. 152 * 153 * @throws Exception if there is a problem with DB connection 154 */ 155 public abstract void postRecord() throws Exception; 156 157 /** Default implementation of clearing action state. Descendant classes may 158 * override this. 159 * @see #resetActionState(HttpServletRequest) 160 * @param request The request-object of the JSP. 161 */ 162 protected void doClearActionState(HttpServletRequest request) { 163 if (getEnumState()==RESET_RECORD || getEnumState()==NO_ACTION) 164 return; 165 setEnumState(GET_RECORD); 166 } 167 168 /** Returns if entity is in a state of adding a new record. 169 * Implementation depends of a descendand class. 170 * @return True if the record doesn't exist in the database, yet 171 */ 172 public abstract boolean getAddingNew(); 173 174 /** Ensures empty request parameters are clearer when entity is posted. 175 * <p> 176 * If bean is used in a JSP, this should be called in the beginning of page. 177 * If form content is "" or null it is not sent via HTTP, so those fields 178 * must be cleared manually. Method may be called from a handler or UI page. 179 * 180 * @param request HTTP request with parameters 181 * @see #doClearEmptyParameters(HttpServletRequest) 182 */ 183 public void clearEmptyParameters(HttpServletRequest request) { 184 super.clearEmptyParameters(request); 185 if (getEnumState()==POST_RECORD) { //or: action!= NO_ACTION? no (continueSearch) 186 kotkabeans.Log.log("Clearing empty parameters"); 187 doClearEmptyParameters(request); 188 } 189 } 190 191 } 192 /*************************************************************************************************** 193 * COPYRIGHT (C) KIURU -PROJECT GROUP 194 * Limited rights granted. Please refer to license. 195 **************************************************************************************************/