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