1 package org.kuali.ole.deliver.lookup;
2
3 import org.kuali.ole.OLEConstants;
4 import org.kuali.ole.OleLookupableImpl;
5 import org.kuali.ole.deliver.bo.OleItemSearch;
6 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
7 import org.kuali.ole.docstore.common.document.Bib;
8 import org.kuali.ole.docstore.common.document.Item;
9 import org.kuali.ole.docstore.common.document.content.enums.DocType;
10 import org.kuali.ole.docstore.common.document.content.instance.Location;
11 import org.kuali.ole.docstore.common.document.content.instance.LocationLevel;
12 import org.kuali.ole.docstore.common.search.SearchParams;
13 import org.kuali.ole.docstore.common.search.SearchResponse;
14 import org.kuali.ole.sys.context.SpringContext;
15 import org.kuali.ole.util.DocstoreUtil;
16 import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
17 import org.kuali.rice.krad.lookup.LookupableImpl;
18 import org.kuali.rice.krad.service.KRADServiceLocator;
19 import org.kuali.rice.krad.util.GlobalVariables;
20 import org.kuali.rice.krad.util.KRADConstants;
21 import org.kuali.rice.krad.web.form.LookupForm;
22
23 import java.util.*;
24
25
26
27
28 public class OleItemSearchLookupableImpl extends OleLookupableImpl {
29
30
31 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleItemSearchLookupableImpl.class);
32 private DocstoreClientLocator docstoreClientLocator;
33 private DocstoreUtil docstoreUtil;
34 private int totalRecCount;
35 private int start;
36 private int pageSize;
37
38 public DocstoreUtil getDocstoreUtil() {
39 if (docstoreUtil == null) {
40 docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
41 }
42 return docstoreUtil;
43 }
44
45 public DocstoreClientLocator getDocstoreClientLocator() {
46 if (docstoreClientLocator == null) {
47 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
48 }
49 return docstoreClientLocator;
50 }
51
52 public boolean getPreviousFlag() {
53 if (this.start == 0)
54 return false;
55 return true;
56 }
57
58 public boolean getNextFlag() {
59 if (this.start + this.pageSize < this.totalRecCount)
60 return true;
61 return false;
62 }
63
64 @Override
65 public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
66 LOG.debug("Inside performSearch()");
67 boolean searchFlag = true;
68
69 Iterator<Map.Entry<String, String>> iter = searchCriteria.entrySet().iterator();
70 while (iter.hasNext()) {
71 Map.Entry<String, String> entry = iter.next();
72 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")) {
73 if (!entry.getValue().trim().equals("")) {
74 searchFlag = false;
75 break;
76 }
77 }
78 }
79
80 if (searchFlag) {
81 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ITM_BLANK_SEARCH_ERROR_MSG);
82 return new ArrayList<Object>();
83 }
84 String title = searchCriteria.get("title") != null ? searchCriteria.get("title") : "";
85 String author = searchCriteria.get("author") != null ? searchCriteria.get("author") : "";
86 String publisher = searchCriteria.get("publisher") != null ? searchCriteria.get("publisher") : "";
87 String callNumber = searchCriteria.get("callNumber") != null ? searchCriteria.get("callNumber") : "";
88 String itemType = searchCriteria.get("itemType") != null ? searchCriteria.get("itemType") : "";
89 String itemBarCode = searchCriteria.get("itemBarCode") != null ? searchCriteria.get("itemBarCode") : "";
90
91 String pageDisplay = searchCriteria.get("pageDisplay") != null ? searchCriteria.get("pageDisplay") : "";
92 String page_Size = searchCriteria.get("pageSize") != null ? searchCriteria.get("pageSize") : "";
93
94 String startIndex = searchCriteria.get("startIndex") != null ? searchCriteria.get("startIndex") : "";
95 List<OleItemSearch> oleItemSearches = new ArrayList<>();
96
97 this.pageSize = Integer.parseInt(page_Size);
98 if (!pageDisplay.isEmpty() && pageDisplay.equalsIgnoreCase("search")) {
99 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
150
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
161
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&methodToCall=load&docCategory=work&docType=holdings&editable=false&docFormat=oleml&docId=" + holdingUuid + "&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