1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.docsearch;
17
18 import org.apache.log4j.Logger;
19 import org.joda.time.LocalDate;
20 import org.kuali.kpme.core.api.assignment.Assignment;
21 import org.kuali.kpme.core.api.document.calendar.CalendarDocumentContract;
22 import org.kuali.kpme.core.api.document.calendar.CalendarDocumentHeaderContract;
23 import org.kuali.kpme.core.api.job.JobContract;
24 import org.kuali.kpme.core.api.workarea.WorkArea;
25 import org.kuali.kpme.core.service.HrServiceLocator;
26 import org.kuali.rice.kew.api.WorkflowDocument;
27 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
28 import org.kuali.rice.kim.api.identity.principal.Principal;
29 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30 import org.kuali.rice.krad.util.GlobalVariables;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 public class TkSearchableAttributeServiceImpl implements
38 TkSearchableAttributeService {
39
40 private static final Logger LOG = Logger.getLogger(TkSearchableAttributeServiceImpl.class);
41
42 public void updateSearchableAttribute(CalendarDocumentContract document, LocalDate asOfDate){
43 WorkflowDocument workflowDocument = null;
44
45
46
47
48
49 if (!document.getDocumentHeader().getDocumentStatus().equals("F")) {
50 try {
51 CalendarDocumentHeaderContract docHeader = (CalendarDocumentHeaderContract) document.getDocumentHeader();
52 workflowDocument = WorkflowDocumentFactory.loadDocument(docHeader.getPrincipalId(), docHeader.getDocumentId());
53 workflowDocument.setApplicationContent(createSearchableAttributeXml(document, asOfDate));
54 workflowDocument.saveDocument("");
55 if (!"I".equals(workflowDocument.getStatus().getCode())) {
56
57 if (GlobalVariables.getUserSession() != null && workflowDocument.getInitiatorPrincipalId().equals(GlobalVariables.getUserSession().getPrincipalId())) {
58 workflowDocument.saveDocument("");
59 } else{
60 workflowDocument.saveDocumentData();
61 }
62 } else{
63 workflowDocument.saveDocument("");
64 }
65
66
67 } catch (Exception e) {
68 LOG.warn("Exception during searchable attribute update.");
69 throw new RuntimeException(e);
70 }
71 }
72 }
73
74 @Override
75 public String createSearchableAttributeXml(CalendarDocumentContract document, LocalDate asOfDate) {
76 List<Long> workAreas = new ArrayList<Long>();
77 Map<String,List<Long>> deptToListOfWorkAreas = new HashMap<String,List<Long>>();
78 List<String> salGroups = new ArrayList<String>();
79 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(document.getDocumentHeader().getPrincipalId());
80 for(Assignment assign: document.getAllAssignments()){
81 if(!workAreas.contains(assign.getWorkArea())){
82 workAreas.add(assign.getWorkArea());
83 }
84 JobContract job = HrServiceLocator.getJobService().getJob(assign.getPrincipalId(), assign.getJobNumber(), assign.getEffectiveLocalDate());
85
86 if(!salGroups.contains(job.getHrSalGroup())){
87 salGroups.add(job.getHrSalGroup());
88 }
89 }
90
91 List<WorkArea> workAreaList = HrServiceLocator.getWorkAreaService().getWorkAreasForList(workAreas, asOfDate);
92 Map<String, String> deptGroupKey = new HashMap<String, String>();
93 for(WorkArea workAreaObj : workAreaList){
94 String department = workAreaObj != null ? workAreaObj.getDept() : null;
95
96 if (department != null) {
97 if(deptToListOfWorkAreas.containsKey(department)){
98 List<Long> deptWorkAreas = deptToListOfWorkAreas.get(workAreaObj.getDept());
99 deptWorkAreas.add(workAreaObj.getWorkArea());
100 } else {
101 List<Long> deptWorkAreas = new ArrayList<Long>();
102 deptWorkAreas.add(workAreaObj.getWorkArea());
103 deptToListOfWorkAreas.put(department, deptWorkAreas);
104 deptGroupKey.put(department, workAreaObj.getGroupKeyCode());
105 }
106 }
107 }
108 StringBuilder sb = new StringBuilder();
109 String className = document.getClass().getSimpleName();
110 sb.append("<documentContext><applicationContent><").append(className).append(">");
111 sb.append("<DEPARTMENTS>");
112 for(Map.Entry<String, List<Long>> entry : deptToListOfWorkAreas.entrySet()){
113 sb.append("<DEPARTMENT value=\""+entry.getKey()+"\">");
114 sb.append("<GROUPKEY value=\""+deptGroupKey.get(entry.getKey())+"\"/>");
115 for(Long workArea : entry.getValue()){
116 sb.append("<WORKAREA value=\""+workArea+"\"/>");
117 }
118 sb.append("</DEPARTMENT>");
119 }
120 sb.append("</DEPARTMENTS>");
121 for(String salGroup : salGroups){
122 sb.append("<SALGROUP value=\""+salGroup+"\"/>");
123 }
124
125 sb.append("<CALENTRYID value=\""+document.getCalendarEntry().getHrCalendarEntryId()+"\"/>");
126 sb.append("<PRINCIPALNAME value=\""+principal.getPrincipalName()+"\"/>");
127 sb.append("<PAYENDDATE value=\""+asOfDate+"\"/>");
128 sb.append("</").append(className).append("></applicationContent></documentContext>");
129
130 return sb.toString();
131 }
132
133 }