1 package org.kuali.ole.docstore.common.document;
2
3
4 import org.apache.commons.lang3.StringUtils;
5 import org.apache.log4j.Logger;
6 import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
7 import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
8 import org.kuali.ole.docstore.common.document.content.enums.DocType;
9 import org.kuali.ole.docstore.common.document.content.instance.*;
10 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
11 import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
12
13 import javax.xml.bind.Marshaller;
14 import javax.xml.bind.Unmarshaller;
15 import javax.xml.bind.annotation.*;
16 import javax.xml.transform.stream.StreamSource;
17 import java.io.ByteArrayInputStream;
18 import java.io.StringWriter;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 @XmlAccessorType(XmlAccessType.FIELD)
49 @XmlType(name = "holdings", propOrder = {
50 "copyNumber",
51 "locationName",
52 "shelvingOrder",
53 "callNumberPrefix",
54 "callNumberType",
55 "callNumber",
56 "holdingsType",
57 "bib",
58 "isBoundWithBib",
59 "bibs",
60 "isSeries"
61 })
62 @XmlRootElement
63
64 public class Holdings
65 extends DocstoreDocument implements Comparable<Holdings> {
66 private static final Logger LOG = Logger.getLogger(Holdings.class);
67
68 public static final String LOCATION_NAME = "LOCATIONNAME";
69 public static final String LOCATION_LEVEL1 = "LOCATIONLEVEL1";
70 public static final String LOCATION_LEVEL2 = "LOCATIONLEVEL2";
71 public static final String LOCATION_LEVEL3 = "LOCATIONLEVEL3";
72 public static final String LOCATION_LEVEL4 = "LOCATIONLEVEL4";
73 public static final String LOCATION_LEVEL5 = "LOCATIONLEVEL5";
74 public static final String CALL_NUMBER = "CALLNUMBER";
75 public static final String CALLNUMBER_PREFIX = "CALLNUMBERPREFIX";
76 public static final String CALLNUMBER_TYPE = "CALLNUMBERTYPE";
77 public static final String COPY_NUMBER = "COPYNUMBER";
78 public static final String BIB_TITLE = "BIBTITLE";
79 public static final String HOLDINGSIDENTIFIER = "HOLDINGSIDENTIFIER";
80 public static final String DESTINATION_FIELD_CALL_NUMBER = "Call Number";
81 public static final String DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE = "Call Number Type";
82 public static final String DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX = "Call Number Prefix";
83 public static final String DESTINATION_FIELD_LOCATION_LEVEL_1 = "Location Level1";
84 public static final String DESTINATION_FIELD_LOCATION_LEVEL_2 = "Location Level2";
85 public static final String DESTINATION_FIELD_LOCATION_LEVEL_3 = "Location Level3";
86 public static final String DESTINATION_FIELD_LOCATION_LEVEL_4 = "Location Level4";
87 public static final String DESTINATION_FIELD_LOCATION_LEVEL_5 = "Location Level5";
88 public static final String DESTINATION_FIELD_COPY_NUMBER = "Copy Number";
89 public static final String DESTINATION_FIELD_PLATFORM = "Platform";
90 protected String copyNumber;
91 protected String locationName;
92 protected String shelvingOrder;
93 protected String callNumberPrefix;
94 protected String callNumberType;
95 protected String callNumber;
96 protected String holdingsType;
97 @XmlTransient
98 protected String locationLevel1;
99 @XmlTransient
100 protected String locationLevel2;
101 @XmlTransient
102 protected String locationLevel3;
103 @XmlTransient
104 protected String locationLevel4;
105 @XmlTransient
106 protected String locationLevel5;
107
108 @XmlTransient
109 protected String level1Location;
110 @XmlTransient
111 protected String level2Location;
112 @XmlTransient
113 protected String level3Location;
114 @XmlTransient
115 protected String level4Location;
116 @XmlTransient
117 protected String level5Location;
118 @XmlElement(name = "bibDoc")
119 protected Bib bib;
120 protected boolean isBoundWithBib;
121 @XmlElement(name = "bibDocs")
122 protected Bibs bibs;
123 protected boolean isSeries = false;
124
125 @XmlTransient
126 protected boolean dataMappingFlag;
127
128 public boolean isDataMappingFlag() {
129 return dataMappingFlag;
130 }
131
132 public void setDataMappingFlag(boolean dataMappingFlag) {
133 this.dataMappingFlag = dataMappingFlag;
134 }
135
136 public Holdings() {
137 category = DocCategory.WORK.getCode();
138 type = DocType.HOLDINGS.getCode();
139 format = DocFormat.OLEML.getCode();
140 }
141
142 public OleHoldings getContentObject() {
143 if (contentObject == null) {
144 contentObject = new OleHoldings();
145 ((OleHoldings) contentObject).setHoldingsType(getHoldingsType());
146 if (content != null) {
147 HoldingOlemlRecordProcessor recordProcessor = new HoldingOlemlRecordProcessor();
148 contentObject = recordProcessor.fromXML(content);
149 }
150 }
151
152 return (OleHoldings) contentObject;
153 }
154
155 public String getLevel1Location() {
156 return level1Location;
157 }
158
159 public void setLevel1Location(String level1Location) {
160 this.level1Location = level1Location;
161 }
162
163 public String getLevel2Location() {
164 return level2Location;
165 }
166
167 public void setLevel2Location(String level2Location) {
168 this.level2Location = level2Location;
169 }
170
171 public String getLevel3Location() {
172 return level3Location;
173 }
174
175 public void setLevel3Location(String level3Location) {
176 this.level3Location = level3Location;
177 }
178
179 public String getLevel4Location() {
180 return level4Location;
181 }
182
183 public void setLevel4Location(String level4Location) {
184 this.level4Location = level4Location;
185 }
186
187 public String getLevel5Location() {
188 return level5Location;
189 }
190
191 public void setLevel5Location(String level5Location) {
192 this.level5Location = level5Location;
193 }
194
195 public String getLocationLevel5() {
196 return locationLevel5;
197 }
198
199 public void setLocationLevel5(String locationLevel5) {
200 this.locationLevel5 = locationLevel5;
201 }
202
203 public String getLocationLevel4() {
204 return locationLevel4;
205 }
206
207 public void setLocationLevel4(String locationLevel4) {
208 this.locationLevel4 = locationLevel4;
209 }
210
211 public String getLocationLevel3() {
212 return locationLevel3;
213 }
214
215 public void setLocationLevel3(String locationLevel3) {
216 this.locationLevel3 = locationLevel3;
217 }
218
219 public String getLocationLevel2() {
220 return locationLevel2;
221 }
222
223 public void setLocationLevel2(String locationLevel2) {
224 this.locationLevel2 = locationLevel2;
225 }
226
227 public String getLocationLevel1() {
228 return locationLevel1;
229 }
230
231 public void setLocationLevel1(String locationLevel1) {
232 this.locationLevel1 = locationLevel1;
233 }
234
235
236
237
238
239
240
241 public String getCopyNumber() {
242 return copyNumber;
243 }
244
245
246
247
248
249
250
251 public void setCopyNumber(String value) {
252 this.copyNumber = value;
253 }
254
255
256
257
258
259
260
261 public String getLocationName() {
262 return locationName;
263 }
264
265
266
267
268
269
270
271 public void setLocationName(String value) {
272 this.locationName = value;
273 }
274
275
276
277
278
279
280
281 public String getShelvingOrder() {
282 return shelvingOrder;
283 }
284
285
286
287
288
289
290
291 public void setShelvingOrder(String value) {
292 this.shelvingOrder = value;
293 }
294
295
296
297
298
299
300
301 public String getCallNumberPrefix() {
302 return callNumberPrefix;
303 }
304
305
306
307
308
309
310
311 public void setCallNumberPrefix(String value) {
312 this.callNumberPrefix = value;
313 }
314
315
316
317
318
319
320
321 public String getCallNumberType() {
322 return callNumberType;
323 }
324
325
326
327
328
329
330
331 public void setCallNumberType(String value) {
332 this.callNumberType = value;
333 }
334
335
336
337
338
339
340
341 public String getCallNumber() {
342 return callNumber;
343 }
344
345
346
347
348
349
350
351 public void setCallNumber(String value) {
352 this.callNumber = value;
353 }
354
355
356
357
358
359
360
361 public String getHoldingsType() {
362 return holdingsType;
363 }
364
365
366
367
368
369
370
371 public void setHoldingsType(String value) {
372 this.holdingsType = value;
373 }
374
375
376
377
378
379
380
381 public Bib getBib() {
382 return bib;
383 }
384
385
386
387
388
389
390
391 public void setBib(Bib value) {
392 this.bib = value;
393 }
394
395 public boolean isBoundWithBib() {
396 return isBoundWithBib;
397 }
398
399 public void setBoundWithBib(boolean boundWithBib) {
400 isBoundWithBib = boundWithBib;
401 }
402
403 public Bibs getBibs() {
404 return bibs;
405 }
406
407 public void setBibs(Bibs bibs) {
408 this.bibs = bibs;
409 }
410
411 public boolean isSeries() {
412 return isSeries;
413 }
414
415 public void setSeries(boolean series) {
416 isSeries = series;
417 }
418
419 @Override
420 public String serialize(Object object) {
421 String result = null;
422 Holdings holdings = (Holdings) object;
423 try {
424 StringWriter sw = new StringWriter();
425 Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(Holdings.class);
426 synchronized (jaxbMarshaller) {
427 jaxbMarshaller.marshal(holdings, sw);
428 }
429 result = sw.toString();
430 } catch (Exception e) {
431 LOG.error("Exception ", e);
432 }
433 return result;
434 }
435
436 @Override
437 public Object deserialize(String content) {
438 Holdings holdings = new Holdings();
439 try {
440 Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(Holdings.class);
441 ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
442 synchronized (unmarshaller) {
443 holdings = unmarshaller.unmarshal(new StreamSource(input), Holdings.class).getValue();
444 }
445 } catch (Exception e) {
446 LOG.error("Exception ", e);
447 }
448 return holdings;
449 }
450
451 public Object deserializeContent(Object object) {
452 return null;
453 }
454
455 @Override
456 public Object deserializeContent(String content) {
457 HoldingOlemlRecordProcessor recordProcessor = new HoldingOlemlRecordProcessor();
458 OleHoldings oleHoldings = recordProcessor.fromXML(content);
459 return oleHoldings;
460 }
461
462 @Override
463 public String serializeContent(Object object) {
464 OleHoldings oleHoldings = (OleHoldings) object;
465 HoldingOlemlRecordProcessor recordProcessor = new HoldingOlemlRecordProcessor();
466 String content = recordProcessor.toXML(oleHoldings);
467 return content;
468 }
469
470 public void serializeContent() {
471 if (contentObject == null) {
472 return;
473 }
474
475 OleHoldings oleHoldings = (OleHoldings) contentObject;
476 Location locationPojo = buildLocationObj();
477
478 oleHoldings.setLocation(locationPojo);
479 HoldingOlemlRecordProcessor recordProcessor = new HoldingOlemlRecordProcessor();
480 content = recordProcessor.toXML(oleHoldings);
481 }
482
483 public void setField(String docField, String fieldValue) {
484
485 OleHoldings oleHoldings = getContentObject();
486 buildItemCallNumber(oleHoldings);
487
488
489 if (docField.equalsIgnoreCase(DESTINATION_FIELD_CALL_NUMBER)) {
490 oleHoldings.getCallNumber().setNumber(fieldValue);
491 String callNumberType = oleHoldings.getCallNumber().getShelvingScheme().getCodeValue();
492 if (StringUtils.isEmpty(callNumberType) || callNumberType.equals(NO_INFO_CALL_NUMBER_TYPE_CODE)) {
493 oleHoldings.getCallNumber().getShelvingScheme().setCodeValue("NONE");
494 }
495 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE)) {
496 setDataMappingFlag(true);
497 oleHoldings.getCallNumber().getShelvingScheme().setCodeValue(fieldValue);
498 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX)) {
499 oleHoldings.getCallNumber().setPrefix(fieldValue);
500 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_1)) {
501 setLocationLevel1(fieldValue);
502 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_2)) {
503 setLocationLevel2(fieldValue);
504 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_3)) {
505 setLocationLevel3(fieldValue);
506 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_4)) {
507 setLocationLevel4(fieldValue);
508 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_5)) {
509 setLocationLevel5(fieldValue);
510 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_COPY_NUMBER)) {
511 oleHoldings.setCopyNumber(fieldValue);
512 }
513 }
514
515
516 public void setDefaultField(String docField, String fieldValue) {
517
518 OleHoldings oleHoldings = getContentObject();
519 buildItemCallNumber(oleHoldings);
520
521
522 if (docField.equalsIgnoreCase(DESTINATION_FIELD_CALL_NUMBER)) {
523 if (oleHoldings.getCallNumber().getNumber() == null) {
524 oleHoldings.getCallNumber().setNumber(fieldValue);
525 String callNumberType = oleHoldings.getCallNumber().getShelvingScheme().getCodeValue();
526 if (StringUtils.isEmpty(callNumberType) || callNumberType.equals(NO_INFO_CALL_NUMBER_TYPE_CODE)) {
527 oleHoldings.getCallNumber().getShelvingScheme().setCodeValue("NONE");
528 }
529 }
530
531 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE)) {
532
533 if (oleHoldings.getCallNumber().getShelvingScheme().getCodeValue() == null || !isDataMappingFlag()) {
534 oleHoldings.getCallNumber().getShelvingScheme().setCodeValue(fieldValue);
535 }
536
537 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX)) {
538 if (oleHoldings.getCallNumber().getPrefix() == null) {
539 oleHoldings.getCallNumber().setPrefix(fieldValue);
540 }
541
542 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_1)) {
543 if (getLocationLevel1() == null) {
544 setLocationLevel1(fieldValue);
545 }
546
547 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_2)) {
548 if (getLocationLevel2() == null) {
549 setLocationLevel2(fieldValue);
550 }
551 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_3)) {
552 if (getLocationLevel3() == null) {
553 setLocationLevel3(fieldValue);
554 }
555 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_4)) {
556 if (getLocationLevel4() == null) {
557 setLocationLevel4(fieldValue);
558 }
559 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_LOCATION_LEVEL_5)) {
560 if (getLocationLevel5() == null) {
561 setLocationLevel5(fieldValue);
562 }
563 } else if (docField.equalsIgnoreCase(DESTINATION_FIELD_COPY_NUMBER)) {
564 if (oleHoldings.getCopyNumber() == null) {
565 oleHoldings.setCopyNumber(fieldValue);
566 }
567
568 }
569 }
570
571 public void buildItemCallNumber(OleHoldings oleHoldings) {
572 if (oleHoldings.getCallNumber() == null) {
573 CallNumber callNumberPojo = new CallNumber();
574 ShelvingOrder shelvingOrder1 = new ShelvingOrder();
575 ShelvingScheme shelvingScheme1 = new ShelvingScheme();
576 callNumberPojo.setShelvingScheme(shelvingScheme1);
577 callNumberPojo.setShelvingOrder(shelvingOrder1);
578 oleHoldings.setCallNumber(callNumberPojo);
579 } else {
580 if (oleHoldings.getCallNumber().getShelvingOrder() == null) {
581 ShelvingOrder shelvingOrder1 = new ShelvingOrder();
582 oleHoldings.getCallNumber().setShelvingOrder(shelvingOrder1);
583 }
584 if (oleHoldings.getCallNumber().getShelvingScheme() == null) {
585 ShelvingScheme shelvingScheme1 = new ShelvingScheme();
586 oleHoldings.getCallNumber().setShelvingScheme(shelvingScheme1);
587 }
588 }
589 }
590
591 public Location buildLocationObj() {
592
593 LocationLevel locationLevel1 = new LocationLevel();
594 locationLevel1.setName(getLocationLevel1());
595 locationLevel1.setLevel(getLevel1Location());
596 LocationLevel locationLevel2 = new LocationLevel();
597 locationLevel2.setName(getLocationLevel2());
598 locationLevel2.setLevel(getLevel2Location());
599 LocationLevel locationLevel3 = new LocationLevel();
600 locationLevel3.setName(getLocationLevel3());
601 locationLevel3.setLevel(getLevel3Location());
602 LocationLevel locationLevel4 = new LocationLevel();
603 locationLevel4.setName(getLocationLevel4());
604 locationLevel4.setLevel(getLevel4Location());
605 LocationLevel locationLevel5 = new LocationLevel();
606 locationLevel5.setName(getLocationLevel5());
607 locationLevel5.setLevel(getLevel5Location());
608
609 List<LocationLevel> locationLevels = new ArrayList<>();
610
611 if (StringUtils.isNotEmpty(locationLevel1.getName())) {
612 locationLevels.add(locationLevel1);
613 }
614 if (StringUtils.isNotEmpty(locationLevel2.getName())) {
615 locationLevels.add(locationLevel2);
616 }
617 if (StringUtils.isNotEmpty(locationLevel3.getName())) {
618 locationLevels.add(locationLevel3);
619 }
620 if (StringUtils.isNotEmpty(locationLevel4.getName())) {
621 locationLevels.add(locationLevel4);
622 }
623 if (StringUtils.isNotEmpty(locationLevel5.getName())) {
624 locationLevels.add(locationLevel5);
625 }
626
627 int locationLevelSize = locationLevels.size();
628
629 for (int i = 0; i < locationLevelSize; i++) {
630 if ((locationLevelSize - 1) != i) {
631 locationLevels.get(i).setLocationLevel(locationLevels.get(i + 1));
632 }
633 }
634
635 Location locationPojo = new Location();
636 if (locationLevels.size() > 0) {
637 locationPojo.setLocationLevel(locationLevels.get(0));
638 }
639
640 return locationPojo;
641 }
642
643
644 public String getHoldingsDataMappingValue(Map<String, String> holdingsDocFields) {
645
646 OleHoldings oleHoldings = (OleHoldings) contentObject;
647
648 StringBuilder holdingsDataValue = new StringBuilder();
649 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER) && oleHoldings.getCallNumber().getNumber() != null) {
650 holdingsDataValue.append(oleHoldings.getCallNumber().getNumber());
651 }
652
653 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER_PREFIX) && oleHoldings.getCallNumber().getPrefix() != null) {
654 holdingsDataValue.append(oleHoldings.getCallNumber().getPrefix());
655 }
656 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER_TYPE) && oleHoldings.getCallNumber().getType() != null) {
657 holdingsDataValue.append(oleHoldings.getCallNumber().getType());
658 }
659
660 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_COPY_NUMBER) && oleHoldings.getCopyNumber() != null) {
661 holdingsDataValue.append(oleHoldings.getCopyNumber());
662 }
663
664 buildLocationLevels(oleHoldings);
665 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_1) && getLocationLevel1() != null) {
666 holdingsDataValue.append(getLocationLevel1());
667 }
668 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_2) && getLocationLevel2() != null) {
669 holdingsDataValue.append(getLocationLevel2());
670 }
671 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_3) && getLocationLevel3() != null) {
672 holdingsDataValue.append(getLocationLevel3());
673 }
674 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_4) && getLocationLevel4() != null) {
675 holdingsDataValue.append(getLocationLevel4());
676 }
677 if (holdingsDocFields.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_5) && getLocationLevel5() != null) {
678 holdingsDataValue.append(getLocationLevel5());
679
680 }
681 return holdingsDataValue.toString();
682 }
683
684 private void buildLocationLevels(OleHoldings oleHoldings) {
685 if (oleHoldings != null) {
686 if (oleHoldings.getLocation() != null) {
687 LocationLevel locationLevel = oleHoldings.getLocation().getLocationLevel();
688 setLocationLevel1(getLocationLevelName(locationLevel, level1Location));
689 setLocationLevel2(getLocationLevelName(locationLevel, level2Location));
690 setLocationLevel3(getLocationLevelName(locationLevel, level3Location));
691 setLocationLevel4(getLocationLevelName(locationLevel, level4Location));
692 setLocationLevel5(getLocationLevelName(locationLevel, level5Location));
693 }
694 }
695 }
696
697
698 private String getLocationLevelName(LocationLevel locationLevel, String level) {
699 if (locationLevel == null || org.apache.commons.lang.StringUtils.isEmpty(locationLevel.getLevel())) return null;
700 if (locationLevel.getLevel().toUpperCase().startsWith(level)) return locationLevel.getName();
701 return getLocationLevelName(locationLevel.getLocationLevel(), level);
702 }
703
704 @Override
705 public int compareTo(Holdings o) {
706 return this.getSortedValue().compareTo(o.getSortedValue());
707 }
708 }