View Javadoc

1   package org.kuali.ole.describe.controller;
2   
3   import org.apache.commons.httpclient.HttpClient;
4   import org.apache.commons.httpclient.methods.GetMethod;
5   import org.apache.commons.io.IOUtils;
6   import org.apache.log4j.Logger;
7   import org.apache.solr.client.solrj.SolrServerException;
8   import org.kuali.ole.PropertyUtil;
9   import org.kuali.ole.describe.bo.OleWorkBibDocument;
10  import org.kuali.ole.describe.form.WorkbenchForm;
11  import org.kuali.ole.docstore.discovery.model.SearchCondition;
12  import org.kuali.ole.docstore.discovery.model.SearchParams;
13  import org.kuali.ole.docstore.discovery.service.QueryService;
14  import org.kuali.ole.docstore.discovery.service.QueryServiceImpl;
15  import org.kuali.ole.docstore.model.bo.WorkBibDocument;
16  import org.kuali.ole.docstore.model.bo.WorkHoldingsDocument;
17  import org.kuali.ole.docstore.model.bo.WorkItemDocument;
18  import org.kuali.ole.docstore.model.enums.DocCategory;
19  import org.kuali.ole.docstore.model.enums.DocFormat;
20  import org.kuali.ole.docstore.model.enums.DocType;
21  import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
22  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
23  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
24  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
25  import org.kuali.rice.krad.uif.UifParameters;
26  import org.kuali.rice.krad.web.controller.UifControllerBase;
27  import org.kuali.rice.krad.web.form.UifFormBase;
28  import org.springframework.stereotype.Controller;
29  import org.springframework.validation.BindingResult;
30  import org.springframework.web.bind.annotation.ModelAttribute;
31  import org.springframework.web.bind.annotation.RequestMapping;
32  import org.springframework.web.servlet.ModelAndView;
33  
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * Created with IntelliJ IDEA.
44   * User: PP7788
45   * Date: 11/21/12
46   * Time: 2:45 PM
47   * To change this template use File | Settings | File Templates.
48   */
49  
50  @Controller
51  @RequestMapping(value = "/describeworkbenchcontroller")
52  public class WorkbenchController extends UifControllerBase {
53  
54      private static final Logger LOG = Logger.getLogger(WorkbenchController.class);
55  
56      @Override
57      protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
58          return new WorkbenchForm();
59      }
60  
61      /**
62       * This method converts UifFormBase to WorkbenchForm
63       *
64       * @param form
65       * @param result
66       * @param request
67       * @param response
68       * @return ModelAndView
69       */
70      @Override
71      @RequestMapping(params = "methodToCall=start")
72      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
73                                HttpServletRequest request, HttpServletResponse response) {
74          LOG.debug("Inside the workbenchForm start method");
75          WorkbenchForm workbenchForm = (WorkbenchForm) form;
76          if (workbenchForm.getSearchParams().getDocType() == null) {
77              workbenchForm.getSearchParams().setDocType(DocType.BIB.getDescription());
78          }
79          workbenchForm.setWorkBibDocumentList(null);
80          workbenchForm.setWorkHoldingsDocumentList(null);
81          workbenchForm.setWorkItemDocumentList(null);
82           workbenchForm.setShowRequestXML(false);
83          return super.navigate(workbenchForm, result, request, response);
84      }
85  
86      /**
87       * Used for Test-case
88       *
89       * @param result
90       * @param request
91       * @param response
92       * @param workbenchForm
93       * @return ModelAndView
94       */
95      protected ModelAndView callSuper(BindingResult result, HttpServletRequest request, HttpServletResponse response, WorkbenchForm workbenchForm) {
96          return super.navigate(workbenchForm, result, request, response);
97      }
98  
99      /**
100      * @param form
101      * @param result
102      * @param request
103      * @param response
104      * @return
105      */
106     @RequestMapping(params = "methodToCall=search")
107     public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
108                                HttpServletRequest request, HttpServletResponse response) {
109         LOG.info("Inside Search Method");
110         WorkbenchForm workbenchForm = (WorkbenchForm) form;
111         workbenchForm.setShowRequestXML(false);
112         try {
113             QueryService queryService = QueryServiceImpl.getInstance();
114             SearchParams searchParams = workbenchForm.getSearchParams();
115             if (workbenchForm.getSearchParams().getDocType().equalsIgnoreCase(DocType.BIB.getDescription())) {
116                 workbenchForm.getSearchParams().setDocCategory(DocCategory.WORK.getCode());
117                 workbenchForm.getSearchParams().setDocFormat("ALL");
118                 workbenchForm.setShowExport(true);
119                 List<OleWorkBibDocument> oleWorkBibDocumentList = new ArrayList<OleWorkBibDocument>();
120                 List<WorkBibDocument> workBibDocumentList = queryService.getBibDocuments(searchParams);
121                 for (WorkBibDocument workBibDocument : workBibDocumentList) {
122                     OleWorkBibDocument oleWorkBibDocument = new OleWorkBibDocument();
123                     oleWorkBibDocument.setDocFormat(workBibDocument.getDocFormat());
124                     oleWorkBibDocument.setId(workBibDocument.getId());
125                     oleWorkBibDocument.setTitle(workBibDocument.getTitle());
126                     oleWorkBibDocument.setAuthor(workBibDocument.getAuthor());
127                     oleWorkBibDocument.setPublicationDate(workBibDocument.getPublicationDate());
128                     oleWorkBibDocumentList.add(oleWorkBibDocument);
129                 }
130                 workbenchForm.setWorkBibDocumentList(oleWorkBibDocumentList);
131                 request.getSession().setAttribute("oleWorkBibDocumentList",oleWorkBibDocumentList);
132                 // request.setAttribute("oleWorkBibDocumentList",oleWorkBibDocumentList);
133             }
134             if (workbenchForm.getSearchParams().getDocType().equalsIgnoreCase(DocType.HOLDINGS.getCode())) {
135                 workbenchForm.getSearchParams().setDocCategory(DocCategory.WORK.getCode());
136                 workbenchForm.getSearchParams().setDocFormat(DocFormat.OLEML.getCode());
137                 List<WorkHoldingsDocument> workHoldingsDocuments = queryService.getHoldingDocuments(searchParams);
138                 workbenchForm.setWorkHoldingsDocumentList(workHoldingsDocuments);
139             }
140             if (workbenchForm.getSearchParams().getDocType().equalsIgnoreCase(DocType.ITEM.getCode())) {
141                 workbenchForm.getSearchParams().setDocCategory(DocCategory.WORK.getCode());
142                 workbenchForm.getSearchParams().setDocFormat(DocFormat.OLEML.getCode());
143                 List<WorkItemDocument> workItemDocuments = queryService.getItemDocuments(searchParams);
144                 workbenchForm.setWorkItemDocumentList(workItemDocuments);
145             }
146         } catch (Exception e) {
147             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
148         }
149 //        return getUIFModelAndView(workbenchForm);
150         return navigate(workbenchForm, result, request, response);
151     }
152 
153     /**
154      * @param form
155      * @param result
156      * @param request
157      * @param response
158      * @return
159      */
160     @RequestMapping(params = "methodToCall=clearSearch")
161     public ModelAndView clearSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
162                                     HttpServletRequest request, HttpServletResponse response) {
163         LOG.info("Inside clearSearch Method");
164         WorkbenchForm workbenchForm = (WorkbenchForm) form;
165         workbenchForm.setSearchParams(new SearchParams());
166         List<SearchCondition> searchConditions = workbenchForm.getSearchParams().getSearchFieldsList();
167         searchConditions.add(new SearchCondition());
168         searchConditions.add(new SearchCondition());
169         workbenchForm.getSearchParams().setDocType("bibliographic");
170         workbenchForm.setWorkBibDocumentList(null);
171         workbenchForm.setWorkHoldingsDocumentList(null);
172         workbenchForm.setWorkItemDocumentList(null);
173         return getUIFModelAndView(workbenchForm);
174 //        return navigate(workbenchForm, result, request, response);
175     }
176 
177     @RequestMapping(params = "methodToCall=select")
178     public ModelAndView select(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
179                                HttpServletRequest request, HttpServletResponse response) {
180         WorkbenchForm workbenchForm = (WorkbenchForm) form;
181         int index = Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
182         String selectedRecord = workbenchForm.getWorkBibDocumentList().get(index).getId();
183         LOG.info("selectedRecord--->" + selectedRecord);
184         return super.navigate(workbenchForm, result, request, response);
185     }
186 
187     @RequestMapping(params = "methodToCall=selectRecords")
188     public ModelAndView selectRecords(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
189                                       HttpServletRequest request, HttpServletResponse response) {
190         WorkbenchForm workbenchForm = (WorkbenchForm) form;
191         List<String> selectedRecordIds = new ArrayList<String>();
192         List<OleWorkBibDocument> oleWorkBibDocuments = workbenchForm.getWorkBibDocumentList();
193         for (OleWorkBibDocument oleWorkBibDocument : oleWorkBibDocuments) {
194             if (oleWorkBibDocument.isSelect()) {
195                 selectedRecordIds.add(oleWorkBibDocument.getId());
196             }
197         }
198         LOG.info("selectedRecords--->" + selectedRecordIds);
199         return getUIFModelAndView(workbenchForm);
200     }
201 
202     /**
203      * Exports the selected bib records as request XML.
204      * @param form
205      * @param result
206      * @param request
207      * @param response
208      * @return
209      * @throws SolrServerException
210      * @throws IOException
211      * @throws ServletException
212      */
213     @RequestMapping(params = "methodToCall=export")
214     public ModelAndView export(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
215                                       HttpServletRequest request, HttpServletResponse response) throws SolrServerException, IOException, ServletException {
216         WorkbenchForm workbenchForm = (WorkbenchForm) form;
217         workbenchForm.setShowRequestXML(true);
218         QueryService queryService = QueryServiceImpl.getInstance();
219 
220         Request docStoreRequest = new Request();
221         docStoreRequest.setOperation(Request.Operation.ingest.toString());
222         docStoreRequest.setUser("ole-khuntley");
223         List<String> selectedRecordIds = new ArrayList<String>();
224         List<OleWorkBibDocument> oleWorkBibDocuments = workbenchForm.getWorkBibDocumentList();
225         for (OleWorkBibDocument oleWorkBibDocument : oleWorkBibDocuments) {
226             if (oleWorkBibDocument.isSelect()) {
227                 selectedRecordIds.add(oleWorkBibDocument.getId());
228             }
229         }
230         LOG.info("selectedRecords--->" + selectedRecordIds);
231         List<RequestDocument> requestDocumentList = new ArrayList<RequestDocument>();
232         for (String selectedRecordId : selectedRecordIds) {
233             RequestDocument requestDocument = new RequestDocument();
234             requestDocument.setUuid(selectedRecordId);
235             requestDocument.setId(selectedRecordId);
236             requestDocumentList.add(requestDocument);
237         }
238         List<RequestDocument> checkOutReqDocs = new ArrayList<RequestDocument>();
239         for (int i = 0; i < requestDocumentList.size(); i++) {
240             String id = requestDocumentList.get(i).getId();
241             RequestDocument requestDocument = new RequestDocument();
242             requestDocument.setCategory(DocCategory.WORK.getCode());
243             requestDocument.setType(DocType.BIB.getDescription());
244             requestDocument.setFormat(DocFormat.MARC.getCode());
245             requestDocument.setId(requestDocumentList.get(i).getId());
246             List<String> instanceIdList = new ArrayList<String>();
247             instanceIdList = queryService.queryForInstances(id);
248             LOG.debug("instance id list-->" + instanceIdList);
249             List<RequestDocument> linkedRequestDocumentList = new ArrayList<RequestDocument>();
250             for (String instanceId : instanceIdList) {
251                 RequestDocument linkedReqDocument = new RequestDocument();
252                 linkedReqDocument.setId(instanceId);
253                 linkedReqDocument.setCategory(DocCategory.WORK.getCode());
254                 linkedReqDocument.setType(DocType.INSTANCE.getCode());
255                 linkedReqDocument.setFormat(DocFormat.OLEML.getCode());
256                 String instanceContent = getContentFromDocStore(instanceId);
257                 Content content = new Content();
258                 if (instanceContent.contains("Failure")) {
259                     instanceContent = new String();
260                     content.setContent(instanceContent);
261                 } else {
262                     content.setContent(instanceContent);
263                 }
264                 linkedReqDocument.setContent(content);
265                 linkedRequestDocumentList.add(linkedReqDocument);
266             }
267             requestDocument.setLinkedRequestDocuments(linkedRequestDocumentList);
268             String bibContent = getContentFromDocStore(requestDocumentList.get(i).getId());
269             Content content = new Content();
270             if (bibContent.contains("Failure")) {
271                 bibContent = new String();
272                 content.setContent(bibContent);
273             } else {
274                 content.setContent(bibContent);
275             }
276             content.setContent(bibContent);
277             requestDocument.setContent(content);
278             checkOutReqDocs.add(requestDocument);
279         }
280 
281         docStoreRequest.setRequestDocuments(checkOutReqDocs);
282         workbenchForm.setRequestXMLTextArea(new RequestHandler().toXML(docStoreRequest));
283         return getUIFModelAndView(workbenchForm);
284     }
285 
286 
287     private String getContentFromDocStore(String uuid) throws IOException {
288         String content = null;
289         String restfulUrl = PropertyUtil.getPropertyUtil().getProperty("docstore.restful.url");
290         restfulUrl = restfulUrl.concat("/") + uuid;
291         LOG.info("restful url-->" + restfulUrl);
292         HttpClient client = new HttpClient();
293         GetMethod getMethod = new GetMethod(restfulUrl);
294         client.executeMethod(getMethod);
295         InputStream inputStream = getMethod.getResponseBodyAsStream();
296         content = IOUtils.toString(inputStream);
297         return content;
298     }
299 
300 }