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