1 package org.kuali.ole.license.controller;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.license.bo.OleCheckListBo;
6 import org.kuali.ole.service.OleCheckListMaintenanceDocumentService;
7 import org.kuali.rice.core.api.CoreApiServiceLocator;
8 import org.kuali.rice.core.api.config.property.ConfigurationService;
9 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
10 import org.kuali.rice.core.api.util.RiceKeyConstants;
11 import org.kuali.rice.kim.api.identity.Person;
12 import org.kuali.rice.krad.bo.PersistableAttachment;
13 import org.kuali.rice.krad.bo.PersistableBusinessObject;
14 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
15 import org.kuali.rice.krad.maintenance.MaintenanceUtils;
16 import org.kuali.rice.krad.service.KRADServiceLocator;
17 import org.kuali.rice.krad.util.GlobalVariables;
18 import org.kuali.rice.krad.util.KRADConstants;
19 import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
20 import org.kuali.rice.krad.web.form.DocumentFormBase;
21 import org.kuali.rice.krad.web.form.MaintenanceForm;
22 import org.kuali.rice.krad.web.form.UifFormBase;
23 import org.springframework.stereotype.Controller;
24 import org.springframework.util.FileCopyUtils;
25 import org.springframework.validation.BindingResult;
26 import org.springframework.web.bind.annotation.ModelAttribute;
27 import org.springframework.web.bind.annotation.RequestMapping;
28 import org.springframework.web.multipart.MultipartFile;
29 import org.springframework.web.servlet.ModelAndView;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import java.io.*;
34 import java.sql.Timestamp;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.UUID;
38
39
40
41
42 @Controller
43 @RequestMapping(value = "/oleCheckListMaintenance")
44 public class OleCheckListController extends MaintenanceDocumentController {
45 protected static final Logger LOG = Logger.getLogger(OleCheckListController.class);
46
47
48
49
50
51
52
53
54
55 @RequestMapping(params = "methodToCall=downloadAttachment")
56 public ModelAndView downloadAttachment(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
57 HttpServletRequest request, HttpServletResponse response) {
58
59 String checkListId = request.getParameter("oleCheckListId");
60 Map criteriaMap = new HashMap();
61 criteriaMap.put("oleCheckListId",checkListId);
62 OleCheckListBo checkListBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleCheckListBo.class,criteriaMap);
63 String fileName = "";
64 String objectId = "";
65 if(checkListBo!= null) {
66 fileName = checkListBo.getFileName();
67 objectId = checkListBo.getObjectId();
68 }
69 String directory = getKualiConfigurationService().getPropertyValueAsString(
70 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY)+ File.separator+"checkList"+File.separator+checkListBo.getRemoteObjectIdentifier();
71 if(LOG.isInfoEnabled()) {
72 LOG.info("file location : " + directory);
73 }
74
75 try {
76 File file = new File(directory) ;
77 response.setContentType(checkListBo.getMimeType());
78 response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
79 response.setHeader("Expires", "0");
80 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
81 response.setHeader("Pragma", "public");
82 response.setContentLength((int) file.length());
83 InputStream fis = new BufferedInputStream(new FileInputStream(file));
84 FileCopyUtils.copy(fis, response.getOutputStream());
85 }
86 catch (IOException e) {
87
88 }
89
90 return null;
91 }
92
93
94
95
96
97
98
99
100
101
102 @RequestMapping(params = "methodToCall=" +"maintenanceDelete")
103 public ModelAndView maintenanceDelete(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
104 HttpServletRequest request, HttpServletResponse response) throws Exception {
105 setupMaintenanceForDelete(form, request,"Delete");
106 return getUIFModelAndView(form);
107 }
108
109
110
111
112
113
114
115
116
117
118 @RequestMapping(params = "methodToCall=" +"deleteDocument")
119 public ModelAndView deleteDocument(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
120 HttpServletRequest request, HttpServletResponse response) throws Exception {
121 MaintenanceDocument document = form.getDocument();
122 OleCheckListBo oleCheckListBo = new OleCheckListBo();
123 if(document.getDocumentDataObject() != null) {
124 oleCheckListBo = (OleCheckListBo)document.getDocumentDataObject();
125 if(oleCheckListBo != null && oleCheckListBo.getRemoteObjectIdentifier() != null) {
126 boolean isDeleted= deleteAttachment(oleCheckListBo);
127 if(isDeleted) {
128 KRADServiceLocator.getBusinessObjectService().delete(oleCheckListBo);
129 GlobalVariables.getMessageMap().putInfoWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
130 OLEConstants.OleLicenseRequest.MSG_DELETE_DOC);
131 }
132 else {
133 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OleLicenseRequest.ERROR_FILE_NOT_FOUND);
134 }
135 }
136 else {
137 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OleLicenseRequest.ERROR_CHECKLIST_NOT_FOUND);
138 }
139 }
140 return close(form, result, request, response);
141 }
142
143
144
145
146
147
148
149 protected void setupMaintenanceForDelete(MaintenanceForm form, HttpServletRequest request, String maintenanceAction) {
150 MaintenanceDocument document = form.getDocument();
151 if (document == null) {
152 document = getMaintenanceDocumentService()
153 .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
154 maintenanceAction);
155
156 form.setDocument(document);
157 form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
158 }
159
160 form.setMaintenanceAction(maintenanceAction);
161 getMaintenanceDocumentService().setupMaintenanceObjectForDelete(document, maintenanceAction, request.getParameterMap());
162 MaintenanceUtils.checkForLockingDocument(document, false);
163 }
164
165
166
167
168
169
170 protected boolean deleteAttachment(OleCheckListBo oleCheckListBo) {
171 if(oleCheckListBo != null) {
172 String fullPathUniqueFileName = getKualiConfigurationService().getPropertyValueAsString(
173 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY)+ File.separator+"checkList"+File.separator+oleCheckListBo.getRemoteObjectIdentifier();
174 File attachmentFile = new File(fullPathUniqueFileName);
175 return attachmentFile.delete();
176 }
177 return false;
178 }
179
180 @Override
181 protected OleCheckListMaintenanceDocumentService getMaintenanceDocumentService() {
182 return GlobalResourceLoader.getService("oleCheckListMaintenanceDocumentService");
183 }
184
185 public ConfigurationService getKualiConfigurationService() {
186 return KRADServiceLocator.getKualiConfigurationService();
187 }
188
189
190
191
192
193
194
195
196
197
198 @Override
199 @RequestMapping(params = "methodToCall=route")
200 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase docForm, BindingResult result,
201 HttpServletRequest request, HttpServletResponse response) {
202
203 ModelAndView modelAndView;
204 MaintenanceForm form = (MaintenanceForm) docForm;
205 MultipartFile attachmentFile = form.getAttachmentFile();
206 MaintenanceDocument document = form.getDocument();
207 OleCheckListBo oleCheckListBo = (OleCheckListBo)document.getNewMaintainableObject().getDataObject();
208 OleCheckListBo oleCheckListBoOld = (OleCheckListBo)document.getOldMaintainableObject().getDataObject();
209 Person kualiUser = GlobalVariables.getUserSession().getPerson();
210 oleCheckListBo.setAuthor(kualiUser.getPrincipalId());
211 oleCheckListBo.setLastModified((Timestamp) CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp());
212
213 if (kualiUser == null) {
214 LOG.error("Current UserSession has a null Person");
215 }
216 if(form.getMaintenanceAction().equals("New")) {
217 try {
218 if ((attachmentFile.getSize() == 0 || (attachmentFile.getOriginalFilename().isEmpty() ||
219 attachmentFile.getOriginalFilename().equalsIgnoreCase("")))) {
220 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
221 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY,"");
222 modelAndView = getUIFModelAndView(form);
223 return modelAndView;
224 }
225 else {
226 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename());
227 oleCheckListBo.setMimeType(attachmentFile.getContentType());
228 String remoteObjectIdentifier = UUID.randomUUID().toString();
229 if(storeAttachment(attachmentFile,remoteObjectIdentifier))
230 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier);
231 }
232 }
233 catch(Exception e) {
234 LOG.error("Error while storing the attachment");
235 }
236 }
237 if(form.getMaintenanceAction().equals("Edit")) {
238 try {
239 if (!(attachmentFile.getOriginalFilename().isEmpty() ||
240 attachmentFile.getOriginalFilename().equalsIgnoreCase(""))) {
241 if(attachmentFile.getSize() == 0) {
242 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
243 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY,attachmentFile.getOriginalFilename());
244 }
245 else {
246 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename());
247 oleCheckListBo.setMimeType(attachmentFile.getContentType());
248 String remoteObjectIdentifier = UUID.randomUUID().toString();
249 if(storeAttachment(attachmentFile,remoteObjectIdentifier))
250 deleteAttachment(oleCheckListBoOld);
251 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier);
252 }
253 }
254 }
255 catch(Exception e) {
256 LOG.error("Error while storing the attachment");
257 }
258 }
259
260
261 modelAndView = super.route(form, result, request, response);
262
263 if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) {
264 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService()
265 .retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject());
266 request.setAttribute("fileName", bo.getFileName());
267 }
268
269 modelAndView = getUIFModelAndView(form);
270
271 return modelAndView;
272 }
273
274
275
276
277
278
279
280
281 public boolean storeAttachment(MultipartFile attachedFile,String fileName) throws IOException{
282 String location = null;
283
284
285
286
287
288 location = getKualiConfigurationService().getPropertyValueAsString(
289 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY)+ File.separator+"checkList";
290
291 if(LOG.isInfoEnabled()) {
292 LOG.info("file location : " + location);
293 }
294 File dirLocation = new File(location);
295 if(!dirLocation.exists()) {
296 boolean success = dirLocation.mkdirs();
297 if(!success) {
298 LOG.error("Could not generate directory for File at: " + dirLocation.getAbsolutePath());
299 return false;
300 }
301 }
302 location = location+File.separator+fileName;
303 File fileOut = new File(location);
304 FileOutputStream streamOut = null;
305 BufferedOutputStream bufferedStreamOut = null;
306 try {
307 InputStream fileContent = attachedFile.getInputStream();
308 streamOut = new FileOutputStream(fileOut);
309 bufferedStreamOut = new BufferedOutputStream(streamOut);
310 int c;
311 while ((c = fileContent.read()) != -1) {
312 bufferedStreamOut.write(c);
313 }
314 }
315 finally {
316 bufferedStreamOut.close();
317 streamOut.close();
318 }
319 return true;
320 }
321 }