View Javadoc
1   package org.kuali.ole.batch.util;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.batch.bo.OLEBatchProcessProfileMatchPoint;
7   import org.kuali.ole.describe.bo.OleLocationLevel;
8   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9   import org.kuali.ole.docstore.common.document.*;
10  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
11  import org.kuali.ole.docstore.common.document.content.bib.marc.ControlField;
12  import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
13  import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
14  import org.kuali.ole.docstore.model.enums.DocType;
15  import org.kuali.ole.sys.context.SpringContext;
16  import org.kuali.rice.krad.service.KRADServiceLocator;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  /**
23   * Created with IntelliJ IDEA.
24   * User: jayabharathreddy
25   * Date: 7/1/14
26   * Time: 2:15 PM
27   * To change this template use File | Settings | File Templates.
28   */
29  public class BatchBibImportUtil {
30  
31  
32      private static DocstoreClientLocator docstoreClientLocator;
33  
34      public static DocstoreClientLocator getDocstoreClientLocator() {
35          if (null == docstoreClientLocator) {
36              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
37          }
38          return docstoreClientLocator;
39      }
40  
41      public static String getBibDataFieldValue(BibMarcRecord bibMarcRecord, String enteredDataField) {
42          String[] dataFiledsArray = enteredDataField.split(" ");
43          String entryDataField = null;
44          String entryInds = null;
45          String entrySubField = null;
46          if (dataFiledsArray.length == 3) {
47              entryDataField = dataFiledsArray[0];
48              entryInds = dataFiledsArray[1];
49              entrySubField = dataFiledsArray[2];
50          } else if (dataFiledsArray.length == 2) {
51              entryDataField = dataFiledsArray[0];
52              entryInds = "##";
53              entrySubField = dataFiledsArray[1];
54          } else if (dataFiledsArray.length == 1) {
55              entryDataField = dataFiledsArray[0];
56              if (entryDataField.equalsIgnoreCase(OLEConstants.OLEBatchProcess.CONTROL_FIELD_001)) {
57                  for (ControlField controlField : bibMarcRecord.getControlFields()) {
58                      if (controlField.getTag().equalsIgnoreCase(OLEConstants.OLEBatchProcess.CONTROL_FIELD_001)) {
59                          return controlField.getValue();
60                      }
61                  }
62                  return null;
63              }
64          }
65          List<String> dataFields = new ArrayList<>();
66          String ind1 = "";
67          String ind2 = "";
68          String dataFieldInds = "";
69          for (DataField dataField : bibMarcRecord.getDataFields()) {
70              if (dataField.getTag().equalsIgnoreCase(entryDataField)) {
71                  ind1 = dataField.getInd1();
72                  ind2 = dataField.getInd2();
73                  if (entryInds.equals("##")) {
74                      ind1 = "#";
75                      ind2 = "#";
76                  } else if (entryInds.startsWith("#")) {
77                      ind1 = "#";
78                  } else if (entryInds.endsWith("#")) {
79                      ind2 = "#";
80                  }
81                  if (ind1.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind1) || ind1.equalsIgnoreCase("\\")) ind1 = "#";
82                  if (ind2.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind2) || ind2.equalsIgnoreCase("\\")) ind2 = "#";
83                  dataFieldInds = ind1 + ind2;
84                  if (dataFieldInds.equalsIgnoreCase(entryInds)) {
85                      if (StringUtils.isNotEmpty(entrySubField)) {
86                          StringBuffer subFieldValue = new StringBuffer("");
87                          entrySubField = entrySubField.replace("$", " ");
88                          String[] subFieldsArray = entrySubField.split(" ");
89                          for (int i = 1; i < subFieldsArray.length; i++) {
90                              String code = subFieldsArray[i];
91                              for (SubField subField : dataField.getSubFields()) {
92                                  if (subField.getCode().equalsIgnoreCase(code)) {
93                                      subFieldValue.append(subField.getValue());
94                                  }
95                              }
96                          }
97                          if (StringUtils.isNotEmpty(subFieldValue.toString())) {
98                              dataFields.add(subFieldValue.toString());
99                          }
100 
101                     }
102                 }
103             }
104         }
105         String dataFieldValue = null;
106 
107         if (dataFields != null && dataFields.size() > 0) {
108             dataFieldValue = dataFields.get(0);
109         }
110         return dataFieldValue;
111     }
112 
113 
114     public static void buildLocationLevels(List<HoldingsTree> holdingsTreeList) {
115         List<String> matchingLocationLevel = getLocationLevel();
116         for (HoldingsTree holdingsTree : holdingsTreeList) {
117             Holdings holdings = holdingsTree.getHoldings();
118             holdings.setLevel1Location(matchingLocationLevel.get(0));
119             holdings.setLevel2Location(matchingLocationLevel.get(1));
120             holdings.setLevel3Location(matchingLocationLevel.get(2));
121             holdings.setLevel4Location(matchingLocationLevel.get(3));
122             holdings.setLevel5Location(matchingLocationLevel.get(4));
123             for (Item item : holdingsTree.getItems()) {
124                 item.setLevel1Location(matchingLocationLevel.get(0));
125                 item.setLevel2Location(matchingLocationLevel.get(1));
126                 item.setLevel3Location(matchingLocationLevel.get(2));
127                 item.setLevel4Location(matchingLocationLevel.get(3));
128                 item.setLevel5Location(matchingLocationLevel.get(4));
129             }
130 
131         }
132     }
133 
134 
135     public static void buildLocationLevels(Holdings holdings) {
136         List<String> matchingLocationLevel = getLocationLevel();
137         holdings.setLevel1Location(matchingLocationLevel.get(0));
138         holdings.setLevel2Location(matchingLocationLevel.get(1));
139         holdings.setLevel3Location(matchingLocationLevel.get(2));
140         holdings.setLevel4Location(matchingLocationLevel.get(3));
141         holdings.setLevel5Location(matchingLocationLevel.get(4));
142     }
143 
144     public static void buildLocationLevels(Item item) {
145         List<String> matchingLocationLevel = getLocationLevel();
146         item.setLevel1Location(matchingLocationLevel.get(0));
147         item.setLevel2Location(matchingLocationLevel.get(1));
148         item.setLevel3Location(matchingLocationLevel.get(2));
149         item.setLevel4Location(matchingLocationLevel.get(3));
150         item.setLevel5Location(matchingLocationLevel.get(4));
151     }
152 
153 
154     private static List<String> getLocationLevel() {
155         List<String> locationLevelName = new ArrayList<>();
156         List<OleLocationLevel> locationLevel = (List<OleLocationLevel>) KRADServiceLocator.getBusinessObjectService().findAllOrderBy(OleLocationLevel.class, OLEConstants.LEVEL_ID, true);
157         for (OleLocationLevel oleLocationLevel : locationLevel) {
158             locationLevelName.add(oleLocationLevel.getLevelName());
159         }
160         return locationLevelName;
161     }
162 
163 
164     /**
165      * Getting Sub field value
166      *
167      * @param bibField
168      * @param dataField
169      * @return
170      */
171     public static String getSubFieldValue(String bibField, DataField dataField) {
172         String subFieldValue = "";
173         DataField dataFieldObj = getDataField(bibField);
174         for (SubField subField : dataField.getSubFields()) {
175             for (SubField bibSubField : dataFieldObj.getSubFields()) {
176                 if (subField.getCode().equalsIgnoreCase(bibSubField.getCode())) {
177                     subFieldValue = subFieldValue + subField.getValue();
178                 }
179             }
180         }
181 
182         return subFieldValue;
183     }
184 
185     public static HoldingsTree buildHoldingsTree(String holdingsType) {
186         HoldingsTree holdingsTree = new HoldingsTree();
187         Holdings holdings = null;
188         if (holdingsType.equals(DocType.HOLDINGS.getCode())) {
189             holdings = new PHoldings();
190         }
191         if (holdingsType.equals(DocType.EHOLDINGS.getCode())) {
192             holdings = new org.kuali.ole.docstore.common.document.EHoldings();
193         }
194 
195         holdingsTree.setHoldings(holdings);
196         return holdingsTree;
197     }
198 
199 
200     /**
201      * Performs to get match data fields list based on enter data enter field from bib record
202      *
203      * @param bibMarcRecord
204      * @param enteredDataField
205      * @return
206      */
207     public static List<String> getMatchedDataField(BibMarcRecord bibMarcRecord, String enteredDataField) {
208         String[] dataFiledsArray = enteredDataField.split(" ");
209         String entryDataField = null;
210         String entryInds = null;
211         String entrySubField = null;
212         if (dataFiledsArray.length == 3) {
213             entryDataField = dataFiledsArray[0];
214             entryInds = dataFiledsArray[1];
215             entrySubField = dataFiledsArray[2];
216         } else if (dataFiledsArray.length == 2) {
217             entryDataField = dataFiledsArray[0];
218             entryInds = "##";
219             entrySubField = dataFiledsArray[1];
220         }
221         List<String> dataFields = new ArrayList<>();
222         String ind1 = "";
223         String ind2 = "";
224         String dataFieldInds = "";
225         for (DataField dataField : bibMarcRecord.getDataFields()) {
226             if (dataField.getTag().equalsIgnoreCase(entryDataField)) {
227                 ind1 = dataField.getInd1();
228                 ind2 = dataField.getInd2();
229                 if (entryInds.equals("##")) {
230                     ind1 = "#";
231                     ind2 = "#";
232                 } else if (entryInds.startsWith("#")) {
233                     ind1 = "#";
234                 } else if (entryInds.endsWith("#")) {
235                     ind2 = "#";
236                 }
237                 if (ind1.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind1) || ind1.equalsIgnoreCase("\\")) ind1 = "#";
238                 if (ind2.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind2) || ind2.equalsIgnoreCase("\\")) ind2 = "#";
239                 dataFieldInds = ind1 + ind2;
240                 if (dataFieldInds.equalsIgnoreCase(entryInds)) {
241                     if (StringUtils.isNotEmpty(entrySubField)) {
242                         StringBuffer subFieldValue = new StringBuffer("");
243                         entrySubField = entrySubField.replace("$", " ");
244                         String[] subFieldsArray = entrySubField.split(" ");
245                         for (int i = 1; i < subFieldsArray.length; i++) {
246                             String code = subFieldsArray[i];
247                             for (SubField subField : dataField.getSubFields()) {
248                                 if (subField.getCode().equalsIgnoreCase(code)) {
249                                     subFieldValue.append(subField.getValue());
250                                 }
251                             }
252                         }
253                         if (StringUtils.isNotEmpty(subFieldValue.toString())) {
254                             dataFields.add(subFieldValue.toString());
255                         }
256 
257                     }
258                 }
259             }
260         }
261         return dataFields;
262     }
263 
264     public static Object getHoldings(HoldingsTree holdingsTree, String docType) {
265         Holdings holdings = null;
266         if ((docType.equals(DocType.HOLDINGS.getCode()) && holdingsTree.getHoldings() instanceof PHoldings)) {
267             holdings = holdingsTree.getHoldings();
268         } else if ((docType.equals(DocType.EHOLDINGS.getCode()) && holdingsTree.getHoldings() instanceof EHoldings)) {
269             holdings = holdingsTree.getHoldings();
270         }
271 
272         return holdings;
273     }
274 
275 
276     public static List<Item> getItem(List<HoldingsTree> holdingsTreeList) {
277         List<Item> items = null;
278         for (HoldingsTree holdingsTree : holdingsTreeList) {
279             if (holdingsTree.getHoldings() instanceof PHoldings) {
280                 items = holdingsTree.getItems();
281 
282             }
283         }
284         return items;
285     }
286 
287 
288     public static List<OLEBatchProcessProfileMatchPoint> buildMatchPointListByDataType(List<OLEBatchProcessProfileMatchPoint> oleBatchProcessProfileMatchPointList, String dataType) {
289         List<OLEBatchProcessProfileMatchPoint> matchPointList = new ArrayList<>();
290         for (OLEBatchProcessProfileMatchPoint profileMatchPoint : oleBatchProcessProfileMatchPointList) {
291             if (profileMatchPoint.getMatchPointType().equalsIgnoreCase(dataType)) {
292                 matchPointList.add(profileMatchPoint);
293             }
294         }
295         return matchPointList;
296     }
297 
298     public static HoldingsTrees getHoldingsTrees(List<String> bibIds) {
299         HoldingsTrees holdingsTrees = null;
300         try {
301             holdingsTrees = getDocstoreClientLocator().getDocstoreClient().retrieveHoldingsDocTrees(bibIds);
302         } catch (Exception e) {
303             e.printStackTrace();
304         }
305 
306 
307         return holdingsTrees;
308     }
309 
310     public static HoldingsTrees getHoldingsTrees(String bibId) {
311         List<String> bibIds = new ArrayList<>();
312         bibIds.add(bibId);
313         return getHoldingsTrees(bibIds);
314 
315     }
316 
317     public static List<String> getItemIds(String holdingId) {
318         HoldingsTree holdingsTree = null;
319         try {
320             holdingsTree = getDocstoreClientLocator().getDocstoreClient().retrieveHoldingsTree(holdingId);
321         } catch (Exception e) {
322             e.printStackTrace();
323         }
324 
325         List<String> itemsIds = new ArrayList<>();
326 
327         for (Item item : holdingsTree.getItems()) {
328             itemsIds.add(item.getId());
329         }
330 
331         return itemsIds;
332     }
333 
334     public static List<HoldingsTree> getHoldingsTrees(Holdings holdings) {
335         List<HoldingsTree> holdingsTreeList = new ArrayList<>();
336         HoldingsTree holdingsTree = new HoldingsTree();
337         holdingsTree.setHoldings(holdings);
338         holdingsTreeList.add(holdingsTree);
339         return holdingsTreeList;
340     }
341 
342     /**
343      * Performs the get match record value for search in doc store
344      *
345      * @param dataField
346      * @return
347      */
348     public static String getDataFieldWithout$(String dataField) {
349         String dataFieldWithout$ = null;
350         if (OLEConstants.OLEBatchProcess.CONTROL_FIELD_001.equals(dataField)) {
351             return OLEConstants.OLEBatchProcess.CONTROL_FIELD_NAME_001;
352         } else {
353 
354             String[] matchRecordSplit = dataField.split(" ");
355             String fullSubField = matchRecordSplit[matchRecordSplit.length - 1];
356             dataFieldWithout$ = matchRecordSplit[0] + fullSubField.substring(fullSubField.length() - 1);
357         }
358         return OLEConstants.PREFIX_FOR_DATA_FIELD + dataFieldWithout$;
359     }
360 
361     /**
362      *
363      * @param dataFieldString
364      * @return
365      */
366     public static DataField getDataField(String dataFieldString) {
367         DataField dataField = new DataField();
368         if (dataFieldString != null) {
369             String[] dataFieldArrray = dataFieldString.split(" ");
370 
371             dataField.setTag(dataFieldArrray[0]);
372             List<SubField> subFields = new ArrayList<>();
373             if (dataFieldArrray.length > 2) {
374                 dataField.setInd1(dataFieldArrray[1].substring(0, 1));
375                 dataField.setInd2(dataFieldArrray[1].substring(1));
376                 String[] subFiledArray = dataFieldArrray[2].split("\\$");
377                 for (String subFieldString : subFiledArray) {
378                     if (StringUtils.isNotEmpty(subFieldString)) {
379                     SubField subField = new SubField();
380                     subField.setCode(subFieldString);
381                     subFields.add(subField);
382                     }
383                 }
384             } else {
385                 if (dataFieldArrray.length == 2) {
386                     String[] subFiledArray = dataFieldArrray[1].split("\\$");
387                     for (String subFieldString : subFiledArray) {
388                         if (StringUtils.isNotEmpty(subFieldString)) {
389                             SubField subField = new SubField();
390                             subField.setCode(subFieldString);
391                             subFields.add(subField);
392                         }
393                     }
394                 }
395             }
396             dataField.setSubFields(subFields);
397 
398         }
399 
400         return dataField;
401 
402     }
403 
404 
405     public static boolean isItemHoldingMapping(Map<String, String> itemHoldingsMapping) {
406 
407         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER)) {
408             return true;
409         }
410 
411         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER_PREFIX)) {
412             return true;
413         }
414         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER_TYPE)) {
415             return true;
416         }
417 
418         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_COPY_NUMBER)) {
419             return true;
420         }
421 
422         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_1)) {
423             return true;
424         }
425         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_2)) {
426             return true;
427         }
428         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_3)) {
429             return true;
430         }
431         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_4)) {
432             return true;
433         }
434         if (itemHoldingsMapping.containsKey(Item.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_5)) {
435             return true;
436         }
437         return false;
438     }
439 
440     /**
441      * This method checks the existence of a '245' data field with a sub field 'a' in the bib marc record.
442      * @param bibRecord
443      * @return
444      */
445     public static boolean has245aDataField(BibMarcRecord bibRecord) {
446         DataField dataField245 = bibRecord.getDataFieldForTag(OLEConstants.MARC_EDITOR_TITLE_245);
447         if (dataField245 != null) {
448             if (CollectionUtils.isNotEmpty(dataField245.getSubFields())) {
449                 for (SubField subField : dataField245.getSubFields()) {
450                     if (!subField.getCode().isEmpty()) {
451                         if (subField.getCode().equals(OLEConstants.A)) {
452                             return true;
453                         }
454                     }
455                 }
456             }
457         }
458         return false;
459     }
460 
461     /**
462      *
463      * @param dataFieldString
464      * @param value
465      * @return
466      */
467     public static DataField buildDataField(String dataFieldString, String value) {
468         DataField dataField = new DataField();
469         if (dataFieldString != null) {
470             String[] dataFieldArrray = dataFieldString.split(" ");
471 
472             dataField.setTag(dataFieldArrray[0]);
473             List<SubField> subFields = new ArrayList<>();
474             if (dataFieldArrray.length > 2) {
475                 dataField.setInd1(dataFieldArrray[1].substring(0, 1));
476                 dataField.setInd2(dataFieldArrray[1].substring(1));
477                 String[] subFiledArray = dataFieldArrray[2].split("\\$");
478                 for (String subFieldString : subFiledArray) {
479                     if (StringUtils.isNotEmpty(subFieldString)) {
480                         SubField subField = new SubField();
481                         subField.setCode(subFieldString);
482                         subField.setValue(value);
483                         subFields.add(subField);
484                     }
485                 }
486             } else {
487                 if (dataFieldArrray.length == 2) {
488                     String[] subFiledArray = dataFieldArrray[1].split("\\$");
489                     for (String subFieldString : subFiledArray) {
490                         if (StringUtils.isNotEmpty(subFieldString)) {
491                             SubField subField = new SubField();
492                             dataField.setInd1(" ");
493                             dataField.setInd2(" ");
494                             subField.setCode(subFieldString);
495                             subField.setValue(value);
496                             subFields.add(subField);
497                         }
498                     }
499                 }
500             }
501             dataField.setSubFields(subFields);
502         }
503         return dataField;
504     }
505 
506 }