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