1 package org.kuali.ole.describe.rule;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.describe.bo.DocumentSelectionTree;
7 import org.kuali.ole.describe.bo.DocumentTreeNode;
8 import org.kuali.ole.describe.form.AnalyticsForm;
9 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
10 import org.kuali.ole.docstore.common.document.*;
11 import org.kuali.ole.sys.context.SpringContext;
12 import org.kuali.rice.core.api.util.tree.Node;
13 import org.kuali.rice.core.api.util.tree.Tree;
14 import org.kuali.rice.krad.util.GlobalVariables;
15 import org.springframework.util.CollectionUtils;
16
17 import java.util.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22
23
24
25
26
27
28
29 public class OLEAnalyticsRule {
30 private static final Logger LOG = Logger.getLogger(OLEAnalyticsRule.class);
31
32 private DocstoreClientLocator docstoreClientLocator;
33
34 public DocstoreClientLocator getDocstoreClientLocator() {
35 if (null == docstoreClientLocator) {
36 return SpringContext.getBean(DocstoreClientLocator.class);
37 }
38 return docstoreClientLocator;
39 }
40
41 public void selectCheckedNodesForSeriesTree(AnalyticsForm analyticsForm, Node<DocumentTreeNode, String> seriesTreeRootElement) {
42 DocumentTreeNode documentTreeNode;
43 BibTree bibTree = new BibTree();
44 HoldingsTree holdingsTree = new HoldingsTree();
45 Holdings holdings = new Holdings();
46 Set<String> selectedBibsList = new HashSet<String>();
47 List<String> selectedBibs = new ArrayList<String>();
48 List<String> selectedBibsFromTree1 = new ArrayList<String>();
49 List<String> selectedInstancesList = new ArrayList<String>();
50 List<String> selectedHoldings = new ArrayList<String>();
51 List<String> selectedHoldingsFromTree1 = new ArrayList<String>();
52 String tree1BibId = null;
53 Bib bib = new Bib();
54 List<Item> items = new ArrayList<Item>();
55 if (seriesTreeRootElement != null) {
56 List<Node<DocumentTreeNode, String>> seriesTreeList = seriesTreeRootElement.getChildren();
57 for (Node<DocumentTreeNode, String> bibNode : seriesTreeList) {
58 documentTreeNode = bibNode.getData();
59 LOG.info("Series-bibId-->" + bibNode.getNodeType());
60 LOG.info("Series-bibNodeTitle-->" + bibNode.getData().getTitle());
61 LOG.info("Series-bibNodeSelected-->" + documentTreeNode.isSelect());
62 List<Node<DocumentTreeNode, String>> holdingsList = bibNode.getChildren();
63 for (Node<DocumentTreeNode, String> holdingsNode : holdingsList) {
64 documentTreeNode = holdingsNode.getData();
65 LOG.info("Series-holdingsId-->" + holdingsNode.getNodeType());
66 LOG.info("Series-holdingsNodeTitle-->" + holdingsNode.getData().getTitle());
67 LOG.info("Series-holdingsNodeSelected-->" + documentTreeNode.isSelect());
68 if (documentTreeNode.isSelect()) {
69 bib.setId(bibNode.getNodeType());
70 selectedBibsList.add(bibNode.getNodeType());
71 selectedBibs.add(bibNode.getNodeType());
72 selectedBibsFromTree1.add(bibNode.getNodeType());
73 bib.setTitle(bibNode.getNodeLabel());
74 tree1BibId = bibNode.getNodeType();
75 analyticsForm.setTree1BibId(tree1BibId);
76 holdings.setId(holdingsNode.getNodeType());
77 selectedInstancesList.add(holdingsNode.getNodeType());
78 selectedHoldings.add(holdingsNode.getNodeType());
79 selectedHoldingsFromTree1.add(holdingsNode.getNodeType());
80 holdings.setLocationName(holdingsNode.getNodeLabel());
81 documentTreeNode.setSelect(true);
82 List<Node<DocumentTreeNode, String>> itemList = holdingsNode.getChildren();
83 for (Node<DocumentTreeNode, String> itemNode : itemList) {
84 Item itemDoc = new Item();
85 documentTreeNode = itemNode.getData();
86 LOG.info("Series-itemId-->" + holdingsNode.getNodeType());
87 LOG.info("Series-itemNodeTitle-->" + holdingsNode.getData().getTitle());
88 LOG.info("Series-itemNodeSelected-->" + documentTreeNode.isSelect());
89 itemDoc.setId(itemNode.getNodeType());
90 itemDoc.setCallNumber(itemNode.getNodeLabel());
91 items.add(itemDoc);
92 }
93 holdingsTree.getItems().addAll(items);
94 holdingsTree.setHoldings(holdings);
95 bibTree.getHoldingsTrees().add(holdingsTree);
96 bibTree.setBib(bib);
97 analyticsForm.setBibTree(bibTree);
98 analyticsForm.setSelectedBibsList(selectedBibsList);
99 analyticsForm.setSelectedInstancesList(selectedInstancesList);
100 analyticsForm.setSelectedBibs(selectedBibs);
101 analyticsForm.setSelectedHoldings(selectedHoldings);
102 analyticsForm.setSelectedBibsFromTree1(selectedBibs);
103 analyticsForm.setSelectedHoldingsFromTree1(selectedHoldings);
104 }
105 }
106 }
107 }
108 }
109
110 public void selectCheckedNodesForAnalyticTree(AnalyticsForm analyticsForm, Node<DocumentTreeNode, String> analyticsTreeRootElement) {
111 DocumentTreeNode documentTreeNode;
112 BibTree bibTree = new BibTree();
113 HoldingsTree holdingsTree = new HoldingsTree();
114 Holdings holdings = new Holdings();
115 List<Item> items = new ArrayList<Item>();
116 Set<String> selectedItemsList = new HashSet<String>();
117 ArrayList<String> setSelectedBibsFromTree2 = new ArrayList<String>();
118 ArrayList<String> setSelectedItemsFromTree2 = new ArrayList<String>();
119 ArrayList<String> selectedItems = new ArrayList<String>();
120 if (analyticsTreeRootElement != null) {
121 List<Node<DocumentTreeNode, String>> analyticTreeList = analyticsTreeRootElement.getChildren();
122 for (Node<DocumentTreeNode, String> bibNode : analyticTreeList) {
123 documentTreeNode = bibNode.getData();
124 LOG.info("Analytic-bibId-->" + bibNode.getNodeType());
125 LOG.info("Analytic-bibNodeTitle-->" + bibNode.getData().getTitle());
126 LOG.info("Analytic-bibNodeSelected-->" + documentTreeNode.isSelect());
127 List<Node<DocumentTreeNode, String>> holdingsList = bibNode.getChildren();
128 for (Node<DocumentTreeNode, String> holdingsNode : holdingsList) {
129 documentTreeNode = holdingsNode.getData();
130 LOG.info("Analytic-holdingsId-->" + holdingsNode.getNodeType());
131 LOG.info("Analytic-holdingsNodeTitle-->" + holdingsNode.getData().getTitle());
132 LOG.info("Analytic-holdingsNodeSelected-->" + documentTreeNode.isSelect());
133 holdings.setId(holdingsNode.getNodeType());
134 holdings.setLocationName(holdingsNode.getNodeLabel());
135 List<Node<DocumentTreeNode, String>> itemList = holdingsNode.getChildren();
136 for (Node<DocumentTreeNode, String> itemNode : itemList) {
137 Item item = new Item();
138 documentTreeNode = itemNode.getData();
139 LOG.info("Analytic-itemId-->" + holdingsNode.getNodeType());
140 LOG.info("Analytic-itemNodeTitle-->" + holdingsNode.getData().getTitle());
141 LOG.info("Analytic-itemNodeSelected-->" + documentTreeNode.isSelect());
142 if (documentTreeNode.isSelect()) {
143 item.setId(itemNode.getNodeType());
144 item.setCallNumber(itemNode.getNodeLabel());
145 items.add(item);
146 setSelectedItemsFromTree2.add(itemNode.getNodeType());
147 selectedItems.add(itemNode.getNodeType());
148 selectedItemsList.add(itemNode.getNodeType());
149 documentTreeNode.setSelect(true);
150 }
151 }
152 analyticsForm.setSelectedItems(selectedItems);
153 analyticsForm.setSelectedItemsFromTree2(setSelectedItemsFromTree2);
154 analyticsForm.setSelectedItemsList(selectedItemsList);
155 }
156 }
157 }
158 }
159
160 public void getAnalyticsSummaryByItemId(AnalyticsForm analyticsForm, String itemId) throws Exception {
161 Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(itemId);
162 if (item != null) {
163 if (!CollectionUtils.isEmpty(item.getHoldings())) {
164 for (Holdings holdings : item.getHoldings()) {
165 if (holdings.isSeries()) {
166 getAnalyticsSummaryByHoldingsId(analyticsForm, holdings.getId());
167 }
168 }
169 }
170 }
171 }
172
173 public void getAnalyticsSummaryByHoldingsId(AnalyticsForm analyticsForm, String holdingsId) throws Exception {
174 DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree();
175 Node<DocumentTreeNode, String> seriesRootNode = null;
176 Node<DocumentTreeNode, String> analyticRootNode = null;
177 List<String> bibUuidList = new ArrayList<String>();
178 List<String> holdingsUuidList = new ArrayList<String>();
179 HoldingsTree holdingsTree = getDocstoreClientLocator().getDocstoreClient().retrieveHoldingsTree(holdingsId);
180 if (holdingsTree.getHoldings().isSeries()) {
181 bibUuidList.add(holdingsTree.getHoldings().getBib().getId());
182 seriesRootNode = documentSelectionTree.add(bibUuidList, org.kuali.ole.docstore.model.enums.DocType.BIB.getDescription(), true);
183 analyticsForm.getLeftTree().setRootElement(seriesRootNode);
184 analyticsForm.setShowSeriesTree(true);
185 bibUuidList.clear();
186 for (Item item : holdingsTree.getItems()) {
187 if (!holdingsId.equalsIgnoreCase(item.getHolding().getId())) {
188 if (!holdingsUuidList.contains(item.getHolding().getId())) {
189 if (!bibUuidList.contains(item.getHolding().getBib().getId())) {
190 bibUuidList.add(item.getHolding().getBib().getId());
191 }
192 holdingsUuidList.add(item.getHolding().getId());
193 }
194 }
195 }
196 if (!CollectionUtils.isEmpty(bibUuidList)) {
197 DocumentSelectionTree documentSelectionTree1 = new DocumentSelectionTree();
198 analyticRootNode = documentSelectionTree1.add(bibUuidList, org.kuali.ole.docstore.model.enums.DocType.BIB.getDescription(), true);
199 if (analyticRootNode != null) {
200 analyticsForm.getRightTree().setRootElement(analyticRootNode);
201 analyticsForm.setShowAnalyticsTree(true);
202 }
203 }
204 } else {
205 analyticsForm.setShowSeriesTree(false);
206 analyticsForm.setShowAnalyticsTree(false);
207 }
208 }
209
210 public Boolean validateInputForAnalytics(AnalyticsForm analyticsForm, String createOrBreak) throws Exception {
211 DocumentTreeNode documentTreeNode;
212 boolean isSeriesBibSelected = false, isSeriesHoldingsSelected = false, isSeriesItemSelected = false;
213 boolean isAnalyticBibSelected = false, isAnalyticHoldingsSelected = false, isAnalyticItemSelected = false;
214 Tree<DocumentTreeNode, String> leftTree = analyticsForm.getLeftTree();
215 Tree<DocumentTreeNode, String> rightTree = analyticsForm.getRightTree();
216 Node<DocumentTreeNode, String> leftTreeRootElement = leftTree.getRootElement();
217 Node<DocumentTreeNode, String> rightTreeRootElement = rightTree.getRootElement();
218
219 if (leftTreeRootElement == null && rightTreeRootElement == null) {
220 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_SERIES_AND_ANALYTICS);
221 } else if (leftTreeRootElement != null && rightTreeRootElement == null) {
222 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_ANALYTICS);
223 } else if (leftTreeRootElement == null && rightTreeRootElement != null) {
224 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_SERIES);
225 } else if (leftTreeRootElement != null && rightTreeRootElement != null) {
226 List<Node<DocumentTreeNode, String>> leftList = leftTree.getRootElement().getChildren();
227 List<Node<DocumentTreeNode, String>> rightList = rightTree.getRootElement().getChildren();
228 if (CollectionUtils.isEmpty(leftList) && CollectionUtils.isEmpty(rightList)) {
229 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_SERIES_AND_ANALYTICS);
230 } else if (!CollectionUtils.isEmpty(leftList) && CollectionUtils.isEmpty(rightList)) {
231 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_ANALYTICS);
232 } else if (CollectionUtils.isEmpty(leftList) && !CollectionUtils.isEmpty(rightList)) {
233 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_SERIES);
234 } else if (!CollectionUtils.isEmpty(leftList) && !CollectionUtils.isEmpty(rightList)) {
235 if (createOrBreak.equalsIgnoreCase(OLEConstants.CREATE_ANALYTICS)) {
236 for (Node<DocumentTreeNode, String> bibNode : leftList) {
237 documentTreeNode = bibNode.getData();
238 LOG.info("bibNode-->" + documentTreeNode.isSelect());
239 if (documentTreeNode.isSelect()) {
240 isSeriesBibSelected = true;
241 }
242 List<Node<DocumentTreeNode, String>> holdingsList = bibNode.getChildren();
243 for (Node<DocumentTreeNode, String> holdingsNode : holdingsList) {
244 documentTreeNode = holdingsNode.getData();
245 LOG.info("holdingsNode-->" + documentTreeNode.isSelect());
246 if (documentTreeNode.isSelect()) {
247 isSeriesHoldingsSelected = true;
248 }
249 List<Node<DocumentTreeNode, String>> itemList = holdingsNode.getChildren();
250 for (Node<DocumentTreeNode, String> itemNode : itemList) {
251 documentTreeNode = itemNode.getData();
252 LOG.info("itemNode-->" + documentTreeNode.isSelect());
253 if (documentTreeNode.isSelect()) {
254 isSeriesItemSelected = true;
255 }
256 }
257 }
258 }
259 if (isSeriesBibSelected || isSeriesItemSelected) {
260 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_ONLY_HOLDINGS);
261 } else if (!isSeriesHoldingsSelected) {
262 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_ONE_HOLDINGS);
263 }
264 }
265
266 BibTree bibTree = null;
267 List<String> analyticItemIds = new ArrayList<>();
268 if (createOrBreak.equalsIgnoreCase(OLEConstants.BREAK_ANALYTICS)) {
269 String bibId = null;
270 bibId = leftList.get(0).getNodeType();
271 bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId);
272 analyticItemIds = getAnalyticItemIdsByBibTree(bibTree);
273 }
274
275 StringBuffer itemLabelsBuffer = new StringBuffer();
276 for (Node<DocumentTreeNode, String> bibNode : rightList) {
277 documentTreeNode = bibNode.getData();
278 LOG.info("bibNode-->" + documentTreeNode.isSelect());
279 if (documentTreeNode.isSelect()) {
280 isAnalyticBibSelected = true;
281 }
282 List<Node<DocumentTreeNode, String>> holdingsList = bibNode.getChildren();
283 for (Node<DocumentTreeNode, String> holdingsNode : holdingsList) {
284 documentTreeNode = holdingsNode.getData();
285 LOG.info("holdingsNode-->" + documentTreeNode.isSelect());
286 if (documentTreeNode.isSelect()) {
287 isAnalyticHoldingsSelected = true;
288 }
289 List<Node<DocumentTreeNode, String>> itemList = holdingsNode.getChildren();
290 for (Node<DocumentTreeNode, String> itemNode : itemList) {
291 documentTreeNode = itemNode.getData();
292 LOG.info("itemNode-->" + documentTreeNode.isSelect());
293 if (documentTreeNode.isSelect()) {
294 isAnalyticItemSelected = true;
295 if (createOrBreak.equalsIgnoreCase(OLEConstants.BREAK_ANALYTICS)) {
296 String itemLabel = validateAnalyticRelatedItem(analyticItemIds, itemNode);
297 if (StringUtils.isNotBlank(itemLabel)) {
298 if (StringUtils.isBlank(itemLabelsBuffer.toString())) {
299 itemLabelsBuffer.append("\n");
300 }
301 itemLabelsBuffer.append(itemLabel).append("\n");
302 }
303 } else {
304 if (isAnalytic(itemNode.getNodeType())) {
305 itemLabelsBuffer.append(itemNode.getNodeLabel());
306 }
307 }
308 }
309 }
310 }
311 }
312 if (isAnalyticBibSelected || isAnalyticHoldingsSelected) {
313 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_ONLY_ITEMS);
314 } else if (!isAnalyticItemSelected) {
315 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_ONE_ITEM);
316 } else if (StringUtils.isNotBlank(itemLabelsBuffer.toString()) && !isAnalyticBibSelected && !isAnalyticHoldingsSelected) {
317 if (createOrBreak.equalsIgnoreCase(OLEConstants.BREAK_ANALYTICS)) {
318 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_ITEM_NOT_ANALYTIC);
319 } else {
320 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_SELECT_ITEM_IN_ANALYTIC_RELATION);
321 }
322 }
323 }
324 }
325
326 int errorCount = GlobalVariables.getMessageMap().getErrorCount();
327 if (errorCount > 0) {
328 return false;
329 } else {
330 return true;
331 }
332 }
333
334 public boolean isAnalytic(String itemId) throws Exception {
335 boolean isAnalytic = false;
336 if (StringUtils.isNotBlank(itemId)) {
337 Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(itemId);
338 if (item.isAnalytic()) {
339 isAnalytic = true;
340 }
341 }
342 return isAnalytic;
343 }
344
345 public List<String> getAnalyticItemIdsByBibTree(BibTree bibTree) {
346 List<String> analyticItemIdList = new ArrayList<>();
347 if (bibTree != null) {
348 if (!CollectionUtils.isEmpty(bibTree.getHoldingsTrees())) {
349 for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
350 if (!CollectionUtils.isEmpty(holdingsTree.getItems())) {
351 for (Item item : holdingsTree.getItems()) {
352 if (item.isAnalytic()) {
353 if (!analyticItemIdList.contains(item.getId())) {
354 analyticItemIdList.add(item.getId());
355 }
356 }
357 }
358 }
359 }
360 }
361 }
362 return analyticItemIdList;
363 }
364
365 public String validateAnalyticRelatedItem(List<String> analyticItemIds, Node<DocumentTreeNode, String> item) {
366 String itemLabel = null;
367 if (!CollectionUtils.isEmpty(analyticItemIds)) {
368 if (!analyticItemIds.contains(item.getNodeType())) {
369 itemLabel = item.getNodeLabel();
370 }
371 }
372 return itemLabel;
373 }
374
375
376 }