001    /***************************************************************************************************
002    *               MODULE DESCRIPTION
003    ****************************************************************************************************
004    *
005    *               NAME:           KiuruHandler.java
006    *               LANGUAGE:       Java2
007    *               DATE:           2.12.2002
008    *               AUTHOR:         Miika Nurminen, Jyväskylän yliopisto
009    *
010    ****************************************************************************************************
011    *               COPYRIGHT (C) Kiuru-projektiryhmä
012    *               Limited rights granted. Please refer to license
013    ****************************************************************************************************
014     
015    ****************************************************************************************************
016    *               UPDATES
017    ****************************************************************************************************
018    *
019    *   2.12.2002 - Initial version / mn
020    *
021    ****************************************************************************************************/
022    package kiurubeans;
023    
024    import java.beans.*;
025    import kotkabeans.*;
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * General-purpose handler bean. Support for User and Error included. 
030     * <p>
031     * This should be used as a superclass for all Handler Beans.
032     *
033     * @author Miika Nurminen
034     */
035    public abstract class KiuruHandler extends Object implements EnumType {
036    
037    // Attributes 
038    
039      /** Default action for all handlers.  */
040      public static final int NO_ACTION=0;
041    
042      /** User data. Includes support for translation operations. */
043      private User user;
044      /** Error handling. This should be imported from JSP page. */
045      private kotkabeans.Error error;
046      /** Determines bean state */
047      private String actionType = "addNew";
048    
049      /** Indicates if user-entered properties are syntactically correct */
050      private boolean ok = true;
051    
052      /** Bean state */
053      private int enumState=NO_ACTION;
054      
055      
056    // Constructors
057      
058      /** Dummy constructor for KiuruHandler */
059      public KiuruHandler() {
060      }
061      
062      /**
063       * Dummy constructor with error.
064       *
065       * @param error error object for this instance.
066       */  
067      public KiuruHandler(kotkabeans.Error error) {
068        setError(error);
069      }
070      
071    // Access methods
072    
073      /** 
074       * Returns current max state value. Inherited classes may override this
075       * when new states are introduced.
076       *
077       * @return max state value
078       */
079      protected int getMaxState() {
080        return 0;
081      }
082    
083      
084      /**
085       * Returns current state.
086       *
087       * @return current state.
088       */
089      public int getEnumState() {
090        return enumState;
091      }
092      
093      /** 
094       * Sets current state.
095       *
096       * @param state constant.
097       * @throws IllegalArgumentException if state is not valid.
098       */
099      public void setEnumState(int state) {
100        if ((state<0) || (state>getMaxState())) 
101          throw new IllegalArgumentException("Illegal state argument "+state+"!");
102        enumState=state;
103      }
104    
105      
106      /**
107       * Returns current user.
108       * @return current user
109       */  
110      public User getUser() { return user; }
111      
112      /**
113       * Sets current user.
114       * @param user user to be set
115       */  
116      public void setUser(User user) { this.user=user; }
117    
118      /**
119       * Returns current error object.
120       * @return current error
121       */  
122      public kotkabeans.Error getError() { return error; }
123      
124      /**
125       * Sets current error object.
126       * @param error error to be set
127       */  
128      public void setError(kotkabeans.Error error) {  this.error=error; }
129    
130      /**
131       * Returns if user-entered properties are ok.
132       * @return if user-entered properties are ok
133       */  
134      public boolean getOk() { return ok; }
135      
136      /**
137       * OK may be false when processing invalid input values 
138       * for entity attributes or search parameters.
139       * @param b value for Ok
140       */  
141      public void setOk(boolean b) {
142        this.ok = b;
143        if (this.ok) 
144          error.getErrors(); // this clears Error.
145      }
146    
147    
148    
149    // Operations
150      
151      /** 
152       * Returns user's language id as String. If user is not assigned, returns 1.
153       * works even if user is not set.
154       *
155       * @return user's language id.
156       */
157      protected String getLangId() {
158        int langid = 1;
159        if (getUser()!=null) 
160          langid=getUser().getLanguageID();
161        return Integer.toString(langid);
162      }
163      
164      /** 
165       * Adds string to errors to be displayed.
166       *
167       * @param s Error string
168       */
169      protected void addError(String s) {
170        if (getError()==null) throw new IllegalStateException("Error must be set for this method!");
171        getError().setError(s);
172      }
173      
174      /**
175       * Add string to notices to be displayed.
176       *
177       * @param s Notice string
178       */  
179      protected void addNotice(String s) {
180        if (getError()==null) throw new IllegalStateException("Error must be set for this method!");
181        getError().setNotice(s);
182      }
183    
184      /**
185       * Performs action based on actionType. Called from performAction.
186       * Descendant classes should override this and call the same method
187       * in inherited class as default.
188       *
189       * @throws Exception If something went wrong during actual executed action.
190       * @see #performAction
191       */  
192      protected abstract void defaultAction() throws java.lang.Exception;
193      
194      /** 
195       * Performs action based on actionType (eg. addNew, modify, post, delete)
196       * 
197       * @return true if action was successful, false otherwise
198       */
199      public boolean performAction() {
200        kotkabeans.Log.log("now performing action"+getEnumState());
201        try  {
202          switch (getEnumState()) {
203            case NO_ACTION:
204            break;
205            default:
206              defaultAction();
207            break;
208          }
209        }
210        catch (Exception e) {
211          kotkabeans.Log.log("Exception occured while performing action "+getEnumState()+"! "+e.getMessage());
212          return false;
213        }
214        return true;
215      }
216      
217      /** 
218       * Implementation of clearing action state. Inherited classes should always override this.
219       *
220       * @param request Http request used for parameter parsing.
221       */
222      protected abstract void doClearActionState(javax.servlet.http.HttpServletRequest request);
223    
224      /**
225       * Implementation for clearing empty parameters. Descendant classes override this.
226       *
227       * @param request Http request used for parameter parsing.
228       */  
229      protected abstract void doClearEmptyParameters(javax.servlet.http.HttpServletRequest request);
230        
231      /** 
232       * Ensures bean state is reseted when editing a new record.
233       * <p>
234       * Method is called in the beginning of UI (not Handler!) JSP page. 
235       * State reset may depend on request parameters.
236       *
237       * @param request HTTP request with parameters
238       * @see #doClearActionState(HttpServletRequest)
239       */
240      public void resetActionState(HttpServletRequest request) {
241        if (request==null) {
242          throw new IllegalArgumentException("Request must be set!");
243        }
244        kotkabeans.Log.log("Clearing action state");
245        doClearActionState(request); // clears action state, may be based on request params.
246      }
247    
248      
249      /**
250       * Ensures empty request parameters are clearer when entity is posted.
251       * <p>
252       * If bean is used in a JSP, this should be called in the beginning of page.
253       * If form content is "" or null it is not sent via HTTP, so those fields
254       * must be cleared manually. Method may be called from a handler or UI page.
255       * Descendant classes should redefine conditions for calling 
256       * doClearEmpty parameters method.
257       *
258       * @param request HTTP request with parameters
259       * @see #doClearEmptyParameters(HttpServletRequest)
260       */
261      public void clearEmptyParameters(HttpServletRequest request) {
262        if (request==null) {
263          throw new IllegalArgumentException("Request must be set!");
264        }
265        /* inherited classes should redefine conditions for calling 
266           doClearEmpty parameters */
267      }  
268    
269      /** 
270       * Returns errorstring (if exists) and resets ok-field & actiontype.
271       * Usually called in the beginning of a page.
272       * <br />
273       * Note. resetErrorState & ResetActionState should not be merged.
274       * ResetActionState is called only on UI pages.
275       *
276       * @return errorstring. Usually not needed. shared include file showerrorblock shows error in JSP page.
277       */
278      public String resetErrorState() {
279        if (!getOk()) {
280            setEnumState(NO_ACTION); // if field values are not ok, we won't update them. adjusting existing values
281        }
282        String result=getError().getErrors(); // clears error, deprecated by include showerrorblock
283        setOk(true);
284        return result;
285      }
286    
287    }
288    /***************************************************************************************************
289     *               COPYRIGHT (C) KIURU -PROJECT GROUP
290     *               Limited rights granted. Please refer to license.
291     **************************************************************************************************/