View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.coa.dataaccess.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.ojb.broker.query.Criteria;
20  import org.apache.ojb.broker.query.QueryFactory;
21  import org.kuali.ole.coa.dataaccess.AccountDelegateGlobalDao;
22  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
23  import org.kuali.rice.krad.maintenance.MaintenanceLock;
24  import org.kuali.rice.krad.util.KRADPropertyConstants;
25  
26  /**
27   * This class is the OJB implementation of AccountDelegateGlobalDao
28   */
29  public class AccountDelegateGlobalDaoOjb extends PlatformAwareDaoBaseOjb implements AccountDelegateGlobalDao {
30  
31     /**
32      * 
33      * @see org.kuali.ole.coa.dataaccess.AccountDelegateGlobalDao#getLockingDocumentNumber(java.lang.String, java.lang.String)
34      */
35      
36      public String getLockingDocumentNumber(String lockingRepresentation, String documentNumber) {
37          String lockingDocNumber = "";
38  
39          lockingRepresentation = convertForLikeCriteria(lockingRepresentation);
40          
41          // build the query criteria
42          Criteria criteria = new Criteria();
43          criteria.addLike("lockingRepresentation", lockingRepresentation);
44  
45          // if a docHeaderId is specified, then it will be excluded from the
46          // locking representation test.
47          if (StringUtils.isNotBlank(documentNumber)) {
48              criteria.addNotEqualTo(KRADPropertyConstants.DOCUMENT_NUMBER, documentNumber);
49          }
50  
51          // attempt to retrieve a document based off this criteria
52          MaintenanceLock maintenanceLock = (MaintenanceLock) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(MaintenanceLock.class, criteria));
53  
54          // if a document was found, then there's already one out there pending, and
55          // we consider it 'locked' and we return the docnumber.
56          if (maintenanceLock != null) {
57              lockingDocNumber = maintenanceLock.getDocumentNumber();
58          }
59          return lockingDocNumber;
60      }
61      
62      /**
63       * Parses the lockingRepresentation and replaces the document type with a wild card.
64       * 
65       * @param lockingRepresentation The String representation of the MaintenanceLock created by this record
66       * @return The String representation of MaintenanceLock with the financialDocumentTypeCode replaced with a wildcard(%).
67       */
68      
69      protected String convertForLikeCriteria(String lockingRepresentation) {
70          //org.kuali.ole.coa.businessobject.AccountDelegate!!chartOfAccountsCode^^BL::accountNumber^^1031400::financialDocumentTypeCode^^IB::accountsDelegatePrmrtIndicator^^true
71          String[] values = StringUtils.split(lockingRepresentation, "::");
72          StringBuilder sb = new StringBuilder();
73          for (String val : values) {
74              if (val.startsWith("financialDocumentTypeCode")) {
75                  String[] meh = StringUtils.split(val, "^^");
76                  meh[1] = "%";
77                  val = meh[0] +"^^"+meh[1];
78                  sb.append(val);
79                  break;
80              } else {
81                  sb.append(val+"::");
82              }
83          }
84          
85          return sb.toString();
86      }
87  }