View Javadoc

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   * InstanceEditorController is the controller class for Instance Editor
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       *   This method creates a instance of InstanceEditorForm
42       * @param httpServletRequest
43       * @return  InstanceEditorForm
44       */
45      @Override
46      protected InstanceEditorForm createInitialForm(HttpServletRequest httpServletRequest) {
47          return new InstanceEditorForm();
48      }
49  
50      /**
51       * This method takes the initial request when click on InstanceEditor Screen.
52       * @param form
53       * @param result
54       * @param request
55       * @param response
56       * @return  ModelAndView
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       *  This method will fetch the holding/Item records based on the AdditionalAttributes.
68       * @param form
69       * @param result
70       * @param request
71       * @param response
72       * @return  ModelAndView
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              //Building Left Pane hierarchy
89              getInstanceEditorFormDataHandler().buildLeftPaneData(instanceEditorForm);
90  
91              //Building MetaData fields
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      *  This method will ingest new record to docStore if uuid is null else it will update exisiting record.
106      *  and nullify the form.
107      * @param form
108      * @param result
109      * @param request
110      * @param response
111      * @return  ModelAndView
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) {   //New Item
131                         //TODO: Change logic to get Instance UUID. As we are loading existing Instance Identifier, we are getting directly from form
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      *   This Method will update the Left Pane hierarchy details.
163      * @param form
164      * @param result
165      * @param request
166      * @param response
167      * @return  ModelAndView
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      * This method adds the existingItem records from docStore in to oleItems and it will be set in InstanceEditorForm.
199      * @param form
200      * @param result
201      * @param request
202      * @param response
203      * @return ModelAndView
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      * This method will get new itemData
226      * @param form
227      * @param result
228      * @param request
229      * @param response
230      * @return
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      *  Used for Test-case
250      * @param result
251      * @param request
252      * @param response
253      * @param instanceEditorForm
254      * @return ModelAndView
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      *  Gets the docstoreHelperService attribute
262      * @return  Returns DocstoreHelperService
263      */
264     private DocstoreHelperService getDocstoreHelperService() {
265         if (null == docstoreHelperService) {
266             return new DocstoreHelperService();
267         }
268         return docstoreHelperService;
269     }
270 
271     /**
272      *    Sets the docstoreHelperService attribute values.
273      * @param docstoreHelperService
274      */
275     public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
276         this.docstoreHelperService = docstoreHelperService;
277     }
278 
279     /**
280      *  Gets the InstanceEditorFormDataHandler attribute.
281      * @return Returns InstanceEditorFormDataHandler.
282      */
283     private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
284         if (null == instanceEditorFormDataHandler) {
285             instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
286         }
287         return instanceEditorFormDataHandler;
288     }
289 
290     /**
291      *  This method returns AdditionalAttributes instance from the list of Extension.
292      * @param extensionList
293      * @return  AdditionalAttributes
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      * Sets the discoveryHelperService attribute value.
308      * @param discoveryHelperService
309      */
310     public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
311         this.discoveryHelperService = discoveryHelperService;
312     }
313 
314     /**
315      *  Gets the discoveryHelperService attribute.
316      * @return Returns DiscoveryHelperService.
317      */
318     public DiscoveryHelperService getDiscoveryHelperService() {
319         if (null == discoveryHelperService) {
320             return new DiscoveryHelperService();
321         }
322         return discoveryHelperService;
323     }
324 }
325