001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.coa.dataaccess.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.ojb.broker.query.Criteria;
020    import org.apache.ojb.broker.query.QueryFactory;
021    import org.kuali.kfs.coa.dataaccess.AccountDelegateGlobalDao;
022    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
023    import org.kuali.rice.kns.document.MaintenanceLock;
024    import org.kuali.rice.kns.util.KNSPropertyConstants;
025    
026    /**
027     * This class is the OJB implementation of AccountDelegateGlobalDao
028     */
029    public class AccountDelegateGlobalDaoOjb extends PlatformAwareDaoBaseOjb implements AccountDelegateGlobalDao {
030    
031       /**
032        * 
033        * @see org.kuali.kfs.coa.dataaccess.AccountDelegateGlobalDao#getLockingDocumentNumber(java.lang.String, java.lang.String)
034        */
035        
036        public String getLockingDocumentNumber(String lockingRepresentation, String documentNumber) {
037            String lockingDocNumber = "";
038    
039            lockingRepresentation = convertForLikeCriteria(lockingRepresentation);
040            
041            // build the query criteria
042            Criteria criteria = new Criteria();
043            criteria.addLike("lockingRepresentation", lockingRepresentation);
044    
045            // if a docHeaderId is specified, then it will be excluded from the
046            // locking representation test.
047            if (StringUtils.isNotBlank(documentNumber)) {
048                criteria.addNotEqualTo(KNSPropertyConstants.DOCUMENT_NUMBER, documentNumber);
049            }
050    
051            // attempt to retrieve a document based off this criteria
052            MaintenanceLock maintenanceLock = (MaintenanceLock) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(MaintenanceLock.class, criteria));
053    
054            // if a document was found, then there's already one out there pending, and
055            // we consider it 'locked' and we return the docnumber.
056            if (maintenanceLock != null) {
057                lockingDocNumber = maintenanceLock.getDocumentNumber();
058            }
059            return lockingDocNumber;
060        }
061        
062        /**
063         * Parses the lockingRepresentation and replaces the document type with a wild card.
064         * 
065         * @param lockingRepresentation The String representation of the MaintenanceLock created by this record
066         * @return The String representation of MaintenanceLock with the financialDocumentTypeCode replaced with a wildcard(%).
067         */
068        
069        protected String convertForLikeCriteria(String lockingRepresentation) {
070            //org.kuali.kfs.coa.businessobject.AccountDelegate!!chartOfAccountsCode^^BL::accountNumber^^1031400::financialDocumentTypeCode^^IB::accountsDelegatePrmrtIndicator^^true
071            String[] values = StringUtils.split(lockingRepresentation, "::");
072            StringBuilder sb = new StringBuilder();
073            for (String val : values) {
074                if (val.startsWith("financialDocumentTypeCode")) {
075                    String[] meh = StringUtils.split(val, "^^");
076                    meh[1] = "%";
077                    val = meh[0] +"^^"+meh[1];
078                    sb.append(val);
079                    break;
080                } else {
081                    sb.append(val+"::");
082                }
083            }
084            
085            return sb.toString();
086        }
087    }