001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.rice.krad.dao.impl;
017
018import java.util.List;
019
020import org.apache.commons.lang.StringUtils;
021import org.apache.ojb.broker.query.Criteria;
022import org.apache.ojb.broker.query.QueryByCriteria;
023import org.apache.ojb.broker.query.QueryFactory;
024import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
025import org.kuali.rice.krad.dao.MaintenanceDocumentDao;
026import org.kuali.rice.krad.maintenance.MaintenanceLock;
027import org.kuali.rice.krad.util.KRADPropertyConstants;
028
029/**
030 * This class is the OJB implementation of the MaintenanceDocumentDao interface.
031 */
032public class MaintenanceDocumentDaoOjb extends PlatformAwareDaoBaseOjb implements MaintenanceDocumentDao {
033
034//    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MaintenanceDocumentDaoOjb.class);
035
036    /**
037     * @see org.kuali.rice.krad.dao.MaintenanceDocumentDao#getLockingDocumentNumber(java.lang.String, java.lang.String)
038     */
039    public String getLockingDocumentNumber(String lockingRepresentation, String documentNumber) {
040
041        String lockingDocNumber = "";
042
043        // build the query criteria
044        Criteria criteria = new Criteria();
045        criteria.addEqualTo("lockingRepresentation", lockingRepresentation);
046
047        // if a docHeaderId is specified, then it will be excluded from the
048        // locking representation test.
049        if (StringUtils.isNotBlank(documentNumber)) {
050            criteria.addNotEqualTo(KRADPropertyConstants.DOCUMENT_NUMBER, documentNumber);
051        }
052
053        // attempt to retrieve a document based off this criteria
054        MaintenanceLock maintenanceLock = (MaintenanceLock) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(MaintenanceLock.class, criteria));
055
056        // if a document was found, then there's already one out there pending, and
057        // we consider it 'locked' and we return the docnumber.
058        if (maintenanceLock != null) {
059            lockingDocNumber = maintenanceLock.getDocumentNumber();
060        }
061        return lockingDocNumber;
062    }
063
064//    /**
065//     * Returns all pending maintenance documents locked by the given business object class.
066//     */
067//    public Collection getPendingDocumentsForClass(Class dataObjectClass) {
068//        Criteria criteria = new Criteria();
069//        criteria.addLike("lockingRepresentation", "%" + dataObjectClass.getName() + "%");
070//
071//        Collection maintenanceLocks = getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(MaintenanceLock.class, criteria));
072//
073//        if (!maintenanceLocks.isEmpty()) {
074//            criteria = new Criteria();
075//            Collection<String> documentNumbers = new ArrayList();
076//
077//            for (Object maintenanceLock : maintenanceLocks) {
078//                documentNumbers.add(((MaintenanceLock) maintenanceLock).getDocumentNumber());
079//            }
080//            criteria.addIn("documentNumber", documentNumbers);
081//
082//            MaintenanceDocumentEntry entry = KRADServiceLocatorInternal.getDataDictionaryService().getDataDictionary().getMaintenanceDocumentEntryForBusinessObjectClass(dataObjectClass);
083//            return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(entry.getStandardDocumentBaseClass(), criteria));
084//        } else {
085//            return maintenanceLocks;
086//        }
087//    }
088
089    /**
090     * @see org.kuali.rice.krad.dao.MaintenanceDocumentDao#deleteLocks(java.lang.String)
091     */
092    public void deleteLocks(String documentNumber) {
093        Criteria criteria = new Criteria();
094        criteria.addEqualTo("documentNumber", documentNumber);
095        QueryByCriteria query = new QueryByCriteria(MaintenanceLock.class, criteria);
096        getPersistenceBrokerTemplate().deleteByQuery(query);
097    }
098
099    /**
100     * @see org.kuali.rice.krad.dao.MaintenanceDocumentDao#storeLocks(java.util.List)
101     */
102    public void storeLocks(List<MaintenanceLock> maintenanceLocks) {
103        for (MaintenanceLock maintenanceLock : maintenanceLocks) {
104            getPersistenceBrokerTemplate().store(maintenanceLock);
105        }
106    }
107
108}