001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.module.admin;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.List;
022:
023: import org.romaframework.aspect.persistence.PersistenceAspect;
024: import org.romaframework.aspect.persistence.QueryByFilter;
025: import org.romaframework.aspect.persistence.QueryByText;
026: import org.romaframework.core.flow.ObjectContext;
027: import org.romaframework.module.admin.domain.Info;
028: import org.romaframework.module.admin.domain.InfoCategory;
029:
030: /**
031: * Helper class to manage Info objects. Caches InfoCategory and Info istances.
032: *
033: * @author Luca Garulli (luca.garulli@assetdata.it)
034: */
035: public class InfoHelper {
036:
037: List<InfoCategory> categories;
038: HashMap<InfoCategory, List<Info>> cache;
039: private static InfoHelper instance = new InfoHelper();
040: private boolean cached = false;
041:
042: protected InfoHelper() {
043: categories = new ArrayList<InfoCategory>();
044: cache = new HashMap<InfoCategory, List<Info>>();
045:
046: // PRE LOAD ALL CATEGORIES AND CACHE RESULTS TO OPTIMIZE PERFORMANCES
047: checkForCategories();
048: }
049:
050: public void invalidateCache() {
051: cached = false;
052: }
053:
054: private void checkForCategories() {
055: if (cached)
056: return;
057:
058: synchronized (categories) {
059: if (!cached) {
060: QueryByText query = new QueryByText(InfoCategory.class,
061: null);
062: query.setStrategy(PersistenceAspect.STRATEGY_DETACHING);
063:
064: categories = ObjectContext.getInstance()
065: .getContextComponent(PersistenceAspect.class)
066: .query(query);
067:
068: cached = true;
069: }
070: }
071: }
072:
073: public List<InfoCategory> getInfoCategoryList() {
074: checkForCategories();
075:
076: return categories;
077: }
078:
079: public InfoCategory[] getInfoCategoryArray() {
080: checkForCategories();
081:
082: InfoCategory[] array = new InfoCategory[categories.size()];
083: if (categories.size() > 0)
084: categories.toArray(array);
085: return array;
086: }
087:
088: public InfoCategory getInfoCategory(String iName) {
089: checkForCategories();
090:
091: synchronized (categories) {
092: for (InfoCategory c : categories) {
093: if (c.getName().equals(iName))
094: return c;
095: }
096: }
097: return null;
098: }
099:
100: public InfoCategory setInfoCategory(String iName) {
101: InfoCategory cat = null;
102:
103: synchronized (categories) {
104: cat = getInfoCategory(iName);
105:
106: if (cat == null) {
107: // NOT FOUND: CREATE IT
108: cat = new InfoCategory(iName);
109:
110: PersistenceAspect db = ObjectContext.getInstance()
111: .getContextComponent(PersistenceAspect.class);
112: db.setStrategy(PersistenceAspect.STRATEGY_DETACHING);
113: cat = db.createObject(cat);
114: categories.add(cat);
115: }
116: }
117: return cat;
118: }
119:
120: public List<Info> getInfoList(String iCategoryName) {
121: return getInfoList(getInfoCategory(iCategoryName));
122: }
123:
124: /**
125: * Return all Info objects for a type. Uses a cache to achieve best performances.
126: *
127: * @param iType
128: * Specifies the category
129: * @return List<Info> containing all Info instances for that category
130: */
131: public List<Info> getInfoList(InfoCategory iType) {
132: List<Info> result;
133:
134: synchronized (cache) {
135: result = cache.get(iType);
136: if (result != null)
137: // FOUND: RETURN OBJECTS IN CACHE
138: return result;
139: }
140:
141: QueryByFilter query = new QueryByFilter(Info.class);
142: query.addItem("category", QueryByFilter.FIELD_EQUALS, iType);
143: query.setStrategy(PersistenceAspect.STRATEGY_DETACHING);
144: result = ObjectContext.getInstance().getContextComponent(
145: PersistenceAspect.class).query(query);
146:
147: synchronized (cache) {
148: cache.put(iType, result);
149: }
150:
151: return result;
152: }
153:
154: public Info[] getInfoArray(String iCategoryName) {
155: return getInfoArray(getInfoCategory(iCategoryName));
156: }
157:
158: /**
159: * Return all Info objects for a category
160: *
161: * @param iType
162: * Specifies the category
163: * @return Info[] array
164: */
165: public Info[] getInfoArray(InfoCategory iType) {
166: List<Info> result = getInfoList(iType);
167: Info[] array = new Info[result.size()];
168: if (result.size() > 0)
169: result.toArray(array);
170: return array;
171: }
172:
173: public Info getInfo(String iCategoryName, String iText) {
174: return getInfo(getInfoCategory(iCategoryName), iText);
175: }
176:
177: public Info getInfo(InfoCategory iType, String iText) {
178: List<Info> result = getInfoList(iType);
179:
180: if (result != null) {
181: // CATEGORY FOUND: SEARCH FOR INFO BY TEXT
182: for (Info i : result) {
183: if (i.getText().equals(iText))
184: // FOUND
185: return i;
186: }
187: }
188: return null;
189: }
190:
191: public Info getInfo(String iCategoryName, Integer iValue) {
192: return getInfo(getInfoCategory(iCategoryName), iValue);
193: }
194:
195: public Info getInfo(InfoCategory iType, Integer iValue) {
196: List<Info> result = getInfoList(iType);
197:
198: if (result != null) {
199: // CATEGORY FOUND: SEARCH FOR INFO BY TEXT
200: for (Info i : result) {
201: if (i.getValue().equals(iValue))
202: // FOUND
203: return i;
204: }
205: }
206: return null;
207: }
208:
209: public Info setInfo(String iCategoryName, String iText) {
210: return setInfo(iCategoryName, iText, 0);
211: }
212:
213: public Info setInfo(String iCategoryName, String iText,
214: Integer iValue) {
215: Info info = storeInfo(new Info(iCategoryName, iText, iValue));
216: return info;
217: }
218:
219: public Info setInfo(InfoCategory iType, String iText) {
220: Info info = storeInfo(new Info(iType, iText));
221: return info;
222: }
223:
224: public Info setInfo(InfoCategory iType, String iText, Integer iValue) {
225: Info info = storeInfo(new Info(iType, iText, iValue));
226: return info;
227: }
228:
229: private Info storeInfo(Info iInfo) {
230: List<Info> result;
231:
232: synchronized (cache) {
233: result = cache.get(iInfo.getCategory());
234: if (result != null) {
235: // CREATE NEW CATEGORY
236: setInfoCategory(iInfo.getCategory().getName());
237:
238: result = cache.get(iInfo.getCategory());
239: result.add(iInfo);
240: }
241: }
242:
243: PersistenceAspect db = ObjectContext.getInstance()
244: .getContextComponent(PersistenceAspect.class);
245: db.setStrategy(PersistenceAspect.STRATEGY_DETACHING);
246: return db.createObject(iInfo);
247: }
248:
249: public static InfoHelper getInstance() {
250: return instance;
251: }
252: }
|