1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.maintenance;
17
18 import org.apache.commons.lang3.ArrayUtils;
19 import org.kuali.rice.core.api.util.RiceKeyConstants;
20 import org.kuali.rice.kew.api.KewApiConstants;
21 import org.kuali.rice.kew.api.exception.WorkflowException;
22 import org.kuali.rice.krad.bo.PersistableAttachment;
23 import org.kuali.rice.krad.bo.PersistableAttachmentList;
24 import org.kuali.rice.krad.datadictionary.DocumentEntry;
25 import org.kuali.rice.krad.document.DocumentControllerServiceImpl;
26 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
27 import org.kuali.rice.krad.service.MaintenanceDocumentService;
28 import org.kuali.rice.krad.uif.UifParameters;
29 import org.kuali.rice.krad.util.GlobalVariables;
30 import org.kuali.rice.krad.util.KRADConstants;
31 import org.kuali.rice.krad.util.KRADUtils;
32 import org.kuali.rice.krad.web.form.DocumentFormBase;
33 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
34 import org.kuali.rice.krad.web.form.UifFormBase;
35 import org.springframework.web.servlet.ModelAndView;
36
37 import javax.servlet.http.HttpServletResponse;
38 import java.io.ByteArrayInputStream;
39 import java.io.IOException;
40
41
42
43
44
45
46 public class MaintenanceDocumentControllerServiceImpl extends DocumentControllerServiceImpl implements MaintenanceDocumentControllerService {
47
48 private MaintenanceDocumentService maintenanceDocumentService;
49
50
51
52
53 @Override
54 public ModelAndView start(UifFormBase form) {
55 setupMaintenanceDocument((MaintenanceDocumentForm) form, KRADConstants.MAINTENANCE_NEW_ACTION);
56
57 return super.start(form);
58 }
59
60
61
62
63 @Override
64 protected void loadDocument(DocumentFormBase form) throws WorkflowException {
65 super.loadDocument(form);
66
67 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
68 maintenanceForm.setMaintenanceAction(
69 (maintenanceForm.getDocument()).getNewMaintainableObject().getMaintenanceAction());
70 }
71
72
73
74
75 @Override
76 protected void createDocument(DocumentFormBase form) throws WorkflowException {
77 setupMaintenanceDocument((MaintenanceDocumentForm) form, KRADConstants.MAINTENANCE_NEW_ACTION);
78 }
79
80
81
82
83 @Override
84 public ModelAndView setupMaintenanceEdit(MaintenanceDocumentForm form) {
85 setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_EDIT_ACTION);
86
87 return getModelAndViewService().getModelAndView(form);
88 }
89
90
91
92
93 @Override
94 public ModelAndView setupMaintenanceCopy(MaintenanceDocumentForm form) {
95 setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_COPY_ACTION);
96
97 return getModelAndViewService().getModelAndView(form);
98 }
99
100
101
102
103 @Override
104 public ModelAndView setupMaintenanceNewWithExisting(MaintenanceDocumentForm form) {
105 setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION);
106
107 return getModelAndViewService().getModelAndView(form);
108 }
109
110
111
112
113 @Override
114 public ModelAndView setupMaintenanceDelete(MaintenanceDocumentForm form) {
115 GlobalVariables.getMessageMap().putWarning(KRADConstants.GLOBAL_MESSAGES, RiceKeyConstants.MESSAGE_DELETE);
116
117 setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_DELETE_ACTION);
118
119 return getModelAndViewService().getModelAndView(form);
120 }
121
122
123
124
125 @Override
126 public void setupMaintenanceDocument(MaintenanceDocumentForm form, String maintenanceAction) {
127 MaintenanceDocument document = form.getDocument();
128
129 if (document == null) {
130 createMaintenanceDocument(form, maintenanceAction);
131
132 document = form.getDocument();
133 }
134
135 form.setMaintenanceAction(maintenanceAction);
136
137
138 getMaintenanceDocumentService().setupMaintenanceObject(document, maintenanceAction,
139 form.getRequest().getParameterMap());
140
141
142 if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
143 MaintenanceUtils.checkForLockingDocument(document, false);
144 }
145
146
147
148 DocumentEntry entry = KRADServiceLocatorWeb.getDocumentDictionaryService().getMaintenanceDocumentEntry(
149 document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
150 document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes());
151 }
152
153
154
155
156
157
158
159 protected void createMaintenanceDocument(MaintenanceDocumentForm form, String maintenanceAction) {
160 MaintenanceDocument document = getMaintenanceDocumentService().setupNewMaintenanceDocument(
161 form.getDataObjectClassName(), form.getDocTypeName(), maintenanceAction);
162
163 form.setDocument(document);
164 form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
165 }
166
167
168
169
170 @Override
171 public void downloadDataObjectAttachment(MaintenanceDocumentForm form, HttpServletResponse response) {
172 MaintenanceDocument document = form.getDocument();
173 Object dataObject = document.getDocumentDataObject();
174
175 if (dataObject instanceof PersistableAttachment) {
176 PersistableAttachment attachment = (PersistableAttachment) dataObject;
177
178 byte[] attachmentContent = attachment.getAttachmentContent();
179 if (attachmentContent != null) {
180 ByteArrayInputStream inputStream = new ByteArrayInputStream(attachmentContent);
181 try {
182 KRADUtils.addAttachmentToResponse(response, inputStream, attachment.getContentType(),
183 attachment.getFileName(), attachmentContent.length);
184 } catch (IOException e) {
185 throw new RuntimeException("Unable to retrieve attachment contents", e);
186 }
187 }
188 } else if (dataObject instanceof PersistableAttachmentList) {
189 PersistableAttachmentList<PersistableAttachment> attachmentList =
190 (PersistableAttachmentList<PersistableAttachment>) dataObject;
191 PersistableAttachmentList<PersistableAttachment> attachmentListBo =
192 (PersistableAttachmentList<PersistableAttachment>) document.getNewMaintainableObject()
193 .getDataObject();
194
195 PersistableAttachment attachment = (PersistableAttachment) attachmentListBo.getAttachments().get(
196 Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX)));
197
198 byte[] attachmentContent = attachment.getAttachmentContent();
199 if (attachmentContent != null) {
200 ByteArrayInputStream inputStream = new ByteArrayInputStream(attachmentContent);
201 try {
202 KRADUtils.addAttachmentToResponse(response, inputStream, attachment.getContentType(),
203 attachment.getFileName(), attachmentContent.length);
204 } catch (IOException e) {
205 throw new RuntimeException("Unable to retrieve attachment contents", e);
206 }
207 }
208 }
209 }
210
211 protected MaintenanceDocumentService getMaintenanceDocumentService() {
212 return maintenanceDocumentService;
213 }
214
215 public void setMaintenanceDocumentService(MaintenanceDocumentService maintenanceDocumentService) {
216 this.maintenanceDocumentService = maintenanceDocumentService;
217 }
218 }