View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.leave.block.web;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.List;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.bcel.generic.NEW;
30  import org.apache.commons.io.FileUtils;
31  import org.apache.commons.lang.ObjectUtils;
32  import org.joda.time.DateTime;
33  import org.joda.time.LocalDate;
34  import org.kuali.kpme.core.util.HrConstants;
35  import org.kuali.kpme.core.util.HrContext;
36  import org.kuali.kpme.core.util.TKUtils;
37  import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
38  import org.kuali.kpme.tklm.common.LMConstants;
39  import org.kuali.kpme.tklm.leave.block.LeaveBlockBo;
40  import org.kuali.kpme.tklm.leave.calendar.exportCalendar.CalendarEvent;
41  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
42  import org.kuali.rice.krad.util.GlobalVariables;
43  import org.kuali.rice.krad.web.controller.UifControllerBase;
44  import org.kuali.rice.krad.web.form.UifFormBase;
45  import org.springframework.stereotype.Controller;
46  import org.springframework.validation.BindingResult;
47  import org.springframework.web.bind.annotation.ModelAttribute;
48  import org.springframework.web.bind.annotation.RequestMapping;
49  import org.springframework.web.servlet.ModelAndView;
50  
51  @Controller
52  @RequestMapping("/leaveBlock")
53  public class LeaveBlockController extends UifControllerBase {
54  	
55  	int eventCount=0;
56  	String fileName;
57  	StringBuffer sb=new StringBuffer();
58  	
59  	@Override
60  	protected UifFormBase createInitialForm(HttpServletRequest request) {
61  		// TODO Auto-generated method stub
62  		return new LeaveBlockForm();
63  	}
64  
65  	@Override
66      @RequestMapping(params = "methodToCall=start")
67      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
68      	LeaveBlockForm leaveBlockForm = (LeaveBlockForm) form;
69          ModelAndView modelAndView = super.start(leaveBlockForm, result, request, response);
70          return modelAndView;
71  	}
72  	
73  	@RequestMapping(params = "methodToCall=download")
74  	public ModelAndView download(@ModelAttribute("KualiForm") UifFormBase form,BindingResult result, HttpServletRequest request,HttpServletResponse response) {
75  		if (sb != null) {
76  			try {
77  				response.setContentType("text/calendar");
78  				response.setHeader("Content-Disposition",
79  						"attachment;filename=" + fileName);
80  				response.getWriter().write(sb.toString());
81  				response.setContentLength(sb.toString().getBytes().length);
82  				response.getWriter().flush();
83  				response.getWriter().close();
84  				sb.setLength(0);
85  			} catch (Exception e) {
86  				// IO EXCEPTION
87  				e.printStackTrace();
88  			}
89  		}
90  		return getUIFModelAndView(form);
91  	}
92  	
93  	@RequestMapping(params = "methodToCall=submit")
94      public ModelAndView submit(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
95  		eventCount=0;
96  		sb.setLength(0);
97  		LeaveBlockForm leaveBlockForm = (LeaveBlockForm)form;
98  		if(leaveBlockForm.getFromDate()!=null && leaveBlockForm.getToDate()!=null && !leaveBlockForm.getFromDate().equals("") && !leaveBlockForm.getToDate().equals("")){
99  			DateTime beginDate = new DateTime(leaveBlockForm.getFromDate());
100 			DateTime endDate = new DateTime(leaveBlockForm.getToDate());
101 			if (beginDate != null && endDate != null) {
102 				exportApprovedLeaves(beginDate.toLocalDate(), endDate.toLocalDate());
103 				leaveBlockForm.setCalendarCount(String.valueOf(eventCount));
104 			}
105 		}
106 		return getUIFModelAndView(leaveBlockForm);
107 	}
108 	
109 	public void exportApprovedLeaves(LocalDate beginDate,LocalDate endDate){
110 		String principalId = HrContext.getTargetPrincipalId();
111 		List<LeaveBlock> approvedLeaves = getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, HrConstants.REQUEST_STATUS.APPROVED);
112 		CalendarEvent mycal = new CalendarEvent();
113 		fileName = mycal.generateFilename();
114 
115 		sb.append(mycal.calendarHeader());
116     	if(approvedLeaves!=null && !approvedLeaves.isEmpty()){
117         	for (LeaveBlock leaveBlock : approvedLeaves) {
118         		String desc = leaveBlock.getDescription();
119         		if(desc == null){
120         			desc = "";
121         		}
122         		String uid = "" + leaveBlock.getBlockId() + leaveBlock.getObjectId();
123         		String event = mycal.createEvent(leaveBlock.getAssignmentTitle(),leaveBlock.getLeaveDateTime(),leaveBlock.getLeaveDateTime(),"000000","000000",leaveBlock.getEarnCode() + "(" + leaveBlock.getLeaveAmount() + ")\n" + desc, uid);
124         		sb.append(event);
125         		eventCount++;
126 			}		
127     	}
128     	sb.append(mycal.calendarFooter());
129     }
130 
131 	private List<LeaveBlock> getLeaveBlocksWithRequestStatus(String principalId, LocalDate beginDate, LocalDate endDate, String requestStatus) {
132 		List<LeaveBlock> plannedLeaves = LmServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR, requestStatus, beginDate, endDate);
133 		
134         Collections.sort(plannedLeaves, new Comparator<LeaveBlock>() {
135             @Override
136             public int compare(LeaveBlock leaveBlock1, LeaveBlock leaveBlock2) {
137                 return ObjectUtils.compare(leaveBlock1.getLeaveDateTime(), leaveBlock2.getLeaveDateTime());
138             }
139         });
140         if(plannedLeaves!=null && !plannedLeaves.isEmpty()){
141         	return plannedLeaves;
142         }else{
143         	return null;
144         }
145 	}
146 	
147 	@Override
148     @RequestMapping(params = "methodToCall=back")
149     public ModelAndView back(@ModelAttribute("KualiForm") UifFormBase form,BindingResult result, HttpServletRequest request,HttpServletResponse response) {
150         return super.back(form, result, request, response);
151     }
152 	
153 }