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