001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.docsearch;
017
018 import java.util.ArrayList;
019 import java.util.Date;
020 import java.util.HashMap;
021 import java.util.List;
022 import java.util.Map;
023
024 import org.apache.log4j.Logger;
025 import org.kuali.hr.core.document.CalendarDocumentHeaderContract;
026 import org.kuali.hr.core.document.calendar.CalendarDocumentContract;
027 import org.kuali.hr.job.Job;
028 import org.kuali.hr.time.assignment.Assignment;
029 import org.kuali.hr.time.service.base.TkServiceLocator;
030 import org.kuali.hr.time.util.TKUtils;
031 import org.kuali.hr.time.workarea.WorkArea;
032 import org.kuali.rice.kew.api.WorkflowDocument;
033 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
034 import org.kuali.rice.krad.util.GlobalVariables;
035
036 public class TkSearchableAttributeServiceImpl implements
037 TkSearchableAttributeService {
038
039 private static final Logger LOG = Logger.getLogger(TkSearchableAttributeServiceImpl.class);
040
041 public void updateSearchableAttribute(CalendarDocumentContract document, Date asOfDate){
042 WorkflowDocument workflowDocument = null;
043 //
044 // djunk - Need to actually look at why this call is here for every
045 // document submission. Rice does not allow save events for
046 // documents in final status. We may add more skips.
047 //
048 if (!document.getDocumentHeader().getDocumentStatus().equals("F")) {
049 try {
050 CalendarDocumentHeaderContract docHeader = document.getDocumentHeader();
051 workflowDocument = WorkflowDocumentFactory.loadDocument(docHeader.getPrincipalId(), docHeader.getDocumentId());
052 workflowDocument.setApplicationContent(createSearchableAttributeXml(document, asOfDate));
053 workflowDocument.saveDocument("");
054 if (!"I".equals(workflowDocument.getStatus().getCode())) {
055 //updateWorkflowTitle(document,workflowDocument);
056 if (GlobalVariables.getUserSession() != null && workflowDocument.getInitiatorPrincipalId().equals(GlobalVariables.getUserSession().getPrincipalId())) {
057 workflowDocument.saveDocument("");
058 } else{
059 workflowDocument.saveDocumentData();
060 }
061 } else{
062 workflowDocument.saveDocument("");
063 }
064
065
066 } catch (Exception e) {
067 LOG.warn("Exception during searchable attribute update.");
068 throw new RuntimeException(e);
069 }
070 }
071 }
072
073 @Override
074 public String createSearchableAttributeXml(CalendarDocumentContract document, Date asOfDate) {
075 List<Long> workAreas = new ArrayList<Long>();
076 Map<String,List<Long>> deptToListOfWorkAreas = new HashMap<String,List<Long>>();
077 List<String> salGroups = new ArrayList<String>();
078
079 for(Assignment assign: document.getAssignments()){
080 if(!workAreas.contains(assign.getWorkArea())){
081 workAreas.add(assign.getWorkArea());
082 }
083 Job job = TkServiceLocator.getJobService().getJob(assign.getPrincipalId(), assign.getJobNumber(), assign.getEffectiveDate());
084
085 if(!salGroups.contains(job.getHrSalGroup())){
086 salGroups.add(job.getHrSalGroup());
087 }
088 }
089
090 for(Long workArea : workAreas){
091 WorkArea workAreaObj = TkServiceLocator.getWorkAreaService().getWorkArea(workArea, TKUtils.getTimelessDate(asOfDate));
092 String department = workAreaObj != null ? workAreaObj.getDept() : null;
093
094 if (department != null) {
095 if(deptToListOfWorkAreas.containsKey(department)){
096 List<Long> deptWorkAreas = deptToListOfWorkAreas.get(workAreaObj.getDept());
097 deptWorkAreas.add(workArea);
098 } else {
099 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 }