View Javadoc

1   package org.kuali.ole.describe.controller;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.UnQualifiedDublinRecordHandler;
5   import org.kuali.ole.describe.form.EditorForm;
6   import org.kuali.ole.describe.form.WorkBibDublinForm;
7   import org.kuali.ole.describe.form.WorkBibMarcForm;
8   import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
9   import org.kuali.ole.editor.bo.DublinEditorField;
10  import org.kuali.ole.editor.bo.DublinEditorFormDataHandler;
11  import org.kuali.ole.editor.service.DocstoreHelperService;
12  import org.kuali.ole.pojo.dublin.unqualified.UnQualifiedDublinRecord;
13  import org.kuali.rice.krad.uif.UifParameters;
14  import org.kuali.rice.krad.web.form.UifFormBase;
15  import org.springframework.validation.BindingResult;
16  import org.springframework.web.bind.annotation.ModelAttribute;
17  import org.springframework.web.bind.annotation.RequestMapping;
18  import org.springframework.web.servlet.ModelAndView;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  /**
27   * Created with IntelliJ IDEA.
28   * User: pp7788
29   * Date: 12/11/12
30   * Time: 12:02 PM
31   * To change this template use File | Settings | File Templates.
32   */
33  /**
34   *  DublinEditorController is the controller class for Dublin Editor
35   */
36  
37  //@Controller
38  //@RequestMapping(value = "/dublincomponents")
39  public class WorkBibDublinEditor implements DocumentEditor {
40      /**
41       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
42       */
43  
44      private DublinEditorFormDataHandler dublinEditorFormDataHandler;
45      private DocstoreHelperService docstoreHelperService;
46  
47      private static WorkBibDublinEditor workBibDublinEditor = new WorkBibDublinEditor();
48  
49      public static WorkBibDublinEditor getInstance() {
50          return workBibDublinEditor;
51      }
52  
53      private WorkBibDublinEditor() {
54      }
55  
56      @Override
57      public EditorForm loadDocument(EditorForm editorForm) {
58          WorkBibDublinForm dublinEditorForm = new WorkBibDublinForm();
59          String docId = editorForm.getDocId();
60          if (null != docId) {
61              try {
62                  //workBibMarcForm.setExisting("true");
63                  String docStoreData = getDocstoreHelperService().getDocstoreData(docId);
64                  UnQualifiedDublinRecord unQualifiedDublinRecord = new UnQualifiedDublinRecordHandler().fromXML(docStoreData);
65                  dublinEditorForm.setExistingDublinFieldList(getDublinEditorFormDataHandler().buildDublinEditorFields(unQualifiedDublinRecord));
66                  dublinEditorForm.setDublinFieldList(new ArrayList<DublinEditorField>(Arrays.asList(new DublinEditorField())));
67              }
68              catch (Exception e) {
69                  // TODO: log the exception
70                  dublinEditorForm.setMessage("Error: Could not load Bib record for id:" + docId);
71              }
72          }
73          dublinEditorForm.setViewId("DublinEditorViewPage");
74          dublinEditorForm.setMessage("Please enter details for new Bib record.");
75          return dublinEditorForm;
76      }
77  
78  
79  
80      @Override
81      public EditorForm deleteDocument(EditorForm editorForm) {
82          return new WorkBibMarcForm();
83      }
84  
85      @Override
86      public EditorForm createNewRecord(EditorForm editorForm, Request request) {
87          return null;  //To change body of implemented methods use File | Settings | File Templates.
88      }
89  
90      @Override
91      public EditorForm editNewRecord(EditorForm editorForm, Request req) {
92          return null;  //To change body of implemented methods use File | Settings | File Templates.
93      }
94  
95      @Override
96      public String saveDocument(Request req) {
97          return null;  //To change body of implemented methods use File | Settings | File Templates.
98      }
99  
100     @Override
101     public EditorForm saveDocument(EditorForm editorForm) {
102         return null;  //To change body of implemented methods use File | Settings | File Templates.
103     }
104 
105 
106     /**
107      *  This method   will add the dublin record based on the selected Line index and updates the component.
108      * @param form
109      * @param result
110      * @param request
111      * @param response
112      * @return ModelAndView
113      */
114     @RequestMapping(params = "methodToCall=addField")
115     public ModelAndView addDublinField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
116                                        HttpServletRequest request, HttpServletResponse response) {
117         WorkBibDublinForm dublinEditorForm = (WorkBibDublinForm) form;
118         int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
119         index++;
120         List<DublinEditorField> dublinEditorFieldList = dublinEditorForm.getDublinFieldList();
121         dublinEditorFieldList.add(index, new DublinEditorField());
122         //return super.updateComponent(dublinEditorForm, result, request, response);
123         return null;
124     }
125 
126     /**
127      *   This method will remove the DublinField based on the index position and updates the component.
128      * @param form
129      * @param result
130      * @param request
131      * @param response
132      * @return  ModelAndView
133      */
134     @RequestMapping(params = "methodToCall=removeField")
135     public ModelAndView removeDublinField(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
136                                           HttpServletRequest request, HttpServletResponse response) {
137         WorkBibDublinForm dublinEditorForm = (WorkBibDublinForm) form;
138         int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
139         String selectedCollection = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
140         List<DublinEditorField> dublinEditorFieldList = dublinEditorForm.getDublinFieldList();
141         List<DublinEditorField> existingEditorFieldList;
142         if (dublinEditorFieldList.size() > 1)
143             dublinEditorFieldList.remove(index);
144         if (selectedCollection.equalsIgnoreCase("existingDublinFieldList")) {
145             existingEditorFieldList = dublinEditorForm.getExistingDublinFieldList();
146             if (existingEditorFieldList.size() > 0) {
147                 existingEditorFieldList.remove(index);
148             }
149         }
150 //        return super.updateComponent(dublinEditorForm, result, request, response);
151         return null;
152     }
153 
154     /**
155      *   This method will ingest new record to docStore if uuid is null else it will update exisiting record.
156      *   Once the responce has been received from docStore,then form has been nullified.
157      *   if response has not received from docStore then form nullified in catch block.
158      * @param form
159      * @param result
160      * @param request
161      * @param response
162      * @return  ModelAndView
163      */
164     @RequestMapping(params = "methodToCall=submit")
165     public ModelAndView submit(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
166                                HttpServletRequest request, HttpServletResponse response) {
167         String responseFromDocstore = null;
168         WorkBibDublinForm dublinEditorForm = (WorkBibDublinForm) form;
169         String uuid = dublinEditorForm.getUuid();
170         String content = getDublinEditorFormDataHandler().buildDublinRecordForDocStore(form,uuid);
171         try {
172             if (uuid != null && !uuid.trim().equals("")) {
173                 responseFromDocstore = getDocstoreHelperService().persistToDocstoreForEditor(content, uuid, OLEConstants.UNQUALIFIED_DUBLIN_FORMAT);
174             } else {
175                 responseFromDocstore = getDocstoreHelperService().persistToDocstoreForEditor(content, null, OLEConstants.UNQUALIFIED_DUBLIN_FORMAT);
176             }
177             System.out.println("responseFromDocstore---->" + responseFromDocstore);
178             dublinEditorForm.setDublinFieldList(Arrays.asList(new DublinEditorField()));
179             dublinEditorForm.setExistingDublinFieldList(new ArrayList<DublinEditorField>());
180             dublinEditorForm.setUuid("");
181             dublinEditorForm.setMessage("Record saved successfully.");
182         } catch (Exception e) {
183             e.printStackTrace();
184             dublinEditorForm.setDublinFieldList(Arrays.asList(new DublinEditorField()));
185             dublinEditorForm.setExistingDublinFieldList(new ArrayList<DublinEditorField>());
186             dublinEditorForm.setMessage("Failed to save record.");
187         }
188         //return getUIFModelAndView(dublinEditorForm, "DublinEditorViewPage");
189         return null;
190     }
191 
192     /**
193      * This method will fetch the DublinEditorForm records.
194      * @param form
195      * @param result
196      * @param request
197      * @param response
198      * @return  ModelAndView
199      */
200     @RequestMapping(params = "methodToCall=loadDublinRecord")
201     public ModelAndView loadDublinRecord(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
202                                          HttpServletRequest request, HttpServletResponse response) {
203         WorkBibDublinForm dublinEditorForm = (WorkBibDublinForm) form;
204         String uuid = dublinEditorForm.getUuid();
205         try {
206             String docStoreData = getDocstoreHelperService().getDocstoreData(uuid);
207             UnQualifiedDublinRecord unQualifiedDublinRecord = new UnQualifiedDublinRecordHandler().fromXML(docStoreData);
208             dublinEditorForm.setExistingDublinFieldList(getDublinEditorFormDataHandler().buildDublinEditorFields(unQualifiedDublinRecord));
209             dublinEditorForm.setDublinFieldList(new ArrayList<DublinEditorField>(Arrays.asList(new DublinEditorField())));
210             //dublinEditorForm.setUuid("");
211         } catch (Exception e) {
212             e.printStackTrace();
213         }
214         //return getUIFModelAndView(dublinEditorForm, "DublinEditorViewPage");
215         return null;
216     }
217 
218     /**
219      *   Gets the DublinEditorFormDataHandler attribute.
220      * @return  Returns DublinEditorFormDataHandler.
221      */
222     private DublinEditorFormDataHandler getDublinEditorFormDataHandler() {
223         if (null == dublinEditorFormDataHandler) {
224             dublinEditorFormDataHandler = new DublinEditorFormDataHandler();
225         }
226         return dublinEditorFormDataHandler;
227     }
228 
229     /**
230      *  Gets the docstoreHelperService attribute.
231      * @return  Returns docstoreHelperService.
232      */
233     private DocstoreHelperService getDocstoreHelperService() {
234         if (null == docstoreHelperService) {
235             return new DocstoreHelperService();
236         }
237         return docstoreHelperService;
238     }
239 
240     /**
241      *   Sets the docstoreHelperService attribute value.
242      * @param docstoreHelperService
243      */
244     public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
245         this.docstoreHelperService = docstoreHelperService;
246     }
247 
248     /**
249      *    used for test case.
250      * @param result
251      * @param request
252      * @param response
253      * @param dublinEditorForm
254      * @return  ModelAndView
255      */
256     protected ModelAndView callSuper(BindingResult result, HttpServletRequest request, HttpServletResponse response, WorkBibDublinForm dublinEditorForm) {
257         //return super.updateComponent(dublinEditorForm, result, request, response);
258         return null;
259     }
260 
261     @Override
262       public EditorForm addORDeleteFields(EditorForm editorForm, HttpServletRequest request) {
263           return null;
264       }
265 
266     @Override
267     public EditorForm deleteVerify(EditorForm editorForm) throws Exception {
268         return null;  //To change body of implemented methods use File | Settings | File Templates.
269     }
270 
271     @Override
272     public EditorForm delete(EditorForm editorForm) throws Exception {
273         return null;  //To change body of implemented methods use File | Settings | File Templates.
274     }
275 
276 }