001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/content/tags/sakai_2-4-1/content-impl/impl/src/java/org/sakaiproject/content/impl/LockManagerImpl.java $
003: * $Id: LockManagerImpl.java 8914 2006-05-04 04:36:26Z lance@indiana.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.content.impl;
021:
022: import java.util.Collection;
023: import java.util.Date;
024: import java.util.List;
025:
026: import org.sakaiproject.content.api.Lock;
027: import org.sakaiproject.content.api.LockManager;
028: import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;
029: import org.springframework.orm.hibernate3.HibernateSystemException;
030: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
031:
032: public class LockManagerImpl extends HibernateDaoSupport implements
033: LockManager {
034:
035: protected final static org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
036: .getLog(LockManagerImpl.class);
037:
038: /*
039: * (non-Javadoc)
040: *
041: * @see org.sakaiproject.component.legacy.content.LockManagerIntf#lockObject(java.lang.String, java.lang.String, java.lang.String, boolean)
042: */
043: public void lockObject(String assetId, String qualifierId,
044: String reason, boolean system) {
045: Lock newLock = findOrCreateLock(assetId, qualifierId, false);
046: newLock.setAsset(assetId);
047: newLock.setQualifier(qualifierId);
048: newLock.setDateAdded(now());
049: newLock.setActive(true);
050: newLock.setReason(reason);
051: newLock.setSystem(true);
052: getHibernateTemplate().saveOrUpdate(newLock);
053: }
054:
055: /*
056: * (non-Javadoc)
057: *
058: * @see org.sakaiproject.component.legacy.content.LockManagerIntf#removeLock(java.lang.String, java.lang.String)
059: */
060: public void removeLock(String assetId, String qualifierId) {
061: Lock oldLock = findOrCreateLock(assetId, qualifierId, true);
062: oldLock.setActive(false);
063: oldLock.setDateRemoved(now());
064: getHibernateTemplate().saveOrUpdate(oldLock);
065: }
066:
067: protected Lock findLock(String assetId, String qualifierId) {
068: try {
069: getHibernateTemplate().setCacheQueries(true);
070: return (Lock) safePopList(getHibernateTemplate()
071: .findByNamedQuery("getLock",
072: new Object[] { assetId, qualifierId }));
073: } catch (HibernateSystemException e) {
074: logger.debug("lock with assetId=" + assetId
075: + " and qualifierId= " + qualifierId
076: + "not found: " + e.getMessage());
077: return null;
078: } catch (HibernateObjectRetrievalFailureException e) {
079: logger.debug("lock with assetId=" + assetId
080: + " and qualifierId= " + qualifierId
081: + "not found: " + e.getMessage());
082: return null;
083: }
084: }
085:
086: protected Lock findOrCreateLock(String assetId, String qualifierId,
087: boolean expected) {
088: Lock lock = findLock(assetId, qualifierId);
089: if (lock == null) {
090: if (expected == true) {
091: logger.warn("expected Lock not found: " + assetId
092: + ", " + qualifierId);
093: }
094: return new org.sakaiproject.content.hbm.Lock();
095: }
096:
097: if (expected == false) {
098: logger.warn("Lock not expected, but found anyway: "
099: + assetId + ", " + qualifierId);
100: }
101: return lock;
102:
103: }
104:
105: protected Object safePopList(List list) {
106: if (list == null)
107: return null;
108: if (list.size() == 0)
109: return null;
110: return list.get(0);
111: }
112:
113: protected Date now() {
114: return java.util.Calendar.getInstance().getTime();
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see org.sakaiproject.component.legacy.content.LockManagerIntf#getLocks(java.lang.String)
121: */
122: public Collection getLocks(String assetId) {
123: Collection locks = null;
124: if (logger.isDebugEnabled()) {
125: logger.debug("getLocks(" + assetId + ")");
126: }
127: try {
128: getHibernateTemplate().setCacheQueries(true);
129: locks = getHibernateTemplate().findByNamedQuery(
130: "activeByAsset", assetId);
131: } catch (HibernateObjectRetrievalFailureException e) {
132: logger.error("", e);
133: throw new RuntimeException(e);
134: }
135: if (locks == null)
136: return null;
137: if (locks.isEmpty())
138: return null;
139: return locks;
140: }
141:
142: // TODO create a faster query (don't need all rows)
143: /*
144: * (non-Javadoc)
145: *
146: * @see org.sakaiproject.component.legacy.content.LockManagerIntf#isLocked(java.lang.String)
147: */
148: public boolean isLocked(String assetId) {
149: Collection c = getLocks(assetId);
150: if (c == null)
151: return false;
152: return true;
153: }
154:
155: /*
156: * (non-Javadoc)
157: *
158: * @see org.sakaiproject.component.legacy.content.LockManagerIntf#removeAllLocks(java.lang.String)
159: */
160: public void removeAllLocks(String qualifier) {
161: Collection locks = getQualifierLocks(qualifier);
162: if (locks != null) {
163: getHibernateTemplate().deleteAll(locks);
164: }
165: }
166:
167: protected Collection getQualifierLocks(String qualifier) {
168: Collection locks = null;
169: if (logger.isDebugEnabled()) {
170: logger.debug("getLocks(" + qualifier + ")");
171: }
172: try {
173: getHibernateTemplate().setCacheQueries(true);
174: locks = getHibernateTemplate().findByNamedQuery(
175: "activeByQualifier", qualifier);
176: } catch (HibernateObjectRetrievalFailureException e) {
177: logger.error("", e);
178: throw new RuntimeException(e);
179: }
180: if (locks == null)
181: return null;
182: if (locks.isEmpty())
183: return null;
184: return locks;
185: }
186: }
|