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
28
29
30
31
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 public Node<DocumentTreeNode, String> add(Collection<String> uuidList, String docType, boolean excludeEHoldings) throws SolrServerException {
60
61 List<Node<DocumentTreeNode, String>> nodeList = null;
62 Node<DocumentTreeNode, String> node = null;
63 if (!StringUtils.isBlank(docType)) {
64 if (docType.equalsIgnoreCase(DocType.BIB.getDescription())) {
65 List<BibTree> bibTreeList = null;
66 bibTreeList = buildDocTreeList(DocCategory.WORK.getCode(), docType, uuidList, excludeEHoldings);
67 if(excludeEHoldings){
68 for(BibTree bibTree : bibTreeList){
69 List<HoldingsTree> holdingsTrees = new ArrayList<>();
70 holdingsTrees = bibTree.getHoldingsTrees();
71 ListIterator<HoldingsTree> holdingsTreeListIterator = holdingsTrees.listIterator();
72 while (holdingsTreeListIterator.hasNext()){
73 if(holdingsTreeListIterator.next().getHoldings().getHoldingsType().equalsIgnoreCase("electronic")){
74 holdingsTreeListIterator.remove();
75 }
76 }
77 }
78 }
79 nodeList = buildNodeList(bibTreeList);
80 for (Node<DocumentTreeNode, String> bibNode : nodeList) {
81 rootNode.addChild(bibNode);
82 }
83 }
84
85 if (docType.equalsIgnoreCase((DocType.HOLDINGS.getCode()))) {
86 List<HoldingsTree> holdingsTreeList = buildHoldingsTreeList(DocCategory.WORK.getCode(), docType, uuidList);
87 for (HoldingsTree holdingsTree : holdingsTreeList) {
88 node = buildHoldingsNode(holdingsTree);
89 rootNode.addChild(node);
90 }
91 }
92
93 if (docType.equalsIgnoreCase(DocType.ITEM.getCode())) {
94 List<Item> itemList = buildItemTreeList(DocCategory.WORK.getCode(), docType, uuidList);
95 for (Item item : itemList) {
96 node = buildItemNode(item);
97 rootNode.addChild(node);
98
99 }
100 }
101
102 if (docType.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) {
103 List<HoldingsTree> holdingsTreeList = buildHoldingsTreeList(DocCategory.WORK.getCode(), docType, uuidList);
104 for (HoldingsTree holdingsTree : holdingsTreeList) {
105 node = buildHoldingsNode(holdingsTree);
106 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
107 if (holdingsTree.getHoldings() != null) {
108 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdingsTree.getHoldings().getContent());
109 if (oleHoldings.getEResourceId() != null && !oleHoldings.getEResourceId().isEmpty()) {
110 OLEEResourceRecordDocument oleeResourceRecordDocument = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OLEEResourceRecordDocument.class, oleHoldings.getEResourceId());
111 node.setNodeLabel(node.getNodeLabel() + OLEConstants.DOCSTORE_NODE + oleeResourceRecordDocument.getTitle());
112 }
113 }
114 rootNode.addChild(node);
115 }
116 }
117 }
118 return rootNode;
119 }
120
121
122
123
124
125
126
127 private List<BibTree> buildDocTreeList(String category, String docType, Collection<String> uuidList) {
128 List<BibTree> bibTrees = buildDocTreeList(category, docType, uuidList, false);
129 return bibTrees;
130 }
131
132 private List<BibTree> buildDocTreeList(String category, String docType, Collection<String> uuidList,boolean excludeEHoldings) {
133
134 List<BibTree> bibTreeList = new ArrayList<>();
135
136 if (uuidList != null && uuidList.size() > 0) {
137 for (String uuid : uuidList) {
138 BibTree bibTree = new BibTree();
139 try {
140 bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(uuid);
141 } catch (Exception e) {
142 LOG.error("Exception occurred in buildDocTreeList() :", e);
143 }
144 bibTreeList.add(bibTree);
145 }
146 }
147 return bibTreeList;
148 }
149
150 private List<HoldingsTree> buildHoldingsTreeList(String category, String docType, Collection<String> uuidList) throws SolrServerException {
151
152 List<HoldingsTree> holdingsTreeList = new ArrayList<HoldingsTree>();
153
154 for (String uuid : uuidList) {
155 HoldingsTree holdingsTree = new HoldingsTree();
156 try {
157 holdingsTree = getDocstoreClientLocator().getDocstoreClient().retrieveHoldingsTree(uuid);
158 } catch (Exception e) {
159 LOG.error("Exception occurred in buildHoldingsTreeList() :", e);
160 }
161 holdingsTreeList.add(holdingsTree);
162 }
163 return holdingsTreeList;
164 }
165
166 private List<Item> buildItemTreeList(String category, String docType, Collection<String> uuidList) throws SolrServerException {
167
168 List<Item> itemList = new ArrayList<Item>();
169
170 for (String uuid : uuidList) {
171 Item item = new Item();
172 try {
173 item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(uuid);
174 } catch (Exception e) {
175 LOG.error("Exception occurred in buildItemTreeList() :", e);
176 }
177 itemList.add(item);
178 }
179 return itemList;
180 }
181
182
183 public void remove() {
184
185 }
186
187
188
189
190
191 public List<Node<DocumentTreeNode, String>> buildNodeList(List<BibTree> bibTreeList) {
192 List<Node<DocumentTreeNode, String>> nodeList = new ArrayList<Node<DocumentTreeNode, String>>();
193 for (BibTree bibTree : bibTreeList) {
194 DocumentTreeNode bibDocumentNode = new DocumentTreeNode();
195 bibDocumentNode.setBib(bibTree.getBib());
196 Node<DocumentTreeNode, String> bibNode = new Node<DocumentTreeNode, String>(
197 new DocumentTreeNode(), bibDocumentNode.getTitle());
198 bibNode.setNodeType(bibDocumentNode.getUuid());
199 bibNode.setNodeLabel(bibDocumentNode.getTitle());
200 List<HoldingsTree> holdingsTreeList = bibTree.getHoldingsTrees();
201 Node<DocumentTreeNode, String> holdingsNode;
202 Node<DocumentTreeNode, String> itemNode = null;
203 if (holdingsTreeList != null && holdingsTreeList.size() > 0) {
204 for (HoldingsTree holdingsTree : holdingsTreeList) {
205 holdingsNode = buildHoldingsNode(holdingsTree);
206 bibNode.addChild(holdingsNode);
207 }
208 }
209 nodeList.add(bibNode);
210 }
211 return nodeList;
212 }
213
214 private Node<DocumentTreeNode, String> buildHoldingsNode(HoldingsTree holdingsTree) {
215 Node<DocumentTreeNode, String> instanceNode;
216 List<Item> itemList = new ArrayList<>();
217 DocumentTreeNode instanceDocumentNode = new DocumentTreeNode();
218 instanceDocumentNode.setHoldings(holdingsTree);
219 instanceNode = new Node<DocumentTreeNode, String>(
220 new DocumentTreeNode(), instanceDocumentNode.getTitle());
221 instanceNode.setNodeType(instanceDocumentNode.getUuid());
222 instanceNode.setNodeLabel(instanceDocumentNode.getTitle());
223 itemList = holdingsTree.getItems();
224 if (itemList != null) {
225 Node<DocumentTreeNode, String> itemNode;
226 for (Item item : itemList) {
227 itemNode = buildHoldingsNode(item, holdingsTree.getHoldings());
228 instanceNode.addChild(itemNode);
229
230 }
231 }
232 return instanceNode;
233 }
234
235
236 private Node<DocumentTreeNode, String> buildItemNode(Item item) {
237 Node<DocumentTreeNode, String> itemNode;
238 DocumentTreeNode itemDocumentNode = new DocumentTreeNode();
239 itemDocumentNode.setWorkItemDocument(item);
240 itemNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(), itemDocumentNode.getTitle());
241 itemNode.setNodeType(itemDocumentNode.getUuid());
242 itemNode.setNodeLabel(itemDocumentNode.getTitle());
243 return itemNode;
244 }
245
246 private Node<DocumentTreeNode, String> buildHoldingsNode(Item item, Holdings holdings) {
247 Node<DocumentTreeNode, String> itemNode;
248 DocumentTreeNode itemDocumentNode = new DocumentTreeNode();
249 itemDocumentNode.setWorkHoldingsDocument(item, holdings);
250 itemNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(), itemDocumentNode.getTitle());
251 itemNode.setNodeType(itemDocumentNode.getUuid());
252 itemNode.setNodeLabel(itemDocumentNode.getTitle());
253 return itemNode;
254 }
255
256
257 public Node<DocumentTreeNode, String> addForTransfer(List<String> uuidList) {
258 List<BibTree> bibTreeList = buildDocTreeList(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), uuidList);
259 List<Node<DocumentTreeNode, String>> nodeList = buildNodeListForTransfer(bibTreeList);
260 for (Node<DocumentTreeNode, String> node : nodeList) {
261 rootNode.addChild(node);
262 }
263 return rootNode;
264 }
265
266
267
268
269
270 public List<Node<DocumentTreeNode, String>> buildNodeListForTransfer(List<BibTree> bibTreeList) {
271 List<Node<DocumentTreeNode, String>> nodeList = new ArrayList<Node<DocumentTreeNode, String>>();
272 for (BibTree bibTree : bibTreeList) {
273 DocumentTreeNode bibDocumentNode = new DocumentTreeNode();
274 bibDocumentNode.setBibTree(bibTree);
275 String title = bibDocumentNode.getBibTree().getBib().getTitle();
276 Node<DocumentTreeNode, String> bibNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(), bibDocumentNode.getTitle());
277 bibNode.setNodeType(bibDocumentNode.getUuid());
278 bibNode.setNodeLabel(bibDocumentNode.getTitle());
279
280 List<HoldingsTree> holdingsTreeList = bibTree.getHoldingsTrees();
281 List<Item> itemList = new ArrayList<>();
282 Node<DocumentTreeNode, String> holdingsNode;
283 Node<DocumentTreeNode, String> itemNode = null;
284 if (holdingsTreeList != null) {
285 for (HoldingsTree holdingsTree : holdingsTreeList) {
286 DocumentTreeNode holdingsDocumentNode = new DocumentTreeNode();
287
288 holdingsDocumentNode.setHoldings(holdingsTree);
289 holdingsNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(), holdingsDocumentNode.getTitle());
290 holdingsNode.setNodeType(holdingsDocumentNode.getUuid());
291 holdingsNode.setNodeLabel(holdingsDocumentNode.getTitle());
292 itemList = holdingsTree.getItems();
293 if (itemList != null) {
294 for (Item item : itemList) {
295 DocumentTreeNode itemDocumentNode = new DocumentTreeNode();
296 itemDocumentNode.setItem(item);
297 itemNode = new Node<DocumentTreeNode, String>(new DocumentTreeNode(),
298 itemDocumentNode.getTitle());
299 itemNode.setNodeType(itemDocumentNode.getUuid());
300 itemNode.setNodeLabel(itemDocumentNode.getTitle());
301 holdingsNode.addChild(itemNode);
302 }
303 }
304 bibNode.addChild(holdingsNode);
305 }
306 }
307 nodeList.add(bibNode);
308 }
309 return nodeList;
310 }
311 }