View Javadoc
1   package org.kuali.ole.describe.bo;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.solr.client.solrj.SolrServerException;
5   import org.kuali.ole.DocumentUniqueIDPrefix;
6   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
7   import org.kuali.ole.docstore.common.document.*;
8   import org.kuali.ole.docstore.common.document.Item;
9   import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
10  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
11  import org.kuali.ole.docstore.model.enums.DocCategory;
12  import org.kuali.ole.docstore.model.enums.DocType;
13  import org.kuali.ole.select.document.OLEEResourceRecordDocument;
14  import org.kuali.ole.sys.OLEConstants;
15  import org.kuali.ole.sys.context.SpringContext;
16  import org.kuali.rice.core.api.util.tree.Node;
17  import org.kuali.rice.krad.service.KRADServiceLocator;
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.ListIterator;
25  
26  /**
27   * Created with IntelliJ IDEA.
28   * User: Sreekanth
29   * Date: 12/20/12
30   * Time: 11:11 AM
31   * To change this template use File | Settings | File Templates.
32   */
33  public class DocumentSelectionTree {
34  
35      private static final Logger LOG = LoggerFactory.getLogger(DocumentSelectionTree.class);
36      private Node<DocumentTreeNode, String> rootNode;
37  
38      public DocumentSelectionTree() {
39          initTree();
40      }
41  
42      private DocstoreClientLocator docstoreClientLocator;
43  
44      public DocstoreClientLocator getDocstoreClientLocator() {
45          if (docstoreClientLocator == null) {
46              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
47          }
48          return docstoreClientLocator;
49      }
50      protected void initTree() {
51          rootNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode());
52      }
53  
54      public Node<DocumentTreeNode, String> add(Collection<String> uuidList, String docType) throws SolrServerException {
55          Node<DocumentTreeNode, String> rootNode = add(uuidList, docType, false);
56          return rootNode;
57      }
58  
59      /**
60       * This method adds the holdings trees to the root node in left pane tree.
61       * @param holdingsTrees
62       * @param docType
63       * @return
64       * @throws SolrServerException
65       */
66      public Node<DocumentTreeNode, String> addHoldingsTree(List<HoldingsTree> holdingsTrees, String docType) throws SolrServerException {
67          if (docType.equalsIgnoreCase((DocType.HOLDINGS.getCode()))) {
68              for (HoldingsTree holdingsTree : holdingsTrees) {
69                  Node<DocumentTreeNode, String> node = buildHoldingsNode(holdingsTree);
70                  rootNode.addChild(node);
71              }
72          } else if (docType.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) {
73              for (HoldingsTree holdingsTree : holdingsTrees) {
74                  Node<DocumentTreeNode, String> node = buildEHoldingsNode(holdingsTree);
75                  rootNode.addChild(node);
76              }
77          }
78          return rootNode;
79      }
80  
81      /**
82       * This method builds the EHoldings node to left pane tree.
83       * @param holdingsTree
84       */
85      private Node<DocumentTreeNode, String>  buildEHoldingsNode(HoldingsTree holdingsTree) {
86          Node<DocumentTreeNode, String> node = buildHoldingsNode(holdingsTree);
87              HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
88              if (holdingsTree.getHoldings() != null) {
89                  OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdingsTree.getHoldings().getContent());
90                  if (oleHoldings.getEResourceId() != null && !oleHoldings.getEResourceId().isEmpty()) {
91                      OLEEResourceRecordDocument oleeResourceRecordDocument = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OLEEResourceRecordDocument.class, oleHoldings.getEResourceId());
92                      node.setNodeLabel(node.getNodeLabel() + OLEConstants.DOCSTORE_NODE + oleeResourceRecordDocument.getTitle());
93                  }
94              }
95          return node;
96      }
97  
98      public Node<DocumentTreeNode, String> add(Collection<String> uuidList, String docType, boolean excludeEHoldings) throws SolrServerException {
99  
100         List<Node<DocumentTreeNode, String>> nodeList = null;
101         Node<DocumentTreeNode, String> node = null;
102         if (!StringUtils.isBlank(docType)) {
103             if (docType.equalsIgnoreCase(DocType.BIB.getDescription())) {
104                 List<BibTree> bibTreeList = null;
105                 bibTreeList = buildDocTreeList(DocCategory.WORK.getCode(), docType, uuidList, excludeEHoldings);
106                 if(excludeEHoldings){
107                     for(BibTree bibTree : bibTreeList){
108                         List<HoldingsTree> holdingsTrees = new ArrayList<>();
109                         holdingsTrees = bibTree.getHoldingsTrees();
110                         ListIterator<HoldingsTree> holdingsTreeListIterator = holdingsTrees.listIterator();
111                         while (holdingsTreeListIterator.hasNext()){
112                             if(holdingsTreeListIterator.next().getHoldings().getHoldingsType().equalsIgnoreCase("electronic")){
113                                 holdingsTreeListIterator.remove();
114                             }
115                         }
116                     }
117                 }
118                 nodeList = buildNodeList(bibTreeList);
119                 for (Node<DocumentTreeNode, String> bibNode : nodeList) {
120                     rootNode.addChild(bibNode);
121                 }
122             }
123 
124             if (docType.equalsIgnoreCase((DocType.HOLDINGS.getCode()))) {
125                 List<HoldingsTree> holdingsTreeList = buildHoldingsTreeList(DocCategory.WORK.getCode(), docType, uuidList);
126                 for (HoldingsTree holdingsTree : holdingsTreeList) {
127                     node = buildHoldingsNode(holdingsTree);
128                     rootNode.addChild(node);
129                 }
130             }
131 
132             if (docType.equalsIgnoreCase(DocType.ITEM.getCode())) {
133                 List<Item> itemList = buildItemTreeList(DocCategory.WORK.getCode(), docType, uuidList);
134                 for (Item item : itemList) {
135                     node = buildItemNode(item);
136                     rootNode.addChild(node);
137 
138                 }
139             }
140 
141             if (docType.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) {
142                 List<HoldingsTree> holdingsTreeList = buildHoldingsTreeList(DocCategory.WORK.getCode(), docType, uuidList);
143                 for (HoldingsTree holdingsTree : holdingsTreeList) {
144                     node = buildEHoldingsNode(holdingsTree);
145                     rootNode.addChild(node);
146                 }
147             }
148         }
149         return rootNode;
150     }
151 
152     /**
153      * @param category
154      * @param docType
155      * @param uuidList
156      * @return
157      */
158     private List<BibTree> buildDocTreeList(String category, String docType, Collection<String> uuidList) {
159         List<BibTree> bibTrees = buildDocTreeList(category, docType, uuidList, false);
160         return bibTrees;
161     }
162 
163     private List<BibTree> buildDocTreeList(String category, String docType, Collection<String> uuidList,boolean excludeEHoldings) {
164 
165         List<BibTree> bibTreeList = new ArrayList<>();
166 
167         if (uuidList != null && uuidList.size() > 0) {
168             for (String uuid : uuidList) {
169                 BibTree bibTree = new BibTree();
170                 try {
171                     bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(uuid);
172                 } catch (Exception e) {
173                     LOG.error("Exception occurred in buildDocTreeList() :", e);
174                 }
175                 bibTreeList.add(bibTree);
176             }
177         }
178         return bibTreeList;
179     }
180 
181     private List<HoldingsTree> buildHoldingsTreeList(String category, String docType, Collection<String> uuidList) throws SolrServerException {
182 
183         List<HoldingsTree> holdingsTreeList = new ArrayList<HoldingsTree>();
184 
185         for (String uuid : uuidList) {
186             HoldingsTree holdingsTree = new HoldingsTree();
187             try {
188                 holdingsTree = getDocstoreClientLocator().getDocstoreClient().retrieveHoldingsTree(uuid);
189             } catch (Exception e) {
190                 LOG.error("Exception occurred in buildHoldingsTreeList() :", e);
191             }
192             holdingsTreeList.add(holdingsTree);
193         }
194         return holdingsTreeList;
195     }
196 
197     private List<Item> buildItemTreeList(String category, String docType, Collection<String> uuidList) throws SolrServerException {
198 
199         List<Item> itemList = new ArrayList<Item>();
200 
201         for (String uuid : uuidList) {
202             Item item = new Item();
203             try {
204                 item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(uuid);
205             } catch (Exception e) {
206                 LOG.error("Exception occurred in buildItemTreeList() :", e);
207             }
208             itemList.add(item);
209         }
210         return itemList;
211     }
212 
213 
214     public void remove() {
215 
216     }
217 
218     /**
219      * @param bibTreeList
220      * @return
221      */
222     public List<Node<DocumentTreeNode, String>> buildNodeList(List<BibTree> bibTreeList) {
223         List<Node<DocumentTreeNode, String>> nodeList = new ArrayList<Node<DocumentTreeNode, String>>();
224         for (BibTree bibTree : bibTreeList) {
225             DocumentTreeNode bibDocumentNode = new DocumentTreeNode();
226             if (bibTree.getBib() != null) {
227                 bibDocumentNode.setBib(bibTree.getBib());
228                 Node<DocumentTreeNode, String> bibNode = new Node<DocumentTreeNode, String>(
229                         new DocumentTreeNode(), bibDocumentNode.getTitle());
230                 bibNode.setNodeType(bibDocumentNode.getUuid());
231                 bibNode.setNodeLabel(bibDocumentNode.getTitle());
232                 List<HoldingsTree> holdingsTreeList = bibTree.getHoldingsTrees();
233                 Node<DocumentTreeNode, String> holdingsNode;
234                 Node<DocumentTreeNode, String> itemNode = null;
235                 if (holdingsTreeList != null && holdingsTreeList.size() > 0) {
236                     for (HoldingsTree holdingsTree : holdingsTreeList) {
237                         holdingsNode = buildHoldingsNode(holdingsTree);
238                         bibNode.addChild(holdingsNode);
239                     }
240                 }
241                 nodeList.add(bibNode);
242             }
243         }
244         return nodeList;
245     }
246 
247     private Node<DocumentTreeNode, String> buildHoldingsNode(HoldingsTree holdingsTree) {
248         Node<DocumentTreeNode, String> instanceNode;
249         List<Item> itemList = new ArrayList<>();
250         DocumentTreeNode instanceDocumentNode = new DocumentTreeNode();
251         instanceDocumentNode.setHoldings(holdingsTree);
252         instanceNode = new Node<DocumentTreeNode, String>(
253                 new DocumentTreeNode(), instanceDocumentNode.getTitle());
254         instanceNode.setNodeType(instanceDocumentNode.getUuid());
255         instanceNode.setNodeLabel(instanceDocumentNode.getTitle());
256         itemList = holdingsTree.getItems();
257         if (itemList != null) {
258             Node<DocumentTreeNode, String> itemNode;
259             for (Item item : itemList) {
260                 itemNode = buildHoldingsNode(item, holdingsTree.getHoldings());
261                 instanceNode.addChild(itemNode);
262 
263             }
264         }
265         return instanceNode;
266     }
267 
268 
269     private Node<DocumentTreeNode, String> buildItemNode(Item item) {
270         Node<DocumentTreeNode, String> itemNode;
271         DocumentTreeNode itemDocumentNode = new DocumentTreeNode();
272         itemDocumentNode.setWorkItemDocument(item);
273         itemNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(), itemDocumentNode.getTitle());
274         itemNode.setNodeType(itemDocumentNode.getUuid());
275         itemNode.setNodeLabel(itemDocumentNode.getTitle());
276         return itemNode;
277     }
278 
279     private Node<DocumentTreeNode, String> buildHoldingsNode(Item item, Holdings holdings) {
280         Node<DocumentTreeNode, String> itemNode;
281         DocumentTreeNode itemDocumentNode = new DocumentTreeNode();
282         itemDocumentNode.setWorkHoldingsDocument(item, holdings);
283         itemNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(), itemDocumentNode.getTitle());
284         itemNode.setNodeType(itemDocumentNode.getUuid());
285         itemNode.setNodeLabel(itemDocumentNode.getTitle());
286         return itemNode;
287     }
288 
289 
290     public Node<DocumentTreeNode, String> addForTransfer(List<String> uuidList, String docType) {
291         if (docType.equalsIgnoreCase((DocType.BIB.getDescription()))) {
292             List<BibTree> bibTreeList = buildDocTreeList(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), uuidList);
293             List<Node<DocumentTreeNode, String>> nodeList = buildNodeListForTransfer(bibTreeList);
294             for (Node<DocumentTreeNode, String> node : nodeList) {
295                 rootNode.addChild(node);
296             }
297         } else if (docType.equalsIgnoreCase((DocType.HOLDINGS.getCode()))) {
298             try {
299                 List<HoldingsTree> holdingsTreeList = buildHoldingsTreeList(DocCategory.WORK.getCode(), docType, uuidList);
300                 for (HoldingsTree holdingsTree : holdingsTreeList) {
301                     Node<DocumentTreeNode,String> node = buildHoldingsNode(holdingsTree);
302                     rootNode.addChild(node);
303                 }
304             } catch (SolrServerException ex) {
305                 LOG.error(ex.getMessage());
306             }
307         }
308 
309 
310         return rootNode;
311     }
312 
313     /**
314      * @param bibTreeList
315      * @return
316      */
317     public List<Node<DocumentTreeNode, String>> buildNodeListForTransfer(List<BibTree> bibTreeList) {
318         List<Node<DocumentTreeNode, String>> nodeList = new ArrayList<Node<DocumentTreeNode, String>>();
319         for (BibTree bibTree : bibTreeList) {
320             DocumentTreeNode bibDocumentNode = new DocumentTreeNode();
321             bibDocumentNode.setBibTree(bibTree);
322             bibDocumentNode.setBib(bibTree.getBib());
323 //            String title = bibDocumentNode.getBibTree().getBib().getTitle();
324             Node<DocumentTreeNode, String> bibNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(), bibDocumentNode.getTitle());
325             bibNode.setNodeType(bibDocumentNode.getUuid());
326             bibNode.setNodeLabel(bibDocumentNode.getTitle());
327 
328             List<HoldingsTree> holdingsTreeList = bibTree.getHoldingsTrees();
329             List<Item> itemList = new ArrayList<>();
330             Node<DocumentTreeNode, String> holdingsNode;
331             Node<DocumentTreeNode, String> itemNode = null;
332             if (holdingsTreeList != null) {
333                 for (HoldingsTree holdingsTree : holdingsTreeList) {
334                     DocumentTreeNode holdingsDocumentNode = new DocumentTreeNode();
335 //                    holdingsDocumentNode.setHoldings(holdingsTree.getHoldings());
336                     holdingsDocumentNode.setHoldings(holdingsTree);
337                     holdingsNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(), holdingsDocumentNode.getTitle());
338                     holdingsNode.setNodeType(holdingsDocumentNode.getUuid());
339                     holdingsNode.setNodeLabel(holdingsDocumentNode.getTitle());
340                     itemList = holdingsTree.getItems();
341                     if (itemList != null) {
342                         for (Item item : itemList) {
343                             DocumentTreeNode itemDocumentNode = new DocumentTreeNode();
344                             itemDocumentNode.setItem(item);
345                              itemNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(),
346                                     itemDocumentNode.getTitle());
347                             itemNode.setNodeType(itemDocumentNode.getUuid());
348                             itemNode.setNodeLabel(itemDocumentNode.getTitle());
349                             holdingsNode.addChild(itemNode);
350                         }
351                     }
352                     bibNode.addChild(holdingsNode);
353                 }
354             }
355             nodeList.add(bibNode);
356         }
357         return nodeList;
358     }
359 }