View Javadoc

1   package org.kuali.ole.util;
2   
3   import org.jfree.util.Log;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
6   import org.kuali.ole.deliver.bo.OleItemSearch;
7   import org.kuali.ole.deliver.processor.LoanProcessor;
8   import org.kuali.ole.describe.keyvalue.LocationValuesBuilder;
9   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
10  import org.kuali.ole.docstore.common.document.*;
11  import org.kuali.ole.docstore.common.document.content.instance.Item;
12  import org.kuali.ole.docstore.common.document.content.instance.Location;
13  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
14  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
15  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
16  import org.kuali.ole.docstore.common.search.SearchParams;
17  import org.kuali.ole.docstore.common.search.SearchResponse;
18  import org.kuali.ole.docstore.common.search.SearchResult;
19  import org.kuali.ole.docstore.common.search.SearchResultField;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.krad.util.GlobalVariables;
22  import org.kuali.rice.krad.util.KRADConstants;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  /**
30   * Created with IntelliJ IDEA.
31   * User: arunag
32   * Date: 1/9/14
33   * Time: 2:48 PM
34   * To change this template use File | Settings | File Templates.
35   */
36  public class DocstoreUtil {
37      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocstoreUtil.class);
38      private DocstoreClientLocator docstoreClientLocator;
39  
40      public DocstoreClientLocator getDocstoreClientLocator() {
41          if (docstoreClientLocator == null) {
42              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
43          }
44          return docstoreClientLocator;
45      }
46  
47      public boolean isItemAvailableInDocStore(OleDeliverRequestBo oleDeliverRequestBo) {
48          LOG.info("Inside isItemAvailableInDocStore");
49          boolean available = false;
50          Map<String, String> itemMap = new HashMap<String, String>();
51          LocationValuesBuilder locationValuesBuilder = new LocationValuesBuilder();
52          String holdingsId = "";
53          try {
54  
55              try {
56                  org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
57                  org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
58                  SearchResponse searchResponse = null;
59                  search_Params.getSearchConditions().add(search_Params.buildSearchCondition("", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.ITEM_BARCODE, oleDeliverRequestBo.getItemId()), ""));
60                  search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "id"));
61                  search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), "id"));
62                  searchResponse = getDocstoreClientLocator().getDocstoreClient().search(search_Params);
63                  for (SearchResult searchResult : searchResponse.getSearchResults()) {
64                      for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
65                          String fieldName = searchResultField.getFieldName();
66                          String fieldValue = searchResultField.getFieldValue() != null ? searchResultField.getFieldValue() : "";
67                          if (fieldName.equalsIgnoreCase("id") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("holdings")) {
68                              holdingsId = fieldValue;
69                          } else {
70                              oleDeliverRequestBo.setItemUuid(fieldValue);
71                              String itemUUID = fieldValue;
72                          }
73                      }
74                  }
75              } catch (Exception ex) {
76                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "Item Exists");
77                  LOG.error(OLEConstants.ITEM_EXIST + ex);
78              }
79              OleItemSearch itemSearchList = getOleItemSearchList(oleDeliverRequestBo.getItemUuid());
80              if (itemSearchList != null) {
81                  oleDeliverRequestBo.setTitle(itemSearchList.getTitle());
82                  oleDeliverRequestBo.setAuthor(itemSearchList.getAuthor());
83                  oleDeliverRequestBo.setCallNumber(itemSearchList.getCallNumber());
84                  oleDeliverRequestBo.setItemType(itemSearchList.getItemType());
85                  oleDeliverRequestBo.setItemLocation(itemSearchList.getShelvingLocation());
86              }
87              LoanProcessor loanProcessor = new LoanProcessor();
88              String itemXml = loanProcessor.getItemXML(oleDeliverRequestBo.getItemUuid());
89              Item oleItem = loanProcessor.getItemPojo(itemXml);
90              oleDeliverRequestBo.setOleItem(oleItem);
91              oleDeliverRequestBo.setCopyNumber(oleItem.getCopyNumber());
92              oleDeliverRequestBo.setItemStatus(oleItem.getItemStatus().getCodeValue());
93              oleDeliverRequestBo.setClaimsReturnedFlag(oleItem.isClaimsReturnedFlag());
94              locationValuesBuilder.getLocation(oleItem, oleDeliverRequestBo, holdingsId);
95              available = true;
96          } catch (Exception e) {
97              LOG.error(OLEConstants.INVAL_LOC + e);
98          }
99          return available;
100     }
101 
102     public OleItemSearch getOleItemSearchList(String itemUuid) {
103 
104         OleItemSearch oleItemSearch = new OleItemSearch();
105         try {
106             org.kuali.ole.docstore.common.document.Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(itemUuid);
107             ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
108             Item itemContent = itemOlemlRecordProcessor.fromXML(item.getContent());
109             oleItemSearch.setTitle(item.getHolding().getBib().getTitle());
110             oleItemSearch.setAuthor(item.getHolding().getBib().getAuthor());
111             oleItemSearch.setVolumeNumber(itemContent.getVolumeNumber());
112             oleItemSearch.setBibUUID(item.getHolding().getBib().getId());
113             StringBuffer locationLevel = new StringBuffer("");
114             String location = "";
115             if (itemContent.getLocation().getLocationLevel() != null) {
116                 location = getLocation(itemContent.getLocation(), locationLevel);
117                 oleItemSearch.setShelvingLocation(location);
118             } else {
119                 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
120                 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(item.getHolding().getContent());
121                 oleItemSearch.setShelvingLocation(getLocation(oleHoldings.getLocation(), locationLevel));
122             }
123             oleItemSearch.setPublisher(item.getHolding().getBib().getPublisher());
124             oleItemSearch.setCallNumber(itemContent.getCallNumber().getNumber());
125             oleItemSearch.setHoldingUUID(item.getHolding().getId());
126             oleItemSearch.setInstanceUUID(item.getHolding().getId());
127             oleItemSearch.setCopyNumber(itemContent.getCopyNumber());
128             oleItemSearch.setItemBarCode(itemContent.getAccessInformation().getBarcode());
129             oleItemSearch.setItemStatus(itemContent.getItemStatus().getFullValue());
130             oleItemSearch.setItemType(itemContent.getItemType().getCodeValue());
131             oleItemSearch.setItemUUID(item.getId());
132         } catch (Exception ex) {
133             ex.printStackTrace();
134             Log.info("Item does not Exist");
135         }
136         return oleItemSearch;
137     }
138 
139 
140     public boolean isItemAvailableInDocStore(String itemBarcode) {
141         LOG.info("Inside isItemAvailableInDocStore");
142         boolean available = false;
143         try {
144             org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
145             org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
146             SearchResponse searchResponse = null;
147             search_Params.getSearchConditions().add(search_Params.buildSearchCondition("", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.ITEM_BARCODE, itemBarcode), ""));
148             search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "id"));
149             searchResponse = getDocstoreClientLocator().getDocstoreClient().search(search_Params);
150             if (searchResponse.getSearchResults() != null && searchResponse.getSearchResults().size() > 0) {
151                 available = true;
152             }
153         } catch (Exception ex) {
154 
155             LOG.error(OLEConstants.ITEM_EXIST + ex);
156         }
157         return available;
158     }
159 
160 
161     public List<OleItemSearch> getOlItemSearchList(SearchParams searchParams) {
162         SearchResponse searchResponse = new SearchResponse();
163         List<OleItemSearch> oleItemSearchList = new ArrayList<>();
164         Bib bib = new BibMarc();
165         org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
166         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "id"));
167         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.ITEM_BARCODE));
168         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.CALL_NUMBER));
169         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.ITEM_TYPE));
170         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.ITEM_STATUS));
171         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.COPY_NUMBER));
172         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), Holdings.LOCATION_NAME));
173         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.VOLUME_NUMBER));
174         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.HOLDINGS.getCode(), "id"));
175         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), "id"));
176         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), bib.TITLE));
177         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), bib.AUTHOR));
178         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), bib.PUBLISHER));
179         try {
180             searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
181             if (searchResponse.getSearchResults() != null && searchResponse.getSearchResults().size() > 0) {
182                 for (SearchResult searchResult : searchResponse.getSearchResults()) {
183                     OleItemSearch oleItemSearch = new OleItemSearch();
184                     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("item")) {
185                         oleItemSearch.setItemUUID(searchResult.getSearchResultFields().get(0).getFieldValue());
186                     }
187                     if (searchResult.getSearchResultFields().get(8).getFieldName().equalsIgnoreCase("id") && searchResult.getSearchResultFields().get(8).getFieldValue() != null && !searchResult.getSearchResultFields().get(8).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(8).getDocType().equalsIgnoreCase("holdings")) {
188                         oleItemSearch.setHoldingUUID(searchResult.getSearchResultFields().get(8).getFieldValue());
189                         oleItemSearch.setInstanceUUID(searchResult.getSearchResultFields().get(8).getFieldValue());
190                     }
191                     if (searchResult.getSearchResultFields().get(1).getFieldName().equalsIgnoreCase(item.ITEM_BARCODE) && searchResult.getSearchResultFields().get(1).getFieldValue() != null && !searchResult.getSearchResultFields().get(1).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(1).getDocType().equalsIgnoreCase("item")) {
192                         oleItemSearch.setItemBarCode(searchResult.getSearchResultFields().get(1).getFieldValue());
193                     }
194                     if (searchResult.getSearchResultFields().get(2).getFieldName().equalsIgnoreCase(item.CALL_NUMBER) && searchResult.getSearchResultFields().get(2).getFieldValue() != null && !searchResult.getSearchResultFields().get(2).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(2).getDocType().equalsIgnoreCase("item")) {
195                         oleItemSearch.setCallNumber(searchResult.getSearchResultFields().get(2).getFieldValue());
196                     }
197                     if (searchResult.getSearchResultFields().get(5).getFieldName().equalsIgnoreCase(item.COPY_NUMBER) && searchResult.getSearchResultFields().get(5).getFieldValue() != null && !searchResult.getSearchResultFields().get(5).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(5).getDocType().equalsIgnoreCase("item")) {
198                         oleItemSearch.setCopyNumber(searchResult.getSearchResultFields().get(5).getFieldValue());
199                     }
200                     if (searchResult.getSearchResultFields().get(6).getFieldName().equalsIgnoreCase(Holdings.LOCATION_NAME) && searchResult.getSearchResultFields().get(6).getFieldValue() != null && !searchResult.getSearchResultFields().get(6).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(6).getDocType().equalsIgnoreCase("holdings")) {
201                         oleItemSearch.setShelvingLocation(searchResult.getSearchResultFields().get(6).getFieldValue());
202                     }
203                     if (searchResult.getSearchResultFields().get(4).getFieldName().equalsIgnoreCase(item.ITEM_STATUS) && searchResult.getSearchResultFields().get(4).getFieldValue() != null && !searchResult.getSearchResultFields().get(4).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(4).getDocType().equalsIgnoreCase("item")) {
204                         oleItemSearch.setItemStatus(searchResult.getSearchResultFields().get(4).getFieldValue());
205                     }
206                     if (searchResult.getSearchResultFields().get(3).getFieldName().equalsIgnoreCase(item.ITEM_TYPE) && searchResult.getSearchResultFields().get(3).getFieldValue() != null && !searchResult.getSearchResultFields().get(3).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(3).getDocType().equalsIgnoreCase("item")) {
207                         oleItemSearch.setItemType(searchResult.getSearchResultFields().get(3).getFieldValue());
208                     }
209                     if (searchResult.getSearchResultFields().get(7).getFieldName().equalsIgnoreCase(item.VOLUME_NUMBER) && searchResult.getSearchResultFields().get(7).getFieldValue() != null && !searchResult.getSearchResultFields().get(7).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(7).getDocType().equalsIgnoreCase("item")) {
210                         oleItemSearch.setVolumeNumber(searchResult.getSearchResultFields().get(7).getFieldValue());
211                     }
212                     if (searchResult.getSearchResultFields().get(10).getFieldName().equalsIgnoreCase(bib.TITLE) && searchResult.getSearchResultFields().get(10).getFieldValue() != null && !searchResult.getSearchResultFields().get(10).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(10).getDocType().equalsIgnoreCase("bibliographic")) {
213                         oleItemSearch.setTitle(searchResult.getSearchResultFields().get(10).getFieldValue());
214                     }
215                     if (searchResult.getSearchResultFields().get(11).getFieldName().equalsIgnoreCase(bib.AUTHOR) && searchResult.getSearchResultFields().get(11).getFieldValue() != null && !searchResult.getSearchResultFields().get(11).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(11).getDocType().equalsIgnoreCase("bibliographic")) {
216                         oleItemSearch.setAuthor(searchResult.getSearchResultFields().get(11).getFieldValue());
217                     }
218                     if (searchResult.getSearchResultFields().get(12).getFieldName().equalsIgnoreCase(bib.PUBLISHER) && searchResult.getSearchResultFields().get(12).getFieldValue() != null && !searchResult.getSearchResultFields().get(12).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(12).getDocType().equalsIgnoreCase("bibliographic")) {
219                         oleItemSearch.setPublisher(searchResult.getSearchResultFields().get(12).getFieldValue());
220                     }
221                     if (searchResult.getSearchResultFields().get(9).getFieldName().equalsIgnoreCase("id") && searchResult.getSearchResultFields().get(9).getFieldValue() != null && !searchResult.getSearchResultFields().get(9).getFieldValue().isEmpty() && searchResult.getSearchResultFields().get(9).getDocType().equalsIgnoreCase("bibliographic")) {
222                         oleItemSearch.setBibUUID(searchResult.getSearchResultFields().get(9).getFieldValue());
223                     }
224                     oleItemSearchList.add(oleItemSearch);
225                 }
226             }
227         } catch (Exception ex) {
228             ex.printStackTrace();
229         }
230         return oleItemSearchList;
231 
232     }
233 
234     public String getLocation(Location location, StringBuffer locationLevel) {
235         StringBuffer locationName = new StringBuffer("");
236         if (location != null && location.getLocationLevel() != null) {
237             locationName = locationName.append(location.getLocationLevel().getName());
238             locationLevel = locationLevel.append(location.getLocationLevel().getLevel());
239             if (location.getLocationLevel().getLocationLevel() != null) {
240                 locationName = locationName.append("/").append(location.getLocationLevel().getLocationLevel().getName());
241                 locationLevel = locationLevel.append("/").append(location.getLocationLevel().getLocationLevel().getLevel());
242                 if (location.getLocationLevel().getLocationLevel().getLocationLevel() != null) {
243                     locationName = locationName.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getName());
244                     locationLevel = locationLevel.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLevel());
245                     if (location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel() != null) {
246                         locationName = locationName.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getName());
247                         locationLevel = locationLevel.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLevel());
248                         if (location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel() != null) {
249                             locationName = locationName.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getName());
250                             locationLevel = locationLevel.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLevel());
251                         }
252                     }
253                 }
254             }
255         }
256         return locationName.toString();
257     }
258 
259     public OleHoldings getOleHoldings(String instanceUUID) throws Exception {
260         LOG.info("--Inside getOleHoldings---");
261         Holdings holdings = new Holdings();
262         holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(instanceUUID);
263         HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
264         OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdings.getContent());
265         return oleHoldings;
266     }
267 
268     public List<OleItemSearch> getItemBibInfo(String bibId) {
269         List<OleItemSearch> oleItemSearchList = new ArrayList<>();
270         try {
271             BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId);
272             for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
273                 for (org.kuali.ole.docstore.common.document.Item item : holdingsTree.getItems()) {
274                     OleItemSearch oleItemSearch = new OleItemSearch();
275                     ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
276                     Item itemContent = itemOlemlRecordProcessor.fromXML(item.getContent());
277                     oleItemSearch.setBibUUID(bibTree.getBib().getId());
278                     oleItemSearch.setTitle(bibTree.getBib().getTitle());
279                     oleItemSearch.setAuthor(bibTree.getBib().getAuthor());
280                     oleItemSearch.setHoldingUUID(holdingsTree.getHoldings().getId());
281                     oleItemSearch.setInstanceUUID(holdingsTree.getHoldings().getId());
282                     oleItemSearch.setItemBarCode(itemContent.getAccessInformation().getBarcode());
283                     oleItemSearch.setItemUUID(item.getId());
284                     oleItemSearch.setCallNumber(itemContent.getCallNumber().getNumber());
285                     oleItemSearch.setCopyNumber(itemContent.getCopyNumber());
286                     StringBuffer locationLevel = new StringBuffer("");
287                     String location = "";
288                     if (itemContent.getLocation().getLocationLevel() != null) {
289                         location = getLocation(itemContent.getLocation(), locationLevel);
290                         oleItemSearch.setShelvingLocation(location);
291                     } else {
292                         HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
293                         OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(item.getHolding().getContent());
294                         oleItemSearch.setShelvingLocation(getLocation(oleHoldings.getLocation(), locationLevel));
295                     }
296 
297                     oleItemSearch.setItemStatus(itemContent.getItemStatus().getCodeValue());
298                     oleItemSearch.setItemType(itemContent.getItemType().getFullValue());
299                     oleItemSearch.setVolumeNumber(itemContent.getVolumeNumber());
300                     oleItemSearchList.add(oleItemSearch);
301                 }
302             }
303         } catch (Exception ex) {
304             ex.printStackTrace();
305         }
306 
307         return oleItemSearchList;
308     }
309 
310 
311 }