View Javadoc
1   package org.kuali.ole.select.util;
2   
3   import org.apache.commons.httpclient.HttpClient;
4   import org.apache.commons.httpclient.NameValuePair;
5   import org.apache.commons.httpclient.methods.DeleteMethod;
6   import org.apache.commons.io.IOUtils;
7   import org.apache.log4j.Logger;
8   import org.kuali.ole.OLEConstants;
9   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
10  import org.kuali.ole.deliver.bo.OleLoanDocument;
11  import org.kuali.ole.describe.bo.DocumentSelectionTree;
12  import org.kuali.ole.describe.bo.DocumentTreeNode;
13  import org.kuali.ole.describe.form.BoundwithForm;
14  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
15  import org.kuali.ole.docstore.common.document.BibTree;
16  import org.kuali.ole.docstore.common.document.Holdings;
17  import org.kuali.ole.docstore.common.document.HoldingsTree;
18  import org.kuali.ole.docstore.common.document.Item;
19  import org.kuali.ole.select.businessobject.OleCopy;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  import org.kuali.rice.core.api.util.tree.Node;
23  import org.kuali.rice.krad.service.BusinessObjectService;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.krad.util.KRADConstants;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * Created with IntelliJ IDEA.
37   * User: nd6967
38   * Date: 16/7/13
39   * Time: 2:43 PM
40   * To change this template use File | Settings | File Templates.
41   */
42  public class TransferUtil {
43      private static final Logger LOG = Logger.getLogger(TransferUtil.class);
44      private static final TransferUtil transferUtil = new TransferUtil();
45  
46      public static TransferUtil getInstance() {
47          return transferUtil;
48      }
49  
50      private DocstoreClientLocator docstoreClientLocator;
51  
52      public DocstoreClientLocator getDocstoreClientLocator() {
53          if (null == docstoreClientLocator) {
54              return SpringContext.getBean(DocstoreClientLocator.class);
55          }
56          return docstoreClientLocator;
57      }
58  
59      public void deleteVerify(BoundwithForm transferForm, List<String> ids) throws Exception {
60          DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree();
61          if (transferForm.getTransferLeftTree()) {
62              transferForm.setInDeleteLeftTree("true");
63              transferForm.setInDeleteRightTree("false");
64          } else if (transferForm.getTransferRighttree()) {
65              transferForm.setInDeleteLeftTree("false");
66              transferForm.setInDeleteRightTree("true");
67          }
68          if (transferForm.getDeleteVerifyResponse().equalsIgnoreCase("success")) {
69              Node<DocumentTreeNode, String> docTree = documentSelectionTree.add(ids, transferForm.getDocType());
70              transferForm.getDeleteConfirmationTree().setRootElement(docTree);
71          } else {
72              transferForm.setInDeleteLeftTree("false");
73              transferForm.setInDeleteRightTree("false");
74          }
75      }
76  
77      public void transferInstances(List<String> selectedSourceInstances, String selectedDestBib) {
78          try {
79              getDocstoreClientLocator().getDocstoreClient().transferHoldings(selectedSourceInstances, selectedDestBib);
80          } catch (Exception e) {
81              e.printStackTrace();
82          }
83      }
84  
85      public void transferItems(List<String> selectedSourceItems, String selectedDestInstance) {
86          try {
87              getDocstoreClientLocator().getDocstoreClient().transferItems(selectedSourceItems, selectedDestInstance);
88          } catch (Exception e) {
89              e.printStackTrace();
90          }
91  
92      }
93  
94      public void copyToTree(BoundwithForm transferForm, List<String> bibIdentifierListForTree, String treeId) {
95          DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree();
96          Node<DocumentTreeNode, String> rootNode = documentSelectionTree.addForTransfer(bibIdentifierListForTree, transferForm.getDocType());
97          if (LOG.isDebugEnabled()){
98              LOG.debug("Tree id-->" + treeId);
99          }
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 }