1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.workarea;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.hr.time.HrBusinessObject;
20 import org.kuali.hr.time.position.Position;
21 import org.kuali.hr.time.roles.TkRole;
22 import org.kuali.hr.time.service.base.TkServiceLocator;
23 import org.kuali.hr.time.task.Task;
24 import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
25 import org.kuali.hr.time.util.TKContext;
26 import org.kuali.rice.kim.api.identity.principal.Principal;
27 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28 import org.kuali.rice.kns.document.MaintenanceDocument;
29 import org.kuali.rice.kns.maintenance.Maintainable;
30 import org.kuali.rice.kns.web.ui.Section;
31 import org.kuali.rice.krad.bo.PersistableBusinessObject;
32 import org.kuali.rice.krad.util.GlobalVariables;
33 import org.kuali.rice.krad.util.KRADConstants;
34
35 import java.sql.Timestamp;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Map;
39
40 public class WorkAreaMaintainableImpl extends HrBusinessObjectMaintainableImpl {
41
42 private static final long serialVersionUID = 6264585236631982347L;
43
44 @Override
45 protected void setNewCollectionLineDefaultValues(String arg0,
46 PersistableBusinessObject arg1) {
47 WorkArea workArea = (WorkArea) this.getBusinessObject();
48 if (arg1 instanceof TkRole) {
49 TkRole role = (TkRole) arg1;
50 role.setEffectiveDate(workArea.getEffectiveDate());
51 } else if (arg1 instanceof Task) {
52 Task task = (Task) arg1;
53 task.setEffectiveDate(workArea.getEffectiveDate());
54 }
55 super.setNewCollectionLineDefaultValues(arg0, arg1);
56 }
57
58 @Override
59 public PersistableBusinessObject getNewCollectionLine(String collectionName) {
60 return super.getNewCollectionLine(collectionName);
61 }
62
63 @Override
64 public void addNewLineToCollection(String collectionName) {
65 if (collectionName.equals("roles")) {
66 TkRole aRole = (TkRole)newCollectionLines.get(collectionName );
67 if ( aRole != null ) {
68 if(!StringUtils.isEmpty(aRole.getPrincipalId()) && !StringUtils.isEmpty(aRole.getPositionNumber())) {
69 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
70 "error.role.principalId.positonNumber", aRole.getPrincipalId());
71 return;
72 }
73 if(aRole.getPrincipalId() != null && !aRole.getPrincipalId().isEmpty()) {
74 Principal aPerson = KimApiServiceLocator.getIdentityService().getPrincipal(aRole.getPrincipalId());
75 if(aPerson == null) {
76 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
77 "error.role.person.notexist", aRole.getPrincipalId());
78 return;
79 }
80 }
81 if(aRole.getPositionNumber() != null && !aRole.getPositionNumber().isEmpty()) {
82 Position aPositon = TkServiceLocator.getPositionService().getPosition(aRole.getPositionNumber(), aRole.getEffectiveDate());
83 if(aPositon == null) {
84 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
85 "error.role.position.notexist", aRole.getPositionNumber());
86 return;
87 }
88 }
89 }
90 }
91 super.addNewLineToCollection(collectionName);
92 }
93
94 @Override
95 public void processAfterEdit(MaintenanceDocument document,
96 Map<String, String[]> parameters) {
97 WorkArea waOld = (WorkArea) document.getOldMaintainableObject()
98 .getBusinessObject();
99 WorkArea waNew = (WorkArea) document.getNewMaintainableObject()
100 .getBusinessObject();
101
102 List<TkRole> positionRoles = TkServiceLocator.getTkRoleService().getPositionRolesForWorkArea(waOld.getWorkArea(), waOld.getEffectiveDate());
103 TkServiceLocator.getWorkAreaService().populateWorkAreaTasks(waOld);
104 TkServiceLocator.getWorkAreaService().populateWorkAreaRoles(waOld);
105 waOld.getRoles().addAll(positionRoles);
106
107 TkServiceLocator.getWorkAreaService().populateWorkAreaTasks(waNew);
108 TkServiceLocator.getWorkAreaService().populateWorkAreaRoles(waNew);
109 waNew.getRoles().addAll(positionRoles);
110 super.processAfterEdit(document, parameters);
111 }
112
113 @SuppressWarnings("rawtypes")
114 @Override
115 public List getSections(MaintenanceDocument document,
116 Maintainable oldMaintainable) {
117 List sections = super.getSections(document, oldMaintainable);
118 for (Object obj : sections) {
119 Section sec = (Section) obj;
120 if (document.isOldBusinessObjectInDocument()
121 && sec.getSectionId().equals("inactiveRoles")) {
122 sec.setHidden(false);
123 } else if (!document.isOldBusinessObjectInDocument()
124 && sec.getSectionId().equals("inactiveRoles")) {
125 sec.setHidden(true);
126 }
127 }
128 return sections;
129 }
130
131 @Override
132 public HrBusinessObject getObjectById(String id) {
133 return TkServiceLocator.getWorkAreaService().getWorkArea(id);
134 }
135
136 @Override
137 public void customSaveLogic(HrBusinessObject hrObj) {
138 WorkArea workArea = (WorkArea) hrObj;
139
140 List<TkRole> roles = new ArrayList<TkRole>();
141 roles.addAll(workArea.getRoles());
142 roles.addAll(workArea.getInactiveRoles());
143 roles.addAll(createInactiveRoles(workArea.getRoles()));
144
145 for (TkRole role : roles) {
146 role.setWorkAreaObj(workArea);
147 role.setUserPrincipalId(TKContext.getPrincipalId());
148 }
149 workArea.setRoles(roles);
150
151 TkServiceLocator.getTkRoleService().saveOrUpdate(roles);
152
153 List<Task> tasks = workArea.getTasks();
154 for (Task task : tasks) {
155 task.setTkTaskId(null);
156 task.setTimestamp(new Timestamp(System.currentTimeMillis()));
157 }
158 workArea.setTasks(tasks);
159
160 TkServiceLocator.getTaskService().saveTasks(tasks);
161 }
162
163 private List<TkRole> createInactiveRoles(List<TkRole> activeRoles) {
164 List<TkRole> inactiveRoles = new ArrayList<TkRole>();
165
166 List<TkRole> oldRoles = new ArrayList<TkRole>();
167 List<TkRole> newRoles = new ArrayList<TkRole>();
168 for (TkRole activeRole : activeRoles) {
169 if (!StringUtils.isEmpty(activeRole.getHrRolesId())) {
170 oldRoles.add(activeRole);
171 } else {
172 newRoles.add(activeRole);
173 }
174 }
175
176 for (TkRole newRole : newRoles) {
177 for (TkRole oldRole : oldRoles) {
178 if (StringUtils.equals(newRole.getRoleName(), oldRole.getRoleName())
179 && StringUtils.equals(newRole.getPrincipalId(), oldRole.getPrincipalId())
180 && StringUtils.equals(newRole.getPositionNumber(), oldRole.getPositionNumber())) {
181
182 TkRole newInactiveRole = new TkRole();
183 newInactiveRole.setPrincipalId(oldRole.getPrincipalId());
184 newInactiveRole.setRoleName(oldRole.getRoleName());
185 newInactiveRole.setWorkArea(oldRole.getWorkArea());
186 newInactiveRole.setDepartment(oldRole.getDepartment());
187 newInactiveRole.setChart(oldRole.getChart());
188 newInactiveRole.setHrDeptId(oldRole.getHrDeptId());
189 newInactiveRole.setPositionNumber(oldRole.getPositionNumber());
190 newInactiveRole.setExpirationDate(oldRole.getExpirationDate());
191 newInactiveRole.setEffectiveDate(newRole.getEffectiveDate());
192 newInactiveRole.setTimestamp(new Timestamp(System.currentTimeMillis()));
193 newInactiveRole.setUserPrincipalId(TKContext.getPrincipalId());
194 newInactiveRole.setActive(false);
195
196 inactiveRoles.add(newInactiveRole);
197 }
198 }
199 }
200
201 return inactiveRoles;
202 }
203
204 @Override
205 public void processAfterNew(MaintenanceDocument document,
206 Map<String, String[]> parameters) {
207 WorkArea workArea = (WorkArea) this.getBusinessObject();
208 if (workArea.getWorkArea() == null) {
209 workArea.setWorkArea(TkServiceLocator.getWorkAreaService().getNextWorkAreaKey());
210 }
211 super.processAfterNew(document, parameters);
212 }
213
214 }