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.kpme.tklm.time.missedpunch.web;
17  
18  import java.util.Collections;
19  import java.util.List;
20  import java.util.Properties;
21  import java.util.Set;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.collections.CollectionUtils;
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.kpme.core.util.HrContext;
29  import org.kuali.kpme.core.util.TKUtils;
30  import org.kuali.kpme.tklm.common.TkConstants;
31  import org.kuali.kpme.tklm.time.clocklog.ClockLog;
32  import org.kuali.kpme.tklm.time.missedpunch.MissedPunch;
33  import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument;
34  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
35  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
36  import org.kuali.rice.core.api.config.property.ConfigContext;
37  import org.kuali.rice.kew.api.exception.WorkflowException;
38  import org.kuali.rice.krad.bo.Note;
39  import org.kuali.rice.krad.document.Document;
40  import org.kuali.rice.krad.exception.ValidationException;
41  import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
42  import org.kuali.rice.krad.service.KRADServiceLocator;
43  import org.kuali.rice.krad.service.SequenceAccessorService;
44  import org.kuali.rice.krad.uif.UifConstants;
45  import org.kuali.rice.krad.uif.UifParameters;
46  import org.kuali.rice.krad.uif.UifConstants.WorkflowAction;
47  import org.kuali.rice.krad.uif.component.Component;
48  import org.kuali.rice.krad.uif.container.CollectionGroup;
49  import org.kuali.rice.krad.uif.widget.Disclosure;
50  import org.kuali.rice.krad.util.GlobalVariables;
51  import org.kuali.rice.krad.util.KRADConstants;
52  import org.kuali.rice.krad.web.controller.DocumentControllerBase;
53  import org.kuali.rice.krad.web.controller.TransactionalDocumentControllerBase;
54  import org.kuali.rice.krad.web.form.DocumentFormBase;
55  import org.kuali.rice.krad.web.form.HistoryFlow;
56  import org.kuali.rice.krad.web.form.UifFormBase;
57  import org.springframework.stereotype.Controller;
58  import org.springframework.validation.BindingResult;
59  import org.springframework.web.bind.annotation.ModelAttribute;
60  import org.springframework.web.bind.annotation.RequestMapping;
61  import org.springframework.web.servlet.ModelAndView;
62  
63  @Controller
64  @RequestMapping("/missedPunch")
65  public class MissedPunchDocumentController extends TransactionalDocumentControllerBase {
66  	
67  	private transient SequenceAccessorService sequenceAccessorService;
68  	
69  	@Override
70  	protected DocumentFormBase createInitialForm(HttpServletRequest request) {
71  		return new MissedPunchForm();
72  	}
73  	
74  	@Override
75      @RequestMapping(params = "methodToCall=start")
76      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
77      	
78      	MissedPunchForm missedPunchForm = (MissedPunchForm) form;
79      	MissedPunch missedPunch = missedPunchForm.getMissedPunch();
80      	
81          TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(missedPunch.getTimesheetDocumentId());
82          if (timesheetDocument != null) {
83          	missedPunch.setPrincipalId(timesheetDocument.getPrincipalId());
84          	missedPunch.setTimesheetDocumentId(missedPunch.getTimesheetDocumentId());
85          }
86  	
87          ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(HrContext.getTargetPrincipalId());
88          if (lastClock != null && !StringUtils.equals(lastClock.getClockAction(), TkConstants.CLOCK_OUT)) {
89          	MissedPunch lastMissedPunch = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
90  	        if (lastMissedPunch != null) {
91  	        	missedPunch.setJobNumber(lastMissedPunch.getJobNumber());
92  	        	missedPunch.setWorkArea(lastMissedPunch.getWorkArea());
93  	        	missedPunch.setTask(lastMissedPunch.getTask());
94  	        } else {
95  	        	missedPunch.setJobNumber(lastClock.getJobNumber());
96  	        	missedPunch.setWorkArea(lastClock.getWorkArea());
97  	        	missedPunch.setTask(lastClock.getTask());
98  	        }
99          }
100         
101         missedPunchForm.setAssignmentReadOnly(isAssignmentReadOnly(missedPunch));
102 
103         return super.start(missedPunchForm, result, request, response);
104 	}
105 	
106 	@Override
107     protected void createDocument(DocumentFormBase form) throws WorkflowException {
108         super.createDocument(form);
109         
110 		MissedPunchForm missedPunchForm = (MissedPunchForm) form;
111 		MissedPunchDocument missedPunchDocument = (MissedPunchDocument) missedPunchForm.getDocument();
112 		
113 	    if (StringUtils.isEmpty(missedPunchDocument.getDocumentHeader().getDocumentDescription())) {
114 	    	missedPunchDocument.getDocumentHeader().setDocumentDescription("Missed Punch: " + missedPunchForm.getMissedPunch().getPrincipalId());
115 	    }
116 	    
117 	    missedPunchDocument.setTkMissedPunchId(missedPunchForm.getMissedPunch().getTkMissedPunchId());
118 	    missedPunchDocument.setMissedPunch(missedPunchForm.getMissedPunch());
119     }
120 	
121     @RequestMapping(params = "methodToCall=submit")
122     public ModelAndView submit(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
123 		MissedPunchForm missedPunchForm = (MissedPunchForm) form;
124 		
125     	createDocument(missedPunchForm);
126         ModelAndView modelAndView = null;
127         try {
128             missedPunchForm.getDocument().validateBusinessRules(new SaveDocumentEvent( missedPunchForm.getDocument()));
129 
130             if (GlobalVariables.getMessageMap().hasNoErrors()) {
131                 modelAndView = save(missedPunchForm, result, request, response);
132 
133                 if (StringUtils.isNotBlank(missedPunchForm.getMissedPunch().getNote())) {
134                     Document doc = missedPunchForm.getDocument();
135                     Note note = new Note();
136                     note.setNoteText(missedPunchForm.getMissedPunch().getNote());
137                     note.setAuthorUniversalIdentifier(HrContext.getPrincipalId());
138                     note.setNotePostedTimestampToCurrent();
139                     doc.setNotes(Collections.<Note>singletonList(note));
140                 }
141 	    	modelAndView = route(missedPunchForm, result, request, response);
142 	    	missedPunchForm.setMissedPunchSubmitted(true);
143 
144 		return modelAndView;
145     }
146     
147 
148         } catch (ValidationException exception) {
149             //ignore
150         }
151         return getUIFModelAndView(form);
152     }
153 
154 	@Override
155     @RequestMapping(params = "methodToCall=route")
156     public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
157 
158 		MissedPunchForm missedPunchForm = (MissedPunchForm) form;
159 		MissedPunchDocument missedPunchDocument = (MissedPunchDocument) missedPunchForm.getDocument();
160 		MissedPunch missedPunch = (MissedPunch) missedPunchDocument.getMissedPunch();
161 		TkServiceLocator.getMissedPunchService().addClockLog(missedPunch, TKUtils.getIPAddressFromRequest(request.getRemoteAddr()));
162 		
163 		missedPunchDocument.setMissedPunch(missedPunch);
164 		missedPunchForm.setDocument(missedPunchDocument);
165    	
166     	return super.route(missedPunchForm, result, request, response);
167     }
168 
169 	@Override
170     @RequestMapping(params = "methodToCall=approve")
171     public ModelAndView approve(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
172 		MissedPunchDocument missedPunchDocument = (MissedPunchDocument) form.getDocument();
173         MissedPunch missedPunch = (MissedPunch) missedPunchDocument.getMissedPunch();
174     	TkServiceLocator.getMissedPunchService().updateClockLog(missedPunch, TKUtils.getIPAddressFromRequest(request.getRemoteAddr()));
175         
176     	return super.approve(form, result, request, response);
177     }
178 
179 	protected boolean isAssignmentReadOnly(MissedPunch missedPunch) {
180 		boolean isAssignmentReadOnly = false;
181 		
182         if (StringUtils.isNotBlank(missedPunch.getClockAction())) {
183         	Set<String> availableActions = TkConstants.CLOCK_AVAILABLE_ACTION_MAP.get(missedPunch.getClockAction());
184         	if (availableActions.contains(TkConstants.CLOCK_OUT)) {
185     			isAssignmentReadOnly = true;
186     		}
187         } else {
188             ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(HrContext.getTargetPrincipalId());
189             if (lastClock != null) {
190             	if (!StringUtils.equals(lastClock.getClockAction(), TkConstants.CLOCK_OUT)) {
191             		isAssignmentReadOnly = true;
192             	}
193             }
194         }
195         
196         return isAssignmentReadOnly;
197 	}
198 	
199     protected SequenceAccessorService getSequenceAccessorService() {
200         if (sequenceAccessorService == null) {
201             sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
202         }
203         return sequenceAccessorService;
204     }
205 
206     public void setSequenceAccessorService(SequenceAccessorService sequenceAccessorService) {
207         this.sequenceAccessorService = sequenceAccessorService;
208     }
209 
210     @Override
211     @RequestMapping(params = "methodToCall=docHandler")
212     public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
213                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
214         ModelAndView mav = super.docHandler(form, result, request, response);
215 
216         //return getUIFModelAndView(form);
217         List<? extends Component> pageComponents = ((MissedPunchForm)form).getView().getItems().get(0).getItems();
218         for (Component c : pageComponents)  {
219             if (c instanceof CollectionGroup) {
220                 CollectionGroup collGroup = (CollectionGroup)c;
221                 if (CollectionUtils.isNotEmpty(collGroup.getItems())) {
222                     Disclosure disclosure = collGroup.getDisclosure();
223                     disclosure.setDefaultOpen(true);
224                 }
225             }
226         }
227         return mav;
228     }
229 
230     @Override
231     @RequestMapping(params = "methodToCall=back")
232     public ModelAndView back(@ModelAttribute("KualiForm") UifFormBase form,
233                              BindingResult result, HttpServletRequest request,
234                              HttpServletResponse response) {
235         if (!StringUtils.contains(form.getReturnLocation(), "dataObjectClassName="+MissedPunch.class.getName())) {
236             form.setReturnLocation(UifConstants.NO_RETURN);
237         }
238         return super.back(form, result, request, response);
239     }
240 
241 }