1 package org.kuali.ole.editor.contoller;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
6 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
7 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
8 import org.kuali.ole.editor.bo.InstanceEditorFormDataHandler;
9 import org.kuali.ole.editor.bo.InstanceRecordMetaData;
10 import org.kuali.ole.editor.form.InstanceEditorForm;
11 import org.kuali.ole.editor.service.DiscoveryHelperService;
12 import org.kuali.ole.editor.service.DocstoreHelperService;
13 import org.kuali.rice.krad.web.controller.UifControllerBase;
14 import org.kuali.rice.krad.web.form.UifFormBase;
15 import org.springframework.stereotype.Controller;
16 import org.springframework.validation.BindingResult;
17 import org.springframework.web.bind.annotation.ModelAttribute;
18 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.bind.annotation.RequestMethod;
20 import org.springframework.web.servlet.ModelAndView;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import java.util.*;
25
26
27
28
29 @Controller
30 @RequestMapping(value = "/instanceeditor")
31
32 public class InstanceEditorController extends UifControllerBase {
33
34 private DocstoreHelperService docstoreHelperService;
35 private InstanceEditorFormDataHandler instanceEditorFormDataHandler;
36 private DiscoveryHelperService discoveryHelperService;
37
38 private static final Logger LOG = Logger.getLogger(InstanceEditorController.class);
39
40
41
42
43
44
45 @Override
46 protected InstanceEditorForm createInitialForm(HttpServletRequest httpServletRequest) {
47 return new InstanceEditorForm();
48 }
49
50
51
52
53
54
55
56
57
58 @Override
59 @RequestMapping(params = "methodToCall=start")
60 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
61 HttpServletRequest request, HttpServletResponse response) {
62 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
63 return super.start(instanceEditorForm, result, request, response);
64 }
65
66
67
68
69
70
71
72
73
74 @RequestMapping(params = "methodToCall=loadInstanceRecord")
75 public ModelAndView loadInstanceRecord(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
76 HttpServletRequest request, HttpServletResponse response) {
77 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
78 String instanceIdentifier = request.getParameter("uuid");
79 if (null == instanceIdentifier) {
80 instanceIdentifier = instanceEditorForm.getUuid();
81 }
82 instanceEditorForm.setExisting("true");
83 try {
84 String docStoreData = getDocstoreHelperService().getDocstoreData(instanceIdentifier);
85 InstanceCollection instanceCollection = new WorkInstanceOlemlRecordProcessor().fromXML(docStoreData);
86 instanceEditorForm.setInstance(instanceCollection.getInstanceCollection().get(0));
87
88
89 getInstanceEditorFormDataHandler().buildLeftPaneData(instanceEditorForm);
90
91
92 getInstanceEditorFormDataHandler().setMetaData(instanceEditorForm, OLEConstants.HOLDING_DOC_TYPE);
93
94 StringBuffer instanceEditorSuccessMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_LOAD_SUCCESS);
95 instanceEditorForm.setMessage(instanceEditorSuccessMessage.toString());
96 } catch (Exception e) {
97 StringBuffer instanceEditorErrorMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_LOAD_FAILURE).append(" :: \n" + e.getMessage());
98 instanceEditorForm.setMessage(instanceEditorErrorMessage.toString());
99 LOG.error(instanceEditorErrorMessage);
100 }
101 return getUIFModelAndView(instanceEditorForm, OLEConstants.HOLDINGS_PAGE);
102 }
103
104
105
106
107
108
109
110
111
112
113 @RequestMapping(params = "methodToCall=submit")
114 public ModelAndView submit(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
115 HttpServletRequest request, HttpServletResponse response) {
116 String responseFromDocstore = null;
117 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
118 String uuid = instanceEditorForm.getUuid();
119 try {
120 if (uuid != null && !uuid.trim().equals("")) {
121 OleHolding holdingData = instanceEditorForm.getInstance().getHoldings();
122 String holdingId = instanceEditorForm.getInstance().getHoldings().getHoldingsIdentifier();
123 String holdingXmlContent = getInstanceEditorFormDataHandler().buildHoldingContent(holdingData);
124 String holdingRecordUpdateResponse =
125 getDocstoreHelperService().updateInstanceRecord(holdingId, OLEConstants.HOLDING_DOC_TYPE, holdingXmlContent);
126 for (int item = 0; item < instanceEditorForm.getInstance().getItem().size(); item++) {
127 OleItem itemData = instanceEditorForm.getInstance().getItem().get(item);
128 String itemId = instanceEditorForm.getInstance().getItem().get(item).getItemIdentifier();
129 String itemXmlContent = getInstanceEditorFormDataHandler().buildItemContent(itemData);
130 if (itemId == null) {
131
132 String instanceUuid = uuid;
133 String itemRecordUpdateResponse =
134 getDocstoreHelperService().createItemForInstanceRecord(instanceUuid, OLEConstants.ITEM_DOC_TYPE, itemXmlContent);
135 } else {
136 String itemRecordUpdateResponse =
137 getDocstoreHelperService().updateInstanceRecord(itemId, OLEConstants.ITEM_DOC_TYPE, itemXmlContent);
138 }
139
140 }
141 } else {
142 OleItem oleItem = instanceEditorForm.getSelectedItem();
143 instanceEditorForm.getInstance().setItem(Arrays.asList(oleItem));
144 String content = getInstanceEditorFormDataHandler().buildInstanceRecordForDocStore(instanceEditorForm, uuid);
145 responseFromDocstore = getDocstoreHelperService().
146 instanceRecordCallToDocstore(content, null, OLEConstants.OLEML_FORMAT);
147 }
148 instanceEditorForm.setUuid("");
149 StringBuffer instanceEditorSuccessMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_SUCCESS);
150 instanceEditorForm.setMessage(instanceEditorSuccessMessage.toString());
151 } catch (Exception e) {
152 StringBuffer instanceEditorErrorMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_FAILURE).append(" :: \n" + e.getMessage());
153 instanceEditorForm.setMessage(instanceEditorErrorMessage.toString());
154 LOG.error(instanceEditorErrorMessage);
155 }
156 InstanceEditorForm finalForm = instanceEditorForm;
157 instanceEditorForm.setInstance(new OleInstance());
158 return getUIFModelAndView(finalForm, OLEConstants.HOLDINGS_PAGE);
159 }
160
161
162
163
164
165
166
167
168
169 @RequestMapping(params = "methodToCall=updateLeftPane")
170 public ModelAndView updateLeftPane(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
171 HttpServletRequest request, HttpServletResponse response) {
172 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
173 String hiddenVal = instanceEditorForm.getHdnUuid();
174 int hdnIndex = instanceEditorForm.getHdnIndex();
175 instanceEditorForm.setSelectedItem(instanceEditorForm.getInstance().getItem().get(hdnIndex));
176 DiscoveryHelperService discoveryHelperService = getDiscoveryHelperService();
177
178 List responseFromSOLR = discoveryHelperService.getResponseFromSOLR("id", hiddenVal);
179 String docType = null;
180 for (Iterator iterator = responseFromSOLR.iterator(); iterator.hasNext(); ) {
181 Map map = (Map) iterator.next();
182 if (map.containsKey(OLEConstants.DOC_TYPE)) {
183 String list = (String) map.get(OLEConstants.DOC_TYPE);
184 docType = list;
185 break;
186 }
187 }
188 if (docType.toString().equals(OLEConstants.ITEM_DOC_TYPE)) {
189 getInstanceEditorFormDataHandler().setMetaData(instanceEditorForm, OLEConstants.ITEM_DOC_TYPE);
190
191 return getUIFModelAndView(instanceEditorForm, OLEConstants.ITEM_PAGE);
192 } else {
193 return getUIFModelAndView(instanceEditorForm, OLEConstants.HOLDINGS_PAGE);
194 }
195 }
196
197
198
199
200
201
202
203
204
205 @RequestMapping(method = RequestMethod.POST,params = "methodToCall=addNewItem")
206 public ModelAndView addItemRecord(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
207 HttpServletRequest request, HttpServletResponse response){
208 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
209 List<OleItem> existingItemsFromDoc = instanceEditorForm.getInstance().getItem();
210 List<OleItem> oleItems = new ArrayList<OleItem>();
211
212 for(int i=0;i<existingItemsFromDoc.size();i++){
213 oleItems.add(existingItemsFromDoc.get(i));
214 }
215 oleItems.add(new OleItem());
216 instanceEditorForm.setItemRecordMetaData(new InstanceRecordMetaData());
217 instanceEditorForm.getInstance().setItem(oleItems);
218 instanceEditorForm.setHdnIndex(oleItems.size());
219 int hdnIndex = instanceEditorForm.getHdnIndex();
220 instanceEditorForm.setSelectedItem(instanceEditorForm.getInstance().getItem().get(hdnIndex-1));
221 return getUIFModelAndView(instanceEditorForm, OLEConstants.ITEM_PAGE);
222 }
223
224
225
226
227
228
229
230
231
232 @RequestMapping(method = RequestMethod.POST,params = "methodToCall=getNewItemData")
233 public ModelAndView getNewItemData(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
234 HttpServletRequest request, HttpServletResponse response){
235 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
236 List<OleItem> existingItemsFromDoc = instanceEditorForm.getInstance().getItem();
237 List<OleItem> oleItems = new ArrayList<OleItem>();
238
239 for(int i=0;i<existingItemsFromDoc.size();i++){
240 oleItems.add(existingItemsFromDoc.get(i));
241 }
242 instanceEditorForm.setHdnIndex(oleItems.size());
243 int hdnIndex = instanceEditorForm.getHdnIndex();
244 instanceEditorForm.setSelectedItem(instanceEditorForm.getInstance().getItem().get(hdnIndex-1));
245 return getUIFModelAndView(instanceEditorForm, OLEConstants.ITEM_PAGE);
246 }
247
248
249
250
251
252
253
254
255
256 protected ModelAndView callSuper(BindingResult result, HttpServletRequest request, HttpServletResponse response, InstanceEditorForm instanceEditorForm) {
257 return super.updateComponent(instanceEditorForm, result, request, response);
258 }
259
260
261
262
263
264 private DocstoreHelperService getDocstoreHelperService() {
265 if (null == docstoreHelperService) {
266 return new DocstoreHelperService();
267 }
268 return docstoreHelperService;
269 }
270
271
272
273
274
275 public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
276 this.docstoreHelperService = docstoreHelperService;
277 }
278
279
280
281
282
283 private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
284 if (null == instanceEditorFormDataHandler) {
285 instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
286 }
287 return instanceEditorFormDataHandler;
288 }
289
290
291
292
293
294
295 private AdditionalAttributes getFirstAdditionalAttributes(List<Extension> extensionList) {
296 for (Extension ext : extensionList) {
297 for (Object obj : ext.getContent()) {
298 if (obj instanceof AdditionalAttributes) {
299 return (AdditionalAttributes) obj;
300 }
301 }
302 }
303 return null;
304 }
305
306
307
308
309
310 public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
311 this.discoveryHelperService = discoveryHelperService;
312 }
313
314
315
316
317
318 public DiscoveryHelperService getDiscoveryHelperService() {
319 if (null == discoveryHelperService) {
320 return new DiscoveryHelperService();
321 }
322 return discoveryHelperService;
323 }
324 }
325