KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExpressoSchema;
68 import com.jcorporate.expresso.core.cache.Cache;
69 import com.jcorporate.expresso.core.cache.CacheManager;
70 import com.jcorporate.expresso.core.cache.CacheSystem;
71 import com.jcorporate.expresso.core.db.DBException;
72 import com.jcorporate.expresso.core.dbobj.DBObject;
73 import com.jcorporate.expresso.core.dbobj.RequestContext;
74 import com.jcorporate.expresso.core.dbobj.Schema;
75 import com.jcorporate.expresso.core.dbobj.SchemaFactory;
76 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
77 import com.jcorporate.expresso.core.dbobj.ValidValue;
78
79 import java.util.Enumeration JavaDoc;
80 import java.util.Iterator JavaDoc;
81 import java.util.Vector JavaDoc;
82
83
84 /**
85  * A DBObjLimit defines the number of records the standard maintenance
86  * servlet will display before it breaks a page. Also controls caching.
87  *
88  * @author Michael Nash
89  */

90 public class DBObjLimit
91         extends SecuredDBObject {
92     private Vector JavaDoc dbObjList = null;
93
94     public static final String JavaDoc TTL = "TTL";
95     public static final String JavaDoc DB_OBJECT_NAME = "DBObjectName";
96     public static final String JavaDoc PAGE_LIMIT = "PageLimit";
97     public static final String JavaDoc CACHE_SIZE = "CacheSize";
98
99
100     /**
101      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#SecuredDBObject
102      */

103     public DBObjLimit()
104             throws DBException {
105         super();
106     } /* DBObjLimit() */
107
108
109     /**
110      * @param uid The owner's uid
111      */

112     public DBObjLimit(int uid)
113             throws DBException {
114         super(uid);
115     }
116
117     /**
118      * For using DBObjects within Controllers. Initializes based upon the current
119      * user and the requested db. [Of course this can be modified later]
120      *
121      * @param request - The controller request handed to you by the framework.
122      * @throws DBException uppon error
123      */

124     public DBObjLimit(RequestContext request)
125             throws DBException {
126         super(request);
127     }
128
129     /**
130      * @param schemaName the name of the schema
131      * @param schemaDescrip the description of the schema
132      * @param oList ??
133      */

134     private void doSchema(String JavaDoc schemaName, String JavaDoc schemaDescrip,
135                           Vector JavaDoc oList)
136             throws DBException {
137         Schema oneSchema = SchemaFactory.getInstance().getSchema(schemaName);
138
139         if (oneSchema == null) {
140             throw new DBException("Error instantiating class '" + schemaName);
141         }
142
143         DBObject oneDBObj = null;
144         ValidValue oneVal = null;
145
146         /* Now add all of the objects in this schema to our list */
147         for (Enumeration JavaDoc e = oneSchema.getMembers(); e.hasMoreElements();) {
148             oneDBObj = (DBObject) e.nextElement();
149             oneVal = new ValidValue(oneDBObj.getClass().getName(),
150                     schemaDescrip + ": " +
151                     oneDBObj.getMetaData().getDescription(this.getLocale()));
152             oList.addElement(oneVal);
153         }
154     } /* doSchema(String, String, Vector) */
155
156
157     /**
158      * Override the method getValidValues to provide specific values for our
159      * multi-valued fields
160      *
161      * @param fieldName Fielname to retrieve values for
162      * @return Hashtable Value/description pairs for this field
163      * @throws DBException If the values cannot be retrieved
164      */

165     public synchronized Vector JavaDoc getValidValues(String JavaDoc fieldName)
166             throws DBException {
167         if (fieldName.equals(DB_OBJECT_NAME)) {
168             if (dbObjList == null) {
169                 SchemaList myList = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
170                 myList.setDataContext(getDataContext());
171
172                 SchemaList oneList = null;
173                 dbObjList = new Vector JavaDoc(10);
174
175                 /* Get any database objects listed as existing in another */
176
177                 /* database */
178                 DBOtherMap otherList = new DBOtherMap();
179                 otherList.setDataContext(getDataContext());
180
181                 DBOtherMap oneOther = null;
182                 ValidValue oneVal;
183
184                 for (Iterator JavaDoc e2 = otherList.searchAndRetrieveList().iterator();
185                      e2.hasNext();) {
186                     oneOther = (DBOtherMap) e2.next();
187                     oneVal = new ValidValue(oneOther.getField(DBOtherMap.DB_OBJECT_NAME),
188                             oneOther.getField(DBOtherMap.DESCRIP));
189                     dbObjList.addElement(oneVal);
190                 }
191
192                 doSchema(ExpressoSchema.class.getName(), "Expresso", dbObjList);
193
194                 /* Now get all of the schemas listed in the database as well */
195                 for (Iterator JavaDoc sl = myList.searchAndRetrieveList().iterator();
196                      sl.hasNext();) {
197                     oneList = (SchemaList) sl.next();
198                     doSchema(oneList.getField(SchemaList.FLD_SCHEMA_CLASS),
199                             oneList.getField(SchemaList.FLD_DESCRIP), dbObjList);
200                 } /* for each schema list */
201
202             }
203
204             return dbObjList;
205         }
206
207         return super.getValidValues(fieldName);
208     } /* getValidValues(String) */
209
210
211     /**
212      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
213      */

214     public void setupFields()
215             throws DBException {
216         setTargetTable("DBOBJLIMIT");
217         setDescription("DBobjLimit");
218         setCharset("ISO-8859-1");
219         addField(DB_OBJECT_NAME, "varchar", 80, false, "dbobjName");
220         addField(PAGE_LIMIT, "int", 0, false, "numEntriesPerPage"); // how many search results
221
addField(CACHE_SIZE, "int", 0, false, "numEntriesCache");
222         addField(TTL, "int", 0, true, "TTL (minutes)");
223         setStringFilter(DB_OBJECT_NAME, "stripFilter");
224         addKey(DB_OBJECT_NAME);
225         setMultiValued(DB_OBJECT_NAME);
226     } /*setupFields() */
227
228
229     /**
230      * Default values for limitations
231      */

232     public synchronized void populateDefaultValues()
233             throws DBException {
234         DBObjLimit oneLimit = new DBObjLimit();
235         oneLimit.setDataContext(getDataContext());
236         oneLimit.clear();
237         oneLimit.setField(DB_OBJECT_NAME, DefaultUserInfo.class.getName());
238         oneLimit.setField(PAGE_LIMIT, "20");
239         oneLimit.setField(CACHE_SIZE, "5");
240         oneLimit.setField(TTL, 30);
241         oneLimit.addIfNeeded();
242         oneLimit.clear();
243         oneLimit.setField(DB_OBJECT_NAME, Event.class.getName());
244         oneLimit.setField(PAGE_LIMIT, "20");
245         oneLimit.setField(CACHE_SIZE, "5");
246         oneLimit.setField(TTL, 90);
247         oneLimit.addIfNeeded();
248         oneLimit.clear();
249         oneLimit.setField(DB_OBJECT_NAME, GroupMembers.class.getName());
250         oneLimit.setField(PAGE_LIMIT, "20");
251         oneLimit.setField(CACHE_SIZE, "10");
252         oneLimit.setField(TTL, 10);
253         oneLimit.addIfNeeded();
254         oneLimit.clear();
255         oneLimit.setField(DB_OBJECT_NAME, RegistrationDomain.class.getName());
256         oneLimit.setField(PAGE_LIMIT, "20");
257         oneLimit.setField(CACHE_SIZE, "1");
258         oneLimit.setField(TTL, 90);
259         oneLimit.addIfNeeded();
260         oneLimit.clear();
261         oneLimit.setField(DB_OBJECT_NAME, UserPreference.class.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.getDataContext());
277         Cache cache = cs.getCache(getField(DB_OBJECT_NAME));
278         if (cache != null) {
279             int newCacheSize = getFieldInt(CACHE_SIZE);
280             cache.setMaxSize(newCacheSize);
281
282             DBObject dbobject;
283             try {
284                 dbobject = (DBObject) Class.forName(getField(DB_OBJECT_NAME)).newInstance();
285             } catch (Exception JavaDoc e) {
286                 throw new DBException("DBObjLimit.update() Error instantiating class: "
287                         + getField(DB_OBJECT_NAME));
288             }
289             dbobject.getMetaData().setCacheSize(newCacheSize);
290
291         }
292     }
293
294 } /* DBObjLimit */
295
296
Popular Tags