1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.inquiry;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.exception.RiceRuntimeException;
20 import org.kuali.rice.krad.bo.PersistableAttachment;
21 import org.kuali.rice.krad.bo.PersistableAttachmentList;
22 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23 import org.kuali.rice.krad.service.ModuleService;
24 import org.kuali.rice.krad.uif.UifParameters;
25 import org.kuali.rice.krad.util.GlobalVariables;
26 import org.kuali.rice.krad.util.KRADConstants;
27 import org.kuali.rice.krad.util.KRADUtils;
28 import org.kuali.rice.krad.web.form.InquiryForm;
29 import org.kuali.rice.krad.web.form.UifFormBase;
30 import org.kuali.rice.krad.web.service.impl.ControllerServiceImpl;
31 import org.springframework.web.servlet.ModelAndView;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import java.io.ByteArrayInputStream;
36 import java.io.IOException;
37 import java.lang.reflect.Method;
38 import java.util.Properties;
39
40
41
42
43
44
45
46 public class InquiryControllerServiceImpl extends ControllerServiceImpl implements InquiryControllerService {
47
48
49
50
51
52
53
54
55
56 @Override
57 public ModelAndView start(UifFormBase form) {
58 InquiryForm inquiryForm = (InquiryForm) form;
59 HttpServletRequest request = form.getRequest();
60
61 Boolean hasRedirectedInquiryParameter = (request.getParameter(UifParameters.REDIRECTED_INQUIRY) != null
62 && request.getParameter(UifParameters.REDIRECTED_INQUIRY).contains("true"));
63
64 if (!inquiryForm.isRedirectedInquiry() && !hasRedirectedInquiryParameter) {
65 ModelAndView redirectModelAndView = checkForModuleInquiryRedirect(inquiryForm, request);
66 if (redirectModelAndView != null) {
67 return redirectModelAndView;
68 }
69 }
70
71
72 Object dataObject = inquiryForm.getInquirable().retrieveDataObject(KRADUtils.translateRequestParameterMap(
73 request.getParameterMap()));
74
75 inquiryForm.setDataObject(dataObject);
76
77 return super.start(inquiryForm);
78 }
79
80
81
82
83
84
85
86
87
88 protected ModelAndView checkForModuleInquiryRedirect(InquiryForm inquiryForm, HttpServletRequest request) {
89 Class<?> inquiryObjectClass;
90 try {
91 inquiryObjectClass = Class.forName(inquiryForm.getDataObjectClassName());
92 } catch (ClassNotFoundException e) {
93 throw new RiceRuntimeException("Unable to get class for name: " + inquiryForm.getDataObjectClassName(), e);
94 }
95
96 ModuleService responsibleModuleService =
97 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass);
98 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) {
99 String inquiryUrl = responsibleModuleService.getExternalizableDataObjectInquiryUrl(inquiryObjectClass,
100 KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
101
102 Properties redirectUrlProps = new Properties();
103 redirectUrlProps.put(UifParameters.REDIRECTED_INQUIRY, "true");
104
105 GlobalVariables.getUifFormManager().removeSessionForm(inquiryForm);
106
107 return getModelAndViewService().performRedirect(inquiryForm, inquiryUrl, redirectUrlProps);
108 }
109
110 return null;
111 }
112
113
114
115
116 @Override
117 public void downloadDataObjectAttachment(InquiryForm form, HttpServletResponse response) {
118 Object dataObject = form.getDataObject();
119
120 if (dataObject instanceof PersistableAttachment) {
121 PersistableAttachment attachment = (PersistableAttachment) dataObject;
122 byte[] attachmentContent = attachment.getAttachmentContent();
123
124 addAttachmentToResponse(response, attachmentContent, attachment.getContentType(), attachment.getFileName());
125 } else if (dataObject instanceof PersistableAttachmentList) {
126 PersistableAttachmentList<PersistableAttachment> attachmentListBo =
127 (PersistableAttachmentList<PersistableAttachment>) dataObject;
128 PersistableAttachment attachment = attachmentListBo.getAttachments().get(Integer.parseInt(
129 form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX)));
130 byte[] attachmentContent = attachment.getAttachmentContent();
131
132 addAttachmentToResponse(response, attachmentContent, attachment.getContentType(), attachment.getFileName());
133 }
134 }
135
136
137
138
139 @Override
140 public void downloadCustomDataObjectAttachment(InquiryForm form, HttpServletRequest request,
141 HttpServletResponse response) throws Exception {
142 String fileName = request.getParameter(KRADConstants.DATA_OBJECT_ATTACHMENT_FILE_NAME);
143 String contentType = request.getParameter(KRADConstants.DATA_OBJECT_ATTACHMENT_FILE_CONTENT_TYPE);
144 String fileContentDataObjField = request.getParameter(KRADConstants.DATA_OBJECT_ATTACHMENT_FILE_CONTENT_FIELD);
145
146 if (fileName == null) {
147 throw new RuntimeException("Request Parameter "
148 + KRADConstants.DATA_OBJECT_ATTACHMENT_FILE_NAME + " must be provided");
149 }
150
151 if (contentType == null) {
152 throw new RuntimeException("Request Parameter "
153 + KRADConstants.DATA_OBJECT_ATTACHMENT_FILE_CONTENT_TYPE + " must be provided");
154 }
155
156 if (fileContentDataObjField == null) {
157 throw new RuntimeException("Request Parameter "
158 + KRADConstants.DATA_OBJECT_ATTACHMENT_FILE_CONTENT_FIELD + " must be provided");
159 }
160
161 checkViewAuthorization(form);
162
163 Object dataObject = form.getDataObject();
164
165 if (dataObject == null && GlobalVariables.getMessageMap().hasNoMessages()) {
166 throw new UnsupportedOperationException("The record you have inquired on does not exist.");
167 }
168
169 Method method = dataObject.getClass().getMethod("get" + StringUtils.capitalize(fileContentDataObjField));
170 byte[] attachmentContent = (byte[]) method.invoke(dataObject);
171
172 addAttachmentToResponse(response, attachmentContent, contentType, fileName);
173 }
174
175
176
177
178
179
180
181
182
183
184 private void addAttachmentToResponse(HttpServletResponse response, byte[] attachmentContent,
185 String contentType, String fileName) {
186 ByteArrayInputStream inputStream = null;
187 int attachmentContentLength;
188
189 if (attachmentContent != null) {
190 inputStream = new ByteArrayInputStream(attachmentContent);
191 attachmentContentLength = attachmentContent.length;
192 } else {
193 attachmentContentLength = 0;
194 }
195
196 try {
197 KRADUtils.addAttachmentToResponse(response, inputStream, contentType, fileName, attachmentContentLength);
198 } catch (IOException e) {
199 throw new RuntimeException("Unable to retrieve attachment contents", e);
200 }
201 }
202 }