001 package org.kuali.ole.license.controller;
002
003 import org.apache.log4j.Logger;
004 import org.kuali.ole.license.bo.OleCheckListBo;
005 import org.kuali.ole.service.OleCheckListMaintenanceDocumentService;
006 import org.kuali.rice.core.api.CoreApiServiceLocator;
007 import org.kuali.rice.core.api.config.property.ConfigurationService;
008 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
009 import org.kuali.rice.kim.api.identity.Person;
010 import org.kuali.rice.krad.bo.PersistableAttachment;
011 import org.kuali.rice.krad.bo.PersistableBusinessObject;
012 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
013 import org.kuali.rice.krad.maintenance.MaintenanceUtils;
014 import org.kuali.rice.krad.service.KRADServiceLocator;
015 import org.kuali.rice.krad.util.GlobalVariables;
016 import org.kuali.rice.krad.util.KRADConstants;
017 import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
018 import org.kuali.rice.krad.web.form.DocumentFormBase;
019 import org.kuali.rice.krad.web.form.MaintenanceForm;
020 import org.kuali.rice.krad.web.form.UifFormBase;
021 import org.springframework.stereotype.Controller;
022 import org.springframework.util.FileCopyUtils;
023 import org.springframework.validation.BindingResult;
024 import org.springframework.web.bind.annotation.ModelAttribute;
025 import org.springframework.web.bind.annotation.RequestMapping;
026 import org.springframework.web.multipart.MultipartFile;
027 import org.springframework.web.servlet.ModelAndView;
028
029 import javax.servlet.http.HttpServletRequest;
030 import javax.servlet.http.HttpServletResponse;
031 import java.io.*;
032 import java.sql.Timestamp;
033 import java.util.HashMap;
034 import java.util.Map;
035 import java.util.UUID;
036
037 /**
038 * OleCheckListController is the controller class for CheckList Maintenance Document.
039 */
040 @Controller
041 @RequestMapping(value = "/oleCheckListMaintenance")
042 public class OleCheckListController extends MaintenanceDocumentController {
043 protected static final Logger LOG = Logger.getLogger(OleCheckListController.class);
044
045 /**
046 * This method is used to download the attachment.
047 * @param form
048 * @param result
049 * @param request
050 * @param response
051 * @return ModelAndView
052 */
053 @RequestMapping(params = "methodToCall=downloadAttachment")
054 public ModelAndView downloadAttachment(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
055 HttpServletRequest request, HttpServletResponse response) {
056
057 String checkListId = request.getParameter("oleCheckListId");
058 Map criteriaMap = new HashMap();
059 criteriaMap.put("oleCheckListId",checkListId);
060 OleCheckListBo checkListBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleCheckListBo.class,criteriaMap);
061 String fileName = "";
062 String objectId = "";
063 if(checkListBo!= null) {
064 fileName = checkListBo.getFileName();
065 objectId = checkListBo.getObjectId();
066 }
067 String directory = getKualiConfigurationService().getPropertyValueAsString(
068 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY)+ File.separator+"checkList"+File.separator+checkListBo.getRemoteObjectIdentifier();
069 if(LOG.isInfoEnabled()) {
070 LOG.info("file location : " + directory);
071 }
072
073 try {
074 File file = new File(directory) ;
075 response.setContentType(checkListBo.getMimeType());
076 response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
077 response.setHeader("Expires", "0");
078 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
079 response.setHeader("Pragma", "public");
080 response.setContentLength((int) file.length());
081 InputStream fis = new BufferedInputStream(new FileInputStream(file));
082 FileCopyUtils.copy(fis, response.getOutputStream());
083 }
084 catch (IOException e) {
085
086 }
087
088 return null;
089 }
090
091 /**
092 * This method invokes setupMaintenanceForDelete method to populate the document for deleting.
093 * @param form
094 * @param result
095 * @param request
096 * @param response
097 * @return ModelAndView
098 * @throws Exception
099 */
100 @RequestMapping(params = "methodToCall=" +"maintenanceDelete")
101 public ModelAndView maintenanceDelete(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
102 HttpServletRequest request, HttpServletResponse response) throws Exception {
103 setupMaintenanceForDelete(form, request,"Delete");
104 return getUIFModelAndView(form);
105 }
106
107 /**
108 * This method invokes deleteAttachment method to delete attachment and set the error accordingly ..
109 * @param form
110 * @param result
111 * @param request
112 * @param response
113 * @return ModelAndView
114 * @throws Exception
115 */
116 @RequestMapping(params = "methodToCall=" +"deleteDocument")
117 public ModelAndView deleteDocument(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
118 HttpServletRequest request, HttpServletResponse response) throws Exception {
119 MaintenanceDocument document = form.getDocument();
120 OleCheckListBo oleCheckListBo = new OleCheckListBo();
121 if(document.getDocumentDataObject() != null) {
122 oleCheckListBo = (OleCheckListBo)document.getDocumentDataObject();
123 if(oleCheckListBo != null) {
124 boolean isDeleted= deleteAttachment(oleCheckListBo);
125 if(isDeleted) {
126 KRADServiceLocator.getBusinessObjectService().delete(oleCheckListBo);
127 GlobalVariables.getMessageMap().putInfoWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, "message.delete.document");
128 }
129 else {
130 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, "The File cannot be deleted" );
131 LOG.error("The File cannot be deleted");
132 }
133 }
134 else {
135 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, "Invalid Document" );
136 LOG.error("Invalid Document");
137 }
138 }
139 return getUIFModelAndView(form);
140 }
141
142 /**
143 * This method populates confirmation to delete the document.
144 * @param form
145 * @param request
146 * @param maintenanceAction
147 */
148 protected void setupMaintenanceForDelete(MaintenanceForm form, HttpServletRequest request, String maintenanceAction) {
149 MaintenanceDocument document = form.getDocument();
150 if (document == null) {
151 document = getMaintenanceDocumentService()
152 .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
153 maintenanceAction);
154
155 form.setDocument(document);
156 form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
157 }
158
159 form.setMaintenanceAction(maintenanceAction);
160 getMaintenanceDocumentService().setupMaintenanceObjectForDelete(document, maintenanceAction, request.getParameterMap());
161 MaintenanceUtils.checkForLockingDocument(document, false);
162 }
163
164 /**
165 * This method is used to delete the attachment(checklist template) file.
166 * @param oleCheckListBo
167 * @return boolean
168 */
169 protected boolean deleteAttachment(OleCheckListBo oleCheckListBo) {
170 if(oleCheckListBo != null) {
171 String fullPathUniqueFileName = getKualiConfigurationService().getPropertyValueAsString(
172 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY)+ File.separator+"checkList"+File.separator+oleCheckListBo.getRemoteObjectIdentifier();
173 File attachmentFile = new File(fullPathUniqueFileName);
174 return attachmentFile.delete();
175 }
176 return false;
177 }
178
179 @Override
180 protected OleCheckListMaintenanceDocumentService getMaintenanceDocumentService() {
181 return GlobalResourceLoader.getService("oleCheckListMaintenanceDocumentService");
182 }
183
184 public ConfigurationService getKualiConfigurationService() {
185 return KRADServiceLocator.getKualiConfigurationService();
186 }
187
188 /**
189 * This method store the uploaded checklist template in the specified path and also populates the document
190 * for routing
191 * @param docForm
192 * @param result
193 * @param request
194 * @param response
195 * @return ModelAndView
196 */
197 @Override
198 @RequestMapping(params = "methodToCall=route")
199 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase docForm, BindingResult result,
200 HttpServletRequest request, HttpServletResponse response) {
201
202 ModelAndView modelAndView;
203 MaintenanceForm form = (MaintenanceForm) docForm;
204 MultipartFile attachmentFile = form.getAttachmentFile();
205 MaintenanceDocument document = form.getDocument();
206 OleCheckListBo oleCheckListBo = (OleCheckListBo)document.getNewMaintainableObject().getDataObject();
207 OleCheckListBo oleCheckListBoOld = (OleCheckListBo)document.getOldMaintainableObject().getDataObject();
208 Person kualiUser = GlobalVariables.getUserSession().getPerson();
209 oleCheckListBo.setAuthor(kualiUser.getPrincipalId());
210 oleCheckListBo.setLastModified((Timestamp) CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp());
211
212
213 if (kualiUser == null) {
214 LOG.error("Current UserSession has a null Person");
215 }
216 if(form.getMaintenanceAction().equals("New")) {
217 try {
218 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename());
219 oleCheckListBo.setMimeType(attachmentFile.getContentType());
220 String remoteObjectIdentifier = UUID.randomUUID().toString();
221 if(storeAttachment(attachmentFile,remoteObjectIdentifier))
222 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier);
223 }
224 catch(Exception e) {
225 LOG.error("Error while storing the attachment");
226 }
227 }
228 if(form.getMaintenanceAction().equals("Edit")) {
229 try {
230
231 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename());
232 oleCheckListBo.setMimeType(attachmentFile.getContentType());
233 String remoteObjectIdentifier = UUID.randomUUID().toString();
234 if(storeAttachment(attachmentFile,remoteObjectIdentifier))
235 deleteAttachment(oleCheckListBoOld);
236 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier);
237 }
238 catch(Exception e) {
239 LOG.error("Error while storing the attachment");
240 }
241 }
242
243
244 modelAndView = super.route(form, result, request, response);
245
246 if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) {
247 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService()
248 .retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject());
249 request.setAttribute("fileName", bo.getFileName());
250 }
251
252 modelAndView = getUIFModelAndView(form);
253
254 return modelAndView;
255 }
256
257 /**
258 * This method stores uploaded checklist template in the specified path.
259 * @param attachedFile
260 * @param fileName
261 * @return boolean
262 * @throws java.io.IOException
263 */
264 public boolean storeAttachment(MultipartFile attachedFile,String fileName) throws IOException{
265 String location = null;
266 /*if(objectId != null) {
267 location = getKualiConfigurationService().getPropertyValueAsString(
268 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY)+ File.separator+"checkList"+File.separator+objectId;
269 }
270 else {*/
271 location = getKualiConfigurationService().getPropertyValueAsString(
272 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY)+ File.separator+"checkList";
273 //}
274 if(LOG.isInfoEnabled()) {
275 LOG.info("file location : " + location);
276 }
277 File dirLocation = new File(location);
278 if(!dirLocation.exists()) {
279 boolean success = dirLocation.mkdirs();
280 if(!success) {
281 LOG.error("Could not generate directory for File at: " + dirLocation.getAbsolutePath());
282 return false;
283 }
284 }
285 location = location+File.separator+fileName;
286 File fileOut = new File(location);
287 FileOutputStream streamOut = null;
288 BufferedOutputStream bufferedStreamOut = null;
289 try {
290 InputStream fileContent = attachedFile.getInputStream();
291 streamOut = new FileOutputStream(fileOut);
292 bufferedStreamOut = new BufferedOutputStream(streamOut);
293 int c;
294 while ((c = fileContent.read()) != -1) {
295 bufferedStreamOut.write(c);
296 }
297 }
298 finally {
299 bufferedStreamOut.close();
300 streamOut.close();
301 }
302 return true;
303 }
304 }