1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.file.FileMeta;
20 import org.kuali.rice.krad.uif.UifConstants;
21 import org.kuali.rice.krad.uif.UifParameters;
22 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
23 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
24 import org.kuali.rice.krad.util.KRADUtils;
25 import org.kuali.rice.krad.web.form.DialogResponse;
26 import org.kuali.rice.krad.web.form.UifFormBase;
27 import org.kuali.rice.krad.web.service.CollectionControllerService;
28 import org.kuali.rice.krad.web.service.FileControllerService;
29 import org.kuali.rice.krad.web.service.ModelAndViewService;
30 import org.springframework.web.multipart.MultipartFile;
31 import org.springframework.web.multipart.MultipartHttpServletRequest;
32 import org.springframework.web.servlet.ModelAndView;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import java.util.Collection;
37 import java.util.Date;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.UUID;
41
42
43
44
45
46
47 public class FileControllerServiceImpl implements FileControllerService {
48
49 protected static final String DELETE_FILE_UPLOAD_LINE_DIALOG = "DialogGroup-DeleteFileUploadLine";
50
51 private CollectionControllerService collectionControllerService;
52 private ModelAndViewService modelAndViewService;
53
54
55
56
57 @Override
58 public ModelAndView addFileUploadLine(final UifFormBase form) {
59 form.setAjaxReturnType(UifConstants.AjaxReturnTypes.UPDATECOMPONENT.getKey());
60 form.setAjaxRequest(true);
61
62 MultipartHttpServletRequest request = (MultipartHttpServletRequest) form.getRequest();
63
64 final String collectionId = request.getParameter(UifParameters.UPDATE_COMPONENT_ID);
65 final String bindingPath = request.getParameter(UifConstants.PostMetadata.BINDING_PATH);
66
67 Class<?> collectionObjectClass = (Class<?>) form.getViewPostMetadata().getComponentPostData(collectionId,
68 UifConstants.PostMetadata.COLL_OBJECT_CLASS);
69
70 Iterator<String> fileNamesItr = request.getFileNames();
71
72 while (fileNamesItr.hasNext()) {
73 String propertyPath = fileNamesItr.next();
74
75 MultipartFile uploadedFile = request.getFile(propertyPath);
76
77 final FileMeta fileObject = (FileMeta) KRADUtils.createNewObjectFromClass(collectionObjectClass);
78 try {
79 fileObject.init(uploadedFile);
80 } catch (Exception e) {
81 throw new RuntimeException("Unable to initialize new file object", e);
82 }
83
84 String id = UUID.randomUUID().toString() + "_" + uploadedFile.getName();
85 fileObject.setId(id);
86
87 fileObject.setDateUploaded(new Date());
88
89 fileObject.setUrl("?methodToCall=getFileFromLine&formKey="
90 + form.getFormKey()
91 + "&fileName="
92 + fileObject.getName()
93 + "&propertyPath="
94 + propertyPath);
95
96 ViewLifecycle.encapsulateLifecycle(form.getView(), form, form.getViewPostMetadata(), null, request,
97 new Runnable() {
98 @Override
99 public void run() {
100 ViewLifecycle.getHelper().processAndAddLineObject(form, fileObject, collectionId,
101 bindingPath);
102 }
103 });
104 }
105
106 return getModelAndViewService().getModelAndView(form);
107 }
108
109
110
111
112 @Override
113 public ModelAndView deleteFileUploadLine(UifFormBase form) {
114 DialogResponse deleteFileUploadLineDialogResponse = form.getDialogResponse(DELETE_FILE_UPLOAD_LINE_DIALOG);
115
116 if (deleteFileUploadLineDialogResponse == null) {
117
118 return getModelAndViewService().showDialog(DELETE_FILE_UPLOAD_LINE_DIALOG, true, form);
119 }
120
121 return getCollectionControllerService().deleteLine(form);
122 }
123
124
125
126
127 @Override
128 public void getFileFromLine(UifFormBase form, HttpServletResponse response) {
129 HttpServletRequest request = form.getRequest();
130
131 String selectedCollectionPath = request.getParameter("propertyPath");
132
133 if (StringUtils.isBlank(selectedCollectionPath)) {
134 throw new RuntimeException("Selected collection was not set for delete line action, cannot delete line");
135 }
136
137 String selectedLine = request.getParameter(UifParameters.SELECTED_LINE_INDEX);
138 int selectedLineIndex;
139 if (StringUtils.isNotBlank(selectedLine)) {
140 selectedLineIndex = Integer.parseInt(selectedLine);
141 } else {
142 selectedLineIndex = -1;
143 }
144
145 if (selectedLineIndex == -1) {
146 throw new RuntimeException("Selected line index was not set for delete line action, cannot delete line");
147 }
148
149 Collection<FileMeta> collection = ObjectPropertyUtils.getPropertyValue(form, selectedCollectionPath);
150
151 if (collection instanceof List) {
152 FileMeta fileLine = ((List<FileMeta>) collection).get(selectedLineIndex);
153 sendFileFromLineResponse(form, response, (List<FileMeta>) collection, fileLine);
154 }
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 protected void sendFileFromLineResponse(UifFormBase form, HttpServletResponse response, List<FileMeta> collection,
181 FileMeta fileLine) {
182
183 }
184
185 protected CollectionControllerService getCollectionControllerService() {
186 return collectionControllerService;
187 }
188
189 public void setCollectionControllerService(CollectionControllerService collectionControllerService) {
190 this.collectionControllerService = collectionControllerService;
191 }
192
193 protected ModelAndViewService getModelAndViewService() {
194 return modelAndViewService;
195 }
196
197 public void setModelAndViewService(ModelAndViewService modelAndViewService) {
198 this.modelAndViewService = modelAndViewService;
199 }
200 }