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.module.ar.dataaccess.impl;
017    
018    import java.math.BigDecimal;
019    import java.util.Iterator;
020    
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.module.ar.ArConstants;
025    import org.kuali.kfs.module.ar.ArKeyConstants;
026    import org.kuali.kfs.module.ar.ArPropertyConstants;
027    import org.kuali.kfs.module.ar.businessobject.CustomerAddress;
028    import org.kuali.kfs.module.ar.dataaccess.CustomerAddressDao;
029    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
030    import org.kuali.rice.kns.util.TransactionalServiceUtils;
031    
032    public class CustomerAddressDaoOjb extends PlatformAwareDaoBaseOjb implements CustomerAddressDao {
033    
034        public CustomerAddress getPrimaryAddress(String customerNumber) {
035            Criteria criteria = new Criteria();
036            criteria.addEqualTo("customerNumber", customerNumber==null?customerNumber:customerNumber.toUpperCase());
037            criteria.addEqualTo("customerAddressTypeCode", ArKeyConstants.CustomerConstants.CUSTOMER_ADDRESS_TYPE_CODE_PRIMARY);
038    
039            return (CustomerAddress) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(CustomerAddress.class, criteria));
040        }
041    
042        public Integer getMaxSquenceNumber(String customerNumber) {
043            Criteria criteria = new Criteria();
044    
045            criteria.addEqualTo(ArPropertyConstants.CustomerFields.CUSTOMER_NUMBER, customerNumber==null?customerNumber:customerNumber.toUpperCase());
046            ReportQueryByCriteria query = QueryFactory.newReportQuery(CustomerAddress.class, criteria);
047            query.setAttributes(new String[] { "max(" + ArPropertyConstants.CustomerFields.CUSTOMER_ADDRESS_IDENTIFIER + ")" });
048            Iterator<?> iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
049            Integer maxSequenceNumber = Integer.valueOf(0);
050    
051            if (iterator.hasNext()) {
052                Object[] data = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator);
053                if (data[0] != null) {
054                    maxSequenceNumber = ((BigDecimal) data[0]).intValue();
055                }
056            }
057            return maxSequenceNumber;
058        }
059    }