View Javadoc
1   package org.kuali.ole.describe.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.apache.solr.client.solrj.SolrServerException;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.describe.bo.DocumentSelectionTree;
8   import org.kuali.ole.describe.bo.DocumentTreeNode;
9   import org.kuali.ole.describe.form.AnalyticsForm;
10  import org.kuali.ole.describe.form.BoundwithForm;
11  import org.kuali.ole.describe.rule.OLEAnalyticsRule;
12  import org.kuali.ole.docstore.common.document.*;
13  import org.kuali.ole.docstore.common.exception.DocstoreException;
14  import org.kuali.ole.docstore.model.enums.DocType;
15  import org.kuali.rice.core.api.util.tree.Node;
16  import org.kuali.rice.core.api.util.tree.Tree;
17  import org.kuali.rice.krad.util.GlobalVariables;
18  import org.kuali.rice.krad.web.form.UifFormBase;
19  import org.springframework.stereotype.Controller;
20  import org.springframework.util.CollectionUtils;
21  import org.springframework.validation.BindingResult;
22  import org.springframework.web.bind.annotation.ModelAttribute;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import java.util.*;
29  
30  /**
31   * Created with IntelliJ IDEA.
32   * User: rajeshbabuk
33   * Date: 3/3/14
34   * Time: 8:32 PM
35   * To change this template use File | Settings | File Templates.
36   */
37  
38  @Controller
39  @RequestMapping(value = "/analyticsController")
40  public class AnalyticsController extends BoundwithController {
41  
42      private static final Logger LOG = Logger.getLogger(AnalyticsController.class);
43  
44      private OLEAnalyticsRule oleAnalyticsRule;
45  
46      public OLEAnalyticsRule getOleAnalyticsRule() {
47          if (oleAnalyticsRule == null) {
48              oleAnalyticsRule = new OLEAnalyticsRule();
49          }
50          return oleAnalyticsRule;
51      }
52  
53      @Override
54      protected UifFormBase createInitialForm(HttpServletRequest request) {
55          return new AnalyticsForm();
56      }
57  
58      @Override
59      @RequestMapping(params = "methodToCall=start")
60      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
61                                HttpServletRequest request, HttpServletResponse response) {
62          LOG.debug("Inside the AnalyticsForm start method");
63          if (request.getSession().getAttribute("LeftList") != null && request.getSession().getAttribute("RightList") != null) {
64              request.getSession().removeAttribute("LeftList");
65              request.getSession().removeAttribute("RightList");
66          }
67          return super.start(form, result, request, response);
68      }
69  
70      /**
71       * This method creates the analytic relation if the uses clicks on Create Analytics relation button.
72       *
73       * @param form
74       * @param result
75       * @param response
76       * @return
77       * @throws Exception
78       */
79      @RequestMapping(params = "methodToCall=createAnalyticsRelation")
80      public ModelAndView createAnalyticsRelation(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
81                                                  HttpServletRequest request, HttpServletResponse response) throws Exception {
82  
83          AnalyticsForm analyticsForm = (AnalyticsForm) form;
84          Boolean isInputValid = getOleAnalyticsRule().validateInputForAnalytics(analyticsForm, OLEConstants.CREATE_ANALYTICS);
85          if (!isInputValid) {
86              return getUIFModelAndView(analyticsForm);
87          }
88  
89          Tree<DocumentTreeNode, String> seriesTree = analyticsForm.getLeftTree();
90          Node<DocumentTreeNode, String> seriesTreeRootElement = seriesTree.getRootElement();
91          getOleAnalyticsRule().selectCheckedNodesForSeriesTree(analyticsForm, seriesTreeRootElement);
92  
93          Tree<DocumentTreeNode, String> analyticsTree = analyticsForm.getRightTree();
94          Node<DocumentTreeNode, String> analyticsTreeRootElement = analyticsTree.getRootElement();
95          getOleAnalyticsRule().selectCheckedNodesForAnalyticTree(analyticsForm, analyticsTreeRootElement);
96  
97          analyticsForm.getDocumentTreeNode().setReturnCheck(true);
98          addAnalyticRelation(analyticsForm);
99          return super.navigate(analyticsForm, result, request, response);
100     }
101 
102     private void addAnalyticRelation(AnalyticsForm analyticsForm) {
103         try {
104             getDocstoreClientLocator().getDocstoreClient().createAnalyticsRelation(analyticsForm.getSelectedHoldingsFromTree1().get(0), analyticsForm.getSelectedItemsFromTree2());
105             analyticsForm.setShowBoundwithTree(true);
106             List<String> uuidList = new ArrayList<String>();
107             uuidList = analyticsForm.getSelectedBibs();
108             uuidList.add(analyticsForm.getTree1BibId());
109             DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree();
110             Node<DocumentTreeNode, String> rootNode = documentSelectionTree.add(uuidList, DocType.BIB.getDescription(), true);
111             analyticsForm.getBoundwithTree().setRootElement(rootNode);
112             GlobalVariables.getMessageMap().putInfoForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.INFO_ANALYTICS_CREATE_SUCCESS);
113             analyticsForm.setBoundwithTreeLabelText("select");
114         } catch (DocstoreException e) {
115             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, e.getErrorCode());
116         } catch (Exception e) {
117             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_ANALYTICS_CREATE_FAILED, e.getMessage());
118         }
119         analyticsForm.setSelectedHoldings(null);
120     }
121 
122     /**
123      * This method displays Analytics Summary if the uses clicks on Series or Analytic Link.
124      *
125      * @param form
126      * @param result
127      * @param response
128      * @return
129      * @throws Exception
130      */
131     @RequestMapping(params = "methodToCall=showAnalyticsSummary")
132     public ModelAndView showAnalyticsSummary(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
133                                              HttpServletRequest request, HttpServletResponse response) throws Exception {
134         AnalyticsForm analyticsForm = (AnalyticsForm) form;
135         String docType = request.getParameter("docType");
136         String holdingsId = request.getParameter("holdingsId");
137         String itemId = request.getParameter("itemId");
138         if (StringUtils.isNotBlank(holdingsId) && StringUtils.isBlank(itemId) && docType.equalsIgnoreCase(DocType.HOLDINGS.getDescription())) {
139             getOleAnalyticsRule().getAnalyticsSummaryByHoldingsId(analyticsForm, holdingsId);
140         } else if (StringUtils.isBlank(holdingsId) && StringUtils.isNotBlank(itemId) && docType.equalsIgnoreCase(DocType.ITEM.getDescription())) {
141             getOleAnalyticsRule().getAnalyticsSummaryByItemId(analyticsForm, itemId);
142         }
143         return super.navigate(analyticsForm, result, request, response);
144     }
145 
146     /**
147      * This method breaks the analytic relation if the uses clicks on Break Analytics relation button.
148      *
149      * @param form
150      * @param result
151      * @param response
152      * @return
153      * @throws Exception
154      */
155     @RequestMapping(params = "methodToCall=breakAnalyticsRelation")
156     public ModelAndView breakAnalyticsRelation(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
157                                                HttpServletRequest request, HttpServletResponse response) throws Exception {
158         AnalyticsForm analyticsForm = (AnalyticsForm) form;
159 
160         Boolean isInputValid = getOleAnalyticsRule().validateInputForAnalytics(analyticsForm, OLEConstants.BREAK_ANALYTICS);
161         if (!isInputValid) {
162             return getUIFModelAndView(analyticsForm);
163         }
164 
165         String seriesHoldingsId = null;
166         Tree<DocumentTreeNode, String> seriesTree = analyticsForm.getLeftTree();
167         Node<DocumentTreeNode, String> seriesTreeRootElement = seriesTree.getRootElement();
168         //selectCheckedNodesForSeriesTree(analyticsForm, seriesTreeRootElement);
169         List<Node<DocumentTreeNode, String>> seriesBibNode = null;
170         List<Node<DocumentTreeNode, String>> seriesHoldingsNode = null;
171         if (seriesTreeRootElement != null && !CollectionUtils.isEmpty(seriesTreeRootElement.getChildren())) {
172             seriesBibNode = seriesTreeRootElement.getChildren();
173             if (null != seriesBibNode.get(0) && !CollectionUtils.isEmpty(seriesBibNode.get(0).getChildren())) {
174                 seriesHoldingsNode = seriesBibNode.get(0).getChildren();
175                 if (null != seriesHoldingsNode.get(0) && null != seriesHoldingsNode.get(0).getNodeType()) {
176                     seriesHoldingsId = seriesTreeRootElement.getChildren().get(0).getChildren().get(0).getNodeType();
177                 }
178             }
179         }
180         Tree<DocumentTreeNode, String> analyticsTree = analyticsForm.getRightTree();
181         Node<DocumentTreeNode, String> analyticsTreeRootElement = analyticsTree.getRootElement();
182         getOleAnalyticsRule().selectCheckedNodesForAnalyticTree(analyticsForm, analyticsTreeRootElement);
183 
184         analyticsForm.getDocumentTreeNode().setReturnCheck(true);
185         try {
186             getDocstoreClientLocator().getDocstoreClient().breakAnalyticsRelation(seriesHoldingsId, analyticsForm.getSelectedItemsFromTree2());
187             GlobalVariables.getMessageMap().putInfo(OLEConstants.ANALYTICS_SUMMARY_VIEW_PAGE, OLEConstants.INFO_ANALYTICS_BREAK_SUCCESS);
188         } catch (DocstoreException e) {
189             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, e.getErrorCode());
190         } catch (Exception e) {
191             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.ANALYTICS_SELECTION_SECTION, OLEConstants.ERROR_ANALYTICS_BREAK_FAILED, e.getMessage());
192         }
193         return getUIFModelAndView(analyticsForm);
194     }
195 
196     /**
197      * This method refreshes the Series Tree and Analytic Tree if the uses clicks on Refresh Trees button.
198      * @param form
199      * @param result
200      * @param request
201      * @param response
202      * @return
203      */
204     @RequestMapping(params = "methodToCall=refreshTrees")
205     public ModelAndView refreshTrees(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
206                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
207         AnalyticsForm analyticsForm = (AnalyticsForm) form;
208         LOG.info("refreshTrees Method");
209         Map<String, String> uuidLeftList = (Map<String, String>) request.getSession().getAttribute("LeftList");
210         if (!CollectionUtils.isEmpty(uuidLeftList)) {
211             List<String> uuidList = new ArrayList<>(uuidLeftList.values());
212             if (!CollectionUtils.isEmpty(uuidList)) {
213                 BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(uuidList.get(0));
214                 for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
215                     getOleAnalyticsRule().getAnalyticsSummaryByHoldingsId(analyticsForm, holdingsTree.getHoldings().getId());
216                 }
217             }
218         }
219         return navigate(analyticsForm, result, request, response);
220     }
221 
222 }