001package org.kuali.ole.deliver.controller; 002 003import org.apache.log4j.Logger; 004import org.codehaus.plexus.util.StringUtils; 005import org.kuali.asr.service.ASRHelperServiceImpl; 006import org.kuali.ole.OLEConstants; 007import org.kuali.ole.deliver.bo.ASRItem; 008import org.kuali.ole.deliver.bo.OleLoanFastAdd; 009import org.kuali.ole.deliver.form.OleLoanForm; 010import org.kuali.ole.deliver.processor.LoanProcessor; 011import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler; 012import org.kuali.ole.docstore.common.client.DocstoreClientLocator; 013import org.kuali.ole.docstore.common.client.DocstoreClientLocatorService; 014import org.kuali.ole.docstore.common.client.impl.DocstoreClientLocatorServiceImpl; 015import org.kuali.ole.docstore.common.document.*; 016import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord; 017import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords; 018import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor; 019import org.kuali.ole.docstore.common.document.content.enums.DocCategory; 020import org.kuali.ole.docstore.common.document.content.enums.DocFormat; 021import org.kuali.ole.docstore.common.document.content.enums.DocType; 022import org.kuali.ole.docstore.common.document.content.instance.OleHoldings; 023import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor; 024import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor; 025import org.kuali.ole.docstore.common.exception.DocstoreException; 026import org.kuali.ole.docstore.model.xmlpojo.ingest.Response; 027import org.kuali.ole.docstore.common.document.content.instance.Item; 028import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler; 029import org.kuali.ole.pojo.bib.BibliographicRecord; 030import org.kuali.ole.sys.context.SpringContext; 031import org.kuali.ole.utility.Constants; 032import org.kuali.rice.krad.service.BusinessObjectService; 033import org.kuali.rice.krad.service.KRADServiceLocator; 034import org.kuali.rice.krad.util.GlobalVariables; 035import org.kuali.rice.krad.util.KRADConstants; 036import org.kuali.rice.krad.web.controller.UifControllerBase; 037import org.kuali.rice.krad.web.form.UifFormBase; 038import org.springframework.stereotype.Controller; 039import org.springframework.validation.BindingResult; 040import org.springframework.web.bind.annotation.ModelAttribute; 041import org.springframework.web.bind.annotation.RequestMapping; 042import org.springframework.web.servlet.ModelAndView; 043 044import javax.servlet.http.HttpServletRequest; 045import javax.servlet.http.HttpServletResponse; 046import java.lang.reflect.Method; 047import java.text.DateFormat; 048import java.text.SimpleDateFormat; 049import java.util.*; 050 051/** 052 * Created with IntelliJ IDEA. 053 * User: palanivel 054 * Date: 3/15/13 055 * Time: 11:08 AM 056 * To change this template use File | Settings | File Templates. 057 */ 058@Controller 059@RequestMapping(value = "/fastAddController") 060public class FastAddItemController extends UifControllerBase { 061 private static final Logger LOG = Logger.getLogger(FastAddItemController.class); 062 private DocstoreClientLocator docstoreClientLocator; 063 private InstanceEditorFormDataHandler instanceEditorFormDataHandler = null; 064 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService(); 065 ASRHelperServiceImpl asrHelperService = new ASRHelperServiceImpl(); 066 067 private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() { 068 if (null == instanceEditorFormDataHandler) { 069 instanceEditorFormDataHandler = new InstanceEditorFormDataHandler(); 070 } 071 return instanceEditorFormDataHandler; 072 } 073 074 public DocstoreClientLocator getDocstoreClientLocator() { 075 076 if (docstoreClientLocator == null) { 077 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class); 078 079 } 080 return docstoreClientLocator; 081 } 082 083 @Override 084 protected OleLoanForm createInitialForm(HttpServletRequest request) { 085 return new OleLoanForm(); 086 } 087 088 @Override 089 @RequestMapping(params = "methodToCall=start") 090 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 091 HttpServletRequest request, HttpServletResponse response) { 092 OleLoanForm oleLoanForm = (OleLoanForm) form; 093 OleLoanFastAdd oleLoanFastAdd = new OleLoanFastAdd(); 094 oleLoanFastAdd.setCallNumberType(OLEConstants.DEFAULT_CALL_NUMBER_TYPE); 095 oleLoanFastAdd.setCallNumber(OLEConstants.DEFAULT_CALL_NUMBER); 096 oleLoanForm.setOleLoanFastAdd(oleLoanFastAdd); 097 return getUIFModelAndView(oleLoanForm, "FastAddItemViewPage"); 098 } 099 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}