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