View Javadoc
1   package org.kuali.ole.describe.controller;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.describe.form.SerialsReceivingRecordForm;
7   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
8   import org.kuali.ole.docstore.common.search.SearchResponse;
9   import org.kuali.ole.docstore.common.search.SearchResult;
10  import org.kuali.ole.docstore.discovery.model.SearchCondition;
11  import org.kuali.ole.docstore.discovery.model.SearchParams;
12  import org.kuali.ole.pojo.OLESerialReceivingRecord;
13  import org.kuali.ole.select.bo.OLESerialReceivingDocument;
14  import org.kuali.ole.service.SerialReceivingSearchService;
15  import org.kuali.ole.service.impl.SerialReceivingSearchServiceImpl;
16  import org.kuali.ole.sys.context.SpringContext;
17  import org.kuali.rice.kim.api.permission.PermissionService;
18  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
19  import org.kuali.rice.krad.util.GlobalVariables;
20  import org.kuali.rice.krad.web.controller.UifControllerBase;
21  import org.kuali.rice.krad.web.form.UifFormBase;
22  import org.springframework.stereotype.Controller;
23  import org.springframework.validation.BindingResult;
24  import org.springframework.web.bind.annotation.ModelAttribute;
25  import org.springframework.web.bind.annotation.RequestMapping;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import java.util.ArrayList;
31  import java.util.HashSet;
32  import java.util.List;
33  import java.util.Set;
34  
35  /**
36   * Created with IntelliJ IDEA.
37   * User: sundarr
38   * Date: 7/2/13
39   * Time: 7:24 PM
40   * To change this template use File | Settings | File Templates.
41   */
42  @Controller
43  @RequestMapping(value = "/serialsReceivingRecordController")
44  public class SerialsReceivingRecordSearchController extends UifControllerBase {
45      private static final Logger LOG = Logger.getLogger(SerialsReceivingRecordSearchController.class);
46      private DocstoreClientLocator docstoreClientLocator;
47      private int totalRecCount;
48      private int start;
49      private int pageSize;
50  
51  
52      public boolean getPreviousFlag() {
53          if (this.start == 0)
54              return false;
55          return true;
56      }
57  
58      public boolean getNextFlag() {
59          if (this.start + this.pageSize < this.totalRecCount)
60              return true;
61          return false;
62      }
63  
64  
65      public String getPageShowEntries() {
66          return "Showing " + ((this.start == 0) ? 1 : this.start + 1) + " to "
67                  + (((this.start + this.pageSize) > this.totalRecCount) ? this.totalRecCount : (this.start + this.pageSize))
68                  + " of " + this.totalRecCount + " entries";
69      }
70  
71  
72      public DocstoreClientLocator getDocstoreClientLocator() {
73  
74          if (docstoreClientLocator == null) {
75              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
76          }
77          return docstoreClientLocator;
78      }
79  
80  
81      @Override
82      protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
83          return new SerialsReceivingRecordForm();
84      }
85  
86      /**
87       * This method converts UifFormBase to SerialsReceivingRecordForm
88       *
89       * @param form
90       * @param result
91       * @param request
92       * @param response
93       * @return ModelAndView
94       */
95      @Override
96      @RequestMapping(params = "methodToCall=start")
97      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
98                                HttpServletRequest request, HttpServletResponse response) {
99          LOG.debug("Inside the serialsReceivingRecordForm start method");
100         SerialsReceivingRecordForm serialsReceivingRecordForm = (SerialsReceivingRecordForm) form;
101         return super.navigate(serialsReceivingRecordForm, result, request, response);
102     }
103 
104 
105     @RequestMapping(params = "methodToCall=search")
106     public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
107                                HttpServletRequest request, HttpServletResponse response) throws Exception {
108         LOG.info("Inside Search Method");
109         SerialsReceivingRecordForm serialsReceivingRecordForm = (SerialsReceivingRecordForm) form;
110         PermissionService service = KimApiServiceLocator.getPermissionService();
111         boolean isAuthorized = service.hasPermission(GlobalVariables.getUserSession().getPrincipalId(), "OLE-SELECT", "SERIAL_RECEIVING_SEARCH");
112         if (!isAuthorized) {
113             serialsReceivingRecordForm.setErrorAuthorisedUserMessage(OLEConstants.OLE_SERIALS_RECEIVING_NOT_AUTHORIZED);
114             return navigate(serialsReceivingRecordForm, result, request, response);
115         }
116         List<SearchCondition> searchConditionList = serialsReceivingRecordForm.getSearchParams().getSearchFieldsList();
117         String title = "";
118         String issn = "";
119         String serialRecordNo = "";
120         String localIdentifier = "";
121         String poId = "";
122         for (SearchCondition sc : searchConditionList) {
123             if (sc.getDocField().equalsIgnoreCase(OLEConstants.TITLE_SEARCH) && sc.getSearchText() != null && !sc.getSearchText().isEmpty()) {
124                 title = sc.getSearchText() != null ? sc.getSearchText() : "";
125             } else if (sc.getDocField().equalsIgnoreCase(OLEConstants.ISSN_SEARCH) && sc.getSearchText() != null && !sc.getSearchText().isEmpty()) {
126                 issn = sc.getSearchText() != null ? sc.getSearchText() : "";
127             } else if (sc.getDocField().equalsIgnoreCase(OLEConstants.SERIAL_SEARCH) && sc.getSearchText() != null && !sc.getSearchText().isEmpty()) {
128                 serialRecordNo = sc.getSearchText() != null ? sc.getSearchText() : "";
129             } else if (sc.getDocField().equalsIgnoreCase(OLEConstants.LOCALID_SEARCH) && sc.getSearchText() != null && !sc.getSearchText().isEmpty()) {
130                 localIdentifier = sc.getSearchText() != null ? sc.getSearchText() : "";
131             } else if (sc.getDocField().equalsIgnoreCase(OLEConstants.PO_SEARCH) && sc.getSearchText() != null && !sc.getSearchText().isEmpty()) {
132                 poId = sc.getSearchText() != null ? sc.getSearchText() : "";
133             }
134         }
135         if (serialsReceivingRecordForm.getSearchFlag().equalsIgnoreCase("search")) {
136             this.start = 0;
137         }
138         this.pageSize = Integer.parseInt(serialsReceivingRecordForm.getSearchLimit());
139 
140         List<OLESerialReceivingRecord> oleSerialReceivingRecordList = new ArrayList<>();
141         SerialReceivingSearchService serialReceivingSearchService = new SerialReceivingSearchServiceImpl();
142         if (title.isEmpty() && issn.isEmpty() && poId.isEmpty() && localIdentifier.isEmpty() && serialRecordNo.isEmpty()) {
143             SearchResponse searchResponse = serialReceivingSearchService.holdingSearch(this.start, Integer.parseInt(serialsReceivingRecordForm.getSearchLimit()),serialsReceivingRecordForm.getSortOrder());
144             this.totalRecCount = searchResponse.getTotalRecordCount();
145             for (SearchResult searchResult : searchResponse.getSearchResults()) {
146                 oleSerialReceivingRecordList.add(serialReceivingSearchService.getSerialRecord(searchResult));
147             }
148             serialsReceivingRecordForm.setPaginationFlag("true");
149             serialsReceivingRecordForm.setPageResultDisplay(getPageShowEntries());
150         } else if ((!title.isEmpty() || !issn.isEmpty() || !localIdentifier.isEmpty()) && (poId.isEmpty() && serialRecordNo.isEmpty())) {
151             SearchResponse searchResponse = serialReceivingSearchService.searchDataFromDocstore(this.start, Integer.parseInt(serialsReceivingRecordForm.getSearchLimit()),
152                     new HashSet<String>(), title, issn, localIdentifier,serialsReceivingRecordForm.getSortOrder());
153             this.totalRecCount = searchResponse.getTotalRecordCount();
154             for (SearchResult searchResult : searchResponse.getSearchResults()) {
155                 oleSerialReceivingRecordList.add(serialReceivingSearchService.getSerialRecord(searchResult));
156             }
157             serialsReceivingRecordForm.setPaginationFlag("true");
158             serialsReceivingRecordForm.setPageResultDisplay(getPageShowEntries());
159         } else if ((!poId.isEmpty() && !poId.contains("*") && serialRecordNo.isEmpty() && StringUtils.isNumeric(poId))) {
160             Set<String> instanceIds = serialReceivingSearchService.getInstanceIdList(poId);
161             if ((instanceIds != null && instanceIds.size() > 0)) {
162                 SearchResponse searchResponse = serialReceivingSearchService.searchDataFromDocstore(this.start, Integer.parseInt(serialsReceivingRecordForm.getSearchLimit()),
163                         instanceIds, title, issn, localIdentifier,serialsReceivingRecordForm.getSortOrder());
164                 this.totalRecCount = searchResponse.getTotalRecordCount();
165                 for (SearchResult searchResult : searchResponse.getSearchResults()) {
166                     oleSerialReceivingRecordList.add(serialReceivingSearchService.getSerialRecord(searchResult));
167                 }
168                 serialsReceivingRecordForm.setPaginationFlag("true");
169                 serialsReceivingRecordForm.setPageResultDisplay(getPageShowEntries());
170             }
171         } else if (!serialRecordNo.isEmpty() && !serialRecordNo.contains("*") && StringUtils.isNumeric(serialRecordNo)) {
172             List<OLESerialReceivingDocument> oleSerialReceivingDocuments = serialReceivingSearchService.getOleSerialReceivingDocuments(serialsReceivingRecordForm);
173             if (oleSerialReceivingDocuments != null && oleSerialReceivingDocuments.size() > 0) {
174                 if (!title.isEmpty() || !issn.isEmpty()) {
175                     Set<String> instanceIds = new HashSet<>();
176                     for (OLESerialReceivingDocument oleSerialReceivingDocument : oleSerialReceivingDocuments) {
177                         instanceIds.add(oleSerialReceivingDocument.getInstanceId());
178                     }
179                     SearchResponse searchResponse = serialReceivingSearchService.searchDataFromDocstore(0, Integer.parseInt(serialsReceivingRecordForm.getSearchLimit()),
180                             instanceIds, title, issn, localIdentifier,serialsReceivingRecordForm.getSortOrder());
181                     this.totalRecCount = searchResponse.getTotalRecordCount();
182                     for (SearchResult searchResult : searchResponse.getSearchResults()) {
183                         oleSerialReceivingRecordList.add(serialReceivingSearchService.getSerialRecord(searchResult));
184                     }
185                     serialsReceivingRecordForm.setPaginationFlag("true");
186                     serialsReceivingRecordForm.setPageResultDisplay(getPageShowEntries());
187                 } else {
188                     oleSerialReceivingRecordList.addAll(serialReceivingSearchService.getOleSerialReceivingList(oleSerialReceivingDocuments));
189                 }
190             }
191         }
192         serialsReceivingRecordForm.setPreviousFlag(getPreviousFlag());
193         serialsReceivingRecordForm.setNextFlag(getNextFlag());
194         if (oleSerialReceivingRecordList != null && oleSerialReceivingRecordList.size() > 0) {
195             //serialsReceivingRecordForm.setOleSerialReceivingRecordList(oleSerialReceivingRecordList.size() > pageSize ? oleSerialReceivingRecordList.subList(0, pageSize) : oleSerialReceivingRecordList);
196             serialsReceivingRecordForm.setOleSerialReceivingRecordList(oleSerialReceivingRecordList);
197             //serialsReceivingRecordForm.setTempSerialReceivingRecordList(oleSerialReceivingRecordList);
198             serialsReceivingRecordForm.setPageResultDisplay(getPageShowEntries());
199         }
200         if (oleSerialReceivingRecordList.size() == 0) {
201             serialsReceivingRecordForm.setOleSerialReceivingRecordList(null);
202             serialsReceivingRecordForm.setTempSerialReceivingRecordList(null);
203             serialsReceivingRecordForm.setErrorAuthorisedUserMessage(OLEConstants.OLE_SERIALS_RECEIVING_NO_RECORD);
204         }
205         return navigate(serialsReceivingRecordForm, result, request, response);
206     }
207 
208 
209     /**
210      * @param form
211      * @param result
212      * @param request
213      * @param response
214      * @return
215      */
216     @RequestMapping(params = "methodToCall=clearSearch")
217     public ModelAndView clearSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
218                                     HttpServletRequest request, HttpServletResponse response) {
219         LOG.info("Inside clearSearch Method");
220         SerialsReceivingRecordForm serialsReceivingRecordForm = (SerialsReceivingRecordForm) form;
221         serialsReceivingRecordForm.setSearchParams(new SearchParams());
222         List<SearchCondition> searchConditions = serialsReceivingRecordForm.getSearchParams().getSearchFieldsList();
223         SearchCondition title = new SearchCondition();
224         title.setDocField(OLEConstants.TITLE_SEARCH);
225         SearchCondition issn = new SearchCondition();
226         issn.setDocField(OLEConstants.ISSN_SEARCH);
227         SearchCondition serialId = new SearchCondition();
228         serialId.setDocField(OLEConstants.SERIAL_SEARCH);
229         SearchCondition bibId = new SearchCondition();
230         bibId.setDocField(OLEConstants.LOCALID_SEARCH);
231         SearchCondition poId = new SearchCondition();
232         poId.setDocField(OLEConstants.PO_SEARCH);
233         searchConditions.add(title);
234         searchConditions.add(issn);
235         searchConditions.add(serialId);
236         searchConditions.add(bibId);
237         searchConditions.add(poId);
238         serialsReceivingRecordForm.setOleSerialReceivingRecordList(null);
239         serialsReceivingRecordForm.setTempSerialReceivingRecordList(null);
240         serialsReceivingRecordForm.setErrorAuthorisedUserMessage("");
241         return getUIFModelAndView(serialsReceivingRecordForm);
242 
243     }
244 
245     @RequestMapping(params = "methodToCall=nextSearch")
246     public ModelAndView nextSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
247                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
248 
249         SerialsReceivingRecordForm serialsReceivingRecordForm = (SerialsReceivingRecordForm) form;
250         //int listSize = serialsReceivingRecordForm.getTempSerialReceivingRecordList().size();
251         //List<OLESerialReceivingRecord> tempSerialReceivingRecordList;
252         SearchParams searchParams = serialsReceivingRecordForm.getSearchParams();
253         int start = Math.max(0, this.start + this.pageSize);
254         /*if (listSize > pageSize) {
255             searchParams.setStart(start);
256             serialsReceivingRecordForm.setSearchFlag("next");
257             this.start = start;
258             tempSerialReceivingRecordList = serialsReceivingRecordForm.getTempSerialReceivingRecordList().subList(start, serialsReceivingRecordForm.getTempSerialReceivingRecordList().size());
259             serialsReceivingRecordForm.setOleSerialReceivingRecordList(tempSerialReceivingRecordList.size() > pageSize ? tempSerialReceivingRecordList.subList(0, pageSize) : tempSerialReceivingRecordList);
260             serialsReceivingRecordForm.setPaginationFlag("true");
261             serialsReceivingRecordForm.setPageResultDisplay(getPageShowEntries());
262 
263             serialsReceivingRecordForm.setNextFlag(getNextFlag());
264             serialsReceivingRecordForm.setPreviousFlag(getPreviousFlag());
265             return getUIFModelAndView(serialsReceivingRecordForm);
266 
267         }*/
268             searchParams.setStart(start);
269             serialsReceivingRecordForm.setSearchFlag("next");
270             this.start = start;
271             return search(serialsReceivingRecordForm, result, request, response);
272 
273 
274     }
275 
276     @RequestMapping(params = "methodToCall=previousSearch")
277     public ModelAndView previousSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
278                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
279 
280         SerialsReceivingRecordForm serialsReceivingRecordForm = (SerialsReceivingRecordForm) form;
281         //int listSize = serialsReceivingRecordForm.getTempSerialReceivingRecordList().size();
282         //List<OLESerialReceivingRecord> tempSerialReceivingRecordList;
283         SearchParams searchParams = serialsReceivingRecordForm.getSearchParams();
284         int start = Math.max(0, this.start - this.pageSize);
285        /* if (listSize < pageSize) {
286             searchParams.setStart(start);
287             serialsReceivingRecordForm.setSearchFlag("previous");
288             this.start = start;
289             tempSerialReceivingRecordList = serialsReceivingRecordForm.getTempSerialReceivingRecordList().subList(start, serialsReceivingRecordForm.getTempSerialReceivingRecordList().size());
290             serialsReceivingRecordForm.setOleSerialReceivingRecordList(tempSerialReceivingRecordList.size() > pageSize ? tempSerialReceivingRecordList.subList(0, pageSize) : tempSerialReceivingRecordList);
291             serialsReceivingRecordForm.setOleSerialReceivingRecordList(tempSerialReceivingRecordList.size() > pageSize ? tempSerialReceivingRecordList.subList(0, pageSize) : tempSerialReceivingRecordList);
292             serialsReceivingRecordForm.setPaginationFlag("true");
293             serialsReceivingRecordForm.setPageResultDisplay(getPageShowEntries());
294 
295             serialsReceivingRecordForm.setNextFlag(getNextFlag());
296             serialsReceivingRecordForm.setPreviousFlag(getPreviousFlag());
297             return getUIFModelAndView(serialsReceivingRecordForm);
298 
299         } */
300             searchParams.setStart(start);
301             serialsReceivingRecordForm.setSearchFlag("previous");
302             this.start = start;
303             return search(serialsReceivingRecordForm, result, request, response);
304 
305 
306     }
307 
308 
309     @RequestMapping(params = "methodToCall=descendingSort")
310     public ModelAndView descendingSort(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
311                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
312 
313         SerialsReceivingRecordForm serialsReceivingRecordForm = (SerialsReceivingRecordForm) form;
314         SearchParams searchParams = serialsReceivingRecordForm.getSearchParams();
315         int start = 0;
316         searchParams.setStart(start);
317         serialsReceivingRecordForm.setSearchFlag("next");
318         this.start = start;
319         serialsReceivingRecordForm.setSortOrder("desc");
320         return search(serialsReceivingRecordForm, result, request, response);
321     }
322 
323     @RequestMapping(params = "methodToCall=ascendingSort")
324     public ModelAndView ascendingSort(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
325                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
326 
327         SerialsReceivingRecordForm serialsReceivingRecordForm = (SerialsReceivingRecordForm) form;
328         SearchParams searchParams = serialsReceivingRecordForm.getSearchParams();
329         int start = 0;
330         searchParams.setStart(start);
331         serialsReceivingRecordForm.setSearchFlag("next");
332         this.start = start;
333         serialsReceivingRecordForm.setSortOrder("asc");
334         return search(serialsReceivingRecordForm, result, request, response);
335     }
336 
337 
338 }