001 /** 002 * Copyright 2004-2014 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.kpme.tklm.time.missedpunch.web; 017 018 import java.util.Collections; 019 import java.util.List; 020 import java.util.Properties; 021 import java.util.Set; 022 023 import javax.servlet.http.HttpServletRequest; 024 import javax.servlet.http.HttpServletResponse; 025 026 import org.apache.commons.collections.CollectionUtils; 027 import org.apache.commons.lang.StringUtils; 028 import org.kuali.kpme.core.util.HrContext; 029 import org.kuali.kpme.core.util.TKUtils; 030 import org.kuali.kpme.tklm.common.TkConstants; 031 import org.kuali.kpme.tklm.time.clocklog.ClockLog; 032 import org.kuali.kpme.tklm.time.missedpunch.MissedPunch; 033 import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument; 034 import org.kuali.kpme.tklm.time.service.TkServiceLocator; 035 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument; 036 import org.kuali.rice.core.api.config.property.ConfigContext; 037 import org.kuali.rice.kew.api.exception.WorkflowException; 038 import org.kuali.rice.krad.bo.Note; 039 import org.kuali.rice.krad.document.Document; 040 import org.kuali.rice.krad.exception.ValidationException; 041 import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent; 042 import org.kuali.rice.krad.service.KRADServiceLocator; 043 import org.kuali.rice.krad.service.SequenceAccessorService; 044 import org.kuali.rice.krad.uif.UifConstants; 045 import org.kuali.rice.krad.uif.UifParameters; 046 import org.kuali.rice.krad.uif.UifConstants.WorkflowAction; 047 import org.kuali.rice.krad.uif.component.Component; 048 import org.kuali.rice.krad.uif.container.CollectionGroup; 049 import org.kuali.rice.krad.uif.widget.Disclosure; 050 import org.kuali.rice.krad.util.GlobalVariables; 051 import org.kuali.rice.krad.util.KRADConstants; 052 import org.kuali.rice.krad.web.controller.DocumentControllerBase; 053 import org.kuali.rice.krad.web.controller.TransactionalDocumentControllerBase; 054 import org.kuali.rice.krad.web.form.DocumentFormBase; 055 import org.kuali.rice.krad.web.form.HistoryFlow; 056 import org.kuali.rice.krad.web.form.UifFormBase; 057 import org.springframework.stereotype.Controller; 058 import org.springframework.validation.BindingResult; 059 import org.springframework.web.bind.annotation.ModelAttribute; 060 import org.springframework.web.bind.annotation.RequestMapping; 061 import org.springframework.web.servlet.ModelAndView; 062 063 @Controller 064 @RequestMapping("/missedPunch") 065 public class MissedPunchDocumentController extends TransactionalDocumentControllerBase { 066 067 private transient SequenceAccessorService sequenceAccessorService; 068 069 @Override 070 protected DocumentFormBase createInitialForm(HttpServletRequest request) { 071 return new MissedPunchForm(); 072 } 073 074 @Override 075 @RequestMapping(params = "methodToCall=start") 076 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) { 077 078 MissedPunchForm missedPunchForm = (MissedPunchForm) form; 079 MissedPunch missedPunch = missedPunchForm.getMissedPunch(); 080 081 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(missedPunch.getTimesheetDocumentId()); 082 if (timesheetDocument != null) { 083 missedPunch.setPrincipalId(timesheetDocument.getPrincipalId()); 084 missedPunch.setTimesheetDocumentId(missedPunch.getTimesheetDocumentId()); 085 } 086 087 ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(HrContext.getTargetPrincipalId()); 088 if (lastClock != null && !StringUtils.equals(lastClock.getClockAction(), TkConstants.CLOCK_OUT)) { 089 MissedPunch lastMissedPunch = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId()); 090 if (lastMissedPunch != null) { 091 missedPunch.setJobNumber(lastMissedPunch.getJobNumber()); 092 missedPunch.setWorkArea(lastMissedPunch.getWorkArea()); 093 missedPunch.setTask(lastMissedPunch.getTask()); 094 } else { 095 missedPunch.setJobNumber(lastClock.getJobNumber()); 096 missedPunch.setWorkArea(lastClock.getWorkArea()); 097 missedPunch.setTask(lastClock.getTask()); 098 } 099 } 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 }