001package org.kuali.ole.batch.impl; 002 003import org.apache.log4j.Logger; 004import org.kuali.ole.OLEConstants; 005import org.kuali.ole.batch.service.BatchProcessDeleteService; 006import org.kuali.ole.deliver.bo.OleDeliverRequestBo; 007import org.kuali.ole.deliver.bo.OleLoanDocument; 008import org.kuali.ole.docstore.common.client.DocstoreClientLocator; 009import org.kuali.ole.docstore.common.document.BibTree; 010import org.kuali.ole.docstore.common.document.Holdings; 011import org.kuali.ole.docstore.common.document.HoldingsTree; 012import org.kuali.ole.docstore.common.document.Item; 013import org.kuali.ole.docstore.common.search.*; 014import org.kuali.ole.docstore.model.xmlpojo.ingest.Response; 015import org.kuali.ole.select.businessobject.OleCopy; 016import org.kuali.ole.sys.context.SpringContext; 017import org.kuali.rice.krad.service.BusinessObjectService; 018import org.kuali.rice.krad.service.KRADServiceLocator; 019import java.util.*; 020 021 022/** 023 * Created with IntelliJ IDEA. 024 * User: mjagan 025 * Date: 6/15/13 026 * Time: 4:40 PM 027 * To change this template use File | Settings | File Templates. 028 */ 029public class BatchProcessDeleteServiceImpl implements BatchProcessDeleteService { 030 031 private static final Logger LOG = Logger.getLogger(BatchProcessDeleteServiceImpl.class); 032 private DocstoreClientLocator docstoreClientLocator; 033 034 public DocstoreClientLocator getDocstoreClientLocator() { 035 if (null == docstoreClientLocator) { 036 return SpringContext.getBean(DocstoreClientLocator.class); 037 } 038 return docstoreClientLocator; 039 } 040 041 /** 042 * This method performs to delete the bib records by using list of bib UUid values 043 * @param docBibIds 044 * @param profileField 045 * @return 046 * @throws Exception 047 */ 048 @Override 049 public int performBatchDelete(List docBibIds, String profileField) throws Exception { 050 int count = docBibIds.size(); 051 List<Response> responseList = null; 052 /*if (docBibIds != null && docBibIds.size() > 0) { 053 responseList = getDocstoreHelperService().batchDeleteRecords(docBibIds); 054 } 055 if (responseList != null && responseList.size() > 0) { 056 count = responseList.get(0).getDocuments().size(); 057 }*/ 058 try { 059 // Remove duplicates from docBibIds 060 List<String> docBibIdList = new ArrayList<>(); 061 if (docBibIds != null && docBibIds.size() > 0) 062 for (int i = 0; i < docBibIds.size(); i++) { 063 String bibIdDelete = (String) docBibIds.get(i); 064 if (!docBibIdList.contains(bibIdDelete)) { 065 docBibIdList.add(bibIdDelete); 066 } 067 } 068 getDocstoreClientLocator().getDocstoreClient().deleteBibs(docBibIdList); 069 } catch (Exception e) { 070 count = 0; 071 } 072 return count; 073 } 074 075 /** 076 * This method return list of item UUids by using bib UUid value 077 * @param bibId 078 * @return 079 * @throws Exception 080 */ 081 public List getItemIdList(String bibId) throws Exception { 082 List itemIdList = new ArrayList(0); 083 List<Item> items = new ArrayList<>(); 084 //itemIdList = getDocstoreHelperService().getItemIdList(bibId); 085 BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId); 086 List<HoldingsTree> holdingsTreeList = bibTree.getHoldingsTrees(); 087 for(HoldingsTree holdingsTree : holdingsTreeList){ 088 items.addAll(holdingsTree.getItems()); 089 } 090 for(Item item : items){ 091 itemIdList.add(item.getId()); 092 } 093 return itemIdList; 094 } 095 096 /** 097 * This method perform to fetch the record from docstore by using file data and profile field value and validate the bib record is using in any requisition and loan and boundwith 098 * 099 * @param searchMrcFieldData 100 * @param profileFiled 101 * @return 102 * @throws Exception 103 */ 104 public Map getBibIdsForBatchDelete(String searchMrcFieldData, String profileFiled) throws Exception { 105 Map batchDeleteMap = new HashMap(); 106 BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService(); 107 searchMrcFieldData = searchMrcFieldData.replace("\"", "'"); 108 Map batchMap = getBibIdsForBatchDeleteWithSearchData(searchMrcFieldData, profileFiled); 109 String failureInfo = (String) batchMap.get(OLEConstants.OLEBatchProcess.FAILURE_INFO); 110 if (failureInfo != null) { 111 return batchMap; 112 } 113 List bibIdList = (List)batchMap.get(OLEConstants.BIB_SEARCH); 114 if (bibIdList != null && bibIdList.size() == 0) { 115 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.BIB_REC_NOT_FOUND); 116 return batchDeleteMap; 117 } else if (bibIdList != null && bibIdList.size() == 1) { 118 Map<String, String> map = new HashMap<>(); 119 map.put(OLEConstants.BIB_ID, (String) bibIdList.get(0)); 120 List<OleCopy> listOfValues = (List<OleCopy>) boService.findMatching(OleCopy.class, map); 121 if (listOfValues.size() != 0) { 122 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.LINK_REQ_PO); 123 return batchDeleteMap; 124 } else { 125 List<String> itemIdList = getItemIdList((String) bibIdList.get(0)); 126 for (String itemId : itemIdList) { 127 Map<String, String> itemMap = new HashMap<>(); 128 itemMap.put("itemUuid", itemId); 129 List<OleLoanDocument> oleLoanDocuments = (List<OleLoanDocument>) boService.findMatching(OleLoanDocument.class, itemMap); 130 if (oleLoanDocuments.size() != 0) { 131 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.ITEM_LOANED); 132 return batchDeleteMap; 133 } else { 134 Map<String, String> reqMap = new HashMap<>(); 135 reqMap.put("itemUuid", itemId); 136 List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>) boService.findMatching(OleDeliverRequestBo.class, reqMap); 137 if (oleDeliverRequestBos.size() != 0) { 138 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.ITEM_ONHOLD); 139 return batchDeleteMap; 140 } 141 } 142 } 143 } 144 } else { 145 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.MORE_BIB_RECORDS); 146 return batchDeleteMap; 147 } 148 149 batchDeleteMap.put(OLEConstants.BIB_SEARCH, bibIdList); 150 return batchDeleteMap; 151 } 152 153 public Map getBibIdsForBatchDeleteWithSearchData(String searchData, String dataField) { 154 155 Map batchDeleteMap = new HashMap(); 156 List bibIDsList = new ArrayList(0); 157 SearchParams searchParams = new SearchParams(); 158 SearchCondition searchCondition = new SearchCondition(); 159 SearchField searchField = new SearchField(); 160 try { 161 searchField.setDocType("bibliographic"); 162 if (OLEConstants.OLEBatchProcess.CONTROL_FIELD_001.equals(dataField)) { 163 dataField = OLEConstants.OLEBatchProcess.CONTROL_FIELD_NAME_001; 164 } else { 165 dataField = OLEConstants.PREFIX_FOR_DATA_FIELD + dataField; 166 } 167 searchField.setFieldName(dataField); 168 searchField.setFieldValue(searchData); 169 searchCondition.setSearchField(searchField); 170 searchCondition.setSearchScope("phrase"); 171 searchParams.getSearchConditions().add(searchCondition); 172 //List solrHits = QueryServiceImpl.getInstance().retriveResults(solrQuery); 173 SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams); 174 if(null != searchResponse && null != searchResponse.getSearchResults()){ 175 if(searchResponse.getSearchResults().size() == 0){ 176 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.BIB_REC_NOT_FOUND); 177 return batchDeleteMap; 178 } else if(searchResponse.getSearchResults().size() > 1){ 179 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.MORE_BIB_RECORDS); 180 return batchDeleteMap; 181 } 182 String bibId = null; 183 for(SearchResult searchResult : searchResponse.getSearchResults()){ 184 List<String> holdingsIdentifierList = new ArrayList(); 185 List<String> eHoldingsIdentifierList = new ArrayList(); 186 List<String> holdingsIdentifierListForHoldings = new ArrayList(); 187 for (SearchResultField searchResultField : searchResult.getSearchResultFields()) { 188 if (searchResultField.getFieldName().equalsIgnoreCase("holdingsIdentifier")) { 189 holdingsIdentifierList.add(searchResultField.getFieldValue()); 190 } 191 if(searchResultField.getFieldName().equalsIgnoreCase("id")){ 192 bibId = searchResultField.getFieldValue(); 193 } 194 } 195 if(holdingsIdentifierList.size()>1){ 196 //Modified code for jira 5641 197 for (Object holdingsObject : holdingsIdentifierList) { 198 String holdingsId = holdingsObject.toString(); 199 Holdings holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(holdingsId); 200 if ("electronic".equalsIgnoreCase(holdings.getHoldingsType()) && (!holdings.isBoundWithBib())) { 201 eHoldingsIdentifierList.add(holdings.getId()); 202 } else if("print".equalsIgnoreCase(holdings.getHoldingsType()) && (!holdings.isBoundWithBib())){ 203 holdingsIdentifierListForHoldings.add(holdings.getId()); 204 } else{ 205 } 206 207 } 208 } 209 //Modified code for jira 5641 210 if (holdingsIdentifierListForHoldings.size() > 1) { 211 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.MORE_HOLDINGS); 212 return batchDeleteMap; 213 } 214 if (eHoldingsIdentifierList.size() > 1) { 215 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.MORE_EHOLDINGS); 216 return batchDeleteMap; 217 } 218 // Holdings holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(holdingsIdentifierList.get(0)); 219 BibTree bibTree = new BibTree(); 220 if (bibId != null) { 221 bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId); 222 } 223 List<HoldingsTree> hodingsTreeList = bibTree.getHoldingsTrees(); 224 if (hodingsTreeList.size() > 0) { 225 for (HoldingsTree holdingsTree : hodingsTreeList) { 226 Boolean boundWithBibFlag = holdingsTree.getHoldings().isBoundWithBib(); 227 if (boundWithBibFlag && holdingsTree.getHoldings().getBib() != null) { 228 batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.BIB_BOUNDS_WITH); 229 return batchDeleteMap; 230 } 231 /*batchDeleteMap.put(OLEConstants.OLEBatchProcess.FAILURE_INFO, OLEConstants.OLEBatchProcess.BIB_BOUNDS_WITH); 232 return batchDeleteMap;*/ 233 } 234 } 235 236 for (SearchResultField searchResultField : searchResult.getSearchResultFields()) { 237 if(searchResultField.getFieldName().equalsIgnoreCase("bibIdentifier")){ 238 bibIDsList.add(searchResultField.getFieldValue()); 239 } 240 } 241 } 242 batchDeleteMap.put(OLEConstants.BIB_SEARCH, bibIDsList); 243 } 244 245 } catch (Exception e) { 246 //e.printStackTrace(); 247 LOG.error("getBibIdsForBatchDelete Exception:" + e); 248 } 249 return batchDeleteMap; 250 } 251 252}