001package org.kuali.ole.deliver.lookup;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.OleLookupableImpl;
005import org.kuali.ole.deliver.bo.OleItemSearch;
006import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
007import org.kuali.ole.docstore.common.document.Bib;
008import org.kuali.ole.docstore.common.document.Item;
009import org.kuali.ole.docstore.common.document.content.enums.DocType;
010import org.kuali.ole.docstore.common.document.content.instance.Location;
011import org.kuali.ole.docstore.common.document.content.instance.LocationLevel;
012import org.kuali.ole.docstore.common.search.SearchParams;
013import org.kuali.ole.docstore.common.search.SearchResponse;
014import org.kuali.ole.sys.context.SpringContext;
015import org.kuali.ole.util.DocstoreUtil;
016import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
017import org.kuali.rice.krad.lookup.LookupableImpl;
018import org.kuali.rice.krad.service.KRADServiceLocator;
019import org.kuali.rice.krad.util.GlobalVariables;
020import org.kuali.rice.krad.util.KRADConstants;
021import org.kuali.rice.krad.web.form.LookupForm;
022
023import java.util.*;
024
025/**
026 * OlePatronLookupableImpl makes validation  and populate the search criteria and return the search results
027 */
028public class OleItemSearchLookupableImpl extends OleLookupableImpl {
029
030
031    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleItemSearchLookupableImpl.class);
032    private DocstoreClientLocator docstoreClientLocator;
033    private DocstoreUtil docstoreUtil;
034    private int totalRecCount;
035    private int start;
036    private int pageSize;
037
038    public DocstoreUtil getDocstoreUtil() {
039        if (docstoreUtil == null) {
040            docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
041        }
042        return docstoreUtil;
043    }
044
045    public DocstoreClientLocator getDocstoreClientLocator() {
046        if (docstoreClientLocator == null) {
047            docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
048        }
049        return docstoreClientLocator;
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    @Override
065    public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
066        LOG.debug("Inside performSearch()");
067        boolean searchFlag = true;
068
069        Iterator<Map.Entry<String, String>> iter = searchCriteria.entrySet().iterator();
070        while (iter.hasNext()) {
071            Map.Entry<String, String> entry = iter.next();
072            if (entry.getKey().equalsIgnoreCase("title") || entry.getKey().equalsIgnoreCase("author") || entry.getKey().equalsIgnoreCase("publisher") || entry.getKey().equalsIgnoreCase("callNumber") || entry.getKey().equalsIgnoreCase("itemType") || entry.getKey().equalsIgnoreCase("itemBarCode")) {
073                if (!entry.getValue().trim().equals("")) {
074                    searchFlag = false;
075                    break;
076                }
077            }
078        }
079
080        if (searchFlag) {
081            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ITM_BLANK_SEARCH_ERROR_MSG);
082            return new ArrayList<Object>();
083        }
084        String title = searchCriteria.get("title") != null ? searchCriteria.get("title") : "";
085        String author = searchCriteria.get("author") != null ? searchCriteria.get("author") : "";
086        String publisher = searchCriteria.get("publisher") != null ? searchCriteria.get("publisher") : "";
087        String callNumber = searchCriteria.get("callNumber") != null ? searchCriteria.get("callNumber") : "";
088        String itemType = searchCriteria.get("itemType") != null ? searchCriteria.get("itemType") : "";
089        String itemBarCode = searchCriteria.get("itemBarCode") != null ? searchCriteria.get("itemBarCode") : "";
090        // String itemUuid = searchCriteria.get("itemUuid") != null ? searchCriteria.get("itemUuid") : "";
091        String pageDisplay = searchCriteria.get("pageDisplay") != null ? searchCriteria.get("pageDisplay") : "";
092        String page_Size = searchCriteria.get("pageSize") != null ? searchCriteria.get("pageSize") : "";
093
094        String startIndex = searchCriteria.get("startIndex") != null ? searchCriteria.get("startIndex") : "";
095        List<OleItemSearch> oleItemSearches = new ArrayList<>();
096        //this.pageSize = Integer.parseInt(getParameter(OLEParameterConstants.ITM_SEARCH_DOCSTORE_ROW_SIZE_VALUE));
097        this.pageSize = Integer.parseInt(page_Size);
098        if (!pageDisplay.isEmpty() && pageDisplay.equalsIgnoreCase("search")) {
099            this.start = 0;
100            startIndex = "0";
101
102
103        }
104        if (!pageDisplay.isEmpty() && pageDisplay.equalsIgnoreCase("next")) {
105
106            if (!startIndex.isEmpty()) {
107                this.start = Integer.parseInt(startIndex);
108                int nextIndex = Math.max(0, this.start + this.pageSize);
109                this.start = nextIndex;
110                startIndex = String.valueOf(nextIndex);
111            }
112
113        }
114        if (!pageDisplay.isEmpty() && pageDisplay.equalsIgnoreCase("previous")) {
115            if (!startIndex.isEmpty()) {
116                this.start = Integer.parseInt(startIndex);
117                int previousIndex = Math.max(0, this.start - this.pageSize);
118                this.start = previousIndex;
119                startIndex = String.valueOf(previousIndex);
120            }
121
122        }
123
124        try {
125            SearchParams item_search_Params = new SearchParams();
126            if (((!title.isEmpty() || !author.isEmpty() || !publisher.isEmpty()) || (!itemBarCode.isEmpty() || !callNumber.isEmpty() || !itemType.isEmpty()))) {
127                item_search_Params.setStartIndex(this.start);
128                item_search_Params.setPageSize(Integer.parseInt(page_Size));
129                if (!callNumber.isEmpty()) {
130                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("AND", item_search_Params.buildSearchField(DocType.ITEM.getCode(), "HoldingsCallNumber_display", callNumber), "OR"));
131                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("AND", item_search_Params.buildSearchField(DocType.ITEM.getCode(), "CallNumber_display", callNumber), "AND"));
132                }
133                if (!title.isEmpty()) {
134                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("AND", item_search_Params.buildSearchField(DocType.ITEM.getCode(), Bib.TITLE, title), "AND"));
135                }
136                if (!author.isEmpty()) {
137                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("AND", item_search_Params.buildSearchField(DocType.ITEM.getCode(), Bib.AUTHOR, author), "AND"));
138                }
139                if (!publisher.isEmpty()) {
140                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("AND", item_search_Params.buildSearchField(DocType.ITEM.getCode(), "Publisher_display", publisher), "AND"));
141                }
142                if (!itemType.isEmpty()) {
143                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("AND", item_search_Params.buildSearchField(DocType.ITEM.getCode(), "TemporaryItemTypeCodeValue_search", itemType), "OR"));
144                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("AND", item_search_Params.buildSearchField(DocType.ITEM.getCode(), "ItemTypeCodeValue_search", itemType), "AND"));
145                }
146                if (!itemBarCode.isEmpty()) {
147                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("phrase", item_search_Params.buildSearchField(DocType.ITEM.getCode(), Item.ITEM_BARCODE, itemBarCode), "AND"));
148                }
149                /*if (!itemUuid.isEmpty()) {
150                    item_search_Params.getSearchConditions().add(item_search_Params.buildSearchCondition("AND", item_search_Params.buildSearchField(DocType.ITEM.getCode(), Item.ID, itemUuid), "AND"));
151                }*/
152                getDocstoreUtil().getSearchResultFields(item_search_Params);
153                SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(item_search_Params);
154                this.totalRecCount = searchResponse.getTotalRecordCount();
155                form.getLookupCriteria().put("totalRec", getPageShowEntries());
156                form.getLookupCriteria().put("nextFlag", Boolean.toString(getNextFlag()));
157                form.getLookupCriteria().put("previousFlag", Boolean.toString(getPreviousFlag()));
158                form.getLookupCriteria().put("startIndex", startIndex);
159                oleItemSearches = getDocstoreUtil().getSearchResults(searchResponse);
160                //this.totalRecCount = oleItemSearches.size();
161                //form.getLookupCriteria().put("pageSize",getParameter(OLEParameterConstants.ITM_SEARCH_DOCSTORE_ROW_SIZE_VALUE));
162
163            }
164
165        } catch (Exception ex) {
166            ex.printStackTrace();
167        }
168        if (oleItemSearches.size() == 0) {
169            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.NO_RECORD_FOUND);
170        }
171
172        return oleItemSearches;
173    }
174
175    public String getPageShowEntries() {
176        return "Showing " + ((this.start == 0) ? 1 : this.start + 1) + " to "
177                + (((this.start + this.pageSize) > this.totalRecCount) ? this.totalRecCount : (this.start + this.pageSize))
178                + " of " + this.totalRecCount + " entries";
179    }
180
181
182    private String getShelvingLocation(Location oleLocation) {
183        if (oleLocation == null) {
184            return "";
185        }
186        LocationLevel locationLevel =
187                oleLocation.getLocationLevel();
188        while (locationLevel.getLocationLevel() != null && !locationLevel.getLevel().equalsIgnoreCase(OLEConstants.OleDeliverRequest.SHELVING)) {
189            locationLevel = locationLevel.getLocationLevel();
190        }
191        return locationLevel.getName();
192    }
193
194    public String getHoldingLinkURL(String holdingUuid, String bibUuid) {
195        String url = "";
196        try {
197            url = "editorcontroller?viewId=EditorView&amp;methodToCall=load&amp;docCategory=work&amp;docType=holdings&amp;editable=false&amp;docFormat=oleml&amp;docId=" + holdingUuid + "&amp;bibId=" + bibUuid;
198        } catch (Exception e) {
199            LOG.error("Exception", e);
200        }
201        return url;
202    }
203
204    public String getParameter(String name) {
205        String parameter = "";
206        try {
207            Map<String, String> criteriaMap = new HashMap<String, String>();
208            criteriaMap.put("namespaceCode", OLEConstants.DLVR_NMSPC);
209            criteriaMap.put("componentCode", OLEConstants.DLVR_CMPNT);
210            criteriaMap.put("name", name);
211            List<ParameterBo> parametersList = (List<ParameterBo>) KRADServiceLocator.getBusinessObjectService().findMatching(ParameterBo.class, criteriaMap);
212            for (ParameterBo parameterBo : parametersList) {
213                parameter = parameterBo.getValue();
214            }
215        } catch (Exception e) {
216            LOG.error("Exception", e);
217        }
218        return parameter;
219    }
220
221
222    @Override
223    public Map<String, String> performClear(LookupForm form, Map<String, String> searchCriteria) {
224        super.performClear(form, searchCriteria);
225        Map<String, String> clearedSearchCriteria = new HashMap<String, String>();
226        for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) {
227            String searchPropertyName = searchKeyValue.getKey();
228            if (searchPropertyName.equals("pageDisplay")) {
229                clearedSearchCriteria.put(searchPropertyName, searchKeyValue.getValue());
230            }
231
232            if (searchPropertyName.equals("totalRec")) {
233                clearedSearchCriteria.put(searchPropertyName, searchKeyValue.getValue());
234            }
235
236            if (searchPropertyName.equals("nextFlag")) {
237                clearedSearchCriteria.put(searchPropertyName, searchKeyValue.getValue());
238            }
239
240            if (searchPropertyName.equals("previousFlag")) {
241                clearedSearchCriteria.put(searchPropertyName, searchKeyValue.getValue());
242            }
243
244            if (searchPropertyName.equals("startIndex")) {
245                clearedSearchCriteria.put(searchPropertyName, searchKeyValue.getValue());
246            }
247            if (searchPropertyName.equals("pageSize")) {
248                clearedSearchCriteria.put(searchPropertyName, searchKeyValue.getValue());
249            }
250        }
251        return clearedSearchCriteria;
252    }
253
254
255}
256