1 package org.kuali.ole.batch.impl;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.batch.service.BatchProcessDeleteService;
6 import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
7 import org.kuali.ole.deliver.bo.OleLoanDocument;
8 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9 import org.kuali.ole.docstore.common.document.BibTree;
10 import org.kuali.ole.docstore.common.document.Holdings;
11 import org.kuali.ole.docstore.common.document.HoldingsTree;
12 import org.kuali.ole.docstore.common.document.Item;
13 import org.kuali.ole.docstore.common.search.*;
14 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
15 import org.kuali.ole.select.businessobject.OleCopy;
16 import org.kuali.ole.sys.context.SpringContext;
17 import org.kuali.rice.krad.service.BusinessObjectService;
18 import org.kuali.rice.krad.service.KRADServiceLocator;
19 import java.util.*;
20
21
22
23
24
25
26
27
28
29 public class BatchProcessDeleteServiceImpl implements BatchProcessDeleteService {
30
31 private static final Logger LOG = Logger.getLogger(BatchProcessDeleteServiceImpl.class);
32 private DocstoreClientLocator docstoreClientLocator;
33
34 public DocstoreClientLocator getDocstoreClientLocator() {
35 if (null == docstoreClientLocator) {
36 return SpringContext.getBean(DocstoreClientLocator.class);
37 }
38 return docstoreClientLocator;
39 }
40
41
42
43
44
45
46
47
48 @Override
49 public int performBatchDelete(List docBibIds, String profileField) throws Exception {
50 int count = docBibIds.size();
51 List<Response> responseList = null;
52
53
54
55
56
57
58 try {
59
60 List<String> docBibIdList = new ArrayList<>();
61 if (docBibIds != null && docBibIds.size() > 0)
62 for (int i = 0; i < docBibIds.size(); i++) {
63 String bibIdDelete = (String) docBibIds.get(i);
64 if (!docBibIdList.contains(bibIdDelete)) {
65 docBibIdList.add(bibIdDelete);
66 }
67 }
68 getDocstoreClientLocator().getDocstoreClient().deleteBibs(docBibIdList);
69 } catch (Exception e) {
70 count = 0;
71 }
72 return count;
73 }
74
75
76
77
78
79
80
81 public List getItemIdList(String bibId) throws Exception {
82 List itemIdList = new ArrayList(0);
83 List<Item> items = new ArrayList<>();
84
85 BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId);
86 List<HoldingsTree> holdingsTreeList = bibTree.getHoldingsTrees();
87 for(HoldingsTree holdingsTree : holdingsTreeList){
88 items.addAll(holdingsTree.getItems());
89 }
90 for(Item item : items){
91 itemIdList.add(item.getId());
92 }
93 return itemIdList;
94 }
95
96
97
98
99
100
101
102
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
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
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
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
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
232
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
247 LOG.error("getBibIdsForBatchDelete Exception:" + e);
248 }
249 return batchDeleteMap;
250 }
251
252 }