View Javadoc
1   package org.kuali.ole.describe.controller;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.kuali.ole.describe.form.EditorForm;
5   import org.kuali.ole.describe.form.WorkBibMarcForm;
6   import org.kuali.ole.describe.form.WorkEInstanceOlemlForm;
7   import org.kuali.ole.describe.form.WorkInstanceOlemlForm;
8   import org.kuali.ole.docstore.common.client.DocstoreClient;
9   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
10  import org.kuali.ole.docstore.common.document.*;
11  import org.kuali.ole.docstore.common.document.content.enums.DocType;
12  import org.kuali.ole.docstore.common.document.content.instance.CallNumber;
13  import org.kuali.ole.docstore.common.document.content.instance.DonorInfo;
14  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
15  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
16  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
17  import org.kuali.ole.docstore.common.exception.DocstoreException;
18  import org.kuali.ole.OLEConstants;
19  import org.kuali.ole.docstore.engine.client.DocstoreLocalClient;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
22  import org.kuali.rice.coreservice.api.parameter.Parameter;
23  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
24  import org.kuali.rice.krad.util.GlobalVariables;
25  import org.kuali.rice.krad.util.KRADConstants;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.print.Doc;
30  import javax.servlet.http.HttpServletRequest;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  public class AbstractEditor implements DocumentEditor {
35  
36      private static final Logger LOG = LoggerFactory.getLogger(AbstractEditor.class);
37      private DocstoreClientLocator docstoreClientLocator;
38      private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
39      private ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
40      private DocstoreClient docstoreClient = null;
41  
42      public DocstoreClient getDocstoreLocalClient() {
43          if (null == docstoreClient) {
44              return new DocstoreLocalClient();
45          }
46          return docstoreClient;
47      }
48  
49      public DocstoreClientLocator getDocstoreClientLocator() {
50          if (null == docstoreClientLocator) {
51              return  SpringContext.getBean(DocstoreClientLocator.class);
52          }
53          return docstoreClientLocator;
54      }
55  
56      public AbstractEditor() {
57      }
58  
59      /**
60       * This method deletes the record from docstore based on the doc type.
61       * @param editorForm
62       * @param docId
63       * @param operation
64       * @throws Exception
65       */
66      protected void getResponseFromDocStore(EditorForm editorForm, String docId, String operation) throws Exception {
67          try {
68              if(DocType.BIB.getCode().equalsIgnoreCase(editorForm.getDocType())){
69                  getDocstoreClientLocator().getDocstoreClient().deleteBib(docId);
70              }  else if(DocType.HOLDINGS.getCode().equalsIgnoreCase(editorForm.getDocType()) || DocType.EHOLDINGS.getCode().equalsIgnoreCase(editorForm.getDocType())){
71                  getDocstoreClientLocator().getDocstoreClient().deleteHoldings(docId);
72              } else if(DocType.ITEM.getCode().equalsIgnoreCase(editorForm.getDocType())){
73                  getDocstoreClientLocator().getDocstoreClient().deleteItem(docId);
74              }
75          } catch (Exception e) {
76              throw e;
77          }
78      }
79  
80      /**
81       * This method returns error message if it has exception else deletes record.
82       * @param editorForm
83       * @return
84       */
85      protected EditorForm deleteFromDocStore(EditorForm editorForm) {
86          String docId = editorForm.getDocId();
87          try {
88              getResponseFromDocStore(editorForm, docId, OLEConstants.DELETE);
89          } catch (Exception e) {
90              DocstoreException docstoreException = (DocstoreException) e;
91              String errorCode = docstoreException.getErrorCode();
92              if (StringUtils.isNotEmpty(errorCode)) {
93                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, errorCode);
94              } else {
95                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
96              }
97              setDocTypeOfViewPage(editorForm);
98              editorForm.getDocumentForm().setCanDeleteEHoldings(Boolean.FALSE);
99              return editorForm.getDocumentForm();
100         }
101         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_INFO, OLEConstants.RECORD_DELETE_MESSAGE);
102         removeDocumentFromTree(editorForm);
103         editorForm.setShowDeleteTree(false);
104         editorForm.setHasLink(true);
105         editorForm.getDocumentForm().setViewId(OLEConstants.DELETE_VIEW_PAGE);
106         return editorForm.getDocumentForm();
107     }
108 
109     /**
110      * This methods sets doc type to return to view page
111      * @param editorForm
112      */
113     private void setDocTypeOfViewPage(EditorForm editorForm) {
114         if (null != editorForm.getDocumentForm()) {
115             if (editorForm.getDocumentForm() instanceof WorkBibMarcForm) {
116                 editorForm.setDocType(DocType.BIB.getCode());
117             } else if (editorForm.getDocumentForm() instanceof WorkInstanceOlemlForm) {
118                 editorForm.setDocType(DocType.HOLDINGS.getCode());
119             }  else if (editorForm.getDocumentForm() instanceof WorkEInstanceOlemlForm) {
120                 editorForm.setDocType(DocType.EHOLDINGS.getCode());
121             }  else {
122                 editorForm.setDocType(DocType.ITEM.getCode());
123             }
124         }
125     }
126 
127     /**
128      * This method removes deleted record from documentForm bib tree to build left pane tree.
129      * @param editorForm
130      */
131     protected void removeDocumentFromTree(EditorForm editorForm) {
132         if (DocType.BIB.getCode().equalsIgnoreCase(editorForm.getDocType())) {
133             List<BibTree> bibTreeList = editorForm.getDocumentForm().getBibTreeList();
134             if (null != bibTreeList.get(0)) {
135                 BibTree bibTree = bibTreeList.get(0);
136                 if (null != bibTree.getBib() && null != bibTree.getBib().getId()) {
137                     if (editorForm.getDocId().equalsIgnoreCase(bibTree.getBib().getId())) {
138                         bibTreeList.remove(bibTree);
139                     }
140                 }
141             }
142         } else if (DocType.HOLDINGS.getCode().equalsIgnoreCase(editorForm.getDocType()) || DocType.EHOLDINGS.getCode().equalsIgnoreCase(editorForm.getDocType())) {
143             List<BibTree> bibTreeList = editorForm.getDocumentForm().getBibTreeList();
144             boolean deleteHoldingsTree = false;
145             int bibTreeIndex = 0;
146             int holdingsTreeIndex = 0;
147             for (bibTreeIndex = 0; bibTreeIndex < bibTreeList.size(); bibTreeIndex++) {
148                 if (!deleteHoldingsTree) {
149                     BibTree bibTree = bibTreeList.get(bibTreeIndex);
150                     if (bibTree != null) {
151                         for (holdingsTreeIndex = 0; holdingsTreeIndex < bibTree.getHoldingsTrees().size(); holdingsTreeIndex++) {
152                             HoldingsTree holdingsTree = bibTree.getHoldingsTrees().get(holdingsTreeIndex);
153                             if (holdingsTree != null) {
154                                 if (null != holdingsTree.getHoldings() && editorForm.getDocId().equalsIgnoreCase(holdingsTree.getHoldings().getId())) {
155                                     deleteHoldingsTree = true;
156                                     break;
157                                 }
158                             }
159                         }
160                     }
161                 } else {
162                     break;
163                 }
164             }
165             if (deleteHoldingsTree) {
166                 editorForm.getDocumentForm().getBibTreeList().get(--bibTreeIndex).getHoldingsTrees().remove(holdingsTreeIndex);
167             }
168         } else if (DocType.ITEM.getCode().equalsIgnoreCase(editorForm.getDocType())) {
169             List<BibTree> bibTreeList = editorForm.getDocumentForm().getBibTreeList();
170             boolean deleteItem = false;
171             int bibTreeIndex = 0;
172             int holdingsTreeIndex = 0;
173             int itemIndex = 0;
174             for (bibTreeIndex = 0; bibTreeIndex < bibTreeList.size(); bibTreeIndex++) {
175                 BibTree bibTree = bibTreeList.get(bibTreeIndex);
176                 if (bibTree != null) {
177                     for (holdingsTreeIndex = 0; holdingsTreeIndex < bibTree.getHoldingsTrees().size(); holdingsTreeIndex++) {
178                         if (!deleteItem) {
179                             HoldingsTree holdingsTree = bibTree.getHoldingsTrees().get(holdingsTreeIndex);
180                             if (holdingsTree != null) {
181                                 for (itemIndex = 0; itemIndex < holdingsTree.getItems().size(); itemIndex++) {
182                                     Item item = holdingsTree.getItems().get(itemIndex);
183                                     if (null != item) {
184                                         if (editorForm.getDocId().equalsIgnoreCase(item.getId())) {
185                                             deleteItem = true;
186                                             break;
187                                         }
188                                     }
189                                 }
190                             }
191                         } else {
192                             break;
193                         }
194                     }
195                 }
196             }
197             if (deleteItem) {
198                 editorForm.getDocumentForm().getBibTreeList().get(--bibTreeIndex).getHoldingsTrees().get(--holdingsTreeIndex).getItems().remove(itemIndex);
199             }
200         }
201     }
202 
203     @Override
204     public EditorForm loadDocument(EditorForm editorForm) {
205         return null;  //To change body of implemented methods use File | Settings | File Templates.
206     }
207 
208     @Override
209     public EditorForm saveDocument(EditorForm editorForm) {
210         return null;  //To change body of implemented methods use File | Settings | File Templates.
211     }
212 
213     @Override
214     public EditorForm deleteDocument(EditorForm editorForm) {
215         return null;  //To change body of implemented methods use File | Settings | File Templates.
216     }
217 
218     @Override
219     public EditorForm createNewRecord(EditorForm editorForm, BibTree bibTree) {
220         return null;  //To change body of implemented methods use File | Settings | File Templates.
221     }
222 
223     @Override
224     public EditorForm editNewRecord(EditorForm editorForm, BibTree bibTree) {
225         return null;  //To change body of implemented methods use File | Settings | File Templates.
226     }
227 
228     @Override
229     public String saveDocument(BibTree bibTree, EditorForm editorForm) {
230         return null;  //To change body of implemented methods use File | Settings | File Templates.
231     }
232 
233     @Override
234     public EditorForm addORDeleteFields(EditorForm editorForm, HttpServletRequest request) {
235         return null;  //To change body of implemented methods use File | Settings | File Templates.
236     }
237 
238     @Override
239     public EditorForm deleteVerify(EditorForm editorForm) throws Exception {
240         return null;  //To change body of implemented methods use File | Settings | File Templates.
241     }
242 
243     @Override
244     public EditorForm delete(EditorForm editorForm) throws Exception {
245         return null;  //To change body of implemented methods use File | Settings | File Templates.
246     }
247 
248     @Override
249     public EditorForm addORRemoveExtentOfOwnership(EditorForm editorForm, HttpServletRequest request) {
250         return null;  //To change body of implemented methods use File | Settings | File Templates.
251     }
252 
253     @Override
254     public EditorForm addORRemoveAccessInformationAndHoldingsNotes(EditorForm editorForm, HttpServletRequest request) {
255         return null;  //To change body of implemented methods use File | Settings | File Templates.
256     }
257 
258     @Override
259     public EditorForm addORRemoveItemNote(EditorForm editorForm, HttpServletRequest request) {
260         return null;  //To change body of implemented methods use File | Settings | File Templates.
261     }
262 
263     @Override
264     public EditorForm addORRemoveItemDonor(EditorForm editorForm, HttpServletRequest request) {
265         return null;  //To change body of implemented methods use File | Settings | File Templates.
266     }
267 
268     @Override
269     public EditorForm showBibs(EditorForm editorForm) {
270         return null;
271     }
272 
273     @Override
274     public EditorForm copy(EditorForm editorForm) {
275         return null;
276     }
277 
278     @Override
279     public Boolean isValidUpdate(EditorForm editorForm) {
280         return true;  //To change body of implemented methods use File | Settings | File Templates.
281     }
282 
283     protected HoldingsTree getHoldingsTree(String eResourceID) {
284         HoldingsTree holdingsTree = new HoldingsTree();
285         holdingsTree.setHoldings(getHoldingsRecord(eResourceID));
286        /* if (!((StringUtils.isNotBlank(linkToOrderOption) && linkToOrderOption.equals(OLEConstants.NB_ELECTRONIC)) || StringUtils.isNotEmpty(eResourceID))) {
287             holdingsTree.getItems().add(getItemRecord());
288         }*/
289         return holdingsTree;
290     }
291 
292     protected Item getItemRecord() {
293         Item item = new ItemOleml();
294         item.setType(DocType.ITEM.getCode());
295         item.setCategory("work");
296         item.setFormat("olml");
297         if(item.getDisplayLabel() == null) {
298             item.setDisplayLabel("Item");
299         }
300         org.kuali.ole.docstore.common.document.content.instance.Item oleItem = new org.kuali.ole.docstore.common.document.content.instance.Item();
301         CallNumber callNumber = new CallNumber();
302         oleItem.setCallNumber(callNumber);
303         List donorInfos = new ArrayList<DonorInfo>();
304         DonorInfo donorInfo = new DonorInfo();
305         donorInfos.add(donorInfo);
306         oleItem.setDonorInfo(donorInfos);
307         item.setContent(itemOlemlRecordProcessor.toXML(oleItem));
308         return item;
309     }
310 
311     protected Holdings getHoldingsRecord(String eResourceID) {
312         Holdings holdings = null;
313         OleHoldings oleHoldings = null;
314         CallNumber callNumber;
315         if (StringUtils.isNotEmpty(eResourceID)) {
316             holdings = new EHoldings();
317             oleHoldings = new OleHoldings();
318             callNumber = new CallNumber();
319             oleHoldings.setCallNumber(callNumber);
320         } /*else {
321             holdings = new PHoldings();
322             oleHoldings = new OleHoldings();
323             callNumber = new CallNumber();
324             oleHoldings.setCallNumber(callNumber);
325         }*/
326         if (holdings != null && oleHoldings != null)  {
327             holdings.setContent(holdingOlemlRecordProcessor.toXML(oleHoldings));
328         }
329         return holdings;
330     }
331 
332     protected  Holdings retrieveHoldings(String holdingsId) throws Exception{
333         Holdings holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(holdingsId);
334         return holdings;
335     }
336 
337     protected  org.kuali.ole.docstore.common.document.Item retrieveItem(String itemId) throws Exception{
338         org.kuali.ole.docstore.common.document.Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(itemId);
339         return item;
340     }
341 
342     @Override
343     public EditorForm bulkUpdate(EditorForm editorForm, List<String> ids) {
344         return null;  //To change body of implemented methods use File | Settings | File Templates.
345     }
346 
347     public String getParameter(String applicationId, String namespace, String componentId, String parameterName) {
348         ParameterKey parameterKey = ParameterKey.create(applicationId, namespace, componentId, parameterName);
349         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
350 
351         return parameter!=null?parameter.getValue():null;
352     }
353 }