KickJava   Java API By Example, From Geeks To Geeks.

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


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.Layout;
21 import com.blandware.atleap.persistence.core.LayoutDAO;
22 import com.blandware.atleap.persistence.exception.DeleteException;
23 import com.blandware.atleap.persistence.exception.UpdateException;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28
29 /**
30  * <p>Hibernate implementation of LayoutDAO</p>
31  * <p><a HREF="LayoutDAOHibernate.java.htm"><i>View Source</i></a></p>
32  *
33  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
34  * @version $Revision: 1.15 $ $Date: 2005/11/28 15:56:28 $
35  */

36 public class LayoutDAOHibernate extends LocalizableDAOHibernate implements LayoutDAO {
37
38     /**
39      * Creates new instance of LayoutDAOHibernate
40      */

41     public LayoutDAOHibernate() {
42     }
43
44     /**
45      * @see com.blandware.atleap.persistence.core.LayoutDAO#createLayout(com.blandware.atleap.model.core.Layout)
46      */

47     public Long JavaDoc createLayout(Layout layout) {
48         return (Long JavaDoc) getHibernateTemplate().save(layout);
49     }
50
51     /**
52      * @see com.blandware.atleap.persistence.core.LayoutDAO#retrieveLayout(java.lang.Long)
53      */

54     public Layout retrieveLayout(Long JavaDoc layoutId) {
55         Object JavaDoc o = getHibernateTemplate().get(Layout.class, layoutId);
56         if ( o instanceof Layout ) {
57             return (Layout) o;
58         } else {
59             return null;
60         }
61     }
62
63     /**
64      * @see com.blandware.atleap.persistence.core.LayoutDAO#updateLayout(com.blandware.atleap.model.core.Layout)
65      */

66     public void updateLayout(Layout layout) throws UpdateException {
67         String JavaDoc cpDefinition = layout.getCpDefinition();
68         if (cpDefinition == null || cpDefinition.length() == 0) {
69             String JavaDoc hql = "select p from Layout l, ContentPage p where l.id = ? and p.layout = l";
70             List JavaDoc list = executeFind(hql, new Object JavaDoc[]{layout.getId()});
71             if ( list != null && list.size() > 0 ) {
72                 String JavaDoc errorMessage = "Cannot update layout, because there is one or more content pages, based on it, so cpDefinition cannot be set to empty";
73                 if ( log.isErrorEnabled() ) {
74                     log.error(errorMessage);
75                 }
76                 throw new UpdateException(errorMessage);
77             }
78         }
79         getHibernateTemplate().update(layout);
80     }
81
82     /**
83      * @see com.blandware.atleap.persistence.core.LayoutDAO#deleteLayout(com.blandware.atleap.model.core.Layout)
84      */

85     public void deleteLayout(Layout layout) throws DeleteException {
86         String JavaDoc hql = "select p from Layout l, ContentPage p where l.id = ? and p.layout = l";
87         List JavaDoc list = executeFind(hql, new Object JavaDoc[]{layout.getId()});
88         if ( list != null && list.size() > 0 ) {
89             String JavaDoc errorMessage = "Cannot delete layout, because there is one or more content pages, based on it";
90             if ( log.isErrorEnabled() ) {
91                 log.error(errorMessage);
92             }
93             throw new DeleteException(errorMessage);
94         }
95         getHibernateTemplate().delete(layout);
96     }
97
98     /**
99      * @see com.blandware.atleap.persistence.core.LayoutDAO#listLayouts(com.blandware.atleap.common.util.QueryInfo)
100      */

101     public PartialCollection listLayouts(QueryInfo queryInfo) {
102         String JavaDoc whereClause = new String JavaDoc();
103         String JavaDoc orderByClause = new String JavaDoc();
104         if ( queryInfo != null ) {
105             whereClause = queryInfo.getWhereClause();
106             orderByClause = queryInfo.getOrderByClause();
107             if ( whereClause != null && whereClause.length() != 0 ) {
108                 whereClause = " where " + whereClause;
109             } else {
110                 whereClause = new String JavaDoc();
111             }
112             if ( orderByClause != null && orderByClause.length() != 0 ) {
113                 orderByClause = " order by " + orderByClause;
114             } else {
115                 orderByClause = new String JavaDoc();
116             }
117         }
118
119         List JavaDoc list = null;
120         Integer JavaDoc total = null;
121         String JavaDoc hqlPart = "from Layout l" + whereClause;
122         if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
123             String JavaDoc hqlForTotal = "select count(l.id) " + hqlPart;
124             total = (Integer JavaDoc) findUniqueResult(hqlForTotal);
125             if ( total == null ) {
126                 total = new Integer JavaDoc(0);
127             }
128         }
129         // If we don't have any info about the total number of results yet or
130
// we know that there's something that will be found, then fetch data
131
if ( total == null || total.intValue() > 0 ) {
132             String JavaDoc hql = "select l " + hqlPart + orderByClause;
133             list = executeFind(hql, queryInfo);
134             if ( total == null ) {
135                 total = new Integer JavaDoc(list.size());
136             }
137         } else {
138             list = new ArrayList JavaDoc();
139         }
140         return new PartialCollection(list, total);
141     }
142
143     /**
144      * @see com.blandware.atleap.persistence.core.LayoutDAO#hasDuplicates(com.blandware.atleap.model.core.Layout)
145      */

146     public boolean hasDuplicates(Layout layout) {
147
148         ArrayList JavaDoc args = new ArrayList JavaDoc();
149         args.add(layout.getName());
150         args.add(layout.getDefinition());
151
152         String JavaDoc hql = "select count(l.id) from Layout l where (l.name = ? or l.definition = ?)";
153         if ( layout.getId() != null ) {
154             hql += " and l.id != ?";
155             args.add(layout.getId());
156         }
157
158         int count = ((Integer JavaDoc) findUniqueResult(hql, args.toArray())).intValue();
159
160         return count > 0;
161     }
162
163     /**
164      * @see com.blandware.atleap.persistence.core.LayoutDAO#findLayoutByContentPageUri(java.lang.String)
165      */

166     public Layout findLayoutByContentPageUri(String JavaDoc contentPageUri) {
167         return (Layout) findUniqueResult("select p.layout from ContentPage p where p.uri = ?", new Object JavaDoc[]{contentPageUri});
168     }
169
170     /**
171      * @see com.blandware.atleap.persistence.core.LayoutDAO#findLayoutByDefinition(java.lang.String)
172      */

173     public Layout findLayoutByDefinition(String JavaDoc definitionName) {
174         return (Layout) findUniqueResult("from Layout l where l.definition = ?", new Object JavaDoc[]{definitionName});
175     }
176
177     /**
178      * @see com.blandware.atleap.persistence.core.LayoutDAO#findLayoutByName(java.lang.String)
179      */

180     public Layout findLayoutByName(String JavaDoc name) {
181         return (Layout) findUniqueResult("from Layout l where l.name = ?", new Object JavaDoc[]{name});
182     }
183 }
184
Popular Tags