001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.dbobj;
066:
067: import com.jcorporate.expresso.core.db.DBException;
068: import com.jcorporate.expresso.core.dbobj.RequestContext;
069: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
070: import com.jcorporate.expresso.core.dbobj.ValidValue;
071: import com.jcorporate.expresso.core.misc.DateTime;
072:
073: import java.util.Hashtable;
074: import java.util.Iterator;
075: import java.util.Vector;
076:
077: /**
078: * This class stores each validation entry
079: *
080: * @author Shash Chatterjee
081: */
082: public class ValidationQueue extends SecuredDBObject {
083: private Hashtable valuesCoded = null;
084: private Vector myParams = null;
085: public static final String FLD_ID = "ValID";
086: public static final String FLD_STATUS_CODE = "StatusCode";
087: public static final String FLD_ADDED_ON = "AddedOn";
088: public static final String FLD_EXPIRES_AT = "ExpiresAt";
089: public static final String FLD_PROCESSED_ON = "ProcessedOn";
090: public static final String FLD_PROCESSED_BY = "ExpUid";
091: public static final String FLD_VAL_CODE = "ValCode";
092: public static final String FLD_VAL_HANDLER = "ValHandler";
093: public static final String JOB_STATUS_NEW = "N";
094: public static final String JOB_STATUS_AVAILABLE = "A";
095: public static final String JOB_STATUS_WAIT = "W";
096: public static final String JOB_STATUS_VALIDATED = "V";
097: public static final String JOB_STATUS_DENIED = "D";
098: public static final String JOB_STATUS_EXPIRED = "E";
099:
100: /**
101: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
102: */
103: public ValidationQueue() throws DBException {
104: super ();
105: }
106:
107: /**
108: * For using DBObjects within Controllers. Initializes based upon the current
109: * user and the requested db. [Of course this can be modified later]
110: *
111: * @param request - The controller request handed to you by the framework.
112: * @throws DBException upon initialization error
113: */
114: public ValidationQueue(RequestContext request) throws DBException {
115: super (request);
116: }
117:
118: /**
119: * Use over (String) constructor. Initializes the object in the context
120: * of the user who's uid belongs to the parameter.
121: *
122: * @param uid id of the user context
123: * @throws DBException if there's an initialization problem
124: */
125: public ValidationQueue(int uid) throws DBException {
126: super (uid);
127: }
128:
129: /**
130: * Extends the usual add method to fetch a next number field
131: * and set the Updated flag to Y
132: *
133: * @throws DBException If the next number cannot be determined or
134: * the add fails
135: */
136: public void add() throws DBException {
137: if (getField(FLD_STATUS_CODE).equals("")) {
138: setField(FLD_STATUS_CODE, JOB_STATUS_NEW);
139: }
140:
141: setField(FLD_ADDED_ON, DateTime.getDateTimeForDB(this
142: .getDataContext()));
143: super .add();
144: }
145:
146: /**
147: * Extends the checkAllRefs method to check for valid job queue entry
148: *
149: * @throws DBException If a referential integrity violation is found
150: */
151: protected void checkAllRefs() throws DBException {
152: checkRef(FLD_PROCESSED_BY, new DefaultUserInfo(), // todo why not use userinfo specified by expresso-config.xml, as in new User().getUserInfo() ?
153: "Invalid "
154: + getString(getMetaData().getDescription(
155: FLD_PROCESSED_BY)));
156: }
157:
158: /**
159: * Extend the normal find method to read the parameters once the find
160: * is done.
161: *
162: * @return boolean
163: */
164: public boolean find() throws DBException {
165: boolean res = super .find();
166: readParams();
167:
168: return res;
169: }
170:
171: /**
172: * Get the job queue parameters associated with this queue entry
173: *
174: * @return Vector A vector of JobQueueParam objects for this entry
175: * @throws DBException If the job queue entry has not yet been retrieved
176: */
177: public synchronized Vector getParams() throws DBException {
178: if (myParams == null) {
179: throw new DBException(
180: "Parameters not available until job queue entry has "
181: + "been retrieved");
182: }
183:
184: return (Vector) myParams.clone();
185: }
186:
187: /**
188: * Get the parameter value for the named parameter code
189: *
190: * @param paramCode Code for which we want the value
191: * @return String The parameter value
192: * @throws DBException If the paramter value cannot be retrieved
193: */
194: public synchronized String getParamValue(String paramCode)
195: throws DBException {
196: if (valuesCoded == null) {
197: readParams();
198: }
199:
200: return (String) valuesCoded.get(paramCode);
201: }
202:
203: /**
204: * Returns true if the validation queue entry has expired by checking the
205: * expires at field. If that field is null, then this function will
206: * return false.
207: *
208: * @return true if the validation queue entry is expired.
209: */
210: public boolean isExpired() throws DBException {
211: java.util.Date dt = this .getFieldDate(FLD_EXPIRES_AT);
212: if (dt == null) {
213: return false;
214: }
215: return (dt.getTime() < System.currentTimeMillis());
216: }
217:
218: /**
219: * Override the method getValidValues to provide specific values for our
220: * multi-valued fields
221: *
222: * @param fieldName Field name for which values are requested
223: * @return Vector The ValidValues field
224: * @throws DBException If the values cannot be retrieved
225: */
226: public Vector getValidValues(String fieldName) throws DBException {
227: if (fieldName.equals(FLD_STATUS_CODE)) {
228: Vector myValues = new Vector(4);
229: myValues.addElement(new ValidValue(JOB_STATUS_NEW, "New"));
230: myValues.addElement(new ValidValue(JOB_STATUS_AVAILABLE,
231: "Available"));
232: myValues.addElement(new ValidValue(JOB_STATUS_WAIT,
233: "Waiting For Validation"));
234: myValues.addElement(new ValidValue(JOB_STATUS_VALIDATED,
235: "Validated"));
236: myValues.addElement(new ValidValue(JOB_STATUS_DENIED,
237: "Denied"));
238: myValues.addElement(new ValidValue(JOB_STATUS_EXPIRED,
239: "Expired"));
240:
241: return myValues;
242: }
243:
244: return super .getValidValues(fieldName);
245: }
246:
247: /**
248: * Read all of the ValidationQueueParam entries associated with this job
249: *
250: * @throws DBException If the entries cannot be read
251: */
252: private void readParams() throws DBException {
253: myParams = new Vector(3);
254: valuesCoded = new Hashtable(3);
255:
256: ValidationQueueParam paramList = new ValidationQueueParam(
257: SecuredDBObject.SYSTEM_ACCOUNT);
258: paramList.setDataContext(getDataContext());
259: paramList.setField(ValidationQueueParam.FLD_QUEUE_ID,
260: getField(FLD_ID));
261:
262: for (Iterator e = paramList.searchAndRetrieveList(
263: ValidationQueueParam.FLD_PARAM_NUM).iterator(); e
264: .hasNext();) {
265: ValidationQueueParam oneParam = (ValidationQueueParam) e
266: .next();
267: myParams.addElement(oneParam);
268: valuesCoded
269: .put(
270: oneParam
271: .getField(ValidationQueueParam.FLD_PARAM_CODE),
272: oneParam
273: .getField(ValidationQueueParam.FLD_PARAM_VAL));
274: }
275: }
276:
277: /**
278: * Extend the normal retrieve method to read the parameters
279: * after the record is retrieved
280: *
281: * @throws DBException If the parameters or the entry cannot be retrieved
282: */
283: public void retrieve() throws DBException {
284: super .retrieve();
285: readParams();
286: }
287:
288: /**
289: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
290: */
291: protected synchronized void setupFields() throws DBException {
292: setTargetTable("VALQUEUE");
293: setDescription("DBvalidQueue");
294: setCharset("ISO-8859-1");
295: addField(FLD_ID, "auto-inc", 0, false, "entryNumber");
296: addField(FLD_STATUS_CODE, "char", 1, false, "statusCode");
297: addField(FLD_ADDED_ON, "datetime", 0, false, "addedOn");
298: addField(FLD_EXPIRES_AT, "datetime", 0, false, "expiresAt");
299: addField(FLD_PROCESSED_ON, "datetime", 0, true, "processedOn");
300: addField(FLD_PROCESSED_BY, "int", 0, true, "processedBy");
301: addField(FLD_VAL_CODE, "varchar", 255, false, "valSecurityCode");
302: addField(FLD_VAL_HANDLER, "varchar", 255, false,
303: "valHandlerClass");
304: addKey(FLD_ID);
305: setMultiValued(FLD_STATUS_CODE);
306: setReadOnly(FLD_ID);
307: setReadOnly(FLD_ADDED_ON);
308: setStringFilter(FLD_VAL_CODE, "rawFilter");
309: setStringFilter(FLD_VAL_HANDLER, "stripFilter");
310: setLookupObject(
311: FLD_PROCESSED_BY,
312: com.jcorporate.expresso.services.dbobj.DefaultUserInfo.class
313: .getName());
314: addDetail(
315: com.jcorporate.expresso.services.dbobj.ValidationQueueParam.class
316: .getName(), FLD_ID,
317: ValidationQueueParam.FLD_QUEUE_ID);
318: }
319: }
|