1 package org.kuali.ole.deliver.controller.checkout;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.asr.service.ASRHelperServiceImpl;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.OLEParameterConstants;
7 import org.kuali.ole.deliver.bo.ASRItem;
8 import org.kuali.ole.deliver.bo.OleLoanFastAdd;
9 import org.kuali.ole.deliver.controller.CircBaseController;
10 import org.kuali.ole.deliver.controller.FastAddItemController;
11 import org.kuali.ole.deliver.form.CircForm;
12 import org.kuali.ole.deliver.service.CircDeskLocationResolver;
13 import org.kuali.ole.deliver.service.ParameterValueResolver;
14 import org.kuali.ole.deliver.util.ErrorMessage;
15 import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler;
16 import org.kuali.ole.describe.bo.OleItemAvailableStatus;
17 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
18 import org.kuali.ole.docstore.common.client.impl.NewDocstoreClientLocator;
19 import org.kuali.ole.docstore.common.document.*;
20 import org.kuali.ole.docstore.common.document.HoldingsTree;
21 import org.kuali.ole.docstore.common.document.Item;
22 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
23 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
24 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
25 import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
26 import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
27 import org.kuali.ole.docstore.common.document.content.enums.DocType;
28 import org.kuali.ole.docstore.common.document.content.instance.*;
29 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
30 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
31 import org.kuali.ole.sys.context.SpringContext;
32 import org.kuali.ole.utility.Constants;
33 import org.kuali.rice.krad.service.BusinessObjectService;
34 import org.kuali.rice.krad.service.KRADServiceLocator;
35 import org.kuali.rice.krad.web.form.UifFormBase;
36 import org.springframework.stereotype.Controller;
37 import org.springframework.validation.BindingResult;
38 import org.springframework.web.bind.annotation.ModelAttribute;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.servlet.ModelAndView;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import java.text.DateFormat;
45 import java.text.SimpleDateFormat;
46 import java.util.*;
47
48
49
50
51 @Controller
52 @RequestMapping(value = "/circFastAddController")
53 public class CircFastAddItemController extends CircBaseController {
54
55 private static final Logger LOG = Logger.getLogger(FastAddItemController.class);
56 private DocstoreClientLocator docstoreClientLocator;
57 private InstanceEditorFormDataHandler instanceEditorFormDataHandler = null;
58 BusinessObjectService businessObjectService;
59 ASRHelperServiceImpl asrHelperService;
60 private ItemOlemlRecordProcessor itemOlemlRecordProcessor;
61 private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor;
62 private DateFormat dateFormat;
63
64 private InstanceEditorFormDataHandler getInstanceEditorFormDataHandler() {
65 if (null == instanceEditorFormDataHandler) {
66 instanceEditorFormDataHandler = new InstanceEditorFormDataHandler();
67 }
68 return instanceEditorFormDataHandler;
69 }
70 @RequestMapping(params = "methodToCall=fastAddItemDialog")
71 public ModelAndView fastAddItemDialog(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
72 HttpServletRequest request, HttpServletResponse response) {
73 CircForm circForm = (CircForm) form;
74 OleLoanFastAdd oleLoanFastAdd = new OleLoanFastAdd();
75 oleLoanFastAdd.setCallNumber("X");
76 circForm.setOleLoanFastAdd(oleLoanFastAdd);
77 String overrideParameters = "{closeBtn:false,autoSize : false}";
78 showDialogWithOverrideParameters("fastAddDialog", form, overrideParameters);
79 return getUIFModelAndView(circForm);
80 }
81
82
83
84
85
86
87
88
89
90
91 @RequestMapping(params = "methodToCall=createFastAddItem")
92 public ModelAndView createFastAddItem(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
93 HttpServletRequest request, HttpServletResponse response) {
94
95 CircForm circForm = (CircForm) form;
96 if(null != circForm.getErrorMessage()){
97 circForm.getErrorMessage().setErrorMessage("");
98 }else{
99 circForm.setErrorMessage(new ErrorMessage());
100 }
101 OleLoanFastAdd oleLoanFastAdd = circForm.getOleLoanFastAdd();
102 oleLoanFastAdd.setItemType(request.getParameter("itemType"));
103 oleLoanFastAdd.setCallNumberType(request.getParameter("callNumberType"));
104 oleLoanFastAdd.setCheckinNote(request.getParameter("checkinNote"));
105 oleLoanFastAdd.setNote(request.getParameter("note"));
106 if (validFields(oleLoanFastAdd)) {
107 Bib bib = processBibMarcRecord(oleLoanFastAdd);
108 org.kuali.ole.docstore.common.document.content.instance.Item item = generateItemRecord(oleLoanFastAdd);
109 org.kuali.ole.docstore.common.document.Item itemXml = generateItemXML(item);
110
111 OleHoldings oleHoldings = generateHoldingsRecord(oleLoanFastAdd);
112 HoldingsTree holdingsTree = processHoldingsTree(itemXml, oleHoldings);
113
114
115 BibTree bibTree = new BibTree();
116 bibTree.setBib(bib);
117 bibTree.getHoldingsTrees().add(holdingsTree);
118
119 NewDocstoreClientLocator.getInstance().getDocstoreClient(true).createBibTree(bibTree);
120
121 if (item.getLocation() != null) {
122 String location = getInstanceEditorFormDataHandler().getLocationCode(item.getLocation().getLocationLevel());
123 if (getAsrHelperService().isAnASRItem(location)) {
124 processASRItem(oleLoanFastAdd, item, oleHoldings);
125 }
126 } else if (oleHoldings.getLocation() != null) {
127 String location = getInstanceEditorFormDataHandler().getLocationCode(oleHoldings.getLocation().getLocationLevel());
128 if (getAsrHelperService().isAnASRItem(location)) {
129 processASRForHoldings(oleLoanFastAdd, item, oleHoldings);
130 }
131 }
132
133 } else {
134 circForm.getErrorMessage().setErrorMessage(OLEConstants.REQ_FIELD);
135 showDialog("generalMessageAndResetUIDialog", circForm, request, response);
136 }
137
138
139 circForm.setItemBarcode(oleLoanFastAdd.getBarcode());
140
141
142 return getUIFModelAndView(circForm);
143 }
144
145 private boolean validFields(OleLoanFastAdd oleLoanFastAdd) {
146 return !oleLoanFastAdd.getBarcode().isEmpty() &&
147 !oleLoanFastAdd.getCallNumberType().isEmpty() &&
148 !oleLoanFastAdd.getCallNumberType().equalsIgnoreCase("#")
149 && !oleLoanFastAdd.getCallNumber().isEmpty() &&
150 !oleLoanFastAdd.getCheckinNote().isEmpty() &&
151 !oleLoanFastAdd.getLocationName().isEmpty() &&
152 !oleLoanFastAdd.getItemType().isEmpty() &&
153 !oleLoanFastAdd.getTitle().isEmpty();
154 }
155
156 private void processASRForHoldings(OleLoanFastAdd oleLoanFastAdd, org.kuali.ole.docstore.common.document.content.instance.Item item, OleHoldings oleHoldings) {
157 String callNumber;
158 String prefix;
159 Map<String, String> asrItemMap = new HashMap<String, String>();
160 asrItemMap.put("itemBarcode", item.getAccessInformation().getBarcode());
161 List<ASRItem> asrItems = (List<ASRItem>) businessObjectService.findMatching(ASRItem.class, asrItemMap);
162 if (asrItems.size() == 0) {
163 ASRItem asrItem = new ASRItem();
164 if (item.getAccessInformation() != null && item.getAccessInformation().getBarcode() != null) {
165 asrItem.setItemBarcode(item.getAccessInformation().getBarcode());
166 }
167 if (oleLoanFastAdd.getTitle() != null) {
168 asrItem.setTitle((oleLoanFastAdd.getTitle().length() > 37) ? oleLoanFastAdd.getTitle().substring(0, 36) : oleLoanFastAdd.getTitle());
169 }
170 if (oleLoanFastAdd.getAuthor() != null) {
171 asrItem.setAuthor((oleLoanFastAdd.getAuthor().length() > 37) ? oleLoanFastAdd.getAuthor().substring(0, 36) : oleLoanFastAdd.getAuthor());
172 }
173 if (item.getCallNumber() != null && item.getCallNumber().getNumber() != null) {
174 if (item.getCallNumber().getNumber() != null && !item.getCallNumber().getNumber().isEmpty()) {
175 callNumber = (item.getCallNumber().getNumber().length() > 37) ? item.getCallNumber().getNumber().substring(0, 36) : item.getCallNumber().getNumber();
176 prefix = item.getCallNumber().getPrefix() != null && !item.getCallNumber().getPrefix().isEmpty() ? item.getCallNumber().getPrefix() : "";
177 asrItem.setCallNumber(prefix + " " + callNumber);
178 } else {
179 callNumber = (oleHoldings.getCallNumber().getNumber().length() > 37) ? oleHoldings.getCallNumber().getNumber().substring(0, 36) : oleHoldings.getCallNumber().getNumber();
180 prefix = oleHoldings.getCallNumber().getPrefix() != null && !oleHoldings.getCallNumber().getPrefix().isEmpty() ? oleHoldings.getCallNumber().getPrefix() : "";
181 asrItem.setCallNumber(prefix + " " + callNumber);
182 }
183 }
184 getBusinessObjectService().save(asrItem);
185 }
186 }
187
188 private void processASRItem(OleLoanFastAdd oleLoanFastAdd, org.kuali.ole.docstore.common.document.content.instance.Item item, OleHoldings oleHoldings) {
189 String callNumber;
190 String prefix;
191 Map<String, String> asrItemMap = new HashMap<String, String>();
192 asrItemMap.put("itemBarcode", item.getAccessInformation().getBarcode());
193 List<ASRItem> asrItems = (List<ASRItem>) getBusinessObjectService().findMatching(ASRItem.class, asrItemMap);
194 if (asrItems.size() == 0) {
195 ASRItem asrItem = new ASRItem();
196 if (item.getAccessInformation() != null && item.getAccessInformation().getBarcode() != null) {
197 asrItem.setItemBarcode(item.getAccessInformation().getBarcode());
198 }
199 if (oleLoanFastAdd.getTitle() != null) {
200 asrItem.setTitle((oleLoanFastAdd.getTitle().length() > 37) ? oleLoanFastAdd.getTitle().substring(0, 36) : oleLoanFastAdd.getTitle());
201 }
202 if (oleLoanFastAdd.getAuthor() != null) {
203 asrItem.setAuthor((oleLoanFastAdd.getAuthor().length() > 37) ? oleLoanFastAdd.getAuthor().substring(0, 36) : oleLoanFastAdd.getAuthor());
204 }
205 if (item.getCallNumber() != null && item.getCallNumber().getNumber() != null) {
206 if (item.getCallNumber().getNumber() != null && !item.getCallNumber().getNumber().isEmpty()) {
207 callNumber = (item.getCallNumber().getNumber().length() > 37) ? item.getCallNumber().getNumber().substring(0, 36) : item.getCallNumber().getNumber();
208 prefix = item.getCallNumber().getPrefix() != null && !item.getCallNumber().getPrefix().isEmpty() ? item.getCallNumber().getPrefix() : "";
209 asrItem.setCallNumber(prefix + " " + callNumber);
210 } else {
211 callNumber = (oleHoldings.getCallNumber().getNumber().length() > 37) ? oleHoldings.getCallNumber().getNumber().substring(0, 36) : oleHoldings.getCallNumber().getNumber();
212 prefix = oleHoldings.getCallNumber().getPrefix() != null && !oleHoldings.getCallNumber().getPrefix().isEmpty() ? oleHoldings.getCallNumber().getPrefix() : "";
213 asrItem.setCallNumber(prefix + " " + callNumber);
214
215 }
216 }
217 getBusinessObjectService().save(asrItem);
218 }
219 }
220
221 private HoldingsTree processHoldingsTree(Item itemXml, OleHoldings oleHoldings) {
222 oleHoldings.setHoldingsType("print");
223 Holdings holdings = new PHoldings();
224 holdings.setContent(getHoldingOlemlRecordProcessor().toXML(oleHoldings));
225 holdings.setCreatedOn(String.valueOf(getDateFormat().format(new Date())));
226 holdings.setLastUpdated(String.valueOf(getDateFormat().format(new Date())));
227 holdings.setPublic(false);
228 holdings.setFastAdd(true);
229 holdings.setStatus("n");
230 HoldingsTree holdingsTree = new HoldingsTree();
231 holdingsTree.setHoldings(holdings);
232 holdingsTree.getItems().add(itemXml);
233 return holdingsTree;
234 }
235
236 private Item generateItemXML(org.kuali.ole.docstore.common.document.content.instance.Item item) {
237 Item itemXml = new ItemOleml();
238 itemXml.setContent(getItemOlemlRecordProcessor().toXML(item));
239 itemXml.setCreatedOn(String.valueOf(getDateFormat().format(new Date())));
240 itemXml.setLastUpdated(String.valueOf(getDateFormat().format(new Date())));
241 itemXml.setPublic(false);
242 itemXml.setFastAdd(true);
243 return itemXml;
244 }
245
246 private Bib processBibMarcRecord(OleLoanFastAdd oleLoanFastAdd) {
247 BibMarcRecord bibMarcRecord = genereateBibRecord(oleLoanFastAdd.getTitle(), oleLoanFastAdd.getAuthor());
248 List<BibMarcRecord> bibMarcRecordList = new ArrayList<>();
249 bibMarcRecordList.add(bibMarcRecord);
250 BibMarcRecords bibMarcRecords = new BibMarcRecords();
251 bibMarcRecords.setRecords(bibMarcRecordList);
252 BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
253 Bib bib = new BibMarc();
254 bib.setCategory(DocCategory.WORK.getCode());
255 bib.setType(DocType.BIB.getCode());
256 bib.setFormat(DocFormat.MARC.getCode());
257 bib.setContent(bibMarcRecordProcessor.toXml(bibMarcRecords));
258 return bib;
259 }
260
261 private BibMarcRecord genereateBibRecord(String title, String author) {
262
263 BibMarcRecord bibMarcRecord = new BibMarcRecord();
264 List<org.kuali.ole.docstore.common.document.content.bib.marc.DataField> dataFieldList = new ArrayList<org.kuali.ole.docstore.common.document.content.bib.marc.DataField>();
265 org.kuali.ole.docstore.common.document.content.bib.marc.DataField titleDataField = new org.kuali.ole.docstore.common.document.content.bib.marc.DataField();
266 titleDataField.setTag(OLEConstants.MARC_EDITOR_TITLE_245);
267 List<org.kuali.ole.docstore.common.document.content.bib.marc.SubField> subFields = new ArrayList<org.kuali.ole.docstore.common.document.content.bib.marc.SubField>();
268 org.kuali.ole.docstore.common.document.content.bib.marc.SubField subField = new org.kuali.ole.docstore.common.document.content.bib.marc.SubField();
269 subField.setCode(OLEConstants.A);
270 subField.setValue(title);
271 subFields.add(subField);
272 titleDataField.setSubFields(subFields);
273 dataFieldList.add(titleDataField);
274
275 if (author != null && !author.trim().isEmpty()) {
276 org.kuali.ole.docstore.common.document.content.bib.marc.DataField authorDataField = new org.kuali.ole.docstore.common.document.content.bib.marc.DataField();
277 authorDataField.setTag(OLEConstants.MARC_EDITOR_TITLE_100);
278 subFields = new ArrayList<org.kuali.ole.docstore.common.document.content.bib.marc.SubField>();
279 subField = new org.kuali.ole.docstore.common.document.content.bib.marc.SubField();
280 subField.setCode(OLEConstants.A);
281 subField.setValue(author);
282 subFields.add(subField);
283 authorDataField.setSubFields(subFields);
284 dataFieldList.add(authorDataField);
285 }
286
287 bibMarcRecord.setDataFields(dataFieldList);
288 return bibMarcRecord;
289
290 }
291
292 private HoldingOlemlRecordProcessor getHoldingOlemlRecordProcessor() {
293 if (null == holdingOlemlRecordProcessor) {
294 holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
295 }
296 return holdingOlemlRecordProcessor;
297 }
298
299 private OleHoldings generateHoldingsRecord(OleLoanFastAdd oleLoanFastAdd) {
300 OleHoldings oleHolding = new OleHoldings();
301 LocationLevel locationLevel = new LocationLevel();
302 locationLevel = getCircDeskLocationResolver().createLocationLevel(oleLoanFastAdd.getLocationName(), locationLevel);
303 Location location = new Location();
304 location.setPrimary(OLEConstants.TRUE);
305 location.setStatus(OLEConstants.PERMANENT);
306 location.setLocationLevel(locationLevel);
307
308 oleHolding.setLocation(location);
309 oleHolding.setCallNumber(getCallNumber(oleLoanFastAdd));
310 oleHolding.setPrimary(OLEConstants.TRUE);
311 return oleHolding;
312
313 }
314
315 private org.kuali.ole.docstore.common.document.content.instance.Item generateItemRecord(OleLoanFastAdd
316 oleLoanFastAdd) {
317 LOG.debug("Inside the getItemRecord method");
318 org.kuali.ole.docstore.common.document.content.instance.Item item = new org.kuali.ole.docstore.common.document.content.instance.Item();
319 ItemType itemType = new ItemType();
320 itemType.setCodeValue(oleLoanFastAdd.getItemType());
321 item.setItemType(itemType);
322 item.setCallNumber(getCallNumber(oleLoanFastAdd));
323 item.setCopyNumberLabel(oleLoanFastAdd.getCopyNumber());
324 item.setEnumeration(oleLoanFastAdd.getEnumeration());
325 item.setCheckinNote(oleLoanFastAdd.getCheckinNote());
326 item.setFastAddFlag(true);
327 item.setNumberOfPieces(oleLoanFastAdd.getNumberOfPieces());
328 List<Note> notes = new ArrayList<Note>();
329 Note note = new Note();
330 note.setValue(oleLoanFastAdd.getNote());
331 notes.add(note);
332 item.setNote(notes);
333 AccessInformation accessInformation = new AccessInformation();
334 accessInformation.setBarcode(oleLoanFastAdd.getBarcode());
335 item.setAccessInformation(accessInformation);
336 OleItemAvailableStatus itemAvailableStatus = validateAndGetItemStatus(getParameter(OLEParameterConstants.FAST_ADD_ITEM_DEFAULT_STATUS));
337 ItemStatus itemStatus = new ItemStatus();
338 itemStatus.setCodeValue(itemAvailableStatus != null ? itemAvailableStatus.getItemAvailableStatusCode() : null);
339 itemStatus.setFullValue(itemAvailableStatus != null ? itemAvailableStatus.getItemAvailableStatusCode() : null);
340 item.setItemStatus(itemStatus);
341 item.setCopyNumber(oleLoanFastAdd.getCopyNumber());
342 return item;
343
344 }
345
346 private String getParameter(String fastAddItemDefaultStatus) {
347 String parameter = ParameterValueResolver.getInstance().getParameter(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants
348 .DLVR_CMPNT, fastAddItemDefaultStatus);
349 return parameter;
350 }
351
352 public CallNumber getCallNumber(OleLoanFastAdd oleLoanFastAdd) {
353 CallNumber callNumber = new CallNumber();
354 callNumber.setNumber(oleLoanFastAdd.getCallNumber());
355 callNumber.setPrefix(oleLoanFastAdd.getCallNumberPrefix());
356 ShelvingScheme shelvingScheme = new ShelvingScheme();
357 shelvingScheme.setCodeValue(oleLoanFastAdd.getCallNumberType());
358 callNumber.setShelvingScheme(shelvingScheme);
359 return callNumber;
360 }
361
362 public OleItemAvailableStatus validateAndGetItemStatus(String itemStatusCode) {
363 Map criteriaMap = new HashMap();
364 criteriaMap.put(OLEConstants.ITEM_STATUS_CODE, itemStatusCode);
365 OleItemAvailableStatus itemAvailableStatus = getBusinessObjectService().findByPrimaryKey(OleItemAvailableStatus.class, criteriaMap);
366 return itemAvailableStatus;
367 }
368
369 public BusinessObjectService getBusinessObjectService() {
370 if (null == businessObjectService) {
371 businessObjectService = KRADServiceLocator.getBusinessObjectService();
372 }
373 return businessObjectService;
374 }
375
376 public CircDeskLocationResolver getCircDeskLocationResolver() {
377 return new CircDeskLocationResolver();
378 }
379
380 public ItemOlemlRecordProcessor getItemOlemlRecordProcessor() {
381 if (itemOlemlRecordProcessor == null) {
382 itemOlemlRecordProcessor = SpringContext.getBean(ItemOlemlRecordProcessor.class);
383 }
384 return itemOlemlRecordProcessor;
385 }
386
387 public DateFormat getDateFormat() {
388 if (null == dateFormat) {
389 dateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
390 }
391 return dateFormat;
392 }
393
394 public ASRHelperServiceImpl getAsrHelperService() {
395 if (null == asrHelperService) {
396 return new ASRHelperServiceImpl();
397 }
398 return asrHelperService;
399 }
400 }