View Javadoc

1   /**
2    * Copyright 2004-2013 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.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          // djunk - Need to actually look at why this call is here for every
45          //         document submission. Rice does not allow save events for
46          //         documents in final status. We may add more skips.
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                      //updateWorkflowTitle(document,workflowDocument);
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 }