1 package org.kuali.ole.deliver.util;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.kuali.ole.batch.util.BatchBibImportUtil;
5 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
6 import org.kuali.ole.docstore.common.document.*;
7 import org.kuali.ole.docstore.common.document.HoldingsTree;
8 import org.kuali.ole.docstore.common.document.Item;
9 import org.kuali.ole.sys.context.SpringContext;
10
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15
16
17
18
19 public class BulkItemUpdateUtil {
20 private DocstoreClientLocator docstoreClientLocator;
21
22 public Map<String, Map> updateItemsToDocstore(List<Item> itemList) {
23 org.kuali.ole.docstore.common.document.BibTrees bibTrees = new org.kuali.ole.docstore.common.document.BibTrees();
24
25 org.kuali.ole.docstore.common.document.BibTree bibTree = new org.kuali.ole.docstore.common.document.BibTree();
26 org.kuali.ole.docstore.common.document.Bib bib = new org.kuali.ole.docstore.common.document.Bib();
27 bibTree.setBib(bib);
28 bib.setId("");
29
30 org.kuali.ole.docstore.common.document.HoldingsTree holdingsTree = new org.kuali.ole.docstore.common.document.HoldingsTree();
31 org.kuali.ole.docstore.common.document.Holdings holdings = new org.kuali.ole.docstore.common.document.Holdings();
32 holdings.setId("");
33 holdingsTree.setHoldings(holdings);
34
35 List<String> locationLevel = BatchBibImportUtil.getLocationLevel();
36
37 for (Iterator<Item> iterator = itemList.iterator(); iterator.hasNext(); ) {
38 Item item = iterator.next();
39 if(CollectionUtils.isNotEmpty(locationLevel)) {
40 populateLocationLevels(item,locationLevel);
41 }
42 item.buildLocationLevels((org.kuali.ole.docstore.common.document.content.instance.Item) item.getContentObject());
43 item.setOperation(org.kuali.ole.docstore.common.document.DocstoreDocument.OperationType.UPDATE);
44 item.serializeContent();
45 holdingsTree.getItems().add(item);
46 }
47
48 bibTree.getHoldingsTrees().add(holdingsTree);
49
50 bibTrees.getBibTrees().add(bibTree);
51 BibTrees responseBibTree = null;
52 try {
53 responseBibTree = getDocstoreClientLocator().getDocstoreClient().processBibTrees(bibTrees);
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57 Map<String, Map> statusMap = generateResultMapForBibTreeWithItemUuidAndResultType(responseBibTree);
58 return statusMap;
59 }
60
61 private void populateLocationLevels(Item item, List<String> locationLevel) {
62 item.setLevel1Location(locationLevel.get(0));
63 item.setLevel2Location(locationLevel.get(1));
64 item.setLevel3Location(locationLevel.get(2));
65 item.setLevel4Location(locationLevel.get(3));
66 item.setLevel5Location(locationLevel.get(4));
67 }
68
69 private Map<String, Map> generateResultMapForBibTreeWithItemUuidAndResultType(BibTrees bibTrees) {
70 Map<String, Map> resultTypeMap = new HashMap<>();
71 if(null != bibTrees){
72 List<BibTree> bibTreeList = bibTrees.getBibTrees();
73 if(CollectionUtils.isNotEmpty(bibTreeList)){
74 for (Iterator<BibTree> bibTreeIterator = bibTreeList.iterator(); bibTreeIterator.hasNext(); ) {
75 BibTree bibTree = bibTreeIterator.next();
76 List<HoldingsTree> holdingsTrees = bibTree.getHoldingsTrees();
77 if(CollectionUtils.isNotEmpty(holdingsTrees)){
78 for (Iterator<HoldingsTree> holdingsTreeIterator = holdingsTrees.iterator(); holdingsTreeIterator.hasNext(); ) {
79 HoldingsTree holdingsTree = holdingsTreeIterator.next();
80 List<Item> items = holdingsTree.getItems();
81 if(CollectionUtils.isNotEmpty(items)){
82 for (Iterator<Item> itemIterator = items.iterator(); itemIterator.hasNext(); ) {
83 Item item = itemIterator.next();
84 Map statusMap = new HashMap();
85 statusMap.put("result", item.getResult());
86 statusMap.put("message", item.getMessage());
87 statusMap.put("barcode", item.getBarcode());
88 resultTypeMap.put(item.getId(),statusMap);
89 }
90 }
91 }
92 }
93 }
94 }
95 }
96 return resultTypeMap;
97 }
98
99 public DocstoreClientLocator getDocstoreClientLocator() {
100 if(null == docstoreClientLocator){
101 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
102 }
103 return docstoreClientLocator;
104 }
105
106 public void setDocstoreClientLocator(DocstoreClientLocator docstoreClientLocator) {
107 this.docstoreClientLocator = docstoreClientLocator;
108 }
109 }