KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContentField;
21 import com.blandware.atleap.model.core.ContentFieldValue;
22 import com.blandware.atleap.model.core.ContentLocale;
23 import com.blandware.atleap.model.core.ContentPage;
24 import com.blandware.atleap.model.core.Layout;
25 import com.blandware.atleap.model.core.Role;
26 import com.blandware.atleap.persistence.core.ContentPageDAO;
27 import com.blandware.atleap.persistence.exception.DeleteException;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ListIterator JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * <p>Hibernate implementation of ContentPageDAO</p>
38  * <p><a HREF="ContentPageDAOHibernate.java.htm"><i>View Source</i></a></p>
39  *
40  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
41  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
42  * @version $Revision: 1.39 $ $Date: 2005/09/27 08:48:24 $
43  */

44 public class ContentPageDAOHibernate extends PageDAOHibernate implements ContentPageDAO {
45
46     /**
47      * Creates new instance of NewsDAOHibernate
48      */

49     public ContentPageDAOHibernate() {
50     }
51
52     /**
53      * @see com.blandware.atleap.persistence.core.ContentPageDAO#createContentPage(com.blandware.atleap.model.core.ContentPage, com.blandware.atleap.model.core.Layout)
54      */

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

139     public ContentPage retrieveContentPage(Long JavaDoc contentPageId) {
140         String JavaDoc hql = new StringBuffer JavaDoc("select distinct page, title.contentLocale.identifier, title.simpleValue, description.simpleValue, keywords.simpleValue ")
141                 .append("from ContentPage 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 ")
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[]{contentPageId});
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 (ContentPage) result.get(0);
161         }
162     }
163
164     /**
165      * @see com.blandware.atleap.persistence.core.ContentPageDAO#updateContentPage(com.blandware.atleap.model.core.ContentPage, com.blandware.atleap.model.core.Layout)
166      */

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

204     public void deleteContentPage(ContentPage contentPage) throws DeleteException {
205         getHibernateTemplate().delete(contentPage);
206         contentPage.getLayout().removeContentPage(contentPage);
207
208         // break relations between roles and deleted content page
209
List JavaDoc roles = new ArrayList JavaDoc(contentPage.getRoles());
210         for ( int i = 0; i < roles.size(); i++ ) {
211             Role role = (Role) roles.get(i);
212             contentPage.removeRole(role);
213         }
214     }
215
216     // ~ Additional methods ================================================================
217

218     /**
219      * @see com.blandware.atleap.persistence.core.ContentPageDAO#listContentPages(com.blandware.atleap.common.util.QueryInfo)
220      */

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

320     /**
321      * @see com.blandware.atleap.persistence.core.ContentPageDAO#findContentPageByUri(java.lang.String)
322      */

323     public ContentPage findContentPageByUri(String JavaDoc contentPageUri) {
324         return (ContentPage) findUniqueResult("from ContentPage page where page.uri = ?", new Object JavaDoc[]{contentPageUri}, true, "query.findContentPageByUri");
325     }
326
327     // ~ Helper methods
328

329     /**
330      * Finds content page by id in list of content pages
331      *
332      * @param contentPages list to search
333      * @param id ID of content page
334      * @return content page
335      */

336     protected ContentPage findContentPageById(List JavaDoc contentPages, Long JavaDoc id) {
337         for ( int i = 0; i < contentPages.size(); i++ ) {
338             ContentPage contentPage = (ContentPage) contentPages.get(i);
339             if ( contentPage.getId().equals(id) ) {
340                 return contentPage;
341             }
342         }
343         return null;
344     }
345
346     /**
347      * Collapses list by setting up values for different content pages and locales into maps
348      *
349      * @param queryResult query result with 5 objects [page, locale, title, description, keywords]
350      * @return collapsed list
351      */

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