KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > persistence > hibernate > core > ActionPageDAOHibernate


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.persistence.hibernate.core;
17
18 import com.blandware.atleap.common.util.PartialCollection;
19 import com.blandware.atleap.common.util.QueryInfo;
20 import com.blandware.atleap.model.core.ActionPage;
21 import com.blandware.atleap.model.core.ContentField;
22 import com.blandware.atleap.model.core.ContentFieldValue;
23 import com.blandware.atleap.model.core.ContentLocale;
24 import com.blandware.atleap.persistence.core.ActionPageDAO;
25 import com.blandware.atleap.persistence.exception.DeleteException;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ListIterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * <p>Hibernate implementation of ActionPageDAO</p>
36  * <p><a HREF="ActionPageDAOHibernate.java.htm"><i>View Source</i></a></p>
37  *
38  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
39  * @version $Revision: 1.32 $ $Date: 2005/09/27 08:48:24 $
40  */

41
42 public class ActionPageDAOHibernate extends PageDAOHibernate implements ActionPageDAO {
43
44     /**
45      * Creates new instance of ActionPageDAOHibernate
46      */

47     public ActionPageDAOHibernate() {
48     }
49
50     // ~ CRUD Methods ================================================================
51

52     /**
53      * @see com.blandware.atleap.persistence.core.ActionPageDAO#createActionPage(com.blandware.atleap.model.core.ActionPage)
54      */

55     public Long JavaDoc createActionPage(ActionPage actionPage) {
56         actionPage.setUsageCounter(new Integer JavaDoc(0));
57         Long JavaDoc actionPageId = (Long JavaDoc) getHibernateTemplate().save(actionPage);
58
59         // title
60
ContentField fTitle = new ContentField();
61         fTitle.setIdentifier("title");
62         fTitle.setType(ContentField.LINE_TYPE);
63         fTitle.setInternal(Boolean.TRUE);
64         actionPage.addContentField(fTitle);
65         getHibernateTemplate().save(fTitle);
66
67         // description
68
ContentField fDescription = new ContentField();
69         fDescription.setIdentifier("description");
70         fDescription.setType(ContentField.LINE_TYPE);
71         fDescription.setInternal(Boolean.TRUE);
72         actionPage.addContentField(fDescription);
73         getHibernateTemplate().save(fDescription);
74
75         // keywords
76
ContentField fKeywords = new ContentField();
77         fKeywords.setIdentifier("keywords");
78         fKeywords.setType(ContentField.LINE_TYPE);
79         fKeywords.setInternal(Boolean.TRUE);
80         actionPage.addContentField(fKeywords);
81         getHibernateTemplate().save(fKeywords);
82
83         // Fill in content field values for title, keywords and description
84
// fields for every content locale
85
List JavaDoc contentLocales = executeFind("from ContentLocale l", true, "query.listContentLocales");
86         for ( int i = 0; i < contentLocales.size(); i++ ) {
87             ContentLocale contentLocale = (ContentLocale) contentLocales.get(i);
88             String JavaDoc localeIdentifier = contentLocale.getIdentifier();
89
90             // values for title, description and keywords in that locale
91
String JavaDoc valueTitle = (String JavaDoc) actionPage.getTitle().get(localeIdentifier);
92             String JavaDoc valueDescription = (String JavaDoc) actionPage.getDescription().get(localeIdentifier);
93             String JavaDoc valueKeywords = (String JavaDoc) actionPage.getKeywords().get(localeIdentifier);
94
95             //title
96
ContentFieldValue fvTitle = new ContentFieldValue();
97             fvTitle.setContentLocale(contentLocale);
98             if ( valueTitle != null ) {
99                 fvTitle.setSimpleValue(valueTitle);
100             } else {
101                 fvTitle.setSimpleValue("");
102             }
103             fvTitle.setLastUpdatedDatetime(new Date JavaDoc());
104             fTitle.addContentFieldValue(fvTitle);
105             getHibernateTemplate().save(fvTitle);
106
107             //description
108
ContentFieldValue fvDescription = new ContentFieldValue();
109             fvDescription.setContentLocale(contentLocale);
110             if ( valueDescription != null ) {
111                 fvDescription.setSimpleValue(valueDescription);
112             } else {
113                 fvDescription.setSimpleValue("");
114             }
115             fvDescription.setLastUpdatedDatetime(new Date JavaDoc());
116             fDescription.addContentFieldValue(fvDescription);
117             getHibernateTemplate().save(fvDescription);
118
119             //keywords
120
ContentFieldValue fvKeywords = new ContentFieldValue();
121             fvKeywords.setContentLocale(contentLocale);
122             if ( valueKeywords != null ) {
123                 fvKeywords.setSimpleValue(valueKeywords);
124             } else {
125                 fvKeywords.setSimpleValue("");
126             }
127             fvKeywords.setLastUpdatedDatetime(new Date JavaDoc());
128             fKeywords.addContentFieldValue(fvKeywords);
129             getHibernateTemplate().save(fvKeywords);
130         }
131
132         return actionPageId;
133
134     }
135
136     /**
137      * @see com.blandware.atleap.persistence.core.ActionPageDAO#retrieveActionPage(java.lang.Long)
138      */

139     public ActionPage retrieveActionPage(Long JavaDoc actionPageId) {
140         String JavaDoc hql = new StringBuffer JavaDoc("select distinct page, title.contentLocale.identifier, title.simpleValue, description.simpleValue, keywords.simpleValue ")
141                 .append("from ActionPage as page left outer join ")
142                 .append("page.contentFields titleField left outer join titleField.contentFieldValues as title left outer join ")
143                 .append("page.contentFields descriptionField left outer join descriptionField.contentFieldValues as description ")
144                 .append("left outer join page.contentFields keywordsField left outer join keywordsField.contentFieldValues as keywords ")
145                 .append("where ")
146                 .append("((title.contentLocale = description.contentLocale and title.contentLocale = keywords.contentLocale) or (title is null and description is null and keywords is null)) ")
147                 .append("and titleField.identifier = 'title' ")
148                 .append("and descriptionField.identifier = 'description' and keywordsField.identifier = 'keywords' and page.id = ?")
149                 .toString();
150
151         // Get a list of quintuples (page, locale identifier, title value,
152
// description value, keywords value)
153
List JavaDoc list = executeFind(hql, new Object JavaDoc[]{actionPageId});
154         // Assign those values to corresponding pages -- get list of pages with
155
// title, description and keywords correctly assigned
156
List JavaDoc result = setLocalizableFields(list);
157         if ( result == null || result.size() <= 0 ) {
158             return null;
159         } else {
160             return (ActionPage) result.get(0);
161         }
162     }
163
164     /**
165      * @see com.blandware.atleap.persistence.core.ActionPageDAO#updateActionPage(com.blandware.atleap.model.core.ActionPage)
166      */

167     public void updateActionPage(ActionPage actionPage) {
168         getHibernateTemplate().update(actionPage);
169         // Update internal fields (title, description and keywords) of this page
170
for ( int i = 0; i < actionPage.getContentFields().size(); i++ ) {
171             ContentField contentField = (ContentField) actionPage.getContentFields().get(i);
172             Map JavaDoc values = null;
173             if ( "title".equalsIgnoreCase(contentField.getIdentifier()) ) {
174                 values = actionPage.getTitle();
175             } else if ( "description".equalsIgnoreCase(contentField.getIdentifier()) ) {
176                 values = actionPage.getDescription();
177             } else if ( "keywords".equalsIgnoreCase(contentField.getIdentifier()) ) {
178                 values = actionPage.getKeywords();
179             } else {
180                 continue;
181             }
182             for ( int j = 0; j < contentField.getContentFieldValues().size(); j++ ) {
183                 ContentFieldValue contentFieldValue = (ContentFieldValue) contentField.getContentFieldValues().get(j);
184                 String JavaDoc localeIdentifier = contentFieldValue.getContentLocale().getIdentifier();
185                 String JavaDoc value = (String JavaDoc) values.get(localeIdentifier);
186                 if ( value != null ) {
187                     contentFieldValue.setSimpleValue(value);
188                 } else {
189                     contentFieldValue.setSimpleValue("");
190                 }
191                 contentFieldValue.setLastUpdatedDatetime(new Date JavaDoc());
192                 contentFieldValue.getContentField().updateContentFieldValue(contentFieldValue);
193                 getHibernateTemplate().update(contentFieldValue);
194             }
195         }
196     }
197
198     /**
199      * @see com.blandware.atleap.persistence.core.ActionPageDAO#deleteActionPage(com.blandware.atleap.model.core.ActionPage)
200      */

201     public void deleteActionPage(ActionPage actionPage) throws DeleteException {
202         getHibernateTemplate().delete(actionPage);
203     }
204
205     // ~ Additional methods ================================================================
206

207     /**
208      * @see com.blandware.atleap.persistence.core.ActionPageDAO#listActionPages(com.blandware.atleap.common.util.QueryInfo)
209      */

210     public PartialCollection listActionPages(QueryInfo queryInfo) {
211         String JavaDoc whereClause = new String JavaDoc();
212         String JavaDoc orderByClause = new String JavaDoc();
213         if ( queryInfo != null ) {
214             whereClause = queryInfo.getWhereClause();
215             orderByClause = queryInfo.getOrderByClause();
216             if ( whereClause == null || whereClause.length() == 0 ) {
217                 whereClause = new String JavaDoc();
218             }
219             if ( orderByClause != null && orderByClause.length() != 0 ) {
220                 orderByClause = " order by " + orderByClause;
221             } else {
222                 orderByClause = new String JavaDoc();
223             }
224         }
225
226         List JavaDoc list = null;
227         Integer JavaDoc total = null;
228
229         String JavaDoc localeIdentifier = null;
230         if ( queryInfo != null ) {
231             if ( queryInfo.getQueryParameters() != null ) {
232                 localeIdentifier = (String JavaDoc) queryInfo.getQueryParameters().get("localeIdentifier");
233             }
234         }
235
236         boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0;
237
238         String JavaDoc hqlPart = new String JavaDoc();
239         ArrayList JavaDoc args = new ArrayList JavaDoc();
240         if ( localeIdentifierPresent ) {
241             args.add(localeIdentifier);
242             hqlPart = new StringBuffer JavaDoc("from ActionPage as page ")
243                     .append("left outer join page.contentFields as titleField ")
244                     .append("left outer join page.contentFields as descriptionField ")
245                     .append("left outer join page.contentFields as keywordsField ")
246                     .append("left outer join titleField.contentFieldValues as title ")
247                     .append("left outer join descriptionField.contentFieldValues as description ")
248                     .append("left outer join keywordsField.contentFieldValues as keywords ")
249                     .append("where ")
250                     .append("titleField.identifier = 'title' and descriptionField.identifier = 'description' and keywordsField.identifier = 'keywords' ")
251                     .append("and title.contentLocale = description.contentLocale and title.contentLocale = keywords.contentLocale ")
252                     .append("and title.contentLocale.identifier = ? ")
253                     .toString();
254             if ( whereClause.length() > 0 ) {
255                 hqlPart += "and " + whereClause;
256             }
257         } else {
258             hqlPart = "from ActionPage page ";
259             if ( whereClause.length() > 0 ) {
260                 hqlPart += "where " + whereClause;
261             }
262         }
263
264         if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
265             // query count
266
String JavaDoc hqlForTotal = "select count(distinct page.id) " + hqlPart;
267             total = (Integer JavaDoc) findUniqueResult(hqlForTotal, args.toArray());
268             if ( total == null ) {
269                 total = new Integer JavaDoc(0);
270             }
271         }
272
273         // If we don't have any info about the total number of results yet or
274
// we know that there's something that will be found, then fetch data
275
if ( total == null || total.intValue() > 0 ) {
276             String JavaDoc hql = new String JavaDoc();
277             if ( localeIdentifierPresent ) {
278                 hql = "select distinct page, title.simpleValue, description.simpleValue, keywords.simpleValue " + hqlPart + orderByClause;
279             } else {
280                 hql = "select page " + hqlPart + orderByClause;
281             }
282             list = executeFind(hql, queryInfo, args.toArray());
283             if ( total == null ) {
284                 total = new Integer JavaDoc(list.size());
285             }
286             if ( localeIdentifierPresent ) {
287                 // Replace each tuple in list with a page with title, description
288
// and keywords assigned
289
for ( ListIterator JavaDoc i = list.listIterator(); i.hasNext(); ) {
290                     Object JavaDoc[] objects = (Object JavaDoc[]) i.next();
291                     ActionPage actionPage = (ActionPage) objects[0];
292                     actionPage.getTitle().put(localeIdentifier, objects[1]);
293                     actionPage.getDescription().put(localeIdentifier, objects[2]);
294                     actionPage.getKeywords().put(localeIdentifier, objects[3]);
295                     i.set(actionPage);
296                 }
297             }
298         } else {
299             list = new ArrayList JavaDoc();
300         }
301
302         return new PartialCollection(list, total);
303
304     }
305     // ~ Finders ================================================================
306

307     /**
308      * @see com.blandware.atleap.persistence.core.ActionPageDAO#findActionPageByUri(java.lang.String)
309      */

310     public ActionPage findActionPageByUri(String JavaDoc actionPageUri) {
311         return (ActionPage) findUniqueResult("from ActionPage page where page.uri = ?", new Object JavaDoc[]{actionPageUri});
312     }
313
314     // ~ Helper methods
315

316     /**
317      * Finds action page by id in list of action pages
318      *
319      * @param actionPages list to search through
320      * @param id ID of action page
321      * @return action page
322      */

323     protected ActionPage findActionPageById(List JavaDoc actionPages, Long JavaDoc id) {
324         for ( int i = 0; i < actionPages.size(); i++ ) {
325             ActionPage actionPage = (ActionPage) actionPages.get(i);
326             if ( actionPage.getId().equals(id) ) {
327                 return actionPage;
328             }
329         }
330         return null;
331     }
332
333     /**
334      * Collapses list by setting up for different action pages and locales into maps
335      *
336      * @param queryResult query result with 5 objects [page, locale, title, description, keywords]
337      * @return collapsed list
338      */

339     protected List JavaDoc setLocalizableFields(List JavaDoc queryResult) {
340         if ( queryResult == null || queryResult.size() <= 0 ) {
341             return null;
342         }
343
344         // First make a list of pages from quintuples (assigning title, description
345
// and keywords values with specified locale)
346
List JavaDoc actionPages = new ArrayList JavaDoc(queryResult.size());
347         for ( int i = 0; i < queryResult.size(); i++ ) {
348             Object JavaDoc[] objects = (Object JavaDoc[]) queryResult.get(i);
349             ActionPage page = (ActionPage) objects[0];
350             String JavaDoc locale = (String JavaDoc) objects[1];
351             if ( locale != null && locale.trim().length() > 0 ) {
352                 page.getTitle().put(locale, objects[2]);
353                 page.getDescription().put(locale, objects[3]);
354                 page.getKeywords().put(locale, objects[4]);
355             }
356             actionPages.add(page);
357         }
358
359         // Now make a such list of pages that each page is encountered maximum
360
// one time (if some page is encountered second time, fore example, its
361
// internal fields' info is added to previous instance)
362
List JavaDoc fullActionPages = new ArrayList JavaDoc(actionPages.size());
363         for ( int i = 0; i < actionPages.size(); i++ ) {
364             ActionPage newActionPage = (ActionPage) actionPages.get(i);
365             ActionPage existingActionPage = findActionPageById(fullActionPages, newActionPage.getId());
366             if ( existingActionPage != null ) {
367                 //title
368
Iterator JavaDoc iterator = newActionPage.getTitle().keySet().iterator();
369                 while ( iterator.hasNext() ) {
370                     String JavaDoc localeIdentifier = (String JavaDoc) iterator.next();
371                     String JavaDoc value = (String JavaDoc) newActionPage.getTitle().get(localeIdentifier);
372                     if ( value == null ) {
373                         continue;
374                     }
375                     existingActionPage.getTitle().put(localeIdentifier, value);
376                 }
377
378                 //description
379
iterator = newActionPage.getDescription().keySet().iterator();
380                 while ( iterator.hasNext() ) {
381                     String JavaDoc localeIdentifier = (String JavaDoc) iterator.next();
382                     String JavaDoc value = (String JavaDoc) newActionPage.getDescription().get(localeIdentifier);
383                     if ( value == null ) {
384                         continue;
385                     }
386                     existingActionPage.getDescription().put(localeIdentifier, value);
387                 }
388
389                 //keywords
390
iterator = newActionPage.getKeywords().keySet().iterator();
391                 while ( iterator.hasNext() ) {
392                     String JavaDoc localeIdentifier = (String JavaDoc) iterator.next();
393                     String JavaDoc value = (String JavaDoc) newActionPage.getKeywords().get(localeIdentifier);
394                     if ( value == null ) {
395                         continue;
396                     }
397                     existingActionPage.getKeywords().put(localeIdentifier, value);
398                 }
399
400             } else {
401                 fullActionPages.add(newActionPage);
402             }
403         }
404
405         return fullActionPages;
406     }
407
408 }
409
Popular Tags