KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > admin > patch > hibernate > HibernatePatchDaoServiceImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.admin.patch.hibernate;
18
19 import java.util.Date JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.alfresco.error.AlfrescoRuntimeException;
24 import org.alfresco.repo.admin.patch.PatchDaoService;
25 import org.alfresco.repo.domain.AppliedPatch;
26 import org.alfresco.repo.domain.hibernate.AppliedPatchImpl;
27 import org.hibernate.Query;
28 import org.hibernate.Session;
29 import org.springframework.orm.hibernate3.HibernateCallback;
30 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
31
32 /**
33  * Hibernate-specific implementation for managing patch persistence.
34  *
35  * @since 1.2
36  * @author Derek Hulley
37  */

38 public class HibernatePatchDaoServiceImpl extends HibernateDaoSupport implements PatchDaoService
39 {
40     public static final String JavaDoc QUERY_GET_ALL_APPLIED_PATCHES = "patch.GetAllAppliedPatches";
41     public static final String JavaDoc QUERY_GET_APPLIED_PATCHES_BY_DATE = "patch.GetAppliedPatchesByDate";
42
43     public AppliedPatch newAppliedPatch(String JavaDoc id)
44     {
45         // check for existence
46
AppliedPatch existing = getAppliedPatch(id);
47         if (existing != null)
48         {
49             throw new AlfrescoRuntimeException("An applied patch already exists: \n" +
50                     " id: " + id);
51         }
52         // construct a new one
53
AppliedPatchImpl patch = new AppliedPatchImpl();
54         patch.setId(id);
55         // save this in hibernate
56
getHibernateTemplate().save(patch);
57         // done
58
return patch;
59     }
60
61     public AppliedPatch getAppliedPatch(String JavaDoc id)
62     {
63         AppliedPatch patch = (AppliedPatch) getHibernateTemplate().get(AppliedPatchImpl.class, id);
64         // done
65
return patch;
66     }
67
68     public void detach(AppliedPatch appliedPatch)
69     {
70         getSession().evict(appliedPatch);
71     }
72
73     /**
74      * @see #QUERY_GET_ALL_APPLIED_PATCHES
75      */

76     @SuppressWarnings JavaDoc("unchecked")
77     public List JavaDoc<AppliedPatch> getAppliedPatches()
78     {
79         HibernateCallback callback = new HibernateCallback()
80         {
81             public Object JavaDoc doInHibernate(Session session)
82             {
83                 Query query = session.getNamedQuery(HibernatePatchDaoServiceImpl.QUERY_GET_ALL_APPLIED_PATCHES);
84                 return query.list();
85             }
86         };
87         List JavaDoc<AppliedPatch> queryResults = (List JavaDoc) getHibernateTemplate().execute(callback);
88         // done
89
return queryResults;
90     }
91
92     /**
93      * @see #QUERY_GET_APPLIED_PATCHES_BY_DATE
94      */

95     @SuppressWarnings JavaDoc("unchecked")
96     public List JavaDoc<AppliedPatch> getAppliedPatches(final Date JavaDoc fromDate, final Date JavaDoc toDate)
97     {
98         HibernateCallback callback = new HibernateCallback()
99         {
100             public Object JavaDoc doInHibernate(Session session)
101             {
102                 Query query = session.getNamedQuery(HibernatePatchDaoServiceImpl.QUERY_GET_ALL_APPLIED_PATCHES);
103                 return query.list();
104             }
105         };
106         List JavaDoc<AppliedPatch> queryResults = (List JavaDoc) getHibernateTemplate().execute(callback);
107         // eliminate results that are out of range
108
Iterator JavaDoc<AppliedPatch> iterator = queryResults.iterator();
109         while (iterator.hasNext())
110         {
111             AppliedPatch appliedPatch = iterator.next();
112             Date JavaDoc appliedOnDate = appliedPatch.getAppliedOnDate();
113             if (fromDate.compareTo(appliedOnDate) >= 0 || toDate.compareTo(appliedOnDate) <= 0)
114             {
115                 // it is out of range
116
iterator.remove();
117             }
118         }
119         // done
120
return queryResults;
121     }
122 }
123
Popular Tags