View Javadoc

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