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 java.util.Iterator;
019
020 import org.apache.commons.lang.StringUtils;
021 import org.apache.ojb.broker.query.Criteria;
022 import org.apache.ojb.broker.query.QueryFactory;
023 import org.apache.ojb.broker.query.ReportQueryByCriteria;
024 import org.kuali.kfs.coa.businessobject.Account;
025 import org.kuali.kfs.coa.businessobject.AccountDelegate;
026 import org.kuali.kfs.coa.dataaccess.AccountDelegateDao;
027 import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
028 import org.kuali.rice.kns.document.MaintenanceLock;
029 import org.kuali.rice.kns.service.DateTimeService;
030 import org.kuali.rice.kns.util.KNSPropertyConstants;
031
032 /**
033 * This class is the OJB implementation of the AccountDelegateDao.
034 */
035 public class AccountDelegateDaoOjb extends PlatformAwareDaoBaseOjb implements AccountDelegateDao {
036 private DateTimeService dateTimeService;
037
038 /**
039 *
040 * @see org.kuali.kfs.coa.dataaccess.AccountDelegateDao#getLockingDocumentNumber(java.lang.String, java.lang.String)
041 */
042
043 public String getLockingDocumentNumber(String lockingRepresentation, String documentNumber) {
044 String lockingDocNumber = "";
045
046 // build the query criteria
047 Criteria criteria = new Criteria();
048 criteria.addEqualTo("lockingRepresentation", lockingRepresentation);
049
050 // if a docHeaderId is specified, then it will be excluded from the
051 // locking representation test.
052 if (StringUtils.isNotBlank(documentNumber)) {
053 criteria.addNotEqualTo(KNSPropertyConstants.DOCUMENT_NUMBER, documentNumber);
054 }
055
056 // attempt to retrieve a document based off this criteria
057 MaintenanceLock maintenanceLock = (MaintenanceLock) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(MaintenanceLock.class, criteria));
058
059 // if a document was found, then there's already one out there pending, and
060 // we consider it 'locked' and we return the docnumber.
061 if (maintenanceLock != null) {
062 lockingDocNumber = maintenanceLock.getDocumentNumber();
063 }
064 return lockingDocNumber;
065 }
066
067 /**
068 * @see org.kuali.kfs.coa.dataaccess.AccountDelegateDao#getAccountDelegationsForPerson(java.lang.String)
069 */
070 public Iterator<AccountDelegate> getAccountDelegationsForPerson(String principalId, boolean primary) {
071 Criteria criteria = new Criteria();
072 criteria.addEqualTo("accountDelegateSystemId", principalId);
073 criteria.addEqualTo("active", "Y");
074 criteria.addEqualTo("accountsDelegatePrmrtIndicator", primary);
075
076 return (Iterator<AccountDelegate>)getPersistenceBrokerTemplate().getIteratorByQuery(QueryFactory.newQuery(AccountDelegate.class, criteria));
077 }
078
079 /**
080 * @see org.kuali.kfs.coa.dataaccess.AccountDelegateDao#isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(java.lang.String)
081 */
082 public boolean isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(String principalId) {
083 return queryPrincipalIsAccountDelegate(principalId, true);
084 }
085
086 /**
087 * @see org.kuali.kfs.coa.dataaccess.AccountDelegateDao#isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(java.lang.String)
088 */
089 public boolean isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(String principalId) {
090 return queryPrincipalIsAccountDelegate(principalId, false);
091 }
092
093 /**
094 * Determines if any non-closed accounts exist where the principal id is an account delegate
095 * @param principalId the principal id to check
096 * @param primary whether to check primary delegations (if true) or secondary delegations (if false)
097 * @return true if the principal has an account delegation
098 */
099 protected boolean queryPrincipalIsAccountDelegate(String principalId, boolean primary) {
100 Criteria criteria = new Criteria();
101 criteria.addEqualTo("accountDelegateSystemId", principalId);
102 criteria.addEqualTo("accountsDelegatePrmrtIndicator", (primary ? "Y" : "N"));
103 criteria.addEqualTo("active", "Y");
104 criteria.addEqualTo("account.active", "Y");
105 criteria.addLessOrEqualThan("accountDelegateStartDate", getDateTimeService().getCurrentSqlDate());
106
107 ReportQueryByCriteria reportQuery = QueryFactory.newReportQuery(AccountDelegate.class, criteria);
108 reportQuery.setAttributes(new String[] { "count(*)" });
109
110 int resultCount = 0;
111 //TODO: getReportQueryIteratorByQuery can be changed to getCount...
112 Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(reportQuery);
113 while (iter.hasNext()) {
114 final Object[] results = (Object[])iter.next();
115 resultCount = (results[0] instanceof Number) ? ((Number)results[0]).intValue() : new Integer(results[0].toString()).intValue();
116 }
117 return resultCount > 0;
118 }
119
120 /**
121 * Gets the dateTimeService attribute.
122 * @return Returns the dateTimeService.
123 */
124 public DateTimeService getDateTimeService() {
125 return dateTimeService;
126 }
127
128 /**
129 * Sets the dateTimeService attribute value.
130 * @param dateTimeService The dateTimeService to set.
131 */
132 public void setDateTimeService(DateTimeService dateTimeService) {
133 this.dateTimeService = dateTimeService;
134 }
135 }