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                            if(deptToListOfWorkAreas.containsKey(workAreaObj.getDept())){
093                                    List<Long> deptWorkAreas = deptToListOfWorkAreas.get(workAreaObj.getDept());
094                                    deptWorkAreas.add(workArea);
095                            } else {
096                                    List<Long> deptWorkAreas = new ArrayList<Long>();
097                                    deptWorkAreas.add(workArea);
098                                    deptToListOfWorkAreas.put(workAreaObj.getDept(), deptWorkAreas);
099                            }
100                    }
101                    StringBuilder sb = new StringBuilder();
102            String className = document.getClass().getSimpleName();
103                    sb.append("<documentContext><applicationContent><").append(className).append(">");
104                    sb.append("<DEPARTMENTS>");
105                    for(String dept : deptToListOfWorkAreas.keySet()){
106                            sb.append("<DEPARTMENT value=\""+dept+"\">");
107                            List<Long> deptWorkAreas = deptToListOfWorkAreas.get(dept);
108                            for(Long workArea : deptWorkAreas){
109                                    sb.append("<WORKAREA value=\""+workArea+"\"/>");
110                            }
111                            sb.append("</DEPARTMENT>");
112                    }
113                    sb.append("</DEPARTMENTS>");
114                    for(String salGroup : salGroups){
115                            sb.append("<SALGROUP value=\""+salGroup+"\"/>");
116                    }
117    
118                    sb.append("<PAYENDDATE value=\""+asOfDate+"\"/>");
119                    sb.append("</").append(className).append("></applicationContent></documentContext>");
120    
121                    return sb.toString();
122            }
123    
124    }