001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.dao.ojb;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.List;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.apache.ojb.broker.query.Criteria;
024: import org.apache.ojb.broker.query.QueryByCriteria;
025: import org.apache.ojb.broker.query.QueryFactory;
026: import org.kuali.RicePropertyConstants;
027: import org.kuali.core.dao.MaintenanceDocumentDao;
028: import org.kuali.core.document.MaintenanceDocumentBase;
029: import org.kuali.core.document.MaintenanceLock;
030:
031: /**
032: * This class is the OJB implementation of the MaintenanceDocumentDao interface.
033: *
034: *
035: */
036: public class MaintenanceDocumentDaoOjb extends PlatformAwareDaoBaseOjb
037: implements MaintenanceDocumentDao {
038:
039: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
040: .getLogger(MaintenanceDocumentDaoOjb.class);
041:
042: /**
043: * @see org.kuali.core.dao.MaintenanceDocumentDao#getLockingDocumentNumber(java.lang.String, java.lang.String)
044: */
045: public String getLockingDocumentNumber(
046: String lockingRepresentation, String documentNumber) {
047:
048: String lockingDocNumber = "";
049:
050: // build the query criteria
051: Criteria criteria = new Criteria();
052: criteria.addEqualTo("lockingRepresentation",
053: lockingRepresentation);
054:
055: // if a docHeaderId is specified, then it will be excluded from the
056: // locking representation test.
057: if (StringUtils.isNotBlank(documentNumber)) {
058: criteria.addNotEqualTo(
059: RicePropertyConstants.DOCUMENT_NUMBER,
060: documentNumber);
061: }
062:
063: // attempt to retrieve a document based off this criteria
064: MaintenanceLock maintenanceLock = (MaintenanceLock) getPersistenceBrokerTemplate()
065: .getObjectByQuery(
066: QueryFactory.newQuery(MaintenanceLock.class,
067: criteria));
068:
069: // if a document was found, then there's already one out there pending, and
070: // we consider it 'locked' and we return the docnumber.
071: if (maintenanceLock != null) {
072: lockingDocNumber = maintenanceLock.getDocumentNumber();
073: }
074: return lockingDocNumber;
075: }
076:
077: /**
078: * Returns all pending maintenance documents locked by the given business object class.
079: */
080: public Collection getPendingDocumentsForClass(
081: Class businessObjectClass) {
082: Criteria criteria = new Criteria();
083: criteria.addLike("lockingRepresentation", "%"
084: + businessObjectClass.getName() + "%");
085:
086: Collection maintenanceLocks = getPersistenceBrokerTemplate()
087: .getCollectionByQuery(
088: QueryFactory.newQuery(MaintenanceLock.class,
089: criteria));
090:
091: if (!maintenanceLocks.isEmpty()) {
092: criteria = new Criteria();
093: Collection<String> documentNumbers = new ArrayList();
094:
095: for (Object maintenanceLock : maintenanceLocks) {
096: documentNumbers.add(((MaintenanceLock) maintenanceLock)
097: .getDocumentNumber());
098: }
099: criteria.addIn("documentNumber", documentNumbers);
100:
101: return getPersistenceBrokerTemplate().getCollectionByQuery(
102: QueryFactory.newQuery(
103: MaintenanceDocumentBase.class, criteria));
104: } else {
105: return maintenanceLocks;
106: }
107: }
108:
109: /**
110: * @see org.kuali.core.dao.MaintenanceDocumentDao#deleteLocks(java.lang.String)
111: */
112: public void deleteLocks(String documentNumber) {
113: Criteria criteria = new Criteria();
114: criteria.addEqualTo("documentNumber", documentNumber);
115: QueryByCriteria query = new QueryByCriteria(
116: MaintenanceLock.class, criteria);
117: getPersistenceBrokerTemplate().deleteByQuery(query);
118: }
119:
120: /**
121: * @see org.kuali.core.dao.MaintenanceDocumentDao#storeLocks(java.util.List)
122: */
123: public void storeLocks(List<MaintenanceLock> maintenanceLocks) {
124: for (MaintenanceLock maintenanceLock : maintenanceLocks) {
125: getPersistenceBrokerTemplate().store(maintenanceLock);
126: }
127: }
128:
129: }
|