001package org.kuali.ole.deliver.lookup;
002
003import org.jfree.util.Log;
004import org.kuali.ole.deliver.bo.OLEFlaggedItems;
005import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
006import org.kuali.ole.docstore.common.document.*;
007import org.kuali.ole.docstore.common.document.content.enums.DocType;
008import org.kuali.ole.docstore.common.search.SearchParams;
009import org.kuali.ole.docstore.common.search.SearchResponse;
010import org.kuali.ole.docstore.common.search.SearchResult;
011import org.kuali.ole.docstore.common.search.SearchResultField;
012import org.kuali.ole.sys.context.SpringContext;
013import org.kuali.rice.krad.lookup.LookupableImpl;
014import org.kuali.rice.krad.web.form.LookupForm;
015import org.apache.commons.lang.StringUtils;
016import org.kuali.ole.OLEConstants;
017
018import java.util.*;
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: aurojyotit
023 * Date: 1/13/14
024 * Time: 3:01 PM
025 * To change this template use File | Settings | File Templates.
026 */
027public class OLEFlaggedItemsLookupableImpl extends LookupableImpl {
028
029    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEFlaggedItemsLookupableImpl.class);
030    private DocstoreClientLocator docstoreClientLocator;
031
032    public DocstoreClientLocator getDocstoreClientLocator() {
033        if (docstoreClientLocator == null) {
034            docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
035        }
036        return docstoreClientLocator;
037    }
038
039    @Override
040    public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
041        LOG.debug("Inside performSearch()");
042        List<OLEFlaggedItems> oleFlaggedItemses = new ArrayList<>();
043        SearchResponse searchResponse = new SearchResponse();
044        SearchParams searchParams = new SearchParams();
045        org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
046        // making field ready
047        String barcode = StringUtils.isNotEmpty(searchCriteria.get("barcode")) ? searchCriteria.get("barcode") : "";
048        String flagType = StringUtils.isNotEmpty(searchCriteria.get("flagType")) ? searchCriteria.get("flagType") : "";
049        boolean isClaimsReturned = false;
050        boolean isDamaged = false;
051        boolean isMissingPieceEnabled = false;
052        boolean defaultSearch = false;
053        int maxRowSize=10000;
054        if (StringUtils.isEmpty(barcode) && StringUtils.isEmpty(flagType)) {
055            defaultSearch = true;
056        }
057        try {
058            searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "", ""), ""));
059            SearchResponse maxRowCountResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
060            maxRowSize=maxRowCountResponse.getTotalRecordCount();
061        } catch (Exception e) {
062            LOG.error("Exception occurred in retrieving the row doc count  "+e);
063        }
064        if (defaultSearch) {
065            searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "ClaimsReturnedFlag_search", "true"), "OR"));
066            searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "MissingPieceFlag_search", "true"), "OR"));
067            searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "ItemDamagedStatus_search", "true"), "OR"));
068        }
069        if (StringUtils.isNotEmpty(barcode)) {
070            searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), Item.ITEM_BARCODE, barcode), "AND"));
071        }
072        if (StringUtils.isNotEmpty(flagType)) {
073            Boolean val = Boolean.valueOf("true");
074            if (flagType.equalsIgnoreCase(OLEConstants.FLAG_TYP_ITM_DAMAGED)) {
075                isDamaged = true;
076                searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "ItemDamagedStatus_search", "true"), "AND"));
077            }
078            if (flagType.equalsIgnoreCase(OLEConstants.FLAG_TYP_ITM_MISSING)) {
079                isMissingPieceEnabled = true;
080                searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "MissingPieceFlag_search", "true"), "AND"));
081            }
082            if (flagType.equalsIgnoreCase(OLEConstants.FLAG_TYP_ITM_CLAIMS_RETURNED)) {
083                isClaimsReturned = true;
084                searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "ClaimsReturnedFlag_search", "true"), "AND"));
085            }
086        }
087        searchParams.setPageSize(maxRowSize);
088        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic", "Title"));
089        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "itembarcode"));
090        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "bibIdentifier"));
091        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "id"));
092        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("holdings", "CallNumber_display"));
093        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "CopyNumber_display"));
094        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "MissingPieceFlag_display"));
095        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "ClaimsReturnedFlag_display"));
096        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "ItemDamagedStatus_display"));
097        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "MissingPieceFlagNote_display"));
098        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "DamagedItemNote_display"));
099        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "ClaimsReturnedNote_display"));
100        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("holdings", "CallNumber_display"));
101        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item", "Location_display"));
102        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("holdings", "Location_display"));
103       // LOG.info("Flagged Item Records Size isNull"+ (searchResponse.getSearchResults()==null?"true":"false"));
104
105        try {
106            searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
107            if (LOG.isDebugEnabled()){
108                if(searchResponse.getSearchResults()!=null && searchResponse.getSearchResults().size()>0){
109                    LOG.debug("Flagged Item Records Size"+searchResponse.getSearchResults().size());
110                } else {
111                    LOG.debug("Flagged Item Records Size is Null");
112                }
113            }
114            for (SearchResult searchResult : searchResponse.getSearchResults()) {
115                OLEFlaggedItems flaggedItems = new OLEFlaggedItems();
116                boolean isItemLevelCallNumberExist = true;
117                boolean isItemLevelLocationExist = true;
118                LOG.debug("DOCSTORE RECORD--------->");
119                for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
120                    String fieldName = searchResultField.getFieldName();
121                    String fieldValue = searchResultField.getFieldValue() != null ? searchResultField.getFieldValue() : "";
122                    if (LOG.isDebugEnabled()){
123                        LOG.debug("Field Name"+fieldName+"  :: " +fieldValue );
124                    }
125                    if (fieldName.equalsIgnoreCase("itemBarcode") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
126                        flaggedItems.setBarcode(fieldValue);
127                    } else if (fieldName.equalsIgnoreCase("MissingPieceFlag_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
128                        if (Boolean.valueOf(fieldValue) || fieldValue.equalsIgnoreCase("true")) {
129                            flaggedItems.setMissingPiece(true);
130                        }
131                    } else if (fieldName.equalsIgnoreCase("ClaimsReturnedFlag_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
132                        if (Boolean.valueOf(fieldValue) || fieldValue.equalsIgnoreCase("true")) {
133                            flaggedItems.setClaimsReturned(true);
134                        }
135                    } else if (fieldName.equalsIgnoreCase("ItemDamagedStatus_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
136                        if (Boolean.valueOf(fieldValue) || fieldValue.equalsIgnoreCase("true")) {
137                            flaggedItems.setDamaged(true);
138                        }
139                    } else if (fieldName.equalsIgnoreCase("title") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase(DocType.BIB.getCode())) {
140                        flaggedItems.setTitle(fieldValue);
141                    } else if (fieldName.equalsIgnoreCase("CallNumber_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("holdings")) {
142                        if (isItemLevelCallNumberExist) {
143                            flaggedItems.setCallNumber(fieldValue);
144                        }
145                    } else if (fieldName.equalsIgnoreCase("CallNumber_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
146                        flaggedItems.setCallNumber(fieldValue);
147                        isItemLevelCallNumberExist = true;
148                    } else if (fieldName.equalsIgnoreCase("CopyNumber_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
149                        flaggedItems.setCopyNumber(fieldValue);
150                    } else if (fieldName.equalsIgnoreCase("Location_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("holdings")) {
151                        if (isItemLevelLocationExist) {
152                            flaggedItems.setLocation(fieldValue);
153                        }
154                    } else if (fieldName.equalsIgnoreCase("Location_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
155                        flaggedItems.setLocation(fieldValue);
156                        isItemLevelLocationExist = true;
157                    } else if (fieldName.equalsIgnoreCase("bibIdentifier") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
158                        flaggedItems.setBibUuid(fieldValue);
159                    } else if (fieldName.equalsIgnoreCase("id") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
160                        flaggedItems.setItemUuid(fieldValue);
161                        flaggedItems.setInstanceUuid(fieldValue);
162                    } else if (fieldName.equalsIgnoreCase("MissingPieceFlagNote_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
163                        flaggedItems.setMissingPieceNote(fieldValue);
164                    } else if (fieldName.equalsIgnoreCase("DamagedItemNote_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
165                        flaggedItems.setDamagedNote(fieldValue);
166                    } else if (fieldName.equalsIgnoreCase("ClaimsReturnedNote_display") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
167                        flaggedItems.setClaimsReturnedNote(fieldValue);
168                    }
169
170                }
171                if (LOG.isDebugEnabled()){
172                    LOG.debug(flaggedItems.toString());
173                }
174                oleFlaggedItemses.add(flaggedItems);
175            }
176
177        } catch (Exception ex) {
178            ex.printStackTrace();
179        }
180        return buildResultForDisplay(oleFlaggedItemses, isClaimsReturned, isDamaged, isMissingPieceEnabled, "");
181    }
182
183    public List<OLEFlaggedItems> buildResultForDisplay(List<OLEFlaggedItems> docResults, boolean isClaimsReturned, boolean isDamaged, boolean isMissingPieceEnabled, String flagNote) {
184        List<OLEFlaggedItems> result = new ArrayList<OLEFlaggedItems>();
185        if (LOG.isDebugEnabled()){
186            LOG.debug("Flagged Item Search : Before building the docItems size"+docResults.size());
187        }
188        boolean defaultSearch = false;
189        if (!(isClaimsReturned || isDamaged || isMissingPieceEnabled)) {
190            defaultSearch = true;
191        }
192        for (OLEFlaggedItems docResult : docResults) {
193            if (defaultSearch) {
194                if (docResult.isClaimsReturned()) {
195                    OLEFlaggedItems flagItem = new OLEFlaggedItems();
196                    flagItem.setBarcode(docResult.getBarcode());
197                    flagItem.setTitle(docResult.getTitle());
198                    flagItem.setCopyNumber(docResult.getCopyNumber());
199                    flagItem.setCallNumber(docResult.getCallNumber());
200                    flagItem.setFlagType("Claims Returned");
201                    flagItem.setFlagNote(docResult.getClaimsReturnedNote());
202                    flagItem.setLocation(docResult.getLocation());
203                    flagItem.setBibUuid(docResult.getBibUuid());
204                    flagItem.setInstanceUuid(docResult.getInstanceUuid());
205                    flagItem.setItemUuid(docResult.getItemUuid());
206                    result.add(flagItem);
207                }
208                if (docResult.isDamaged()) {
209                    OLEFlaggedItems flagItem = new OLEFlaggedItems();
210                    flagItem.setBarcode(docResult.getBarcode());
211                    flagItem.setTitle(docResult.getTitle());
212                    flagItem.setCopyNumber(docResult.getCopyNumber());
213                    flagItem.setCallNumber(docResult.getCallNumber());
214                    flagItem.setFlagType("Damaged");
215                    flagItem.setFlagNote(docResult.getDamagedNote());
216                    flagItem.setLocation(docResult.getLocation());
217                    flagItem.setBibUuid(docResult.getBibUuid());
218                    flagItem.setInstanceUuid(docResult.getInstanceUuid());
219                    flagItem.setItemUuid(docResult.getItemUuid());
220                    result.add(flagItem);
221                }
222                if (docResult.isMissingPiece()) {
223                    OLEFlaggedItems flagItem = new OLEFlaggedItems();
224                    flagItem.setBarcode(docResult.getBarcode());
225                    flagItem.setTitle(docResult.getTitle());
226                    flagItem.setCopyNumber(docResult.getCopyNumber());
227                    flagItem.setCallNumber(docResult.getCallNumber());
228                    flagItem.setFlagType("Missing Piece");
229                    flagItem.setFlagNote(docResult.getMissingPieceNote());
230                    flagItem.setLocation(docResult.getLocation());
231                    flagItem.setBibUuid(docResult.getBibUuid());
232                    flagItem.setInstanceUuid(docResult.getInstanceUuid());
233                    flagItem.setItemUuid(docResult.getItemUuid());
234                    result.add(flagItem);
235                }
236            }
237            if (docResult.isClaimsReturned() && isClaimsReturned && !defaultSearch) {
238                OLEFlaggedItems flagItem = new OLEFlaggedItems();
239                flagItem.setBarcode(docResult.getBarcode());
240                flagItem.setTitle(docResult.getTitle());
241                flagItem.setCopyNumber(docResult.getCopyNumber());
242                flagItem.setCallNumber(docResult.getCallNumber());
243                flagItem.setFlagType("Claims Returned");
244                flagItem.setFlagNote(docResult.getClaimsReturnedNote());
245                flagItem.setLocation(docResult.getLocation());
246                flagItem.setBibUuid(docResult.getBibUuid());
247                flagItem.setInstanceUuid(docResult.getInstanceUuid());
248                flagItem.setItemUuid(docResult.getItemUuid());
249                result.add(flagItem);
250            }
251            if (docResult.isDamaged() && isDamaged && !defaultSearch) {
252                OLEFlaggedItems flagItem = new OLEFlaggedItems();
253                flagItem.setBarcode(docResult.getBarcode());
254                flagItem.setTitle(docResult.getTitle());
255                flagItem.setCopyNumber(docResult.getCopyNumber());
256                flagItem.setCallNumber(docResult.getCallNumber());
257                flagItem.setFlagType("Damaged");
258                flagItem.setFlagNote(docResult.getDamagedNote());
259                flagItem.setLocation(docResult.getLocation());
260                flagItem.setBibUuid(docResult.getBibUuid());
261                flagItem.setInstanceUuid(docResult.getInstanceUuid());
262                flagItem.setItemUuid(docResult.getItemUuid());
263                result.add(flagItem);
264            }
265            if (docResult.isMissingPiece() && isMissingPieceEnabled && !defaultSearch) {
266                OLEFlaggedItems flagItem = new OLEFlaggedItems();
267                flagItem.setBarcode(docResult.getBarcode());
268                flagItem.setTitle(docResult.getTitle());
269                flagItem.setCopyNumber(docResult.getCopyNumber());
270                flagItem.setCallNumber(docResult.getCallNumber());
271                flagItem.setFlagType("Missing Piece");
272                flagItem.setFlagNote(docResult.getMissingPieceNote());
273                flagItem.setLocation(docResult.getLocation());
274                flagItem.setBibUuid(docResult.getBibUuid());
275                flagItem.setInstanceUuid(docResult.getInstanceUuid());
276                flagItem.setItemUuid(docResult.getItemUuid());
277                result.add(flagItem);
278            }
279        }
280        if (LOG.isDebugEnabled()){
281            LOG.debug("Flagged Item Search : After building the docItems size"+result.size());
282        }
283        return result;
284    }
285
286
287}