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 String department = workAreaObj != null ? workAreaObj.getDept() : null;
93
94 if (department != null) {
95 if(deptToListOfWorkAreas.containsKey(department)){
96 List<Long> deptWorkAreas = deptToListOfWorkAreas.get(workAreaObj.getDept());
97 deptWorkAreas.add(workArea);
98 } else {
99 List<Long> deptWorkAreas = new ArrayList<Long>();
100 deptWorkAreas.add(workArea);
101 deptToListOfWorkAreas.put(department, deptWorkAreas);
102 }
103 }
104 }
105 StringBuilder sb = new StringBuilder();
106 String className = document.getClass().getSimpleName();
107 sb.append("<documentContext><applicationContent><").append(className).append(">");
108 sb.append("<DEPARTMENTS>");
109 for(Map.Entry<String, List<Long>> entry : deptToListOfWorkAreas.entrySet()){
110 sb.append("<DEPARTMENT value=\""+entry.getKey()+"\">");
111
112 for(Long workArea : entry.getValue()){
113 sb.append("<WORKAREA value=\""+workArea+"\"/>");
114 }
115 sb.append("</DEPARTMENT>");
116 }
117 sb.append("</DEPARTMENTS>");
118 for(String salGroup : salGroups){
119 sb.append("<SALGROUP value=\""+salGroup+"\"/>");
120 }
121
122 sb.append("<PAYENDDATE value=\""+asOfDate+"\"/>");
123 sb.append("</").append(className).append("></applicationContent></documentContext>");
124
125 return sb.toString();
126 }
127
128 }