View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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          // djunk - Need to actually look at why this call is here for every
44          //         document submission. Rice does not allow save events for
45          //         documents in final status. We may add more skips.
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                      //updateWorkflowTitle(document,workflowDocument);
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(), document.getAsOfDate());
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 }