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.ExpressoSchema;
068: import com.jcorporate.expresso.core.cache.Cache;
069: import com.jcorporate.expresso.core.cache.CacheManager;
070: import com.jcorporate.expresso.core.cache.CacheSystem;
071: import com.jcorporate.expresso.core.db.DBException;
072: import com.jcorporate.expresso.core.dbobj.DBObject;
073: import com.jcorporate.expresso.core.dbobj.RequestContext;
074: import com.jcorporate.expresso.core.dbobj.Schema;
075: import com.jcorporate.expresso.core.dbobj.SchemaFactory;
076: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
077: import com.jcorporate.expresso.core.dbobj.ValidValue;
078:
079: import java.util.Enumeration;
080: import java.util.Iterator;
081: import java.util.Vector;
082:
083: /**
084: * A DBObjLimit defines the number of records the standard maintenance
085: * servlet will display before it breaks a page. Also controls caching.
086: *
087: * @author Michael Nash
088: */
089: public class DBObjLimit extends SecuredDBObject {
090: private Vector dbObjList = null;
091:
092: public static final String TTL = "TTL";
093: public static final String DB_OBJECT_NAME = "DBObjectName";
094: public static final String PAGE_LIMIT = "PageLimit";
095: public static final String CACHE_SIZE = "CacheSize";
096:
097: /**
098: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#SecuredDBObject
099: */
100: public DBObjLimit() throws DBException {
101: super ();
102: } /* DBObjLimit() */
103:
104: /**
105: * @param uid The owner's uid
106: */
107: public DBObjLimit(int uid) throws DBException {
108: super (uid);
109: }
110:
111: /**
112: * For using DBObjects within Controllers. Initializes based upon the current
113: * user and the requested db. [Of course this can be modified later]
114: *
115: * @param request - The controller request handed to you by the framework.
116: * @throws DBException uppon error
117: */
118: public DBObjLimit(RequestContext request) throws DBException {
119: super (request);
120: }
121:
122: /**
123: * @param schemaName the name of the schema
124: * @param schemaDescrip the description of the schema
125: * @param oList ??
126: */
127: private void doSchema(String schemaName, String schemaDescrip,
128: Vector oList) throws DBException {
129: Schema oneSchema = SchemaFactory.getInstance().getSchema(
130: schemaName);
131:
132: if (oneSchema == null) {
133: throw new DBException("Error instantiating class '"
134: + schemaName);
135: }
136:
137: DBObject oneDBObj = null;
138: ValidValue oneVal = null;
139:
140: /* Now add all of the objects in this schema to our list */
141: for (Enumeration e = oneSchema.getMembers(); e
142: .hasMoreElements();) {
143: oneDBObj = (DBObject) e.nextElement();
144: oneVal = new ValidValue(oneDBObj.getClass().getName(),
145: schemaDescrip
146: + ": "
147: + oneDBObj.getMetaData().getDescription(
148: this .getLocale()));
149: oList.addElement(oneVal);
150: }
151: } /* doSchema(String, String, Vector) */
152:
153: /**
154: * Override the method getValidValues to provide specific values for our
155: * multi-valued fields
156: *
157: * @param fieldName Fielname to retrieve values for
158: * @return Hashtable Value/description pairs for this field
159: * @throws DBException If the values cannot be retrieved
160: */
161: public synchronized Vector getValidValues(String fieldName)
162: throws DBException {
163: if (fieldName.equals(DB_OBJECT_NAME)) {
164: if (dbObjList == null) {
165: SchemaList myList = new SchemaList(
166: SecuredDBObject.SYSTEM_ACCOUNT);
167: myList.setDataContext(getDataContext());
168:
169: SchemaList oneList = null;
170: dbObjList = new Vector(10);
171:
172: /* Get any database objects listed as existing in another */
173:
174: /* database */
175: DBOtherMap otherList = new DBOtherMap();
176: otherList.setDataContext(getDataContext());
177:
178: DBOtherMap oneOther = null;
179: ValidValue oneVal;
180:
181: for (Iterator e2 = otherList.searchAndRetrieveList()
182: .iterator(); e2.hasNext();) {
183: oneOther = (DBOtherMap) e2.next();
184: oneVal = new ValidValue(oneOther
185: .getField(DBOtherMap.DB_OBJECT_NAME),
186: oneOther.getField(DBOtherMap.DESCRIP));
187: dbObjList.addElement(oneVal);
188: }
189:
190: doSchema(ExpressoSchema.class.getName(), "Expresso",
191: dbObjList);
192:
193: /* Now get all of the schemas listed in the database as well */
194: for (Iterator sl = myList.searchAndRetrieveList()
195: .iterator(); sl.hasNext();) {
196: oneList = (SchemaList) sl.next();
197: doSchema(oneList
198: .getField(SchemaList.FLD_SCHEMA_CLASS),
199: oneList.getField(SchemaList.FLD_DESCRIP),
200: dbObjList);
201: } /* for each schema list */
202:
203: }
204:
205: return dbObjList;
206: }
207:
208: return super .getValidValues(fieldName);
209: } /* getValidValues(String) */
210:
211: /**
212: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
213: */
214: public void setupFields() throws DBException {
215: setTargetTable("DBOBJLIMIT");
216: setDescription("DBobjLimit");
217: setCharset("ISO-8859-1");
218: addField(DB_OBJECT_NAME, "varchar", 80, false, "dbobjName");
219: addField(PAGE_LIMIT, "int", 0, false, "numEntriesPerPage"); // how many search results
220: addField(CACHE_SIZE, "int", 0, false, "numEntriesCache");
221: addField(TTL, "int", 0, true, "TTL (minutes)");
222: setStringFilter(DB_OBJECT_NAME, "stripFilter");
223: addKey(DB_OBJECT_NAME);
224: setMultiValued(DB_OBJECT_NAME);
225: } /*setupFields() */
226:
227: /**
228: * Default values for limitations
229: */
230: public synchronized void populateDefaultValues() throws DBException {
231: DBObjLimit oneLimit = new DBObjLimit();
232: oneLimit.setDataContext(getDataContext());
233: oneLimit.clear();
234: oneLimit.setField(DB_OBJECT_NAME, DefaultUserInfo.class
235: .getName());
236: oneLimit.setField(PAGE_LIMIT, "20");
237: oneLimit.setField(CACHE_SIZE, "5");
238: oneLimit.setField(TTL, 30);
239: oneLimit.addIfNeeded();
240: oneLimit.clear();
241: oneLimit.setField(DB_OBJECT_NAME, Event.class.getName());
242: oneLimit.setField(PAGE_LIMIT, "20");
243: oneLimit.setField(CACHE_SIZE, "5");
244: oneLimit.setField(TTL, 90);
245: oneLimit.addIfNeeded();
246: oneLimit.clear();
247: oneLimit.setField(DB_OBJECT_NAME, GroupMembers.class.getName());
248: oneLimit.setField(PAGE_LIMIT, "20");
249: oneLimit.setField(CACHE_SIZE, "10");
250: oneLimit.setField(TTL, 10);
251: oneLimit.addIfNeeded();
252: oneLimit.clear();
253: oneLimit.setField(DB_OBJECT_NAME, RegistrationDomain.class
254: .getName());
255: oneLimit.setField(PAGE_LIMIT, "20");
256: oneLimit.setField(CACHE_SIZE, "1");
257: oneLimit.setField(TTL, 90);
258: oneLimit.addIfNeeded();
259: oneLimit.clear();
260: oneLimit.setField(DB_OBJECT_NAME, UserPreference.class
261: .getName());
262: oneLimit.setField(PAGE_LIMIT, "20");
263: oneLimit.setField(CACHE_SIZE, "5");
264: oneLimit.setField(TTL, 30);
265: oneLimit.addIfNeeded();
266: }
267:
268: /**
269: * Update the database as well as updating the cache size.
270: *
271: * @throws DBException if there's an error while updating the record.
272: */
273: public void update() throws DBException {
274: super .update();
275:
276: CacheSystem cs = CacheManager.getCacheSystem(this
277: .getDataContext());
278: Cache cache = cs.getCache(getField(DB_OBJECT_NAME));
279: if (cache != null) {
280: int newCacheSize = getFieldInt(CACHE_SIZE);
281: cache.setMaxSize(newCacheSize);
282:
283: DBObject dbobject;
284: try {
285: dbobject = (DBObject) Class.forName(
286: getField(DB_OBJECT_NAME)).newInstance();
287: } catch (Exception e) {
288: throw new DBException(
289: "DBObjLimit.update() Error instantiating class: "
290: + getField(DB_OBJECT_NAME));
291: }
292: dbobject.getMetaData().setCacheSize(newCacheSize);
293:
294: }
295: }
296:
297: } /* DBObjLimit */
|