001package org.kuali.ole.select.util; 002 003import org.apache.commons.httpclient.HttpClient; 004import org.apache.commons.httpclient.NameValuePair; 005import org.apache.commons.httpclient.methods.DeleteMethod; 006import org.apache.commons.io.IOUtils; 007import org.apache.log4j.Logger; 008import org.kuali.ole.OLEConstants; 009import org.kuali.ole.deliver.bo.OleDeliverRequestBo; 010import org.kuali.ole.deliver.bo.OleLoanDocument; 011import org.kuali.ole.describe.bo.DocumentSelectionTree; 012import org.kuali.ole.describe.bo.DocumentTreeNode; 013import org.kuali.ole.describe.form.BoundwithForm; 014import org.kuali.ole.docstore.common.client.DocstoreClientLocator; 015import org.kuali.ole.docstore.common.document.BibTree; 016import org.kuali.ole.docstore.common.document.Holdings; 017import org.kuali.ole.docstore.common.document.HoldingsTree; 018import org.kuali.ole.docstore.common.document.Item; 019import org.kuali.ole.select.businessobject.OleCopy; 020import org.kuali.ole.sys.context.SpringContext; 021import org.kuali.rice.core.api.config.property.ConfigContext; 022import org.kuali.rice.core.api.util.tree.Node; 023import org.kuali.rice.krad.service.BusinessObjectService; 024import org.kuali.rice.krad.service.KRADServiceLocator; 025import org.kuali.rice.krad.util.GlobalVariables; 026import org.kuali.rice.krad.util.KRADConstants; 027 028import java.io.IOException; 029import java.io.InputStream; 030import java.util.ArrayList; 031import java.util.HashMap; 032import java.util.List; 033import java.util.Map; 034 035/** 036 * Created with IntelliJ IDEA. 037 * User: nd6967 038 * Date: 16/7/13 039 * Time: 2:43 PM 040 * To change this template use File | Settings | File Templates. 041 */ 042public class TransferUtil { 043 private static final Logger LOG = Logger.getLogger(TransferUtil.class); 044 private static final TransferUtil transferUtil = new TransferUtil(); 045 046 public static TransferUtil getInstance() { 047 return transferUtil; 048 } 049 050 private DocstoreClientLocator docstoreClientLocator; 051 052 public DocstoreClientLocator getDocstoreClientLocator() { 053 if (null == docstoreClientLocator) { 054 return SpringContext.getBean(DocstoreClientLocator.class); 055 } 056 return docstoreClientLocator; 057 } 058 059 public void deleteVerify(BoundwithForm transferForm, List<String> ids) throws Exception { 060 DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree(); 061 if (transferForm.getTransferLeftTree()) { 062 transferForm.setInDeleteLeftTree("true"); 063 transferForm.setInDeleteRightTree("false"); 064 } else if (transferForm.getTransferRighttree()) { 065 transferForm.setInDeleteLeftTree("false"); 066 transferForm.setInDeleteRightTree("true"); 067 } 068 if (transferForm.getDeleteVerifyResponse().equalsIgnoreCase("success")) { 069 Node<DocumentTreeNode, String> docTree = documentSelectionTree.add(ids, transferForm.getDocType()); 070 transferForm.getDeleteConfirmationTree().setRootElement(docTree); 071 } else { 072 transferForm.setInDeleteLeftTree("false"); 073 transferForm.setInDeleteRightTree("false"); 074 } 075 } 076 077 public void transferInstances(List<String> selectedSourceInstances, String selectedDestBib) { 078 try { 079 getDocstoreClientLocator().getDocstoreClient().transferHoldings(selectedSourceInstances, selectedDestBib); 080 } catch (Exception e) { 081 e.printStackTrace(); 082 } 083 } 084 085 public void transferItems(List<String> selectedSourceItems, String selectedDestInstance) { 086 try { 087 getDocstoreClientLocator().getDocstoreClient().transferItems(selectedSourceItems, selectedDestInstance); 088 } catch (Exception e) { 089 e.printStackTrace(); 090 } 091 092 } 093 094 public void copyToTree(BoundwithForm transferForm, List<String> bibIdentifierListForTree, String treeId) { 095 DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree(); 096 Node<DocumentTreeNode, String> rootNode = documentSelectionTree.addForTransfer(bibIdentifierListForTree, transferForm.getDocType()); 097 if (LOG.isDebugEnabled()){ 098 LOG.debug("Tree id-->" + treeId); 099 } 100 if (treeId != null) { 101 if (treeId.equalsIgnoreCase("leftTree")) { 102 transferForm.getLeftTree().setRootElement(rootNode); 103 transferForm.setLabelText("select"); 104 } 105 if (treeId.equalsIgnoreCase("rightTree")) { 106 transferForm.getRightTree().setRootElement(rootNode); 107 transferForm.setTree2LabelText("select"); 108 } 109 110 } 111 112 } 113 114 @Deprecated 115 public String getDeleteResponseFromDocStore(String operation, String uuid, BoundwithForm transferForm) 116 throws IOException { 117 String restfulUrl = ConfigContext.getCurrentContextConfig().getProperty("docstore.restful.url"); 118 restfulUrl = restfulUrl.concat("/") + uuid; 119 HttpClient httpClient = new HttpClient(); 120 DeleteMethod deleteMethod = new DeleteMethod(restfulUrl); 121 NameValuePair nvp1 = new NameValuePair("identifierType", "UUID"); 122 NameValuePair nvp2 = new NameValuePair("operation", operation); 123 124 NameValuePair category = new NameValuePair("docCategory", transferForm.getDocCategory()); 125 NameValuePair type = new NameValuePair("docType", transferForm.getDocType()); 126 NameValuePair format = new NameValuePair("docFormat", transferForm.getDocFormat()); 127 deleteMethod.setQueryString(new NameValuePair[]{nvp1, nvp2, category, type, format}); 128 int statusCode = httpClient.executeMethod(deleteMethod); 129 if (LOG.isDebugEnabled()){ 130 LOG.debug("statusCode-->" + statusCode); 131 } 132 InputStream inputStream = deleteMethod.getResponseBodyAsStream(); 133 return IOUtils.toString(inputStream); 134 } 135 136 public String checkItemExistsInOleForBibs(List<String> bibIds) throws Exception { 137 boolean itemCheck = false; 138 BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService(); 139 for (String bibId : bibIds) { 140 Map<String, String> map = new HashMap<>(); 141 map.put(OLEConstants.BIB_ID, bibId); 142 List<OleCopy> listOfValues = (List<OleCopy>) boService.findMatching(OleCopy.class, map); 143 if (listOfValues.size() != 0) { 144 itemCheck = true; 145 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DELETE_PURCHASE_ORDER_FAIL_MESSAGE, "Item"); 146 } else { 147 List<String> itemList = getItemsForBib(bibId); 148 for (String itemUuid : itemList) { 149 Map<String, String> uuidMap = new HashMap<>(); 150 uuidMap.put("itemUuid", itemUuid); 151 List<OleLoanDocument> oleLoanDocuments = (List<OleLoanDocument>) boService.findMatching(OleLoanDocument.class, uuidMap); 152 if (oleLoanDocuments.size() != 0) { 153 itemCheck = true; 154 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DELETE_LOANED_FAIL_MESSAGE); 155 break; 156 } else { 157 List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>) boService.findMatching(OleDeliverRequestBo.class, uuidMap); 158 if (oleDeliverRequestBos.size() != 0) { 159 itemCheck = true; 160 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DELETE_REQUEST_FAIL_MESSAGE); 161 break; 162 } 163 } 164 } 165 } 166 } 167 if (!itemCheck) { 168 return "success"; 169 } 170 return "Failed"; 171 } 172 173 private List<String> getItemsForBib(String bibId) throws Exception { 174 BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId); 175 List<String> itemsId = new ArrayList<>(); 176 for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) { 177 for (Item item : holdingsTree.getItems()) { 178 itemsId.add(item.getId()); 179 } 180 } 181 return itemsId; 182 } 183 184 185 private List<String> getItemsForHoldings(String holdingsId) throws Exception { 186 HoldingsTree holdingsTree = getDocstoreClientLocator().getDocstoreClient().retrieveHoldingsTree(holdingsId); 187 List<String> itemsIds = new ArrayList<>(); 188 for (Item item : holdingsTree.getItems()) { 189 itemsIds.add(item.getId()); 190 } 191 return itemsIds; 192 } 193 194 public String checkItemExistsInOleForHoldings(List<String> holdingsIds) throws Exception { 195 BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService(); 196 boolean itemCheck = false; 197 List<String> itemIdList = new ArrayList<>(); 198 for (String holdingsId : holdingsIds) { 199 itemIdList.addAll(getItemsForHoldings(holdingsId)); 200 } 201 for (String itemUuid : itemIdList) { 202 Map<String, String> map = new HashMap<>(); 203 Map<String, String> uuidMap = new HashMap<>(); 204 map.put("itemUUID", itemUuid); 205 uuidMap.put("itemUuid", itemUuid); 206 List<OleCopy> listOfValues = (List<OleCopy>) boService.findMatching(OleCopy.class, map); 207 if (listOfValues.size() != 0) { 208 itemCheck = true; 209 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DELETE_PURCHASE_ORDER_FAIL_MESSAGE, "Item"); 210 break; 211 } else { 212 List<OleLoanDocument> oleLoanDocuments = (List<OleLoanDocument>) boService.findMatching(OleLoanDocument.class, uuidMap); 213 if (oleLoanDocuments.size() != 0) { 214 itemCheck = true; 215 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DELETE_LOANED_FAIL_MESSAGE); 216 break; 217 } else { 218 List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>) boService.findMatching(OleDeliverRequestBo.class, uuidMap); 219 if (oleDeliverRequestBos.size() != 0) { 220 itemCheck = true; 221 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.DELETE_REQUEST_FAIL_MESSAGE); 222 break; 223 } 224 } 225 } 226 } 227 if (!itemCheck) { 228 return "success"; 229 } 230 return "Failed"; 231 } 232 233 public boolean checkItemIsBoundWith(List<String> holdingsIds) throws Exception { 234 boolean isBoundwith = false; 235 for (String id : holdingsIds) { 236 Holdings holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(id); 237 if (holdings.isBoundWithBib()) { 238 isBoundwith = true; 239 break; 240 } 241 } 242 return isBoundwith; 243 } 244 245}