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.*;
9 import org.kuali.ole.editor.form.InstanceEditorForm;
10 import org.kuali.ole.editor.service.DiscoveryHelperService;
11 import org.kuali.ole.editor.service.DocstoreHelperService;
12 import org.kuali.rice.krad.uif.UifParameters;
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 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=navigate")
67 public ModelAndView navigate(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
68 HttpServletRequest request, HttpServletResponse response) {
69 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
70 String pageId = form.getActionParamaterValue(UifParameters.NAVIGATE_TO_PAGE_ID);
71
72 return getUIFModelAndView(instanceEditorForm, pageId);
73 }
74
75
76
77
78
79
80
81
82
83 @RequestMapping(params = "methodToCall=loadInstanceRecord")
84 public ModelAndView loadInstanceRecord(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
85 HttpServletRequest request, HttpServletResponse response) {
86 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
87 String instanceIdentifier = request.getParameter("uuid");
88 if (null == instanceIdentifier) {
89 instanceIdentifier = instanceEditorForm.getUuid();
90 }
91 instanceEditorForm.setExisting("true");
92 try {
93 String docStoreData = getDocstoreHelperService().getDocstoreData(instanceIdentifier);
94 InstanceCollection instanceCollection = new WorkInstanceOlemlRecordProcessor().fromXML(docStoreData);
95 instanceEditorForm.setInstance(instanceCollection.getInstance().get(0));
96 instanceEditorForm.setSelectedHolding(instanceEditorForm.getInstance().getOleHoldings());
97 instanceEditorForm.setSelectedItem(instanceEditorForm.getInstance().getItems().getItem().get(0));
98 if(instanceEditorForm.getInstance().getSourceHoldings() != null) {
99 instanceEditorForm.setSelectedSourceHolding(
100 getInstanceEditorFormDataHandler().buildSourceHoldingForUI(instanceEditorForm));
101 }
102
103 getInstanceEditorFormDataHandler().setLocationDetails(instanceEditorForm);
104
105 getInstanceEditorFormDataHandler().buildLeftPaneData(instanceEditorForm);
106
107
108
109
110 StringBuffer instanceEditorSuccessMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_LOAD_SUCCESS);
111 instanceEditorForm.setMessage(instanceEditorSuccessMessage.toString());
112 } catch (Exception e) {
113 StringBuffer instanceEditorErrorMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_LOAD_FAILURE).append(" :: \n" + e.getMessage());
114 instanceEditorForm.setMessage(instanceEditorErrorMessage.toString());
115 LOG.error(instanceEditorErrorMessage);
116 }
117 return getUIFModelAndView(instanceEditorForm, OLEConstants.HOLDINGS_PAGE);
118 }
119
120
121
122
123
124
125
126
127
128
129 @RequestMapping(params = "methodToCall=submit")
130 public ModelAndView submit(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
131 HttpServletRequest request, HttpServletResponse response) {
132 String responseFromDocstore = null;
133 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
134 String uuid = instanceEditorForm.getUuid();
135 try {
136 if (uuid != null && !uuid.trim().equals("")) {
137 OleHoldings holdingData = instanceEditorForm.getInstance().getOleHoldings();
138 String holdingId = instanceEditorForm.getInstance().getOleHoldings().getHoldingsIdentifier();
139 String holdingXmlContent = getInstanceEditorFormDataHandler().buildHoldingContent(holdingData);
140 String holdingRecordUpdateResponse =
141 getDocstoreHelperService().updateInstanceRecord(holdingId, OLEConstants.HOLDING_DOC_TYPE, holdingXmlContent);
142
143 for (int item = 0; item < instanceEditorForm.getInstance().getItems().getItem().size(); item++) {
144 Item itemData = instanceEditorForm.getInstance().getItems().getItem().get(item);
145 String itemId = instanceEditorForm.getInstance().getItems().getItem().get(item).getItemIdentifier();
146 String itemXmlContent = getInstanceEditorFormDataHandler().buildItemContent(itemData);
147 if (itemId == null) {
148
149 String instanceUuid = uuid;
150 String itemRecordUpdateResponse =
151 getDocstoreHelperService().createItemForInstanceRecord(instanceUuid, OLEConstants.ITEM_DOC_TYPE, itemXmlContent);
152 } else {
153 String itemRecordUpdateResponse =
154 getDocstoreHelperService().updateInstanceRecord(itemId, OLEConstants.ITEM_DOC_TYPE, itemXmlContent);
155 }
156 }
157 SourceHoldings sourceHoldings = getInstanceEditorFormDataHandler().buildSourceHoldings(instanceEditorForm);
158 String sourceHoldingId = sourceHoldings.getHoldingsIdentifier();
159 String sourceHoldinXml = getInstanceEditorFormDataHandler().buildSourceHoldingContent(sourceHoldings);
160 String sourceHoldingUpdateResponse =
161 getDocstoreHelperService().updateInstanceRecord(sourceHoldingId, "sourceHoldings", sourceHoldinXml);
162 } else {
163 Instance instance = new Instance();
164 instanceEditorForm.setInstance(instance);
165 Item oleItem = instanceEditorForm.getSelectedItem();
166 Items items = new Items();
167 items.setItem(Arrays.asList(oleItem));
168 instanceEditorForm.getInstance().setItems(items);
169 instanceEditorForm.getInstance().setOleHoldings(instanceEditorForm.getSelectedHolding());
170 String content = getInstanceEditorFormDataHandler().buildInstanceRecordForDocStore(instanceEditorForm, uuid);
171 responseFromDocstore = getDocstoreHelperService().
172 instanceRecordCallToDocstore(content, null, OLEConstants.OLEML_FORMAT);
173 }
174 instanceEditorForm.setUuid("");
175 StringBuffer instanceEditorSuccessMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_SUCCESS);
176 instanceEditorForm.setMessage(instanceEditorSuccessMessage.toString());
177 } catch (Exception e) {
178 StringBuffer instanceEditorErrorMessage = new StringBuffer(OLEConstants.INSTANCE_EDITOR_FAILURE).append(" :: \n" + e.getMessage());
179 instanceEditorForm.setMessage(instanceEditorErrorMessage.toString());
180 LOG.error(instanceEditorErrorMessage);
181 }
182 InstanceEditorForm finalForm = instanceEditorForm;
183 instanceEditorForm.setInstance(new Instance());
184 instanceEditorForm.setSelectedHolding(new OleHoldings());
185 instanceEditorForm.setSelectedSourceHolding(new SourceEditorForUI());
186 instanceEditorForm.setSelectedItem(new Item());
187 return getUIFModelAndView(finalForm, OLEConstants.HOLDINGS_PAGE);
188
189 }
190
191
192
193
194
195
196
197
198
199 @RequestMapping(params = "methodToCall=updateLeftPane")
200 public ModelAndView updateLeftPane(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
201 HttpServletRequest request, HttpServletResponse response) {
202 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
203 String hiddenVal = instanceEditorForm.getHdnUuid();
204 int hdnIndex = instanceEditorForm.getHdnIndex();
205 instanceEditorForm.setSelectedItem(instanceEditorForm.getInstance().getItems().getItem().get(hdnIndex));
206 DiscoveryHelperService discoveryHelperService = getDiscoveryHelperService();
207
208 List responseFromSOLR = discoveryHelperService.getResponseFromSOLR("id", hiddenVal);
209 String docType = null;
210 for (Iterator iterator = responseFromSOLR.iterator(); iterator.hasNext(); ) {
211 Map map = (Map) iterator.next();
212 if (map.containsKey(OLEConstants.DOC_TYPE)) {
213 String list = (String) map.get(OLEConstants.DOC_TYPE);
214 docType = list;
215 break;
216 }
217 }
218 if (docType.toString().equals(OLEConstants.ITEM_DOC_TYPE)) {
219
220
221 return getUIFModelAndView(instanceEditorForm, OLEConstants.ITEM_PAGE);
222 } else {
223 return getUIFModelAndView(instanceEditorForm, OLEConstants.HOLDINGS_PAGE);
224 }
225 }
226
227
228
229
230
231
232
233
234
235 @RequestMapping(method = RequestMethod.POST,params = "methodToCall=addNewItem")
236 public ModelAndView addItemRecord(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
237 HttpServletRequest request, HttpServletResponse response){
238 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
239 List<Item> existingItemsFromDoc = instanceEditorForm.getInstance().getItems().getItem();
240 List<Item> oleItems = new ArrayList<Item>();
241
242 for(int i=0;i<existingItemsFromDoc.size();i++){
243 oleItems.add(existingItemsFromDoc.get(i));
244 }
245 oleItems.add(new Item());
246 instanceEditorForm.setItemRecordMetaData(new InstanceRecordMetaData());
247 instanceEditorForm.getInstance().getItems().setItem(oleItems);
248 instanceEditorForm.setHdnIndex(oleItems.size());
249 int hdnIndex = instanceEditorForm.getHdnIndex();
250 instanceEditorForm.setSelectedItem(instanceEditorForm.getInstance().getItems().getItem().get(hdnIndex-1));
251 return getUIFModelAndView(instanceEditorForm, OLEConstants.ITEM_PAGE);
252 }
253
254
255
256
257
258
259
260
261
262 @RequestMapping(method = RequestMethod.POST,params = "methodToCall=getNewItemData")
263 public ModelAndView getNewItemData(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
264 HttpServletRequest request, HttpServletResponse response){
265 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
266 List<Item> existingItemsFromDoc = instanceEditorForm.getInstance().getItems().getItem();
267 List<Item> oleItems = new ArrayList<Item>();
268
269 for(int i=0;i<existingItemsFromDoc.size();i++){
270 oleItems.add(existingItemsFromDoc.get(i));
271 }
272 instanceEditorForm.setHdnIndex(oleItems.size());
273 int hdnIndex = instanceEditorForm.getHdnIndex();
274 instanceEditorForm.setSelectedItem(instanceEditorForm.getInstance().getItems().getItem().get(hdnIndex-1));
275 return getUIFModelAndView(instanceEditorForm, OLEConstants.ITEM_PAGE);
276 }
277
278
279
280
281
282
283
284
285
286 protected ModelAndView callSuper(BindingResult result, HttpServletRequest request, HttpServletResponse response, InstanceEditorForm instanceEditorForm) {
287 return super.updateComponent(instanceEditorForm, result, request, response);
288 }
289
290
291
292
293
294 private DocstoreHelperService getDocstoreHelperService() {
295 if (null == docstoreHelperService) {
296 return new DocstoreHelperService();
297 }
298 return docstoreHelperService;
299 }
300
301
302
303
304
305 public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
306 this.docstoreHelperService = docstoreHelperService;
307 }
308
309
310
311
312
313 private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
314 if (null == instanceEditorFormDataHandler) {
315 instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
316 }
317 return instanceEditorFormDataHandler;
318 }
319
320
321
322
323
324
325 private AdditionalAttributes getFirstAdditionalAttributes(List<Extension> extensionList) {
326 for (Extension ext : extensionList) {
327 for (Object obj : ext.getContent()) {
328 if (obj instanceof AdditionalAttributes) {
329 return (AdditionalAttributes) obj;
330 }
331 }
332 }
333 return null;
334 }
335
336
337
338
339
340 public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
341 this.discoveryHelperService = discoveryHelperService;
342 }
343
344
345
346
347
348 public DiscoveryHelperService getDiscoveryHelperService() {
349 if (null == discoveryHelperService) {
350 return new DiscoveryHelperService();
351 }
352 return discoveryHelperService;
353 }
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 @RequestMapping(params = "methodToCall=addAccessInformation")
378 public ModelAndView addAccessInformation(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
379 HttpServletRequest request, HttpServletResponse response) {
380 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
381 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
382 index++;
383 List<Uri> accessInformation = instanceEditorForm.getSelectedHolding().getUri();
384 accessInformation.add(index, new Uri());
385 return super.updateComponent(instanceEditorForm, result, request, response);
386 }
387
388 @RequestMapping(params = "methodToCall=removeAccessInformation")
389 public ModelAndView removeAccessInformation(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
390 HttpServletRequest request, HttpServletResponse response) {
391 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
392 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
393 List<Uri> accessInformation = instanceEditorForm.getSelectedHolding().getUri();
394 if (accessInformation.size() > 1)
395 accessInformation.remove(index);
396 return super.updateComponent(instanceEditorForm, result, request, response);
397 }
398
399 @RequestMapping(params = "methodToCall=addHoldingNotes")
400 public ModelAndView addHoldingNotes(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
401 HttpServletRequest request, HttpServletResponse response) {
402 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
403 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
404 index++;
405 List<Note> holdingsNote = instanceEditorForm.getSelectedHolding().getNote();
406 holdingsNote.add(index, new Note());
407 return super.updateComponent(instanceEditorForm, result, request, response);
408 }
409
410 @RequestMapping(params = "methodToCall=removeHoldingNotes")
411 public ModelAndView removeHoldingNotes(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
412 HttpServletRequest request, HttpServletResponse response) {
413 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
414 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
415 List<Note> holdingsNote = instanceEditorForm.getSelectedHolding().getNote();
416 if (holdingsNote.size() > 1)
417 holdingsNote.remove(index);
418 return super.updateComponent(instanceEditorForm, result, request, response);
419 }
420
421
422
423
424
425
426
427
428
429 @RequestMapping(params = "methodToCall=addControlField")
430 public ModelAndView addControlField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
431 HttpServletRequest request, HttpServletResponse response) {
432 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
433 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
434 index++;
435 List<MarcEditorControlField> marcEditorControlFieldList = instanceEditorForm.getSelectedSourceHolding().getControlFields();
436 marcEditorControlFieldList.add(index, new MarcEditorControlField());
437 return super.updateComponent(instanceEditorForm, result, request, response);
438 }
439
440
441
442
443
444
445
446
447
448 @RequestMapping(params = "methodToCall=removeControlField")
449 public ModelAndView removeControlField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
450 HttpServletRequest request, HttpServletResponse response) {
451 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
452 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
453 List<MarcEditorControlField> editorControlFieldList = instanceEditorForm.getSelectedSourceHolding().getControlFields();
454 if (editorControlFieldList.size() > 1)
455 editorControlFieldList.remove(index);
456 return super.updateComponent(instanceEditorForm, result, request, response);
457 }
458
459
460
461
462
463
464
465
466
467 @RequestMapping(params = "methodToCall=addDataField")
468 public ModelAndView addDataField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
469 HttpServletRequest request, HttpServletResponse response) {
470 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
471 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
472 index++;
473 List<MarcEditorDataField> editorDataFieldList = instanceEditorForm.getSelectedSourceHolding().getDataFields();
474 editorDataFieldList.add(index, new MarcEditorDataField());
475 return super.updateComponent(instanceEditorForm, result, request, response);
476 }
477
478
479
480
481
482
483
484
485
486 @RequestMapping(params = "methodToCall=removeDataField")
487 public ModelAndView removeDataField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
488 HttpServletRequest request, HttpServletResponse response) {
489 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) form;
490 int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
491 List<MarcEditorDataField> marcEditorDataFieldList = instanceEditorForm.getSelectedSourceHolding().getDataFields();
492 if (marcEditorDataFieldList.size() > 1)
493 marcEditorDataFieldList.remove(index);
494 return super.updateComponent(instanceEditorForm, result, request, response);
495 }
496
497
498 }
499