View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.tem.document.service.impl;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.kuali.kfs.module.tem.TemPropertyConstants.TemProfileProperties;
27  import org.kuali.kfs.module.tem.businessobject.TemProfileArranger;
28  import org.kuali.kfs.module.tem.document.TravelArrangerDocument;
29  import org.kuali.kfs.module.tem.document.service.TravelArrangerDocumentService;
30  import org.kuali.rice.krad.service.BusinessObjectService;
31  import org.kuali.rice.krad.util.KRADPropertyConstants;
32  import org.kuali.rice.krad.util.ObjectUtils;
33  
34  @SuppressWarnings("rawtypes")
35  public class TravelArrangerDocumentServiceImpl implements TravelArrangerDocumentService {
36  
37      protected BusinessObjectService businessObjectService;
38  
39      @Override
40      public void createTravelProfileArranger(TravelArrangerDocument arrangerDoc) {
41          Integer profileId = arrangerDoc.getProfileId();
42          String arrangerId = arrangerDoc.getArrangerId();
43  
44          TemProfileArranger profileArranger = findProfileArranger(arrangerId, profileId);
45          if(ObjectUtils.isNull(profileArranger)) {
46             profileArranger = createNewTravelProfileArranger(arrangerDoc);
47          } else {
48              //update the existing profile
49              profileArranger.setActive(true);
50              profileArranger.setPrimary(arrangerDoc.getPrimaryInd());
51              profileArranger.setTaInd(arrangerDoc.getTaInd());
52              profileArranger.setTrInd(arrangerDoc.getTrInd());
53          }
54  
55          businessObjectService.save(profileArranger);
56      }
57  
58      @Override
59      public void inactivateTravelProfileArranger(TravelArrangerDocument arrangerDoc) {
60          Integer profileId = arrangerDoc.getProfileId();
61          String arrangerId = arrangerDoc.getArrangerId();
62          TemProfileArranger profileArranger = findTemProfileArranger(arrangerId, profileId);
63          if(ObjectUtils.isNotNull(profileArranger)) {
64              profileArranger.setActive(Boolean.FALSE);
65              businessObjectService.save(profileArranger);
66          }
67  
68      }
69  
70      @Override
71      public TemProfileArranger findPrimaryTravelProfileArranger(String arrangerId, Integer profileId) {
72          Map fieldValues = new HashMap();
73          fieldValues.put("profileId", profileId);
74  
75          List<TemProfileArranger> profileArrangers = new ArrayList<TemProfileArranger>( businessObjectService.findMatching(TemProfileArranger.class, fieldValues));
76  
77          for(TemProfileArranger profileArranger: profileArrangers) {
78              if(profileArranger.getPrimary() && !profileArranger.getPrincipalId().equals(arrangerId)) {
79                  return profileArranger;
80              }
81          }
82          return null;
83      }
84  
85      /**
86       *
87       * @param arrangerDoc
88       * @return
89       */
90      protected TemProfileArranger createNewTravelProfileArranger(TravelArrangerDocument arrangerDoc) {
91          TemProfileArranger profileArranger = new TemProfileArranger();
92          profileArranger.setActive(true);
93          profileArranger.setProfileId(arrangerDoc.getProfileId());
94          profileArranger.setPrincipalId(arrangerDoc.getArrangerId());
95          profileArranger.setPrimary(arrangerDoc.getPrimaryInd());
96          profileArranger.setTaInd(arrangerDoc.getTaInd());
97          profileArranger.setTrInd(arrangerDoc.getTrInd());
98  
99          return profileArranger;
100     }
101 
102     protected TemProfileArranger findProfileArranger(String principalId, Integer profileId) {
103         Map fieldValues = new HashMap();
104         fieldValues.put(TemProfileProperties.PRINCIPAL_ID, principalId);
105         fieldValues.put(TemProfileProperties.PROFILE_ID, profileId);
106 
107         List<TemProfileArranger> profileArrangers = new ArrayList<TemProfileArranger>( businessObjectService.findMatching(TemProfileArranger.class, fieldValues));
108         if(profileArrangers.size() == 1) {
109             return profileArrangers.get(0);
110         } else if (profileArrangers.size() == 0) {
111             return null;
112         }
113         return null;
114     }
115 
116     /**
117      * @see org.kuali.kfs.module.tem.document.service.TravelArrangerDocumentService#findTemProfileArranger(java.lang.Integer, java.lang.String)
118      */
119     @Override
120     public TemProfileArranger findTemProfileArranger(String principalId, Integer profileId) {
121         Map fieldValues = new HashMap();
122         fieldValues.put(TemProfileProperties.PRINCIPAL_ID, principalId);
123         fieldValues.put(TemProfileProperties.PROFILE_ID, profileId);
124         //find active profile arrangers only
125         fieldValues.put(KRADPropertyConstants.ACTIVE, "Y");
126 
127         List<TemProfileArranger> profileArrangers = new ArrayList<TemProfileArranger>( businessObjectService.findMatching(TemProfileArranger.class, fieldValues));
128         if(profileArrangers.size() == 1) {
129             return profileArrangers.get(0);
130         } else if (profileArrangers.size() == 0) {
131             return null;
132         }
133         return null;
134     }
135 
136     /**
137      * @see org.kuali.kfs.module.tem.document.service.TravelArrangerDocumentService#hasArrangees(java.lang.String)
138      */
139     @Override
140     public boolean hasArrangees(String principalId) {
141         Map<String, String> fieldValues = new HashMap<String, String>();
142         fieldValues.put(TemProfileProperties.PRINCIPAL_ID, principalId);
143         fieldValues.put(KRADPropertyConstants.ACTIVE, "Y");
144 
145         final int arrangeeCount = businessObjectService.countMatching(TemProfileArranger.class, fieldValues);
146         return arrangeeCount > 0;
147     }
148 
149     public BusinessObjectService getBusinessObjectService() {
150         return businessObjectService;
151     }
152 
153     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
154         this.businessObjectService = businessObjectService;
155     }
156 }