View Javadoc
1   package org.kuali.ole.select.lookup;
2   
3   import org.kuali.ole.DocumentUniqueIDPrefix;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.lookup.OleItemSearchLookupableImpl;
6   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
7   import org.kuali.ole.docstore.common.document.*;
8   import org.kuali.ole.docstore.common.document.content.enums.DocType;
9   import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
10  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
11  import org.kuali.ole.docstore.common.search.SearchParams;
12  import org.kuali.ole.docstore.common.search.SearchResponse;
13  import org.kuali.ole.docstore.common.search.SearchResult;
14  import org.kuali.ole.select.bo.OLEInstanceSearch;
15  import org.kuali.ole.sys.context.SpringContext;
16  import org.kuali.ole.util.DocstoreUtil;
17  import org.kuali.rice.krad.lookup.LookupableImpl;
18  import org.kuali.rice.krad.util.GlobalVariables;
19  import org.kuali.rice.krad.util.KRADConstants;
20  import org.kuali.rice.krad.web.form.LookupForm;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * Created with IntelliJ IDEA.
29   * User: arunag
30   * Date: 7/4/13
31   * Time: 11:33 AM
32   * To change this template use File | Settings | File Templates.
33   */
34  public class OLEInstanceSearchLookupableImpl extends LookupableImpl {
35  
36  
37      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleItemSearchLookupableImpl.class);
38      private DocstoreClientLocator docstoreClientLocator;
39      private DocstoreUtil docstoreUtil;
40  
41      public DocstoreUtil getDocstoreUtil() {
42          if (docstoreUtil == null) {
43              docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
44          }
45          return docstoreUtil;
46      }
47  
48      public DocstoreClientLocator getDocstoreClientLocator() {
49          if (docstoreClientLocator == null) {
50              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
51          }
52          return docstoreClientLocator;
53      }
54  
55      private String title;
56      private String author;
57      private String publisher;
58      private String localId;
59  
60      @Override
61      public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
62          LOG.debug("Inside performSearch()");
63          boolean searchFlag = true;
64          for (Map.Entry<String, String> entry : searchCriteria.entrySet()) {
65              if (!entry.getValue().trim().equals("")) {
66                  searchFlag = false;
67                  break;
68              }
69          }
70          if (searchFlag) {
71              GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.INSTANCE_BLANK_SEARCH_ERROR_MSG);
72              return new ArrayList<Object>();
73          }
74          List<OLEInstanceSearch> oleInstanceSearchList = new ArrayList<>();
75          title = searchCriteria.get("title") != null ? searchCriteria.get("title") : "";
76          author = searchCriteria.get("author") != null ? searchCriteria.get("author") : "";
77          publisher = searchCriteria.get("publisher") != null ? searchCriteria.get("publisher") : "";
78          localId = searchCriteria.get("localId") != null ? searchCriteria.get("localId") : "";
79          if (!title.isEmpty() || !author.isEmpty() || !publisher.isEmpty() || !localId.isEmpty()) {
80              SearchParams searchParams = new SearchParams();
81              SearchResponse searchResponse = new SearchResponse();
82              searchParams.setPageSize(25);
83              try {
84                  if (!title.isEmpty() || !author.isEmpty() || !publisher.isEmpty()) {
85                      if (!title.isEmpty()) {
86                          searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(), Bib.TITLE, title), "AND"));
87                      }
88                      if (!author.isEmpty()) {
89                          searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(), Bib.AUTHOR, author), "AND"));
90                      }
91                      if (!publisher.isEmpty()) {
92                          searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(), Bib.PUBLISHER, publisher), "AND"));
93                      }
94                      if (!localId.isEmpty() && (!localId.contains(DocumentUniqueIDPrefix.PREFIX_WORK_HOLDINGS_OLEML))) {
95                          searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(), "holdingsIdentifier", DocumentUniqueIDPrefix.getPrefixedId(DocumentUniqueIDPrefix.PREFIX_WORK_HOLDINGS_OLEML, localId)), "AND"));
96                      }
97                      searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), "id"));
98                      searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
99                      if (searchResponse.getSearchResults() != null && searchResponse.getSearchResults().size() > 0) {
100                         for (SearchResult searchResult : searchResponse.getSearchResults()) {
101                             String fieldName = searchResult.getSearchResultFields().get(0).getFieldName() != null ? searchResult.getSearchResultFields().get(0).getFieldName() : "";
102                             String fieldValue = searchResult.getSearchResultFields().get(0).getFieldValue() != null ? searchResult.getSearchResultFields().get(0).getFieldValue() : "";
103                             if (!fieldName.isEmpty() && fieldName.equalsIgnoreCase("id") && !fieldValue.isEmpty()) {
104                                 oleInstanceSearchList.addAll(getHoldingsBibInfo(fieldValue, localId));
105                             }
106                         }
107                     }
108                 } else if (!localId.isEmpty() && !localId.contains(DocumentUniqueIDPrefix.PREFIX_WORK_HOLDINGS_OLEML)) {
109                     searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.HOLDINGS.getCode(), OLEConstants.LOCALID_SEARCH, localId), ""));
110                     oleInstanceSearchList.addAll(getOleInstanceList(searchParams));
111                 }
112             } catch (Exception ex) {
113                 ex.printStackTrace();
114             }
115         }
116 
117         if (oleInstanceSearchList.size() == 0) {
118             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.NO_RECORD_FOUND);
119         }
120         return oleInstanceSearchList;
121     }
122 
123 
124     public List<OLEInstanceSearch> getHoldingsBibInfo(String bibId, String localId) {
125         List<OLEInstanceSearch> oleInstanceSearches = new ArrayList<>();
126         try {
127             BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId);
128             for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
129                 if (holdingsTree.getHoldings().getId().equalsIgnoreCase(DocumentUniqueIDPrefix.getPrefixedId(DocumentUniqueIDPrefix.PREFIX_WORK_HOLDINGS_OLEML, localId)) || localId.isEmpty()) {
130                     OLEInstanceSearch oleInstanceSearch = new OLEInstanceSearch();
131                     HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
132                     OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdingsTree.getHoldings().getContent());
133                     oleInstanceSearch.setBibId(bibTree.getBib().getId());
134                     oleInstanceSearch.setTitle(bibTree.getBib().getTitle());
135                     oleInstanceSearch.setAuthor(bibTree.getBib().getAuthor());
136                     oleInstanceSearch.setIssn(bibTree.getBib().getIssn());
137                     oleInstanceSearch.setPublisher(bibTree.getBib().getPublisher());
138                     oleInstanceSearch.setLocalId(DocumentUniqueIDPrefix.getDocumentId(bibTree.getBib().getId()));
139                     oleInstanceSearch.setInstanceId(holdingsTree.getHoldings().getId());
140                     oleInstanceSearch.setInstanceLocalId(DocumentUniqueIDPrefix.getDocumentId(holdingsTree.getHoldings().getId()));
141                     oleInstanceSearch.setCallNumber(oleHoldings.getCallNumber()!=null? oleHoldings.getCallNumber().getNumber() : null);
142                     oleInstanceSearch.setCopyNumber(oleHoldings.getCopyNumber());
143                     StringBuffer locationLevel = new StringBuffer("");
144                     oleInstanceSearch.setLocation(getDocstoreUtil().getLocation(oleHoldings.getLocation(), locationLevel));
145                     oleInstanceSearches.add(oleInstanceSearch);
146                 }
147             }
148         } catch (Exception ex) {
149             ex.printStackTrace();
150         }
151         return oleInstanceSearches;
152     }
153 
154     public List<OLEInstanceSearch> getOleInstanceList(SearchParams searchParams) {
155         SearchResponse searchResponse = new SearchResponse();
156         List<OLEInstanceSearch> oleInstanceSearchList = new ArrayList<>();
157         Bib bib = new BibMarc();
158         org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
159         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), "id"));
160         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), Holdings.CALL_NUMBER));
161         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), Holdings.COPY_NUMBER));
162         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), Holdings.LOCATION_NAME));
163         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), OLEConstants.LOCALID_SEARCH));
164         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), "id"));
165         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), bib.TITLE));
166         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), bib.AUTHOR));
167         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), bib.PUBLISHER));
168         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), bib.ISSN));
169         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), OLEConstants.LOCALID_SEARCH));
170         try {
171             searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
172             if (searchResponse.getSearchResults() != null && searchResponse.getSearchResults().size() > 0) {
173                 for (SearchResult searchResult : searchResponse.getSearchResults()) {
174                     OLEInstanceSearch oleInstanceSearch = new OLEInstanceSearch();
175                     if (searchResult.getSearchResultFields().get(0).getFieldName().equalsIgnoreCase("id") && searchResult.getSearchResultFields().get(0).getFieldValue() != null && !searchResult.getSearchResultFields().get(0).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(0).getDocType().equalsIgnoreCase("holdings")) {
176                         oleInstanceSearch.setInstanceId(searchResult.getSearchResultFields().get(0).getFieldValue());
177                     }
178                     if (searchResult.getSearchResultFields().get(1).getFieldName().equalsIgnoreCase(Holdings.CALL_NUMBER) && searchResult.getSearchResultFields().get(1).getFieldValue() != null && !searchResult.getSearchResultFields().get(1).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(1).getDocType().equalsIgnoreCase("holdings")) {
179                         oleInstanceSearch.setCallNumber(searchResult.getSearchResultFields().get(1).getFieldValue());
180                     }
181                     if (searchResult.getSearchResultFields().get(2).getFieldName().equalsIgnoreCase(Holdings.COPY_NUMBER) && searchResult.getSearchResultFields().get(2).getFieldValue() != null && !searchResult.getSearchResultFields().get(2).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(2).getDocType().equalsIgnoreCase("holdings")) {
182                         oleInstanceSearch.setCopyNumber(searchResult.getSearchResultFields().get(2).getFieldValue());
183                     }
184                     if (searchResult.getSearchResultFields().get(3).getFieldName().equalsIgnoreCase(Holdings.LOCATION_NAME) && searchResult.getSearchResultFields().get(3).getFieldValue() != null && !searchResult.getSearchResultFields().get(3).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(3).getDocType().equalsIgnoreCase("holdings")) {
185                         oleInstanceSearch.setLocation(searchResult.getSearchResultFields().get(3).getFieldValue());
186                     }
187                     if (searchResult.getSearchResultFields().get(4).getFieldName().equalsIgnoreCase(OLEConstants.LOCALID_SEARCH) && searchResult.getSearchResultFields().get(4).getFieldValue() != null && !searchResult.getSearchResultFields().get(4).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(4).getDocType().equalsIgnoreCase("holdings")) {
188                         oleInstanceSearch.setInstanceLocalId(searchResult.getSearchResultFields().get(4).getFieldValue());
189                     }
190                     if (searchResult.getSearchResultFields().get(5).getFieldName().equalsIgnoreCase("id") && searchResult.getSearchResultFields().get(5).getFieldValue() != null && !searchResult.getSearchResultFields().get(5).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(5).getDocType().equalsIgnoreCase("bibliographic")) {
191                         oleInstanceSearch.setBibId(searchResult.getSearchResultFields().get(5).getFieldValue());
192                     }
193                     if (searchResult.getSearchResultFields().get(6).getFieldName().equalsIgnoreCase(Bib.TITLE) && searchResult.getSearchResultFields().get(6).getFieldValue() != null && !searchResult.getSearchResultFields().get(6).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(6).getDocType().equalsIgnoreCase("bibliographic")) {
194                         oleInstanceSearch.setTitle(searchResult.getSearchResultFields().get(6).getFieldValue());
195                     }
196                     if (searchResult.getSearchResultFields().get(7).getFieldName().equalsIgnoreCase(Bib.AUTHOR) && searchResult.getSearchResultFields().get(7).getFieldValue() != null && !searchResult.getSearchResultFields().get(7).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(7).getDocType().equalsIgnoreCase("bibliographic")) {
197                         oleInstanceSearch.setAuthor(searchResult.getSearchResultFields().get(7).getFieldValue());
198                     }
199                     if (searchResult.getSearchResultFields().get(8).getFieldName().equalsIgnoreCase(Bib.PUBLISHER) && searchResult.getSearchResultFields().get(8).getFieldValue() != null && !searchResult.getSearchResultFields().get(8).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(8).getDocType().equalsIgnoreCase("bibliographic")) {
200                         oleInstanceSearch.setPublisher(searchResult.getSearchResultFields().get(8).getFieldValue());
201                     }
202                     if (searchResult.getSearchResultFields().get(9).getFieldName().equalsIgnoreCase(Bib.ISSN) && searchResult.getSearchResultFields().get(9).getFieldValue() != null && !searchResult.getSearchResultFields().get(9).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(9).getDocType().equalsIgnoreCase("bibliographic")) {
203                         oleInstanceSearch.setIssn(searchResult.getSearchResultFields().get(9).getFieldValue());
204                     }
205                     if (searchResult.getSearchResultFields().get(10).getFieldName().equalsIgnoreCase(OLEConstants.LOCALID_SEARCH) && searchResult.getSearchResultFields().get(10).getFieldValue() != null && !searchResult.getSearchResultFields().get(10).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(10).getDocType().equalsIgnoreCase("bibliographic")) {
206                         oleInstanceSearch.setLocalId(searchResult.getSearchResultFields().get(10).getFieldValue());
207                     }
208                     oleInstanceSearchList.add(oleInstanceSearch);
209                 }
210             }
211 
212         } catch (Exception ex) {
213             ex.printStackTrace();
214         }
215         return oleInstanceSearchList;
216 
217     }
218 
219 }