001package org.kuali.ole.describe.controller;
002
003import org.apache.commons.lang3.StringUtils;
004import org.apache.log4j.Logger;
005import org.kuali.ole.OLEConstants;
006import org.kuali.ole.describe.form.SerialsReceivingRecordForm;
007import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
008import org.kuali.ole.docstore.common.search.SearchResponse;
009import org.kuali.ole.docstore.common.search.SearchResult;
010import org.kuali.ole.docstore.discovery.model.SearchCondition;
011import org.kuali.ole.docstore.discovery.model.SearchParams;
012import org.kuali.ole.pojo.OLESerialReceivingRecord;
013import org.kuali.ole.select.bo.OLESerialReceivingDocument;
014import org.kuali.ole.service.SerialReceivingSearchService;
015import org.kuali.ole.service.impl.SerialReceivingSearchServiceImpl;
016import org.kuali.ole.sys.context.SpringContext;
017import org.kuali.rice.kim.api.permission.PermissionService;
018import org.kuali.rice.kim.api.services.KimApiServiceLocator;
019import org.kuali.rice.krad.util.GlobalVariables;
020import org.kuali.rice.krad.web.controller.UifControllerBase;
021import org.kuali.rice.krad.web.form.UifFormBase;
022import org.springframework.stereotype.Controller;
023import org.springframework.validation.BindingResult;
024import org.springframework.web.bind.annotation.ModelAttribute;
025import org.springframework.web.bind.annotation.RequestMapping;
026import org.springframework.web.servlet.ModelAndView;
027
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030import java.util.ArrayList;
031import java.util.HashSet;
032import java.util.List;
033import java.util.Set;
034
035/**
036 * Created with IntelliJ IDEA.
037 * User: sundarr
038 * Date: 7/2/13
039 * Time: 7:24 PM
040 * To change this template use File | Settings | File Templates.
041 */
042@Controller
043@RequestMapping(value = "/serialsReceivingRecordController")
044public class SerialsReceivingRecordSearchController extends UifControllerBase {
045    private static final Logger LOG = Logger.getLogger(SerialsReceivingRecordSearchController.class);
046    private DocstoreClientLocator docstoreClientLocator;
047    private int totalRecCount;
048    private int start;
049    private int pageSize;
050
051
052    public boolean getPreviousFlag() {
053        if (this.start == 0)
054            return false;
055        return true;
056    }
057
058    public boolean getNextFlag() {
059        if (this.start + this.pageSize < this.totalRecCount)
060            return true;
061        return false;
062    }
063
064
065    public String getPageShowEntries() {
066        return "Showing " + ((this.start == 0) ? 1 : this.start + 1) + " to "
067                + (((this.start + this.pageSize) > this.totalRecCount) ? this.totalRecCount : (this.start + this.pageSize))
068                + " of " + this.totalRecCount + " entries";
069    }
070
071
072    public DocstoreClientLocator getDocstoreClientLocator() {
073
074        if (docstoreClientLocator == null) {
075            docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
076        }
077        return docstoreClientLocator;
078    }
079
080
081    @Override
082    protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
083        return new SerialsReceivingRecordForm();
084    }
085
086    /**
087     * This method converts UifFormBase to SerialsReceivingRecordForm
088     *
089     * @param form
090     * @param result
091     * @param request
092     * @param response
093     * @return ModelAndView
094     */
095    @Override
096    @RequestMapping(params = "methodToCall=start")
097    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
098                              HttpServletRequest request, HttpServletResponse response) {
099        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}