KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > common > counter > hibernate > HibernateVersionCounterDaoServiceImpl


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.version.common.counter.hibernate;
18
19 import org.alfresco.repo.domain.StoreKey;
20 import org.alfresco.repo.domain.VersionCount;
21 import org.alfresco.repo.domain.hibernate.VersionCountImpl;
22 import org.alfresco.repo.version.common.counter.VersionCounterDaoService;
23 import org.alfresco.service.cmr.repository.StoreRef;
24 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
25
26 /**
27  * Version counter DAO service implemtation using Hibernate.
28  * <p>
29  * The object should execute within its own transaction, and is limited to single-thread
30  * entry. If it becomes a bottleneck, the transaction synchronization should be moved
31  * over to reentrant locks and/or the hibernate mappings should be optimized for better
32  * read-write access.
33  *
34  * @author Derek Hulley
35  */

36 public class HibernateVersionCounterDaoServiceImpl extends HibernateDaoSupport implements VersionCounterDaoService
37 {
38     /**
39      * Retrieves or creates a version counter
40      *
41      * @param storeKey
42      * @return Returns a current or new version counter
43      */

44     private VersionCount getVersionCounter(StoreRef storeRef)
45     {
46         StoreKey storeKey = new StoreKey(storeRef.getProtocol(), storeRef.getIdentifier());
47         // get the version counter
48
VersionCount versionCounter = (VersionCount) getHibernateTemplate().get(VersionCountImpl.class, storeKey);
49         // check if it exists
50
if (versionCounter == null)
51         {
52             // create a new one
53
versionCounter = new VersionCountImpl();
54             versionCounter.setKey(storeKey);
55             getHibernateTemplate().save(versionCounter);
56         }
57         return versionCounter;
58     }
59     
60     /**
61      * Get the next available version number for the specified store.
62      *
63      * @param storeRef the version store id
64      * @return the next version number
65      */

66     public synchronized int nextVersionNumber(StoreRef storeRef)
67     {
68         // get the version counter
69
VersionCount versionCounter = getVersionCounter(storeRef);
70         // get an incremented count
71
return versionCounter.incrementVersionCount();
72     }
73     
74     /**
75      * Gets the current version number for the specified store.
76      *
77      * @param storeRef the store reference
78      * @return the current version number, zero if no version yet allocated.
79      */

80     public synchronized int currentVersionNumber(StoreRef storeRef)
81     {
82         // get the version counter
83
VersionCount versionCounter = getVersionCounter(storeRef);
84         // get an incremented count
85
return versionCounter.getVersionCount();
86     }
87     
88     /**
89      * Resets the version number for a the specified store.
90      *
91      * WARNING: calling this method will completely reset the current
92      * version count for the specified store and cannot be undone.
93      *
94      * @param storeRef the store reference
95      */

96     public synchronized void resetVersionNumber(StoreRef storeRef)
97     {
98         // get the version counter
99
VersionCount versionCounter = getVersionCounter(storeRef);
100         // get an incremented count
101
versionCounter.resetVersionCount();
102     }
103 }
104
Popular Tags