001 /**
002 * Copyright 2004-2012 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.job.Job;
026 import org.kuali.hr.time.assignment.Assignment;
027 import org.kuali.hr.time.service.base.TkServiceLocator;
028 import org.kuali.hr.time.timesheet.TimesheetDocument;
029 import org.kuali.hr.time.util.TKContext;
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
035 public class TkSearchableAttributeServiceImpl implements
036 TkSearchableAttributeService {
037
038 private static final Logger LOG = Logger.getLogger(TkSearchableAttributeServiceImpl.class);
039
040 public void updateSearchableAttribute(TimesheetDocument document, Date asOfDate){
041 WorkflowDocument workflowDocument = null;
042 //
043 // djunk - Need to actually look at why this call is here for every
044 // document submission. Rice does not allow save events for
045 // documents in final status. We may add more skips.
046 //
047 if (!document.getDocumentHeader().getDocumentStatus().equals("F")) {
048 try {
049 workflowDocument = WorkflowDocumentFactory.loadDocument(document.getPrincipalId(), document.getDocumentId());
050 workflowDocument.setApplicationContent(createSearchableAttributeXml(document, asOfDate));
051 workflowDocument.saveDocument("");
052 if (!"I".equals(workflowDocument.getStatus().getCode())) {
053 //updateWorkflowTitle(document,workflowDocument);
054 if(workflowDocument.getInitiatorPrincipalId().equals(TKContext.getPrincipalId())){
055 workflowDocument.saveDocument("");
056 }else{
057 workflowDocument.saveDocumentData();
058 }
059 }else{
060 workflowDocument.saveDocument("");
061 }
062
063
064 } catch (Exception e) {
065 LOG.warn("Exception during searchable attribute update.");
066 throw new RuntimeException(e);
067 }
068 }
069 }
070
071 @Override
072 public String createSearchableAttributeXml(TimesheetDocument document, Date asOfDate) {
073 List<Long> workAreas = new ArrayList<Long>();
074 Map<String,List<Long>> deptToListOfWorkAreas = new HashMap<String,List<Long>>();
075 List<String> salGroups = new ArrayList<String>();
076
077 for(Assignment assign: document.getAssignments()){
078 if(!workAreas.contains(assign.getWorkArea())){
079 workAreas.add(assign.getWorkArea());
080 }
081 Job job = TkServiceLocator.getJobService().getJob(assign.getPrincipalId(), assign.getJobNumber(), document.getAsOfDate());
082
083 if(!salGroups.contains(job.getHrSalGroup())){
084 salGroups.add(job.getHrSalGroup());
085 }
086 }
087
088 for(Long workArea : workAreas){
089 WorkArea workAreaObj = TkServiceLocator.getWorkAreaService().getWorkArea(workArea, TKUtils.getTimelessDate(asOfDate));
090 if(deptToListOfWorkAreas.containsKey(workAreaObj.getDept())){
091 List<Long> deptWorkAreas = deptToListOfWorkAreas.get(workAreaObj.getDept());
092 deptWorkAreas.add(workArea);
093 } else {
094 List<Long> deptWorkAreas = new ArrayList<Long>();
095 deptWorkAreas.add(workArea);
096 deptToListOfWorkAreas.put(workAreaObj.getDept(), deptWorkAreas);
097 }
098 }
099 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 }