View Javadoc
1   package org.kuali.ole.deliver.controller;
2   
3   import org.apache.log4j.Logger;
4   import org.codehaus.plexus.util.StringUtils;
5   import org.kuali.asr.service.ASRHelperServiceImpl;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.bo.ASRItem;
8   import org.kuali.ole.deliver.bo.OleLoanFastAdd;
9   import org.kuali.ole.deliver.form.OleLoanForm;
10  import org.kuali.ole.deliver.processor.LoanProcessor;
11  import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler;
12  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
13  import org.kuali.ole.docstore.common.client.DocstoreClientLocatorService;
14  import org.kuali.ole.docstore.common.client.impl.DocstoreClientLocatorServiceImpl;
15  import org.kuali.ole.docstore.common.document.*;
16  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
17  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
18  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
19  import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
20  import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
21  import org.kuali.ole.docstore.common.document.content.enums.DocType;
22  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
23  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
24  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
25  import org.kuali.ole.docstore.common.exception.DocstoreException;
26  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
27  import org.kuali.ole.docstore.common.document.content.instance.Item;
28  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
29  import org.kuali.ole.pojo.bib.BibliographicRecord;
30  import org.kuali.ole.sys.context.SpringContext;
31  import org.kuali.rice.krad.service.BusinessObjectService;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.KRADConstants;
35  import org.kuali.rice.krad.web.controller.UifControllerBase;
36  import org.kuali.rice.krad.web.form.UifFormBase;
37  import org.springframework.stereotype.Controller;
38  import org.springframework.validation.BindingResult;
39  import org.springframework.web.bind.annotation.ModelAttribute;
40  import org.springframework.web.bind.annotation.RequestMapping;
41  import org.springframework.web.servlet.ModelAndView;
42  
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  import java.lang.reflect.Method;
46  import java.text.DateFormat;
47  import java.text.SimpleDateFormat;
48  import java.util.*;
49  
50  /**
51   * Created with IntelliJ IDEA.
52   * User: palanivel
53   * Date: 3/15/13
54   * Time: 11:08 AM
55   * To change this template use File | Settings | File Templates.
56   */
57  @Controller
58  @RequestMapping(value = "/fastAddController")
59  public class FastAddItemController extends UifControllerBase {
60      private static final Logger LOG = Logger.getLogger(FastAddItemController.class);
61      private DocstoreClientLocator docstoreClientLocator;
62      private InstanceEditorFormDataHandler instanceEditorFormDataHandler = null;
63      BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
64      ASRHelperServiceImpl asrHelperService = new ASRHelperServiceImpl();
65  
66      private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
67          if (null == instanceEditorFormDataHandler) {
68              instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
69          }
70          return instanceEditorFormDataHandler;
71      }
72  
73      public DocstoreClientLocator getDocstoreClientLocator() {
74  
75          if (docstoreClientLocator == null) {
76              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
77  
78          }
79          return docstoreClientLocator;
80      }
81  
82      @Override
83      protected OleLoanForm createInitialForm(HttpServletRequest request) {
84          return new OleLoanForm();
85      }
86  
87      @Override
88      @RequestMapping(params = "methodToCall=start")
89      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
90                                HttpServletRequest request, HttpServletResponse response) {
91          OleLoanForm oleLoanForm = (OleLoanForm) form;
92          OleLoanFastAdd oleLoanFastAdd = new OleLoanFastAdd();
93          oleLoanFastAdd.setCallNumberType(OLEConstants.DEFAULT_CALL_NUMBER_TYPE);
94          oleLoanFastAdd.setCallNumber(OLEConstants.DEFAULT_CALL_NUMBER);
95          oleLoanForm.setOleLoanFastAdd(oleLoanFastAdd);
96          return getUIFModelAndView(oleLoanForm, "FastAddItemViewPage");
97      }
98  
99      /**
100      * Create a Bib and Instance record in docstore.
101      *
102      * @param form
103      * @param result
104      * @param request
105      * @param response
106      * @return
107      */
108     @RequestMapping(params = "methodToCall=createFastAddItem")
109     public ModelAndView createFastAddItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
110                                           HttpServletRequest request, HttpServletResponse response) {
111 
112         LOG.debug("Inside the create fast add item method");
113         OleLoanForm oleLoanForm = (OleLoanForm) form;
114         oleLoanForm.setItemFlag("false");
115         String callNumber;
116         String prefix;
117         String maxSessionTime = oleLoanForm.getMaxTimeForCheckOutConstant();
118         if (LOG.isInfoEnabled()){
119             LOG.info("session timeout:" + maxSessionTime);
120         }
121         if (maxSessionTime != null && !maxSessionTime.equalsIgnoreCase(""))
122             oleLoanForm.setMaxSessionTime(Integer.parseInt(maxSessionTime));
123         oleLoanForm.setInformation("");
124         oleLoanForm.setReturnInformation("");
125         LoanProcessor loanProcessor = new LoanProcessor();
126         if (!oleLoanForm.getOleLoanFastAdd().getBarcode().isEmpty() && !oleLoanForm.getOleLoanFastAdd().getCallNumberType().isEmpty() && !oleLoanForm.getOleLoanFastAdd().getCallNumberType().equalsIgnoreCase("#") && !oleLoanForm.getOleLoanFastAdd().getCallNumber().isEmpty() && !oleLoanForm.getOleLoanFastAdd().getCheckinNote().isEmpty() && !oleLoanForm.getOleLoanFastAdd().getLocationName().isEmpty() && !oleLoanForm.getOleLoanFastAdd().getItemType().isEmpty()&&!oleLoanForm.getOleLoanFastAdd().getTitle().isEmpty()) {
127             DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
128             Item item = loanProcessor.getItemRecord(oleLoanForm.getOleLoanFastAdd());
129             OleHoldings oleHoldings = loanProcessor.getHoldingRecord(oleLoanForm.getOleLoanFastAdd());
130             oleHoldings.setHoldingsType("print");
131             HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
132             holdingOlemlRecordProcessor.toXML(oleHoldings);
133             BibMarcRecord bibMarcRecord = loanProcessor.getBibMarcRecord(oleLoanForm.getOleLoanFastAdd().getTitle(), oleLoanForm.getOleLoanFastAdd().getAuthor());
134             List<BibMarcRecord> bibMarcRecordList = new ArrayList<>();
135             bibMarcRecordList.add(bibMarcRecord);
136             BibMarcRecords bibMarcRecords = new BibMarcRecords();
137             bibMarcRecords.setRecords(bibMarcRecordList);
138             BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
139             Bib bib = new BibMarc();
140             bib.setCategory(DocCategory.WORK.getCode());
141             bib.setType(DocType.BIB.getCode());
142             bib.setFormat(DocFormat.MARC.getCode());
143             bib.setContent(bibMarcRecordProcessor.toXml(bibMarcRecords));
144             ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
145             org.kuali.ole.docstore.common.document.Item itemXml = new ItemOleml();
146             itemXml.setContent(itemOlemlRecordProcessor.toXML(item));
147             itemXml.setCreatedOn(String.valueOf(dateFormat.format(new Date())));
148             itemXml.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
149             itemXml.setPublic(false);
150             itemXml.setFastAdd(true);
151             //itemXml.setHarvestable("true");
152             Holdings holdings = new PHoldings();
153             holdings.setContent(holdingOlemlRecordProcessor.toXML(oleHoldings));
154             holdings.setCreatedOn(String.valueOf(dateFormat.format(new Date())));
155             holdings.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
156             holdings.setPublic(false);
157             holdings.setFastAdd(true);
158             //holdings.setHarvestable("true");
159             holdings.setStatus("n"); // new Record
160             HoldingsTree holdingsTree = new HoldingsTree();
161             holdingsTree.setHoldings(holdings);
162             holdingsTree.getItems().add(itemXml);
163             BibTree bibTree = new BibTree();
164             bibTree.setBib(bib);
165             bibTree.getHoldingsTrees().add(holdingsTree);
166             try {
167                 getDocstoreClientLocator().getDocstoreClient().createBibTree(bibTree);
168                 if (item.getLocation() != null) {
169                     String location = instanceEditorFormDataHandler.getLocationCode(item.getLocation().getLocationLevel());
170                     if (asrHelperService.isAnASRItem(location)) {
171 
172                         Map<String, String> asrItemMap = new HashMap<String, String>();
173                         asrItemMap.put("itemBarcode", item.getAccessInformation().getBarcode());
174                         List<ASRItem> asrItems = (List<ASRItem>) businessObjectService.findMatching(ASRItem.class, asrItemMap);
175                         if (asrItems.size() == 0) {
176                             ASRItem asrItem = new ASRItem();
177                             if (item.getAccessInformation() != null && item.getAccessInformation().getBarcode() != null) {
178                                 asrItem.setItemBarcode(item.getAccessInformation().getBarcode());
179                             }
180                             if (oleLoanForm.getOleLoanFastAdd().getTitle() != null) {
181                                 asrItem.setTitle((oleLoanForm.getOleLoanFastAdd().getTitle().length() > 37) ? oleLoanForm.getOleLoanFastAdd().getTitle().substring(0, 36) : oleLoanForm.getOleLoanFastAdd().getTitle());
182                             }
183                             if (oleLoanForm.getOleLoanFastAdd().getAuthor() != null) {
184                                 asrItem.setAuthor((oleLoanForm.getOleLoanFastAdd().getAuthor().length() > 37) ? oleLoanForm.getOleLoanFastAdd().getAuthor().substring(0, 36) : oleLoanForm.getOleLoanFastAdd().getAuthor());
185                             }
186                             if (item.getCallNumber() != null && item.getCallNumber().getNumber() != null) {
187                                 if (item.getCallNumber().getNumber() != null && !item.getCallNumber().getNumber().isEmpty()) {
188                                     callNumber = (item.getCallNumber().getNumber().length() > 37) ? item.getCallNumber().getNumber().substring(0, 36) : item.getCallNumber().getNumber();
189                                     prefix=item.getCallNumber().getPrefix() !=null&&!item.getCallNumber().getPrefix() .isEmpty()?item.getCallNumber().getPrefix() :"";
190                                     asrItem.setCallNumber(prefix+ " " + callNumber);
191                                 } else {
192                                     callNumber = (oleHoldings.getCallNumber().getNumber().length() > 37) ? oleHoldings.getCallNumber().getNumber().substring(0, 36) : oleHoldings.getCallNumber().getNumber();
193                                     prefix=oleHoldings.getCallNumber().getPrefix()!=null&&!oleHoldings.getCallNumber().getPrefix().isEmpty()?oleHoldings.getCallNumber().getPrefix():"";
194                                     asrItem.setCallNumber(prefix+" " + callNumber);
195 
196                                 }
197                             }
198                             businessObjectService.save(asrItem);
199                         }
200                     }
201                 } else if (oleHoldings.getLocation() != null) {
202                     String location = getInstanceEditorFormDataHandler().getLocationCode(oleHoldings.getLocation().getLocationLevel());
203                     if (asrHelperService.isAnASRItem(location)) {
204                         Map<String, String> asrItemMap = new HashMap<String, String>();
205                         asrItemMap.put("itemBarcode", item.getAccessInformation().getBarcode());
206                         List<ASRItem> asrItems = (List<ASRItem>) businessObjectService.findMatching(ASRItem.class, asrItemMap);
207                         if (asrItems.size() == 0) {
208                             ASRItem asrItem = new ASRItem();
209                             if (item.getAccessInformation() != null && item.getAccessInformation().getBarcode() != null) {
210                                 asrItem.setItemBarcode(item.getAccessInformation().getBarcode());
211                             }
212                             if (oleLoanForm.getOleLoanFastAdd().getTitle() != null) {
213                                 asrItem.setTitle((oleLoanForm.getOleLoanFastAdd().getTitle().length() > 37) ? oleLoanForm.getOleLoanFastAdd().getTitle().substring(0, 36) : oleLoanForm.getOleLoanFastAdd().getTitle());
214                             }
215                             if (oleLoanForm.getOleLoanFastAdd().getAuthor() != null) {
216                                 asrItem.setAuthor((oleLoanForm.getOleLoanFastAdd().getAuthor().length() > 37) ? oleLoanForm.getOleLoanFastAdd().getAuthor().substring(0, 36) : oleLoanForm.getOleLoanFastAdd().getAuthor());
217                             }
218                             if (item.getCallNumber() != null && item.getCallNumber().getNumber() != null) {
219                                 if (item.getCallNumber().getNumber() != null && !item.getCallNumber().getNumber().isEmpty()) {
220                                     callNumber = (item.getCallNumber().getNumber().length() > 37) ? item.getCallNumber().getNumber().substring(0, 36) : item.getCallNumber().getNumber();
221                                     prefix=item.getCallNumber().getPrefix()!=null&&!item.getCallNumber().getPrefix().isEmpty()?item.getCallNumber().getPrefix():"";
222                                     asrItem.setCallNumber(prefix + " " + callNumber);
223                                 } else {
224                                     callNumber = (oleHoldings.getCallNumber().getNumber().length() > 37) ? oleHoldings.getCallNumber().getNumber().substring(0, 36) : oleHoldings.getCallNumber().getNumber();
225                                     prefix=oleHoldings.getCallNumber().getPrefix()!=null&&!oleHoldings.getCallNumber().getPrefix().isEmpty()?oleHoldings.getCallNumber().getPrefix():"";
226                                     asrItem.setCallNumber(prefix+ " " + callNumber);
227                                 }
228                             }
229                             businessObjectService.save(asrItem);
230                         }
231                     }
232                 }
233             } catch (DocstoreException e) {
234                 LOG.error("Exception while creating fast add item",e);
235                 DocstoreException docstoreException = (DocstoreException) e;
236                 if (org.apache.commons.lang3.StringUtils.isNotEmpty(docstoreException.getErrorCode())) {
237                     Map<String, String> paramsMap = docstoreException.getErrorParams();
238                     if (paramsMap != null && paramsMap.size() > 0 && paramsMap.containsKey("barcode")) {
239                         GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode(), paramsMap.get("barcode"));
240                     } else {
241                         GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, docstoreException.getErrorCode());
242                     }
243                 } else {
244                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, e.getMessage());
245                 }
246                 oleLoanForm.setItemFlag("true");
247             } catch (Exception e) {
248                 LOG.error("Exception ", e);
249                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "docstore.response", e.getMessage());
250                 oleLoanForm.setItemFlag("true");
251             }
252         } else {
253             oleLoanForm.setInformation(OLEConstants.REQ_FIELD);
254         }
255         oleLoanForm.setItem(oleLoanForm.getOleLoanFastAdd().getBarcode());
256         oleLoanForm.setFastAddItemIndicator(true);
257         LoanController.fastAddBarcode = oleLoanForm.getItem();
258         return getUIFModelAndView(oleLoanForm, "FastAddItemViewPage");
259     }
260 
261 
262 }