001package org.kuali.ole.service.impl;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.DocumentUniqueIDPrefix;
005import org.kuali.ole.OLEConstants;
006import org.kuali.ole.describe.bo.OleReceiptStatus;
007import org.kuali.ole.describe.form.SerialsReceivingRecordForm;
008import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
009import org.kuali.ole.docstore.common.document.Bib;
010import org.kuali.ole.docstore.common.document.Holdings;
011import org.kuali.ole.docstore.common.document.content.enums.DocType;
012import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
013import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
014import org.kuali.ole.docstore.common.search.SearchResponse;
015import org.kuali.ole.docstore.common.search.SearchResult;
016import org.kuali.ole.docstore.common.search.SearchResultField;
017import org.kuali.ole.docstore.discovery.model.SearchCondition;
018import org.kuali.ole.docstore.model.enums.DocCategory;
019import org.kuali.ole.docstore.model.enums.DocFormat;
020import org.kuali.ole.pojo.OLESerialReceivingRecord;
021import org.kuali.ole.select.bo.OLESerialReceivingDocument;
022import org.kuali.ole.select.businessobject.OleCopy;
023import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
024import org.kuali.ole.select.document.OlePurchaseOrderDocument;
025import org.kuali.ole.service.SerialReceivingSearchService;
026import org.kuali.ole.sys.context.SpringContext;
027import org.kuali.ole.util.DocstoreUtil;
028import org.kuali.rice.krad.service.BusinessObjectService;
029
030import java.util.*;
031
032/**
033 * Created with IntelliJ IDEA.
034 * User: arunag
035 * Date: 4/3/14
036 * Time: 3:14 PM
037 * To change this template use File | Settings | File Templates.
038 */
039public class SerialReceivingSearchServiceImpl implements SerialReceivingSearchService {
040
041    private static final Logger LOG = Logger.getLogger(SerialReceivingSearchService.class);
042    private BusinessObjectService businessObjectService;
043
044    public BusinessObjectService getBusinessObjectService() {
045        if (null == businessObjectService) {
046            businessObjectService = SpringContext.getBean(BusinessObjectService.class);
047        }
048        return businessObjectService;
049    }
050
051    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
052        this.businessObjectService = businessObjectService;
053
054    }
055
056    private DocstoreClientLocator docstoreClientLocator;
057
058    public DocstoreClientLocator getDocstoreClientLocator() {
059
060        if (docstoreClientLocator == null) {
061            docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
062        }
063        return docstoreClientLocator;
064    }
065
066    private DocstoreUtil docstoreUtil;
067
068    public DocstoreUtil getDocstoreUtil() {
069
070        if (docstoreUtil == null) {
071            docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
072
073        }
074        return docstoreUtil;
075    }
076
077    public SearchResponse holdingSearch(int startIndex, int searchLimit,String sortingOrder) {
078        SearchResponse searchResponse = new SearchResponse();
079        org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
080        search_Params.setStartIndex(startIndex);
081        search_Params.setPageSize(searchLimit);
082        search_Params.getSearchConditions().add(search_Params.buildSearchCondition("", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), "", ""), ""));
083        search_Params.getSortConditions().add(search_Params.buildSortCondition("Title_sort", sortingOrder));
084        try {
085            searchResponse = getDocstoreClientLocator().getDocstoreClient().search(getSearchParams(search_Params));
086        } catch (Exception ex) {
087            ex.printStackTrace();
088        }
089        return searchResponse;
090    }
091
092    public SearchResponse searchDataFromDocstore(int startIndex, int searchLimit, Set<String> instance_Ids, String title, String issn, String localIdentifier,String sortingOrder) {
093        SearchResponse searchResponse = new SearchResponse();
094        org.kuali.ole.docstore.common.search.SearchParams searchParam = new org.kuali.ole.docstore.common.search.SearchParams();
095        searchParam.setStartIndex(startIndex);
096        searchParam.setPageSize(searchLimit);
097        if (!title.isEmpty()) {
098            searchParam.getSearchConditions().add(searchParam.buildSearchCondition("AND", searchParam.buildSearchField(DocType.HOLDINGS.getCode(), Bib.TITLE, title), "AND"));
099        }
100        if (!issn.isEmpty()) {
101            searchParam.getSearchConditions().add(searchParam.buildSearchCondition("AND", searchParam.buildSearchField(DocType.HOLDINGS.getCode(), Bib.ISSN, issn), "AND"));
102        }
103        if (!localIdentifier.isEmpty()) {
104            searchParam.getSearchConditions().add(searchParam.buildSearchCondition("", searchParam.buildSearchField(DocType.HOLDINGS.getCode(), OLEConstants.LOCALID_SEARCH, localIdentifier), "AND"));
105
106        }
107        if (instance_Ids != null && instance_Ids.size() > 0) {
108            for (String instanceId : instance_Ids) {
109                searchParam.getSearchConditions().add(searchParam.buildSearchCondition("AND", searchParam.buildSearchField(DocType.HOLDINGS.getCode(), "id", instanceId), "OR"));
110            }
111        }
112        searchParam.getSortConditions().add(searchParam.buildSortCondition("Title_sort", sortingOrder));
113        try {
114            searchResponse = getDocstoreClientLocator().getDocstoreClient().search(getSearchParams(searchParam));
115            LOG.info(searchResponse.getTotalRecordCount());
116
117        } catch (Exception ex) {
118            ex.printStackTrace();
119        }
120        return searchResponse;
121    }
122
123    private org.kuali.ole.docstore.common.search.SearchParams getSearchParams(org.kuali.ole.docstore.common.search.SearchParams search_Params) {
124        search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), "id"));
125        search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), Holdings.CALL_NUMBER));
126        search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), Holdings.LOCATION_NAME));
127        search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), "id"));
128        search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), "Title_sort"));
129        search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), Bib.ISSN));
130        search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), "staffOnlyFlag"));
131        return search_Params;
132    }
133
134
135    public OLESerialReceivingRecord getSerialRecord(SearchResult searchResult) {
136        OLESerialReceivingRecord oleSerialReceivingRecord = new OLESerialReceivingRecord();
137        Map instanceValue = new HashMap();
138        if (searchResult.getSearchResultFields() != null && searchResult.getSearchResultFields().size() > 0) {
139            for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
140                if (searchResultField.getDocType().equalsIgnoreCase("holdings") && searchResultField.getFieldName().equalsIgnoreCase("id") && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) {
141                    oleSerialReceivingRecord.setInstanceId(searchResultField.getFieldValue());
142
143                    instanceValue.put(OLEConstants.INSTANCE_ID, searchResultField.getFieldValue());
144                }
145
146                if (searchResultField.getDocType().equalsIgnoreCase("holdings") && searchResultField.getFieldName().equalsIgnoreCase(Holdings.CALL_NUMBER) && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) {
147                    oleSerialReceivingRecord.setCallNumber(searchResultField.getFieldValue());
148                }
149                if (searchResultField.getDocType().equalsIgnoreCase("holdings") && searchResultField.getFieldName().equalsIgnoreCase(Holdings.LOCATION_NAME) && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) {
150                    oleSerialReceivingRecord.setBoundLocation(searchResultField.getFieldValue());
151                }
152                if (searchResultField.getDocType().equalsIgnoreCase("bibliographic") && searchResultField.getFieldName().equalsIgnoreCase("id") && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) {
153                    if(oleSerialReceivingRecord.getBibId()==null){
154                    oleSerialReceivingRecord.setBibId(searchResultField.getFieldValue());
155                    }
156                }
157                if (searchResultField.getDocType().equalsIgnoreCase("bibliographic") && searchResultField.getFieldName().equalsIgnoreCase("Title_sort") && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) {
158                    if(oleSerialReceivingRecord.getTitle()==null){
159                    oleSerialReceivingRecord.setTitle(searchResultField.getFieldValue());
160                    }
161                }
162                if (searchResultField.getDocType().equalsIgnoreCase("bibliographic") && searchResultField.getFieldName().equalsIgnoreCase("staffOnlyFlag") && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty() && searchResultField.getFieldValue().equalsIgnoreCase("true")) {
163                    oleSerialReceivingRecord.setStaffOnlyFlag(OLEConstants.STAFF_ONLY_COLOR);
164                    oleSerialReceivingRecord.setStaffOnlyFlagStyle(OLEConstants.STAFF_ONLY_STYLE);
165                } else {
166                    oleSerialReceivingRecord.setStaffOnlyFlag(OLEConstants.NON_STAFF_ONLY_COLOR);
167                    oleSerialReceivingRecord.setStaffOnlyFlagStyle(OLEConstants.NON_STAFF_ONLY_STYLE);
168                }
169                if (searchResultField.getDocType().equalsIgnoreCase("bibliographic") && searchResultField.getFieldName().equalsIgnoreCase(Bib.ISSN) && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) {
170                    if(oleSerialReceivingRecord.getIssn()==null){
171                    oleSerialReceivingRecord.setIssn(searchResultField.getFieldValue());
172                    }
173                }
174            }
175        }
176        List<OLESerialReceivingDocument> oleSerialReceivingDocument = (List<OLESerialReceivingDocument>) getBusinessObjectService().findMatching(OLESerialReceivingDocument.class, instanceValue);
177        if (oleSerialReceivingDocument.isEmpty()) {
178            oleSerialReceivingRecord.setAction(OLEConstants.SERIAL_SEARCH_CREATE_NEW);
179            oleSerialReceivingRecord.setHref("serialReceiving?viewId=OLESerialReceivingView&amp;methodToCall=docHandler&amp;command=initiate&amp;bibId=" + oleSerialReceivingRecord.getBibId() + "&amp;instanceId=" + oleSerialReceivingRecord.getInstanceId());
180        } else {
181            for (OLESerialReceivingDocument oleSerialReceiving_Document : oleSerialReceivingDocument) {
182                if (!oleSerialReceiving_Document.isActive()) {
183                    oleSerialReceivingRecord.setAction(OLEConstants.SERIAL_SEARCH_CREATE_NEW);
184                    oleSerialReceivingRecord.setHref("serialReceiving?viewId=OLESerialReceivingView&amp;methodToCall=docHandler&amp;command=initiate&amp;bibId=" + oleSerialReceivingRecord.getBibId() + "&amp;instanceId=" + oleSerialReceivingRecord.getInstanceId());
185                } else {
186                    oleSerialReceivingRecord.setAction(OLEConstants.SERIAL_SEARCH_SHOW_RECORD);
187                    oleSerialReceivingRecord.setHref("serialReceiving?viewId=OLESerialReceivingView&amp;methodToCall=docHandler&amp;docId=" + oleSerialReceiving_Document.getDocumentNumber() + "&amp;command=displayDocSearchView&amp;bibId=" + oleSerialReceiving_Document.getBibId() + "&amp;instanceId=" + oleSerialReceiving_Document.getInstanceId());
188                    oleSerialReceivingRecord.setCheckInWorkUnit(oleSerialReceiving_Document.getSerialReceiptLocation());
189                    oleSerialReceivingRecord.setUnboundLocation(oleSerialReceiving_Document.getUnboundLocation());
190                    Map<String, String> subscriptionStatusMap = new HashMap<String, String>();
191                    subscriptionStatusMap.put("receiptStatusCode", oleSerialReceiving_Document.getSubscriptionStatus());
192                    List<OleReceiptStatus> oleReceiptStatuses = (List<OleReceiptStatus>) getBusinessObjectService().findMatching(OleReceiptStatus.class, subscriptionStatusMap);
193                    oleSerialReceivingRecord.setSubscriptionStatus(oleReceiptStatuses != null && oleReceiptStatuses.size() > 0 ? oleReceiptStatuses.get(0).getReceiptStatusName() : "");
194                    break;
195                }
196            }
197        }
198
199        return oleSerialReceivingRecord;
200
201    }
202
203    public Set<String> getInstanceIdList(String poId) {
204        Set<String> instanceIds = new HashSet<>();
205        Map parentCriterial = new HashMap();
206        parentCriterial.put("purchaseOrder.purapDocumentIdentifier", poId);
207        List<OlePurchaseOrderItem> purchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, parentCriterial);
208        if (purchaseOrderItems != null && purchaseOrderItems.size() > 0) {
209            for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
210                String bibId = olePurchaseOrderItem.getItemTitleId();
211                String poDocId = olePurchaseOrderItem.getPurapDocument().getDocumentNumber();
212                OlePurchaseOrderDocument olePurchaseOrderDocument = olePurchaseOrderItem.getPurapDocument();
213                if (olePurchaseOrderDocument.getPurchaseOrderCurrentIndicatorForSearching()) {
214                    parentCriterial = new HashMap();
215                    parentCriterial.put(OLEConstants.BIB_ID, bibId);
216                    parentCriterial.put("poDocNum", poDocId);
217                    List<OleCopy> copyList = (List<OleCopy>) getBusinessObjectService().findMatching(OleCopy.class, parentCriterial);
218                    if (copyList != null && copyList.size() > 0) {
219                        for (OleCopy copy : copyList) {
220                            instanceIds.add(copy.getInstanceId());
221                        }
222                    }
223                }
224            }
225        }
226        return instanceIds;
227    }
228
229    public List<OLESerialReceivingRecord> getOleSerialReceivingList(List<OLESerialReceivingDocument> oleSerialReceivingDocuments) {
230        List<OLESerialReceivingRecord> oleSerialReceivingRecords = new ArrayList<>();
231        try {
232            for (OLESerialReceivingDocument serialReceivingDocument : oleSerialReceivingDocuments) {
233                Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(serialReceivingDocument.getBibId());
234                Holdings holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(serialReceivingDocument.getInstanceId());
235                OleHoldings oleHoldings = new HoldingOlemlRecordProcessor().fromXML(holdings.getContent());
236                OLESerialReceivingRecord oleSerialReceivingRecord = new OLESerialReceivingRecord();
237                oleSerialReceivingRecord.setAction(OLEConstants.SERIAL_SEARCH_SHOW_RECORD);
238                oleSerialReceivingRecord.setTitle(bib.getTitle());
239                oleSerialReceivingRecord.setHref("serialReceiving?viewId=OLESerialReceivingView&amp;methodToCall=docHandler&amp;docId=" + serialReceivingDocument.getDocumentNumber() + "&amp;command=displayDocSearchView&amp;bibId=" + serialReceivingDocument.getBibId() + "&amp;instanceId=" + serialReceivingDocument.getInstanceId());
240                oleSerialReceivingRecord.setCallNumber(serialReceivingDocument.getCallNumber());
241                oleSerialReceivingRecord.setCheckInWorkUnit(serialReceivingDocument.getSerialReceiptLocation());
242                oleSerialReceivingRecord.setBoundLocation(getDocstoreUtil().getLocation(oleHoldings.getLocation(), new StringBuffer("")));
243                oleSerialReceivingRecord.setUnboundLocation(serialReceivingDocument.getUnboundLocation());
244                oleSerialReceivingRecord.setIssn(bib.getIssn());
245                oleSerialReceivingRecords.add(oleSerialReceivingRecord);
246            }
247        } catch (Exception ex) {
248            ex.printStackTrace();
249        }
250        return oleSerialReceivingRecords;
251    }
252
253    public List<OLESerialReceivingDocument> getOleSerialReceivingDocuments(SerialsReceivingRecordForm serialsReceivingRecordForm) {
254        Map serialMap = new HashMap();
255        if (serialsReceivingRecordForm.getSearchParams().getSearchFieldsList() != null && serialsReceivingRecordForm.getSearchParams().getSearchFieldsList().size() > 0) {
256            serialMap.put(OLEConstants.OleHoldings.ACTIVE, true);
257            for (SearchCondition sc : serialsReceivingRecordForm.getSearchParams().getSearchFieldsList()) {
258                if (sc.getDocField().equalsIgnoreCase(OLEConstants.LOCALID_SEARCH) && sc.getSearchText() != null && !sc.getSearchText().isEmpty()) {
259                    String holdingsPrefix = DocumentUniqueIDPrefix.getPrefix(DocCategory.WORK.getCode(), org.kuali.ole.docstore.model.enums.DocType.HOLDINGS.getCode(), DocFormat.OLEML.getCode());
260                    if (!sc.getSearchText().contains(holdingsPrefix)) {
261                        serialMap.put(OLEConstants.INSTANCE_ID, holdingsPrefix + "-" + sc.getSearchText());
262                    } else {
263                        serialMap.put(OLEConstants.INSTANCE_ID,holdingsPrefix);
264                    }
265                } else if (sc.getDocField().equalsIgnoreCase(OLEConstants.PO_SEARCH) && sc.getSearchText() != null && !sc.getSearchText().isEmpty()) {
266                    serialMap.put(OLEConstants.PO_ID, sc.getSearchText());
267                } else if (sc.getDocField().equalsIgnoreCase(OLEConstants.SERIAL_SEARCH) && sc.getSearchText() != null && !sc.getSearchText().isEmpty()) {
268                    serialMap.put(OLEConstants.SERIAL_RECEIVING_RECORD_ID, sc.getSearchText());
269                }
270            }
271        }
272        List<OLESerialReceivingDocument> oleSerialReceivingDocuments = (List<OLESerialReceivingDocument>) getBusinessObjectService().findMatching(OLESerialReceivingDocument.class, serialMap);
273        return oleSerialReceivingDocuments;
274    }
275
276}