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