1 package org.kuali.ole.service.impl;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.DocumentUniqueIDPrefix;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.describe.bo.OleReceiptStatus;
7 import org.kuali.ole.describe.form.SerialsReceivingRecordForm;
8 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9 import org.kuali.ole.docstore.common.document.Bib;
10 import org.kuali.ole.docstore.common.document.Holdings;
11 import org.kuali.ole.docstore.common.document.content.enums.DocType;
12 import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
13 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
14 import org.kuali.ole.docstore.common.search.SearchResponse;
15 import org.kuali.ole.docstore.common.search.SearchResult;
16 import org.kuali.ole.docstore.common.search.SearchResultField;
17 import org.kuali.ole.docstore.discovery.model.SearchCondition;
18 import org.kuali.ole.docstore.model.enums.DocCategory;
19 import org.kuali.ole.docstore.model.enums.DocFormat;
20 import org.kuali.ole.pojo.OLESerialReceivingRecord;
21 import org.kuali.ole.select.bo.OLESerialReceivingDocument;
22 import org.kuali.ole.select.businessobject.OleCopy;
23 import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
24 import org.kuali.ole.select.document.OlePurchaseOrderDocument;
25 import org.kuali.ole.service.SerialReceivingSearchService;
26 import org.kuali.ole.sys.context.SpringContext;
27 import org.kuali.ole.util.DocstoreUtil;
28 import org.kuali.rice.krad.service.BusinessObjectService;
29
30 import java.util.*;
31
32
33
34
35
36
37
38
39 public class SerialReceivingSearchServiceImpl implements SerialReceivingSearchService {
40
41 private static final Logger LOG = Logger.getLogger(SerialReceivingSearchService.class);
42 private BusinessObjectService businessObjectService;
43
44 public BusinessObjectService getBusinessObjectService() {
45 if (null == businessObjectService) {
46 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
47 }
48 return businessObjectService;
49 }
50
51 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
52 this.businessObjectService = businessObjectService;
53
54 }
55
56 private DocstoreClientLocator docstoreClientLocator;
57
58 public DocstoreClientLocator getDocstoreClientLocator() {
59
60 if (docstoreClientLocator == null) {
61 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
62 }
63 return docstoreClientLocator;
64 }
65
66 private DocstoreUtil docstoreUtil;
67
68 public DocstoreUtil getDocstoreUtil() {
69
70 if (docstoreUtil == null) {
71 docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
72
73 }
74 return docstoreUtil;
75 }
76
77 public SearchResponse holdingSearch(int startIndex, int searchLimit,String sortingOrder) {
78 SearchResponse searchResponse = new SearchResponse();
79 org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
80 search_Params.setStartIndex(startIndex);
81 search_Params.setPageSize(searchLimit);
82 search_Params.getSearchConditions().add(search_Params.buildSearchCondition("", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), "", ""), ""));
83 search_Params.getSortConditions().add(search_Params.buildSortCondition("Title_sort", sortingOrder));
84 try {
85 searchResponse = getDocstoreClientLocator().getDocstoreClient().search(getSearchParams(search_Params));
86 } catch (Exception ex) {
87 ex.printStackTrace();
88 }
89 return searchResponse;
90 }
91
92 public SearchResponse searchDataFromDocstore(int startIndex, int searchLimit, Set<String> instance_Ids, String title, String issn, String localIdentifier,String sortingOrder) {
93 SearchResponse searchResponse = new SearchResponse();
94 org.kuali.ole.docstore.common.search.SearchParams searchParam = new org.kuali.ole.docstore.common.search.SearchParams();
95 searchParam.setStartIndex(startIndex);
96 searchParam.setPageSize(searchLimit);
97 if (!title.isEmpty()) {
98 searchParam.getSearchConditions().add(searchParam.buildSearchCondition("AND", searchParam.buildSearchField(DocType.HOLDINGS.getCode(), Bib.TITLE, title), "AND"));
99 }
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&methodToCall=docHandler&command=initiate&bibId=" + oleSerialReceivingRecord.getBibId() + "&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&methodToCall=docHandler&command=initiate&bibId=" + oleSerialReceivingRecord.getBibId() + "&instanceId=" + oleSerialReceivingRecord.getInstanceId());
185 } else {
186 oleSerialReceivingRecord.setAction(OLEConstants.SERIAL_SEARCH_SHOW_RECORD);
187 oleSerialReceivingRecord.setHref("serialReceiving?viewId=OLESerialReceivingView&methodToCall=docHandler&docId=" + oleSerialReceiving_Document.getDocumentNumber() + "&command=displayDocSearchView&bibId=" + oleSerialReceiving_Document.getBibId() + "&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&methodToCall=docHandler&docId=" + serialReceivingDocument.getDocumentNumber() + "&command=displayDocSearchView&bibId=" + serialReceivingDocument.getBibId() + "&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 }