KickJava   Java API By Example, From Geeks To Geeks.

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


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.ApplicationResource;
21 import com.blandware.atleap.persistence.core.ApplicationResourceDAO;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * <p>Hibernate implementation of ApplicationResourceDAO</p>
29  * <p><a HREF="ApplicationResourceDAOHibernate.java.htm"><i>View Source</i></a></p>
30  *
31  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
32  * @version $Revision: 1.4 $ $Date: 2005/08/06 17:13:04 $
33  */

34 public class ApplicationResourceDAOHibernate extends BaseDAOHibernate implements ApplicationResourceDAO {
35
36     /**
37      * Creates new instance of ApplicationResourceDAOHibernate
38      */

39     public ApplicationResourceDAOHibernate() {
40     }
41
42     /**
43      * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#createApplicationResource(com.blandware.atleap.model.core.ApplicationResource)
44      */

45     public void createApplicationResource(ApplicationResource applicationResource) {
46         getHibernateTemplate().save(applicationResource);
47     }
48
49     /**
50      * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#retrieveApplicationResource(com.blandware.atleap.model.core.ApplicationResource.ApplicationResourceID)
51      */

52     public ApplicationResource retrieveApplicationResource(ApplicationResource.ApplicationResourceID id) {
53         return (ApplicationResource) getHibernateTemplate().get(ApplicationResource.class, id);
54     }
55
56     /**
57      * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#updateApplicationResource(com.blandware.atleap.model.core.ApplicationResource)
58      */

59     public void updateApplicationResource(ApplicationResource applicationResource) {
60         getHibernateTemplate().saveOrUpdate(applicationResource);
61     }
62
63     /**
64      * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#deleteApplicationResource(com.blandware.atleap.model.core.ApplicationResource)
65      */

66     public void deleteApplicationResource(ApplicationResource applicationResource) {
67         getHibernateTemplate().delete(applicationResource);
68     }
69
70     /**
71      * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#listApplicationResources(com.blandware.atleap.common.util.QueryInfo)
72      */

73     public PartialCollection listApplicationResources(QueryInfo queryInfo) {
74         String JavaDoc whereClause = new String JavaDoc();
75         String JavaDoc orderByClause = new String JavaDoc();
76         if ( queryInfo != null ) {
77             whereClause = queryInfo.getWhereClause();
78             orderByClause = queryInfo.getOrderByClause();
79             if ( whereClause == null ) {
80                 whereClause = new String JavaDoc();
81             }
82             if ( orderByClause != null && orderByClause.length() != 0 ) {
83                 orderByClause = " order by " + orderByClause;
84             } else {
85                 orderByClause = " order by r.id.key";
86             }
87         }
88
89
90         ArrayList JavaDoc args = new ArrayList JavaDoc();
91         String JavaDoc localeIdentifier = null;
92         if ( queryInfo != null ) {
93             Map JavaDoc queryParameters = queryInfo.getQueryParameters();
94             localeIdentifier = (String JavaDoc) queryParameters.get("localeIdentifier");
95         }
96
97         if ( localeIdentifier != null ) {
98             if ( whereClause.length() > 0 ) {
99                 whereClause += " and";
100             }
101             whereClause += " (r.id.locale.identifier = ? or r.id.locale.defaultInstance = 'T')";
102             args.add(localeIdentifier);
103         }
104
105         if ( whereClause.trim().length() > 0 ) {
106             whereClause = " where " + whereClause;
107         }
108
109         List JavaDoc list = null;
110         Integer JavaDoc total = null;
111         String JavaDoc hqlPart = "from ApplicationResource r" + whereClause;
112         if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
113             String JavaDoc hqlForTotal = "select count(distinct r.id.key) " + hqlPart;
114             total = (Integer JavaDoc) findUniqueResult(hqlForTotal, args.toArray());
115             if ( total == null ) {
116                 total = new Integer JavaDoc(0);
117             }
118         }
119         // If we don't have any info about the total number of results yet or
120
// we know that there's something that will be found, then fetch data
121
if ( total == null || total.intValue() > 0 ) {
122             String JavaDoc hql = "select r " + hqlPart + orderByClause;
123             list = executeFind(hql, queryInfo, args.toArray());
124             if ( total == null ) {
125                 total = new Integer JavaDoc(list.size());
126             }
127         } else {
128             list = new ArrayList JavaDoc();
129         }
130         return new PartialCollection(list, total);
131     }
132
133 }
134
Popular Tags