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.util.Collection;
019    
020    import org.kuali.kfs.module.ar.businessobject.Customer;
021    import org.kuali.kfs.module.ar.dataaccess.CustomerDao;
022    import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
023    import org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService;
024    import org.kuali.kfs.module.ar.document.service.CustomerService;
025    import org.kuali.rice.kns.bo.Note;
026    import org.kuali.rice.kns.service.BusinessObjectService;
027    import org.kuali.rice.kns.service.NoteService;
028    import org.kuali.rice.kns.service.SequenceAccessorService;
029    import org.springframework.transaction.annotation.Transactional;
030    
031    @Transactional
032    public class CustomerServiceImpl implements CustomerService {
033    
034        private CustomerDao customerDao;
035        private SequenceAccessorService sequenceAccessorService;
036        private BusinessObjectService businessObjectService;
037        private CustomerInvoiceDocumentService customerInvoiceDocumentService;
038        private NoteService noteService;
039        protected static final String CUSTOMER_NUMBER_SEQUENCE = "CUST_NBR_SEQ";
040        
041        /**
042         * @see org.kuali.kfs.module.ar.document.service.CustomerService#getByPrimaryKey(java.lang.String)
043         */
044        public Customer getByPrimaryKey(String customerNumber) {
045           return customerDao.getByPrimaryId(customerNumber);
046        }
047    
048        public Customer getByTaxNumber(String taxNumber) {
049            return customerDao.getByTaxNumber(taxNumber);
050        }
051        
052        public CustomerDao getCustomerDao() {
053            return customerDao;
054        }
055    
056        public void setCustomerDao(CustomerDao customerDao) {
057            this.customerDao = customerDao;
058        }
059    
060        /**
061         * @see org.kuali.kfs.module.ar.document.service.CustomerService#getNextCustomerNumber(org.kuali.kfs.module.ar.businessobject.Customer)
062         */
063        public String getNextCustomerNumber(Customer newCustomer) {
064            try {
065                Long customerNumberSuffix = sequenceAccessorService.getNextAvailableSequenceNumber(
066                        CUSTOMER_NUMBER_SEQUENCE, Customer.class);
067                String customerNumberPrefix = newCustomer.getCustomerName().substring(0, 3);
068                String customerNumber = customerNumberPrefix + String.valueOf(customerNumberSuffix);
069        
070                return customerNumber;
071            } catch(StringIndexOutOfBoundsException sibe) {
072                // The customer number generation expects all customer names to be at least three characters long.
073                throw new StringIndexOutOfBoundsException("Customer name is less than three characters in length.");
074            }
075        }
076    
077        /**
078         * This method gets the sequenceAccessorService
079         * 
080         * @return the sequenceAccessorService
081         */
082        public SequenceAccessorService getSequenceAccessorService() {
083            return sequenceAccessorService;
084        }
085    
086        /**
087         * This method sets the sequenceAccessorService
088         * 
089         * @param sequenceAccessorService
090         */
091        public void setSequenceAccessorService(SequenceAccessorService sequenceAccessorService) {
092            this.sequenceAccessorService = sequenceAccessorService;
093        }
094    
095        /**
096         * @see org.kuali.kfs.module.ar.document.service.CustomerService#getCustomerByName(java.lang.String)
097         */
098        public Customer getCustomerByName(String customerName) {
099            return customerDao.getByName(customerName);
100            }
101            
102        public BusinessObjectService getBusinessObjectService() {
103            return businessObjectService;
104        }
105    
106        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
107            this.businessObjectService = businessObjectService;
108        }
109    
110        public CustomerInvoiceDocumentService getCustomerInvoiceDocumentService() {
111            return customerInvoiceDocumentService;
112        }
113    
114        public void setCustomerInvoiceDocumentService(CustomerInvoiceDocumentService customerInvoiceDocumentService) {
115            this.customerInvoiceDocumentService = customerInvoiceDocumentService;
116        }
117    
118        public Collection<CustomerInvoiceDocument> getInvoicesForCustomer(Customer customer) {
119            Collection<CustomerInvoiceDocument> invoices = null;
120            if(null != customer) {
121                invoices = getInvoicesForCustomer(customer.getCustomerNumber());
122            }
123            return invoices;
124        }
125    
126        public Collection<CustomerInvoiceDocument> getInvoicesForCustomer(String customerNumber) {
127            return customerInvoiceDocumentService.getCustomerInvoiceDocumentsByCustomerNumber(customerNumber);
128        }
129    
130        public void createCustomerNote(String customerNumber, String customerNote) {
131            Customer customer = getByPrimaryKey(customerNumber);
132            Note note = new Note();
133            note.setNoteText(customerNote);
134            try {
135                note = noteService.createNote(note, customer);
136                noteService.save(note);
137            } catch (Exception e){
138                throw new RuntimeException("Problems creating note for Customer " + customerNumber);
139            }
140        }
141    
142        public NoteService getNoteService() {
143            return noteService;
144        }
145    
146        public void setNoteService(NoteService noteService) {
147            this.noteService = noteService;
148        }
149    
150    }