KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > dbobj > ValidationQueue


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.dbobj;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.dbobj.RequestContext;
69 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
70 import com.jcorporate.expresso.core.dbobj.ValidValue;
71 import com.jcorporate.expresso.core.misc.DateTime;
72
73 import java.util.Hashtable JavaDoc;
74 import java.util.Iterator JavaDoc;
75 import java.util.Vector JavaDoc;
76
77
78 /**
79  * This class stores each validation entry
80  *
81  * @author Shash Chatterjee
82  */

83 public class ValidationQueue
84         extends SecuredDBObject {
85     private Hashtable JavaDoc valuesCoded = null;
86     private Vector JavaDoc myParams = null;
87     public static final String JavaDoc FLD_ID = "ValID";
88     public static final String JavaDoc FLD_STATUS_CODE = "StatusCode";
89     public static final String JavaDoc FLD_ADDED_ON = "AddedOn";
90     public static final String JavaDoc FLD_EXPIRES_AT = "ExpiresAt";
91     public static final String JavaDoc FLD_PROCESSED_ON = "ProcessedOn";
92     public static final String JavaDoc FLD_PROCESSED_BY = "ExpUid";
93     public static final String JavaDoc FLD_VAL_CODE = "ValCode";
94     public static final String JavaDoc FLD_VAL_HANDLER = "ValHandler";
95     public static final String JavaDoc JOB_STATUS_NEW = "N";
96     public static final String JavaDoc JOB_STATUS_AVAILABLE = "A";
97     public static final String JavaDoc JOB_STATUS_WAIT = "W";
98     public static final String JavaDoc JOB_STATUS_VALIDATED = "V";
99     public static final String JavaDoc JOB_STATUS_DENIED = "D";
100     public static final String JavaDoc JOB_STATUS_EXPIRED = "E";
101
102     /**
103      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
104      */

105     public ValidationQueue()
106             throws DBException {
107         super();
108     }
109
110     /**
111      * For using DBObjects within Controllers. Initializes based upon the current
112      * user and the requested db. [Of course this can be modified later]
113      *
114      * @param request - The controller request handed to you by the framework.
115      * @throws DBException upon initialization error
116      */

117     public ValidationQueue(RequestContext request)
118             throws DBException {
119         super(request);
120     }
121
122     /**
123      * Use over (String) constructor. Initializes the object in the context
124      * of the user who's uid belongs to the parameter.
125      *
126      * @param uid id of the user context
127      * @throws DBException if there's an initialization problem
128      */

129     public ValidationQueue(int uid)
130             throws DBException {
131         super(uid);
132     }
133
134     /**
135      * Extends the usual add method to fetch a next number field
136      * and set the Updated flag to Y
137      *
138      * @throws DBException If the next number cannot be determined or
139      * the add fails
140      */

141     public void add()
142             throws DBException {
143         if (getField(FLD_STATUS_CODE).equals("")) {
144             setField(FLD_STATUS_CODE, JOB_STATUS_NEW);
145         }
146
147         setField(FLD_ADDED_ON, DateTime.getDateTimeForDB(this.getDataContext()));
148         super.add();
149     }
150
151     /**
152      * Extends the checkAllRefs method to check for valid job queue entry
153      *
154      * @throws DBException If a referential integrity violation is found
155      */

156     protected void checkAllRefs()
157             throws DBException {
158         checkRef(FLD_PROCESSED_BY, new DefaultUserInfo(), // todo why not use userinfo specified by expresso-config.xml, as in new User().getUserInfo() ?
159
"Invalid " + getString(getMetaData().getDescription(FLD_PROCESSED_BY)));
160     }
161
162     /**
163      * Extend the normal find method to read the parameters once the find
164      * is done.
165      *
166      * @return boolean
167      */

168     public boolean find()
169             throws DBException {
170         boolean res = super.find();
171         readParams();
172
173         return res;
174     }
175
176     /**
177      * Get the job queue parameters associated with this queue entry
178      *
179      * @return Vector A vector of JobQueueParam objects for this entry
180      * @throws DBException If the job queue entry has not yet been retrieved
181      */

182     public synchronized Vector JavaDoc getParams()
183             throws DBException {
184         if (myParams == null) {
185             throw new DBException("Parameters not available until job queue entry has " +
186                     "been retrieved");
187         }
188
189         return (Vector JavaDoc) myParams.clone();
190     }
191
192     /**
193      * Get the parameter value for the named parameter code
194      *
195      * @param paramCode Code for which we want the value
196      * @return String The parameter value
197      * @throws DBException If the paramter value cannot be retrieved
198      */

199     public synchronized String JavaDoc getParamValue(String JavaDoc paramCode)
200             throws DBException {
201         if (valuesCoded == null) {
202             readParams();
203         }
204
205         return (String JavaDoc) valuesCoded.get(paramCode);
206     }
207
208     /**
209      * Returns true if the validation queue entry has expired by checking the
210      * expires at field. If that field is null, then this function will
211      * return false.
212      *
213      * @return true if the validation queue entry is expired.
214      */

215     public boolean isExpired() throws DBException {
216         java.util.Date JavaDoc dt = this.getFieldDate(FLD_EXPIRES_AT);
217         if (dt == null) {
218             return false;
219         }
220         return (dt.getTime() < System.currentTimeMillis());
221     }
222
223     /**
224      * Override the method getValidValues to provide specific values for our
225      * multi-valued fields
226      *
227      * @param fieldName Field name for which values are requested
228      * @return Vector The ValidValues field
229      * @throws DBException If the values cannot be retrieved
230      */

231     public Vector JavaDoc getValidValues(String JavaDoc fieldName)
232             throws DBException {
233         if (fieldName.equals(FLD_STATUS_CODE)) {
234             Vector JavaDoc myValues = new Vector JavaDoc(4);
235             myValues.addElement(new ValidValue(JOB_STATUS_NEW, "New"));
236             myValues.addElement(new ValidValue(JOB_STATUS_AVAILABLE,
237                     "Available"));
238             myValues.addElement(new ValidValue(JOB_STATUS_WAIT,
239                     "Waiting For Validation"));
240             myValues.addElement(new ValidValue(JOB_STATUS_VALIDATED,
241                     "Validated"));
242             myValues.addElement(new ValidValue(JOB_STATUS_DENIED, "Denied"));
243             myValues.addElement(new ValidValue(JOB_STATUS_EXPIRED, "Expired"));
244
245             return myValues;
246         }
247
248         return super.getValidValues(fieldName);
249     }
250
251     /**
252      * Read all of the ValidationQueueParam entries associated with this job
253      *
254      * @throws DBException If the entries cannot be read
255      */

256     private void readParams()
257             throws DBException {
258         myParams = new Vector JavaDoc(3);
259         valuesCoded = new Hashtable JavaDoc(3);
260
261         ValidationQueueParam paramList = new ValidationQueueParam(SecuredDBObject.SYSTEM_ACCOUNT);
262         paramList.setDataContext(getDataContext());
263         paramList.setField(ValidationQueueParam.FLD_QUEUE_ID, getField(FLD_ID));
264
265         for (Iterator JavaDoc e = paramList.searchAndRetrieveList(ValidationQueueParam.FLD_PARAM_NUM).iterator();
266              e.hasNext();) {
267             ValidationQueueParam oneParam = (ValidationQueueParam) e.next();
268             myParams.addElement(oneParam);
269             valuesCoded.put(oneParam.getField(ValidationQueueParam.FLD_PARAM_CODE),
270                     oneParam.getField(ValidationQueueParam.FLD_PARAM_VAL));
271         }
272     }
273
274     /**
275      * Extend the normal retrieve method to read the parameters
276      * after the record is retrieved
277      *
278      * @throws DBException If the parameters or the entry cannot be retrieved
279      */

280     public void retrieve()
281             throws DBException {
282         super.retrieve();
283         readParams();
284     }
285
286     /**
287      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
288      */

289     protected synchronized void setupFields()
290             throws DBException {
291         setTargetTable("VALQUEUE");
292         setDescription("DBvalidQueue");
293         setCharset("ISO-8859-1");
294         addField(FLD_ID, "auto-inc", 0, false, "entryNumber");
295         addField(FLD_STATUS_CODE, "char", 1, false, "statusCode");
296         addField(FLD_ADDED_ON, "datetime", 0, false, "addedOn");
297         addField(FLD_EXPIRES_AT, "datetime", 0, false, "expiresAt");
298         addField(FLD_PROCESSED_ON, "datetime", 0, true, "processedOn");
299         addField(FLD_PROCESSED_BY, "int", 0, true, "processedBy");
300         addField(FLD_VAL_CODE, "varchar", 255, false, "valSecurityCode");
301         addField(FLD_VAL_HANDLER, "varchar", 255, false, "valHandlerClass");
302         addKey(FLD_ID);
303         setMultiValued(FLD_STATUS_CODE);
304         setReadOnly(FLD_ID);
305         setReadOnly(FLD_ADDED_ON);
306         setStringFilter(FLD_VAL_CODE, "rawFilter");
307         setStringFilter(FLD_VAL_HANDLER, "stripFilter");
308         setLookupObject(FLD_PROCESSED_BY,
309                 com.jcorporate.expresso.services.dbobj.DefaultUserInfo.class.getName());
310         addDetail(com.jcorporate.expresso.services.dbobj.ValidationQueueParam.class.getName(),
311                 FLD_ID, ValidationQueueParam.FLD_QUEUE_ID);
312     }
313 }
Popular Tags