View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.core.department.service;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.joda.time.LocalDate;
20  import org.kuali.kpme.core.api.department.Department;
21  import org.kuali.kpme.core.api.department.DepartmentService;
22  import org.kuali.kpme.core.api.groupkey.HrGroupKey;
23  import org.kuali.kpme.core.api.groupkey.HrGroupKeyService;
24  import org.kuali.kpme.core.department.DepartmentBo;
25  import org.kuali.kpme.core.department.dao.DepartmentDao;
26  import org.kuali.rice.core.api.mo.ModelObjectUtils;
27  
28  import java.util.*;
29  
30  public class DepartmentServiceImpl implements DepartmentService {
31  
32  	private DepartmentDao departmentDao;
33      private HrGroupKeyService hrGroupKeyService;
34      private static final ModelObjectUtils.Transformer<DepartmentBo, Department> toDepartment =
35              new ModelObjectUtils.Transformer<DepartmentBo, Department>() {
36                  public Department transform(DepartmentBo input) {
37                      return DepartmentBo.to(input);
38                  };
39              };
40  	
41  	public DepartmentDao getDepartmentDao() {
42  		return departmentDao;
43  	}
44  	
45  	public void setDepartmentDao(DepartmentDao departmentDao) {
46  		this.departmentDao = departmentDao;
47  	}
48  	
49  	@Override
50      public Department getDepartment(String hrDeptId) {
51          return DepartmentBo.to(getDepartmentBo(hrDeptId));
52      }
53  
54  
55  	protected DepartmentBo getDepartmentBo(String hrDeptId) {
56  		DepartmentBo departmentObj = departmentDao.getDepartment(hrDeptId);
57  
58  		return departmentObj;
59  	}
60      
61  	@Override
62  	public int getDepartmentCount(String department, String groupKeyCode) {
63  		return departmentDao.getDepartmentCount(department, groupKeyCode);
64  	}
65  
66      @Override
67      public Department getDepartment(String department, String groupKeyCode, LocalDate asOfDate) {
68          return DepartmentBo.to(getDepartmentBo(department, groupKeyCode, asOfDate));
69      }
70  
71      protected DepartmentBo getDepartmentBo(String department,  String groupKeyCode, LocalDate asOfDate) {
72          return departmentDao.getDepartment(department, groupKeyCode, asOfDate);
73      }
74  
75      @Override
76      public List<String> getDepartmentValuesWithLocation(String location, LocalDate asOfDate) {
77          List<String> groupKeyCodes = getGroupKeyCodesFromList(hrGroupKeyService.getHrGroupKeysWithLocation(location, asOfDate));
78          List<DepartmentBo> departmentObjs = departmentDao.getDepartmentsWithGroupKeys(groupKeyCodes, asOfDate);
79          List<String> depts = new ArrayList<String>();
80          for (DepartmentBo departmentObj : departmentObjs) {
81              depts.add(departmentObj.getBusinessKeyId());
82          }
83  
84          return depts;
85      }
86  
87      @Override
88      public List<String> getDepartmentValuesWithLocations(List<String> locations, LocalDate asOfDate) {
89          if (CollectionUtils.isEmpty(locations)) {
90              return Collections.emptyList();
91          }
92          
93          List<HrGroupKey> groupKeys = hrGroupKeyService.getHrGroupKeysForLocations(locations, asOfDate);
94          List<String> groupKeyCodes = getGroupKeyCodesFromList(groupKeys);
95          List<DepartmentBo> departmentObjs = departmentDao.getDepartmentsWithGroupKeys(groupKeyCodes, asOfDate);
96          List<String> depts = new ArrayList<String>();
97          for (DepartmentBo departmentObj : departmentObjs) {
98              depts.add(departmentObj.getBusinessKeyId());
99          }
100 
101         return depts;
102     }
103 
104     @Override
105     public List<Department> getDepartmentsWithLocation(String location, LocalDate asOfDate) {
106       	List<HrGroupKey> groupKeys = hrGroupKeyService.getHrGroupKeysWithLocation(location, asOfDate);
107         List<String> groupKeyCodes = getGroupKeyCodesFromList(groupKeys);
108 
109     	List<DepartmentBo> departmentObjs = departmentDao.getDepartmentsWithGroupKeys(groupKeyCodes, asOfDate);
110     	
111         return ModelObjectUtils.transform(departmentObjs, toDepartment);
112     }
113 
114     @Override
115     public List<Department> getDepartmentsWithGroupKey(String groupKeyCode, LocalDate asOfDate)
116     {
117         List<String> groupKeyCodes = new ArrayList<String>();
118         groupKeyCodes.add(groupKeyCode);
119 
120         List<DepartmentBo> departmentObjs = departmentDao.getDepartmentsWithGroupKeys(groupKeyCodes, asOfDate);
121 
122         return ModelObjectUtils.transform(departmentObjs, toDepartment);
123     }
124 
125 
126     @Override
127     public List<Department> getDepartments(String department, String location, LocalDate asOfDate) {
128         List<HrGroupKey> groupKeys = hrGroupKeyService.getHrGroupKeysWithLocation(location, asOfDate);
129         List<String> groupKeyCodes = getGroupKeyCodesFromList(groupKeys);
130     	List<DepartmentBo> departmentObjs = departmentDao.getDepartmentsWithDepartmentAndGroupKeys(department, groupKeyCodes, asOfDate);
131 
132         return ModelObjectUtils.transform(departmentObjs, toDepartment);
133     }
134 
135 
136     @Override
137     public List<Department> getDepartments(LocalDate asOfDate)
138     {
139         List<DepartmentBo> departmentObjs = departmentDao.getDepartments(asOfDate);
140 
141         return ModelObjectUtils.transform(departmentObjs, toDepartment);
142     }
143 
144 
145     public void setHrGroupKeyService(HrGroupKeyService hrGroupKeyService) {
146         this.hrGroupKeyService = hrGroupKeyService;
147     }
148 
149     protected List<String> getGroupKeyCodesFromList(List<HrGroupKey> groupKeys) {
150         List<String> groupKeyCodes = new ArrayList<String>();
151         for (HrGroupKey key : groupKeys) {
152             groupKeyCodes.add(key.getGroupKeyCode());
153         }
154         return groupKeyCodes;
155     }
156 }