001/* 002 * Copyright 2009 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.ole.coa.dataaccess.impl; 017 018import java.sql.Date; 019import java.util.Iterator; 020 021import org.apache.commons.lang.StringUtils; 022import org.apache.ojb.broker.query.Criteria; 023import org.apache.ojb.broker.query.QueryFactory; 024import org.apache.ojb.broker.query.ReportQueryByCriteria; 025import org.kuali.ole.coa.businessobject.AccountDelegate; 026import org.kuali.ole.coa.dataaccess.AccountDelegateDao; 027import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 028import org.kuali.rice.krad.maintenance.MaintenanceLock; 029import org.kuali.rice.krad.util.KRADPropertyConstants; 030 031/** 032 * This class is the OJB implementation of the AccountDelegateDao. 033 */ 034public class AccountDelegateDaoOjb extends PlatformAwareDaoBaseOjb implements AccountDelegateDao { 035 036 /** 037 * @see org.kuali.ole.coa.dataaccess.AccountDelegateDao#getLockingDocumentNumber(java.lang.String, java.lang.String) 038 */ 039 040 public String getLockingDocumentNumber(String lockingRepresentation, String documentNumber) { 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 * @see org.kuali.ole.coa.dataaccess.AccountDelegateDao#getAccountDelegationsForPerson(java.lang.String) 066 */ 067 public Iterator<AccountDelegate> getAccountDelegationsForPerson(String principalId, boolean primary) { 068 Criteria criteria = new Criteria(); 069 criteria.addEqualTo("accountDelegateSystemId", principalId); 070 criteria.addEqualTo("active", "Y"); 071 criteria.addEqualTo("accountsDelegatePrmrtIndicator", primary); 072 073 return (Iterator<AccountDelegate>) getPersistenceBrokerTemplate().getIteratorByQuery(QueryFactory.newQuery(AccountDelegate.class, criteria)); 074 } 075 076 /** 077 * @see org.kuali.ole.coa.dataaccess.AccountDelegateDao#isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(java.lang.String, java.sql.Date) 078 */ 079 public boolean isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(String principalId, Date currentSqlDate) { 080 return queryPrincipalIsAccountDelegate(principalId, true, currentSqlDate); 081 } 082 083 /** 084 * @see org.kuali.ole.coa.dataaccess.AccountDelegateDao#isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(java.lang.String, java.sql.Date) 085 */ 086 public boolean isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(String principalId, Date currentSqlDate) { 087 return queryPrincipalIsAccountDelegate(principalId, false, currentSqlDate); 088 } 089 090 /** 091 * Determines if any non-closed accounts exist where the principal id is an account delegate 092 * 093 * @param principalId the principal id to check 094 * @param primary whether to check primary delegations (if true) or secondary delegations (if false) 095 * @param currentSqlDate current sql date 096 * @return true if the principal has an account delegation 097 */ 098 protected boolean queryPrincipalIsAccountDelegate(String principalId, boolean primary, Date currentSqlDate) { 099 Criteria criteria = new Criteria(); 100 criteria.addEqualTo("accountDelegateSystemId", principalId); 101 criteria.addEqualTo("accountsDelegatePrmrtIndicator", (primary ? "Y" : "N")); 102 criteria.addEqualTo("active", "Y"); 103 criteria.addEqualTo("account.active", "Y"); 104 criteria.addLessOrEqualThan("accountDelegateStartDate", currentSqlDate); 105 106 ReportQueryByCriteria reportQuery = QueryFactory.newReportQuery(AccountDelegate.class, criteria); 107 reportQuery.setAttributes(new String[] { "count(*)" }); 108 109 int resultCount = 0; 110 // TODO: getReportQueryIteratorByQuery can be changed to getCount... 111 Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(reportQuery); 112 while (iter.hasNext()) { 113 final Object[] results = (Object[]) iter.next(); 114 resultCount = (results[0] instanceof Number) ? ((Number) results[0]).intValue() : new Integer(results[0].toString()).intValue(); 115 } 116 return resultCount > 0; 117 } 118}