Source Code Cross Referenced for InfoHelper.java in  » Web-Framework » roma-webwizard » org » romaframework » module » admin » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » roma webwizard » org.romaframework.module.admin 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.