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.job.Job;
26 import org.kuali.hr.time.assignment.Assignment;
27 import org.kuali.hr.time.service.base.TkServiceLocator;
28 import org.kuali.hr.time.timesheet.TimesheetDocument;
29 import org.kuali.hr.time.util.TKContext;
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
35 public class TkSearchableAttributeServiceImpl implements
36 TkSearchableAttributeService {
37
38 private static final Logger LOG = Logger.getLogger(TkSearchableAttributeServiceImpl.class);
39
40 public void updateSearchableAttribute(TimesheetDocument document, Date asOfDate){
41 WorkflowDocument workflowDocument = null;
42
43
44
45
46
47 if (!document.getDocumentHeader().getDocumentStatus().equals("F")) {
48 try {
49 workflowDocument = WorkflowDocumentFactory.loadDocument(document.getPrincipalId(), document.getDocumentId());
50 workflowDocument.setApplicationContent(createSearchableAttributeXml(document, asOfDate));
51 workflowDocument.saveDocument("");
52 if (!"I".equals(workflowDocument.getStatus().getCode())) {
53
54 if(workflowDocument.getInitiatorPrincipalId().equals(TKContext.getPrincipalId())){
55 workflowDocument.saveDocument("");
56 }else{
57 workflowDocument.saveDocumentData();
58 }
59 }else{
60 workflowDocument.saveDocument("");
61 }
62
63
64 } catch (Exception e) {
65 LOG.warn("Exception during searchable attribute update.");
66 throw new RuntimeException(e);
67 }
68 }
69 }
70
71 @Override
72 public String createSearchableAttributeXml(TimesheetDocument document, Date asOfDate) {
73 List<Long> workAreas = new ArrayList<Long>();
74 Map<String,List<Long>> deptToListOfWorkAreas = new HashMap<String,List<Long>>();
75 List<String> salGroups = new ArrayList<String>();
76
77 for(Assignment assign: document.getAssignments()){
78 if(!workAreas.contains(assign.getWorkArea())){
79 workAreas.add(assign.getWorkArea());
80 }
81 Job job = TkServiceLocator.getJobService().getJob(assign.getPrincipalId(), assign.getJobNumber(), assign.getEffectiveDate());
82
83 if(!salGroups.contains(job.getHrSalGroup())){
84 salGroups.add(job.getHrSalGroup());
85 }
86 }
87
88 for(Long workArea : workAreas){
89 WorkArea workAreaObj = TkServiceLocator.getWorkAreaService().getWorkArea(workArea, TKUtils.getTimelessDate(asOfDate));
90 if(deptToListOfWorkAreas.containsKey(workAreaObj.getDept())){
91 List<Long> deptWorkAreas = deptToListOfWorkAreas.get(workAreaObj.getDept());
92 deptWorkAreas.add(workArea);
93 } else {
94 List<Long> deptWorkAreas = new ArrayList<Long>();
95 deptWorkAreas.add(workArea);
96 deptToListOfWorkAreas.put(workAreaObj.getDept(), deptWorkAreas);
97 }
98 }
99 StringBuilder sb = new StringBuilder();
100 sb.append("<documentContext><applicationContent><TimesheetDocument>");
101 sb.append("<DEPARTMENTS>");
102 for(String dept : deptToListOfWorkAreas.keySet()){
103 sb.append("<DEPARTMENT value=\""+dept+"\">");
104 List<Long> deptWorkAreas = deptToListOfWorkAreas.get(dept);
105 for(Long workArea : deptWorkAreas){
106 sb.append("<WORKAREA value=\""+workArea+"\"/>");
107 }
108 sb.append("</DEPARTMENT>");
109 }
110 sb.append("</DEPARTMENTS>");
111 for(String salGroup : salGroups){
112 sb.append("<SALGROUP value=\""+salGroup+"\"/>");
113 }
114
115 sb.append("<PAYENDDATE value=\""+asOfDate+"\"/>");
116 sb.append("</TimesheetDocument></applicationContent></documentContext>");
117
118 return sb.toString();
119 }
120
121 }