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.approval.web;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.HashMap;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.log4j.Logger;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.hsqldb.lib.StringUtil;
35  import org.json.simple.JSONValue;
36  import org.kuali.hr.time.base.web.TkAction;
37  import org.kuali.hr.time.base.web.ApprovalForm;
38  import org.kuali.hr.time.person.TKPerson;
39  import org.kuali.hr.time.service.base.TkServiceLocator;
40  import org.kuali.hr.time.timesheet.TimesheetDocument;
41  import org.kuali.hr.time.timesummary.TimeSummary;
42  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
43  
44  public class TimeApprovalWSAction extends TkAction {
45  
46      private static final Logger LOG = Logger.getLogger(TimeApprovalWSAction.class);
47  
48      /**
49       * Action called via AJAX. (ajaj really...)
50       * <p/>
51       * This search returns quick-results to the search box for the user to further
52       * refine upon. The end value can then be form submitted.
53       */
54      public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
55          TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
56          List<Map<String, String>> results = new LinkedList<Map<String, String>>();
57          if(StringUtils.isNotEmpty(taaf.getPayBeginDateForSearch()) 
58          		&& StringUtils.isNotEmpty(taaf.getPayEndDateForSearch()) ) {
59  	        Date beginDate = new SimpleDateFormat("MM/dd/yyyy").parse(taaf.getPayBeginDateForSearch());
60  	        Date endDate = new SimpleDateFormat("MM/dd/yyyy").parse(taaf.getPayEndDateForSearch());
61  	        
62  	        List<String> workAreaList = new ArrayList<String>();
63  	        if(StringUtil.isEmpty(taaf.getSelectedWorkArea())) {
64  	        	for(Long aKey : taaf.getWorkAreaDescr().keySet()) {
65  	        		workAreaList.add(aKey.toString());
66  	        	}
67  	        } else {
68  	        	workAreaList.add(taaf.getSelectedWorkArea());
69  	        }
70  	        List<String> principalIds = TkServiceLocator.getTimeApproveService()
71  				.getTimePrincipalIdsWithSearchCriteria(workAreaList, taaf.getSelectedPayCalendarGroup(),
72  					new java.sql.Date(endDate.getTime()), new java.sql.Date(beginDate.getTime()), new java.sql.Date(endDate.getTime())); 
73  	        
74  	        List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
75  	        
76  	        if (StringUtils.equals(taaf.getSearchField(), ApprovalForm.ORDER_BY_PRINCIPAL)) {
77  	            for (String id : principalIds) {
78  	                if(StringUtils.contains(id, taaf.getSearchTerm())) {
79  	                    Map<String, String> labelValue = new HashMap<String, String>();
80  	                    labelValue.put("id", id);
81  	                    labelValue.put("result", id);
82  	                    results.add(labelValue);
83  	                }
84  	            }
85  	        } else if (StringUtils.equals(taaf.getSearchField(), ApprovalForm.ORDER_BY_DOCID)) {
86  	            Map<String, TimesheetDocumentHeader> principalDocumentHeaders =
87  	                    TkServiceLocator.getTimeApproveService().getPrincipalDocumehtHeader(persons, beginDate, endDate);
88  	
89  	            for (Map.Entry<String,TimesheetDocumentHeader> entry : principalDocumentHeaders.entrySet()) {
90  	                if (StringUtils.contains(entry.getValue().getDocumentId(), taaf.getSearchTerm())) {
91  	                    Map<String, String> labelValue = new HashMap<String, String>();
92  	                    labelValue.put("id", entry.getValue().getDocumentId() + " (" + entry.getValue().getPrincipalId() + ")");
93  	                    labelValue.put("result", entry.getValue().getPrincipalId());
94  	                    results.add(labelValue);
95  	                }
96  	            }
97  	        }
98          }
99       
100         taaf.setOutputString(JSONValue.toJSONString(results));
101         return mapping.findForward("ws");
102     }
103     
104     public ActionForward getTimeSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
105         TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
106         TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(taaf.getDocumentId());
107 		TimeSummary ts = TkServiceLocator.getTimeSummaryService().getTimeSummary(td);
108 		
109         taaf.setOutputString(ts.toJsonString());
110         return mapping.findForward("ws");
111     }
112         
113 }