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.document.service.impl;
017    
018    import java.sql.Timestamp;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.module.ar.businessobject.CustomerAddress;
024    import org.kuali.kfs.module.ar.dataaccess.CustomerAddressDao;
025    import org.kuali.kfs.module.ar.document.service.CustomerAddressService;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.rice.kns.service.BusinessObjectService;
028    import org.kuali.rice.kns.service.DateTimeService;
029    import org.kuali.rice.kns.service.SequenceAccessorService;
030    import org.kuali.rice.kns.util.ObjectUtils;
031    import org.springframework.transaction.annotation.Transactional;
032    
033    @Transactional
034    public class CustomerAddressServiceImpl implements CustomerAddressService {
035        private CustomerAddressDao customerAddressDao;
036    
037        private BusinessObjectService businessObjectService;
038        private SequenceAccessorService sequenceAccessorService;
039    
040        protected static final String CUST_ADDR_ID_SEQ = "CUST_ADDR_ID_SEQ";
041    
042        @SuppressWarnings("unchecked")
043        public CustomerAddress getByPrimaryKey(String customerNumber, Integer customerAddressIdentifier) {
044    
045            CustomerAddress customerAddress = null;
046            if (StringUtils.isNotBlank(customerNumber) && ObjectUtils.isNotNull(customerAddressIdentifier)) {
047                Map criteria = new HashMap();
048                criteria.put("customerNumber", customerNumber);
049                criteria.put("customerAddressIdentifier", customerAddressIdentifier);
050    
051                customerAddress = (CustomerAddress) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(CustomerAddress.class, criteria);
052            }
053            return customerAddress;
054        }
055        
056        @SuppressWarnings("unchecked")
057        public CustomerAddress getPrimaryAddress(String customerNumber) {
058            CustomerAddress primaryAddress = null;
059            if (StringUtils.isNotBlank(customerNumber)) {
060                Map criteria = new HashMap();
061                criteria.put("customerNumber", customerNumber);
062                criteria.put("customerAddressTypeCode", "P");
063                
064                primaryAddress = (CustomerAddress)SpringContext.getBean(BusinessObjectService.class).findMatching(CustomerAddress.class, criteria).iterator().next();
065            }
066            
067            return primaryAddress;
068        }
069    
070        /**
071         * This method returns true if customer address is active
072         * 
073         * @param customerNumber
074         * @param customerAddressIdentifier
075         * @return
076         */
077        public boolean customerAddressActive(String customerNumber, Integer customerAddressIdentifier) {
078            
079            CustomerAddress customerAddress = getByPrimaryKey(customerNumber, customerAddressIdentifier);
080    
081            if (ObjectUtils.isNotNull(customerAddress)) {
082                if (ObjectUtils.isNotNull(customerAddress.getCustomerAddressEndDate())) {
083                    Timestamp currentDateTimestamp = new Timestamp(SpringContext.getBean(DateTimeService.class).getCurrentDate().getTime());
084                    Timestamp addressEndDateTimestamp = new Timestamp(customerAddress.getCustomerAddressEndDate().getTime());
085                    if (addressEndDateTimestamp.before(currentDateTimestamp)) {
086                        return false;
087                    }
088                }
089            }
090            return true;
091        }
092    
093        public BusinessObjectService getBusinessObjectService() {
094            return businessObjectService;
095        }
096    
097        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
098            this.businessObjectService = businessObjectService;
099        }
100    
101        /**
102         * @see org.kuali.kfs.module.ar.document.service.CustomerAddressService#customerAddressExists(java.lang.String, java.lang.Integer)
103         */
104        public boolean customerAddressExists(String customerNumber, Integer customerAddressIdentifier) {
105            return ObjectUtils.isNotNull(getByPrimaryKey(customerNumber, customerAddressIdentifier));
106        }
107    
108    
109        /**
110         * This method sets customer address dao
111         * 
112         * @return
113         */
114        public CustomerAddressDao getCustomerAddressDao() {
115            return customerAddressDao;
116        }
117    
118        /**
119         * This method gets customer address dao
120         * 
121         * @param customerAddressDao
122         */
123        public void setCustomerAddressDao(CustomerAddressDao customerAddressDao) {
124            this.customerAddressDao = customerAddressDao;
125        }
126    
127        /**
128         * @see org.kuali.kfs.module.ar.document.service.CustomerAddressService#getNextCustomerAddressIdentifier()
129         */
130        public Integer getNextCustomerAddressIdentifier() {
131    
132            Long nextId = sequenceAccessorService.getNextAvailableSequenceNumber(
133                    CUST_ADDR_ID_SEQ, CustomerAddress.class);
134    
135            return nextId.intValue();
136    
137        }
138    
139        /**
140         * This method gets the sequenceAccessorService
141         * 
142         * @return
143         */
144        public SequenceAccessorService getSequenceAccessorService() {
145            return sequenceAccessorService;
146        }
147    
148        /**
149         * This method sets the sequenceAccessorService
150         * 
151         * @param sequenceAccessorService
152         */
153        public void setSequenceAccessorService(SequenceAccessorService sequenceAccessorService) {
154            this.sequenceAccessorService = sequenceAccessorService;
155        }
156    
157    }