View Javadoc

1   package org.kuali.ole.describe.controller;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.UnQualifiedDublinRecordHandler;
6   import org.kuali.ole.describe.bo.OleBibliographicRecordStatus;
7   import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
8   import org.kuali.ole.describe.bo.DublinEditorField;
9   import org.kuali.ole.describe.bo.DublinEditorFormDataHandler;
10  import org.kuali.ole.describe.form.DublinEditorForm;
11  import org.kuali.ole.describe.service.DocstoreHelperService;
12  import org.kuali.ole.pojo.dublin.unqualified.UnQualifiedDublinRecord;
13  import org.kuali.rice.krad.service.BusinessObjectService;
14  import org.kuali.rice.krad.service.KRADServiceLocator;
15  import org.kuali.rice.krad.uif.UifParameters;
16  import org.kuali.rice.krad.util.GlobalVariables;
17  import org.kuali.rice.krad.web.controller.UifControllerBase;
18  import org.kuali.rice.krad.web.form.UifFormBase;
19  import org.springframework.stereotype.Controller;
20  import org.springframework.validation.BindingResult;
21  import org.springframework.web.bind.annotation.ModelAttribute;
22  import org.springframework.web.bind.annotation.RequestMapping;
23  import org.springframework.web.servlet.ModelAndView;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import java.util.*;
28  
29  /**
30   * DublinEditorController is the controller class for Dublin Editor
31   */
32  
33  @Controller
34  @RequestMapping(value = "/dublincomponents")
35  public class DublinEditorController
36          extends UifControllerBase {
37      /**
38       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
39       */
40      private static final Logger LOG = Logger.getLogger(DublinEditorController.class);
41  
42      private DublinEditorFormDataHandler dublinEditorFormDataHandler;
43      private DocstoreHelperService docstoreHelperService;
44  
45      /**
46       * This method creates a instance of DublinEditorForm
47       *
48       * @param request
49       * @return DublinEditorForm
50       */
51      @Override
52      protected DublinEditorForm createInitialForm(HttpServletRequest request) {
53          return new DublinEditorForm();
54      }
55  
56      /**
57       * This method takes the initial request when click on DublinEditor Screen.
58       *
59       * @param form
60       * @param result
61       * @param request
62       * @param response
63       * @return ModelAndView
64       */
65      @Override
66      @RequestMapping(params = "methodToCall=start")
67      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
68                                HttpServletRequest request, HttpServletResponse response) {
69          DublinEditorForm dublinEditorForm = (DublinEditorForm) form;
70          return super.start(dublinEditorForm, result, request, response);
71      }
72  
73      /**
74       * This method   will add the dublin record based on the selected Line index and updates the component.
75       *
76       * @param form
77       * @param result
78       * @param request
79       * @param response
80       * @return ModelAndView
81       */
82      @RequestMapping(params = "methodToCall=addField")
83      public ModelAndView addDublinField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
84                                         HttpServletRequest request, HttpServletResponse response) {
85          DublinEditorForm dublinEditorForm = (DublinEditorForm) form;
86          int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
87          index++;
88          List<DublinEditorField> dublinEditorFieldList = dublinEditorForm.getDublinFieldList();
89          dublinEditorFieldList.add(index, new DublinEditorField());
90  //        return super.updateComponent(dublinEditorForm, result, request, response);
91          return super.getUIFModelAndView(dublinEditorForm);
92      }
93  
94      /**
95       * This method will remove the DublinField based on the index position and updates the component.
96       *
97       * @param form
98       * @param result
99       * @param request
100      * @param response
101      * @return ModelAndView
102      */
103     @RequestMapping(params = "methodToCall=removeField")
104     public ModelAndView removeDublinField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
105                                           HttpServletRequest request, HttpServletResponse response) {
106         DublinEditorForm dublinEditorForm = (DublinEditorForm) form;
107         int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
108         String selectedCollection = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
109         List<DublinEditorField> dublinEditorFieldList = dublinEditorForm.getDublinFieldList();
110         List<DublinEditorField> existingEditorFieldList;
111         if (dublinEditorFieldList.size() > 1)
112             dublinEditorFieldList.remove(index);
113         if (selectedCollection.equalsIgnoreCase("existingDublinFieldList")) {
114             existingEditorFieldList = dublinEditorForm.getExistingDublinFieldList();
115             if (existingEditorFieldList.size() > 0) {
116                 existingEditorFieldList.remove(index);
117             }
118         }
119 //        return super.updateComponent(dublinEditorForm, result, request, response);
120         return super.getUIFModelAndView(dublinEditorForm);
121     }
122 
123     /**
124      * This method will ingest new record to docStore if uuid is null else it will update exisiting record.
125      * Once the responce has been received from docStore,then form has been nullified.
126      * if response has not received from docStore then form nullified in catch block.
127      *
128      * @param form
129      * @param result
130      * @param request
131      * @param response
132      * @return ModelAndView
133      */
134     @RequestMapping(params = "methodToCall=submit")
135     public ModelAndView submit(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
136                                HttpServletRequest request, HttpServletResponse response) {
137         String responseFromDocstore = null;
138         DublinEditorForm dublinEditorForm = (DublinEditorForm) form;
139         String bibliographicRecordStatusCode = dublinEditorForm.getBibStatus();
140         String uuid = dublinEditorForm.getUuid();
141         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
142         String content = "";
143         BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService();
144         Map parentCriteria = new HashMap();
145         parentCriteria.put("bibliographicRecordStatusCode", bibliographicRecordStatusCode);
146         OleBibliographicRecordStatus bibliographicRecordStatus = boService
147                 .findBySinglePrimaryKey(OleBibliographicRecordStatus.class, parentCriteria);
148         if (bibliographicRecordStatus != null) {
149             dublinEditorForm.setCreatedBy(GlobalVariables.getUserSession().getLoggedInUserPrincipalName());
150             dublinEditorForm.setCreatedDate(bibliographicRecordStatus.getSourceDate());
151             content = getDublinEditorFormDataHandler().buildDublinRecordForDocStore(form, uuid);
152             additionalAttributes.setAttribute("dateEntered", bibliographicRecordStatus.getSourceDate().toString());
153             additionalAttributes.setAttribute("createdBy", GlobalVariables.getUserSession().getLoggedInUserPrincipalName());
154         }
155         try {
156             if (uuid != null && !uuid.trim().equals("")) {
157                 responseFromDocstore = getDocstoreHelperService()
158                         .persistToDocstoreWithAdditionalAttributesForEditor(content, additionalAttributes, uuid,
159                                 OLEConstants.UNQUALIFIED_DUBLIN_FORMAT);
160             } else {
161                 responseFromDocstore = getDocstoreHelperService()
162                         .persistToDocstoreWithAdditionalAttributesForEditor(content, additionalAttributes, null,
163                                 OLEConstants.UNQUALIFIED_DUBLIN_FORMAT);
164             }
165             LOG.info("responseFromDocstore---->" + responseFromDocstore);
166             dublinEditorForm.setDublinFieldList(Arrays.asList(new DublinEditorField()));
167             dublinEditorForm.setExistingDublinFieldList(new ArrayList<DublinEditorField>());
168             dublinEditorForm.setUuid("");
169             dublinEditorForm.setMessage("Record saved successfully.");
170         } catch (Exception e) {
171             //e.printStackTrace();
172             LOG.error("refreshLeftTree Exception:" + e);
173             dublinEditorForm.setDublinFieldList(Arrays.asList(new DublinEditorField()));
174             dublinEditorForm.setExistingDublinFieldList(new ArrayList<DublinEditorField>());
175             dublinEditorForm.setMessage("Failed to save record.");
176         }
177         return getUIFModelAndView(dublinEditorForm, "DublinEditorViewPage");
178     }
179 
180     /**
181      * This method will fetch the DublinEditorForm records.
182      *
183      * @param form
184      * @param result
185      * @param request
186      * @param response
187      * @return ModelAndView
188      */
189     @RequestMapping(params = "methodToCall=loadDublinRecord")
190     public ModelAndView loadDublinRecord(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
191                                          HttpServletRequest request, HttpServletResponse response) {
192         DublinEditorForm dublinEditorForm = (DublinEditorForm) form;
193         String uuid = dublinEditorForm.getUuid();
194         try {
195             String docStoreData = getDocstoreHelperService().getDocstoreData(uuid);
196             UnQualifiedDublinRecord unQualifiedDublinRecord = new UnQualifiedDublinRecordHandler().fromXML(docStoreData);
197             dublinEditorForm.setExistingDublinFieldList(getDublinEditorFormDataHandler().buildDublinEditorFields(unQualifiedDublinRecord));
198             dublinEditorForm.setDublinFieldList(new ArrayList<DublinEditorField>(Arrays.asList(new DublinEditorField())));
199             //dublinEditorForm.setUuid("");
200         } catch (Exception e) {
201             //e.printStackTrace();
202             LOG.error("loadDublinRecord Exception:" + e);
203         }
204         return getUIFModelAndView(dublinEditorForm, "DublinEditorViewPage");
205     }
206 
207     /**
208      * Gets the DublinEditorFormDataHandler attribute.
209      *
210      * @return Returns DublinEditorFormDataHandler.
211      */
212     private DublinEditorFormDataHandler getDublinEditorFormDataHandler() {
213         if (null == dublinEditorFormDataHandler) {
214             dublinEditorFormDataHandler = new DublinEditorFormDataHandler();
215         }
216         return dublinEditorFormDataHandler;
217     }
218 
219     /**
220      * Gets the docstoreHelperService attribute.
221      *
222      * @return Returns docstoreHelperService.
223      */
224     private DocstoreHelperService getDocstoreHelperService() {
225         if (null == docstoreHelperService) {
226             return new DocstoreHelperService();
227         }
228         return docstoreHelperService;
229     }
230 
231     /**
232      * Sets the docstoreHelperService attribute value.
233      *
234      * @param docstoreHelperService
235      */
236     public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
237         this.docstoreHelperService = docstoreHelperService;
238     }
239 
240     /**
241      * used for test case.
242      *
243      * @param result
244      * @param request
245      * @param response
246      * @param dublinEditorForm
247      * @return ModelAndView
248      */
249     protected ModelAndView callSuper(BindingResult result, HttpServletRequest request, HttpServletResponse response, DublinEditorForm dublinEditorForm) {
250 //        return super.updateComponent(dublinEditorForm, result, request, response);
251         return super.getUIFModelAndView(dublinEditorForm);
252     }
253 
254 }