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  
12  import javax.xml.bind.JAXBContext;
13  import javax.xml.bind.JAXBElement;
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         StringWriter sw = new StringWriter();
424         Holdings holdings = (Holdings) object;
425         try {
426             JAXBContext jaxbContext = JAXBContext.newInstance(Holdings.class);
427             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
428             jaxbMarshaller.marshal(holdings, sw);
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 
439         JAXBElement<Holdings> holdingsElement = null;
440         try {
441             JAXBContext jaxbContext = JAXBContext.newInstance(Holdings.class);
442             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
443             ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
444             holdingsElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), Holdings.class);
445         } catch (Exception e) {
446             e.printStackTrace();
447         }
448         return holdingsElement.getValue();
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 }