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