View Javadoc
1   package org.kuali.ole.describe.bo;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.describe.form.BoundwithForm;
6   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
7   import org.kuali.ole.docstore.model.enums.DocType;
8   import org.kuali.ole.select.util.TransferUtil;
9   import org.kuali.ole.sys.context.SpringContext;
10  import org.kuali.rice.core.api.util.tree.Node;
11  import org.kuali.rice.core.api.util.tree.Tree;
12  import org.kuali.rice.krad.util.GlobalVariables;
13  import org.springframework.validation.BindingResult;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  /**
21   * Created by IntelliJ IDEA.
22   * User: nd6967
23   * Date: 1/18/13
24   * Time: 8:28 PM
25   * To change this template use File | Settings | File Templates.
26   */
27  public class TransferRightToLeft {
28      private static final Logger LOG = Logger.getLogger(TransferRightToLeft.class);
29      private DocstoreClientLocator docstoreClientLocator;
30  
31      public DocstoreClientLocator getDocstoreClientLocator() {
32          if (null == docstoreClientLocator) {
33              return SpringContext.getBean(DocstoreClientLocator.class);
34          }
35          return docstoreClientLocator;
36      }
37  
38      public void transferRightToLeft(BoundwithForm transferForm, BindingResult result, HttpServletRequest request,
39                                      HttpServletResponse response) throws Exception {
40          List<String> bibIdentifierListForRightTree = new ArrayList<String>();
41          List<String> bibInstanceListForRightTree = new ArrayList<String>();
42          List<String> bibInstanceListForLeftTree = new ArrayList<String>();
43          List<String> bibInstanceListAllForRightTree = new ArrayList<String>();
44          List<String> bibItemListForRightTree = new ArrayList<String>();
45          List<String> bibIdentifierListForLeftTree = new ArrayList<String>();
46          List<String> destBibIdentifierListForLeftTree = new ArrayList<String>();
47          List<String> destInstanceIdentifierListForLeftTree = new ArrayList<String>();
48  
49  
50          StringBuffer stringBufferLeftTree = new StringBuffer();
51          StringBuffer stringBufferRightTree = new StringBuffer();
52          String isRightBibsSelected = "false";
53          String isLeftItemsSelected = "false";
54          String status1 = "";
55          String status2 = "";
56          List<String> bibIdentifiersListWhenInstanceSelectedForRightTree = new ArrayList<String>();
57          List<String> instanceIdentifiersListWhenItemSelectedForRightTree = new ArrayList<String>();
58          boolean transferInstance = true;
59          boolean transferItem = true;
60          List<String> bibIdentifiersToDelete = new ArrayList<String>();
61          List<String> instanceIdentifiersToDelete = new ArrayList<String>();
62          Tree<DocumentTreeNode, String> leftTree = transferForm.getLeftTree();
63          Node<DocumentTreeNode, String> leftTreeRootElement = leftTree.getRootElement();
64  
65          Tree<DocumentTreeNode, String> rightTree = transferForm.getRightTree();
66          Node<DocumentTreeNode, String> rightTreeRootElement = rightTree.getRootElement();
67          if(transferForm.getDocType().equalsIgnoreCase(OLEConstants.BIB_DOC_TYPE)) {
68              status1 = selectCheckedNodesRightTree(rightTreeRootElement, bibIdentifierListForRightTree,
69                      bibInstanceListForRightTree, bibItemListForRightTree,
70                      stringBufferRightTree, bibIdentifiersListWhenInstanceSelectedForRightTree,
71                      instanceIdentifiersListWhenItemSelectedForRightTree,
72                      bibIdentifiersToDelete, instanceIdentifiersToDelete); //source
73              selectCheckedNodesLeftTree(leftTreeRootElement, bibIdentifierListForLeftTree, stringBufferLeftTree,
74                      destBibIdentifierListForLeftTree, destInstanceIdentifierListForLeftTree);//dest
75          } else if(transferForm.getDocType().equalsIgnoreCase(OLEConstants.HOLDING_DOC_TYPE)) {
76              selectCheckedNodesRightTreeForItemTransfer(rightTreeRootElement, bibInstanceListForRightTree, bibInstanceListAllForRightTree,
77                                                         stringBufferRightTree, bibItemListForRightTree, instanceIdentifiersListWhenItemSelectedForRightTree, instanceIdentifiersToDelete); //source
78  
79              selectCheckedNodesLeftTreeForItemTransfer(leftTreeRootElement, bibInstanceListForLeftTree, stringBufferLeftTree, destInstanceIdentifierListForLeftTree);//dest
80          }
81  
82          isRightBibsSelected = stringBufferRightTree.toString();
83          isLeftItemsSelected = stringBufferLeftTree.toString();
84          if (leftTreeRootElement == null) {
85              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.empty.records", "left");
86          } else if (rightTreeRootElement == null) {
87              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.empty.records", "right");
88          }else if (bibInstanceListForRightTree.size() > 0 && transferForm.getDocType().equalsIgnoreCase(OLEConstants.HOLDING_DOC_TYPE)) {
89              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.invalid", "Holding", "right");
90          }else if (isRightBibsSelected.equalsIgnoreCase("true")) {
91              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.invalid", "Bib", "right");
92          } else if (bibInstanceListForRightTree.size() == 0 && bibItemListForRightTree.size() == 0) {
93              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.selected.none", "right");
94          } else if (isLeftItemsSelected.equalsIgnoreCase("true")) {
95              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.invalid", "Item", "right");
96          } else if (destBibIdentifierListForLeftTree.size() == 0 && destInstanceIdentifierListForLeftTree.size() == 0) {
97              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_LEFT_TREE_SECTION, "error.transfer.selected.none", "left");
98          } else if (bibInstanceListForRightTree.size() > 0 && bibItemListForRightTree.size() > 0) {
99              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.selected.multiple", "Instances", "items", "right");
100         } else if (destBibIdentifierListForLeftTree.size() > 0 && destInstanceIdentifierListForLeftTree.size() > 0) {
101             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_LEFT_TREE_SECTION, "error.transfer.selected.multiple", "Bibs", "instances", "left");
102         } else if (destBibIdentifierListForLeftTree.size() > 1) {
103             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_LEFT_TREE_SECTION, "error.transfer.selected.invalid", "bib", "left");
104         } else if (destInstanceIdentifierListForLeftTree.size() > 1) {
105             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_LEFT_TREE_SECTION, "error.transfer.selected.invalid", "instance", "left");
106         } else if (bibInstanceListForRightTree.size() > 0 && destInstanceIdentifierListForLeftTree.size() > 0) {
107             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.right", "Instances", "Instances");
108         } else if (bibItemListForRightTree.size() > 0 && destBibIdentifierListForLeftTree.size() > 0) {
109             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_LEFT_TREE_SECTION, "error.transfer.right", "Items", "Bibs");
110         }
111         else if (bibInstanceListForRightTree.size() > 0) {
112 
113             for (String bibUuid : bibIdentifiersListWhenInstanceSelectedForRightTree) {
114                 if (destBibIdentifierListForLeftTree.contains(bibUuid)) {
115                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.bib");
116                     transferInstance = false;
117                     break;
118                 }
119             }
120             if (transferInstance) {
121                 String destBibIdentifier = destBibIdentifierListForLeftTree.get(0);
122                 if (bibIdentifiersToDelete.size() > 0) {
123                     transferForm.setDocFormat(OLEConstants.MARC_FORMAT);
124                     transferForm.setDocCategory(OLEConstants.WORK_CATEGORY);
125                     transferForm.setDocType(OLEConstants.BIB_DOC_TYPE);
126                     transferForm.setBibInstanceListForTree1(bibInstanceListForRightTree);
127                     transferForm.setDestBibIdentifier(destBibIdentifier);
128 
129                     // According to OLE-6912 no need to check the item which is exist in Requisition or  PO or Loan
130                     transferForm.setDeleteVerifyResponse("success");
131 //                    transferForm.setDeleteVerifyResponse(TransferUtil.getInstance().checkItemExistsInOleForBibs(bibIdentifiersToDelete));
132                     transferForm.setDeleteIds(bibIdentifiersToDelete);
133                     TransferUtil.getInstance().deleteVerify(transferForm, bibIdentifiersToDelete);
134                     // Holding wouldn't transfer, if item is attached with Loan, PO, etc.
135                     if (transferForm.getDeleteVerifyResponse().equalsIgnoreCase(OLEConstants.OLEBatchProcess.RESPONSE_STATUS_FAILED)) {
136                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, OLEConstants.TRANSFER_FAIL_MESSAGE_ITEM_ATTACHED_OLE);
137                     }
138                     return;
139                 }
140                 TransferUtil.getInstance().transferInstances(bibInstanceListForRightTree, destBibIdentifier);
141                 TransferUtil.getInstance().copyToTree(transferForm, bibIdentifierListForRightTree, OLEConstants.RIGHT_TREE);
142                 TransferUtil.getInstance().copyToTree(transferForm, bibIdentifierListForLeftTree, OLEConstants.LEFT_TREE);
143                 GlobalVariables.getMessageMap().putInfoForSectionId(OLEConstants.TRANSFER_LEFT_TREE_SECTION, "info.transfer", "Instances");
144             }
145         } else if (bibItemListForRightTree.size() > 0) {
146             //If Transfer of items happen between same left holding and right holding.
147             for (String instanceUuid : instanceIdentifiersListWhenItemSelectedForRightTree) {
148                 if (destInstanceIdentifierListForLeftTree.contains(instanceUuid)) {
149                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.item");
150                     transferItem = false;
151                     break;
152                 }
153             }
154             if (transferItem) {
155                 String destInstanceIdentifier = destInstanceIdentifierListForLeftTree.get(0);
156                 if (instanceIdentifiersToDelete.size() > 0) {
157                     transferForm.setDocFormat(OLEConstants.OLEML_FORMAT);
158                     transferForm.setDocCategory(OLEConstants.WORK_CATEGORY);
159                     transferForm.setDocType(OLEConstants.HOLDING_DOC_TYPE);
160                     transferForm.setBibItemListForTree1(bibItemListForRightTree);
161                     transferForm.setDestInstanceIdentifier(destInstanceIdentifier);
162                     // According to OLE-6912 no need to check the item which is exist in Requisition or  PO or Loan
163                     transferForm.setDeleteVerifyResponse("success");
164 //                    transferForm.setDeleteVerifyResponse(TransferUtil.getInstance().checkItemExistsInOleForHoldings(instanceIdentifiersToDelete));
165                     transferForm.setDeleteIds(instanceIdentifiersToDelete);
166                     TransferUtil.getInstance().deleteVerify(transferForm, instanceIdentifiersToDelete);
167                     // Item wouldn't transfer, if item is attached with Loan, PO, etc.
168                     if (transferForm.getDeleteVerifyResponse().equalsIgnoreCase(OLEConstants.OLEBatchProcess.RESPONSE_STATUS_FAILED)) {
169                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, OLEConstants.TRANSFER_FAIL_MESSAGE_ITEM_ATTACHED_OLE);
170                     }
171                     return;
172                 }
173                 TransferUtil.getInstance().transferItems(bibItemListForRightTree, destInstanceIdentifier);
174 
175                 if (transferForm.getDocType().equalsIgnoreCase((DocType.BIB.getDescription()))) {
176                     TransferUtil.getInstance().copyToTree(transferForm, bibIdentifierListForRightTree, OLEConstants.RIGHT_TREE);
177                     TransferUtil.getInstance().copyToTree(transferForm, bibIdentifierListForLeftTree, OLEConstants.LEFT_TREE);
178                 } else if (transferForm.getDocType().equalsIgnoreCase((DocType.HOLDINGS.getCode()))) {
179                     TransferUtil.getInstance().copyToTree(transferForm, bibInstanceListAllForRightTree, OLEConstants.RIGHT_TREE);
180                     TransferUtil.getInstance().copyToTree(transferForm, bibInstanceListForLeftTree, OLEConstants.LEFT_TREE);
181                 }
182                 GlobalVariables.getMessageMap().putInfoForSectionId(OLEConstants.TRANSFER_LEFT_TREE_SECTION, "info.transfer", "Items");
183             }
184         } else {
185             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.TRANSFER_RIGHT_TREE_SECTION, "error.transfer.bib");
186         }
187         return;
188     }
189 
190     private void selectCheckedNodesLeftTreeForItemTransfer(Node<DocumentTreeNode, String> rootElement, List<String> bibInstanceListForLeftTree,
191                                                            StringBuffer stringBufferLeftTree,
192                                                            List<String> destInstanceIdentifierListForLeftTree) {
193 
194         DocumentTreeNode documentTreeNode;
195         if (rootElement != null) {
196             List<Node<DocumentTreeNode, String>> instanceList = rootElement.getChildren();
197 
198                 for (Node<DocumentTreeNode, String> instanceNode : instanceList) {
199                     documentTreeNode = instanceNode.getData();
200                     bibInstanceListForLeftTree.add(instanceNode.getNodeType());
201                     if (documentTreeNode.isSelect()) {
202                         String instanceUUID = instanceNode.getNodeType();
203                         destInstanceIdentifierListForLeftTree.add(instanceUUID);
204                     }
205                     List<Node<DocumentTreeNode, String>> itemList = instanceNode.getChildren();
206                     for (Node<DocumentTreeNode, String> itemNode : itemList) {
207                         documentTreeNode = itemNode.getData();
208                         if (documentTreeNode.isSelect()) {
209                             stringBufferLeftTree.append("true");
210                             break;
211                         }
212                     }
213 
214                 }
215         }
216 
217     }
218 
219     private void selectCheckedNodesRightTreeForItemTransfer(Node<DocumentTreeNode, String> rootElement, List<String> bibInstanceListForRightTree,
220                                                             List<String> bibInstanceListAllForRightTree,
221                                                             StringBuffer stringBufferRightTree,
222                                                             List<String> bibItemListForRightTree,
223                                                             List<String> instanceIdentifiersListWhenItemSelectedForRightTree,
224                                                             List<String> instanceIdentifiersToDelete) {
225         DocumentTreeNode documentTreeNode;
226         int itemCount = 0;
227         List<String> instanceIdentifiersListTemp = new ArrayList<String>();
228         if (rootElement != null) {
229             List<Node<DocumentTreeNode, String>> instanceList = rootElement.getChildren();
230             for (Node<DocumentTreeNode, String> instanceNode : instanceList) {
231                 String instanceUUID = "";
232                 documentTreeNode = instanceNode.getData();
233                 instanceUUID = instanceNode.getNodeType();
234                 bibInstanceListAllForRightTree.add(instanceUUID);
235                 if (documentTreeNode.isSelect()) {
236                     LOG.info("documentTreeNode.isSelectTree1()-->" + documentTreeNode.isSelect());
237 //                    stringBufferRightTree.append("true");
238                     bibInstanceListForRightTree.add(instanceUUID);
239                     break;
240                 }
241                 List<Node<DocumentTreeNode, String>> itemList = null;
242                 instanceIdentifiersListTemp.clear();
243                 itemCount = 0;
244                 itemList = instanceNode.getChildren();
245                 for (Node<DocumentTreeNode, String> itemNode : itemList) {
246                         //itemCount=0;
247                         documentTreeNode = itemNode.getData();
248                         if (documentTreeNode.isSelect()) {
249                             itemCount++;
250                             String itemUUID = itemNode.getNodeType();
251                             bibItemListForRightTree.add(itemUUID);
252                             instanceIdentifiersListWhenItemSelectedForRightTree.add(instanceUUID);
253                         }
254                     } //item loop end
255                     if (itemCount == itemList.size()) {
256                         instanceIdentifiersListTemp.add(instanceUUID);
257                         LOG.debug("in if of no items instanceidenifiersListTemp " + instanceIdentifiersListTemp);
258                         //status="Instance will remain with no items if this transfer takes place. Instance must have atleast one item. Transfer failed";
259                 }
260                 //instance loop end
261 
262                 instanceIdentifiersToDelete.addAll(instanceIdentifiersListTemp);
263                 LOG.debug("instanceIdentifiersToDelete " + instanceIdentifiersToDelete);
264 
265             }//bib loop end
266         }
267 
268 
269     }
270 
271     private String selectCheckedNodesRightTree(Node<DocumentTreeNode, String> rootElement,
272                                                List<String> bibIdentifierListForTree1,
273                                                List<String> bibInstanceListForTree1, List<String> bibItemListForTree1,
274                                                StringBuffer stringBufferRightTree,
275                                                List<String> bibIdentifiersListWhenInstanceSelectedForRightTree,
276                                                List<String> instanceIdentifiersListWhenItemSelectedForRightTree,
277                                                List<String> bibIdentifiersToDelete,
278                                                List<String> instanceIdentifiersToDelete) {
279 
280         DocumentTreeNode documentTreeNode;
281         String status = "success";
282         int instanceCount = 0;
283         int itemCount = 0;
284         List<String> instanceidenifiersListTemp = new ArrayList<String>();
285         if (rootElement != null) {
286             List<Node<DocumentTreeNode, String>> list = rootElement.getChildren();
287             for (Node<DocumentTreeNode, String> marcNode : list) {
288                 instanceCount = 0;
289                 String instanceUUID = "";
290                 documentTreeNode = marcNode.getData();
291                 String bibUUID = marcNode.getNodeType();
292                 LOG.debug(" bibUUID " + bibUUID);
293                 bibIdentifierListForTree1.add(bibUUID);
294                 LOG.info("documentTreeNode.isSelectTree1()-->" + documentTreeNode.isSelect());
295                 if (documentTreeNode.isSelect()) {
296                     stringBufferRightTree.append("true");
297                     break;
298                 }
299                 List<Node<DocumentTreeNode, String>> instanceList = marcNode.getChildren();
300                 List<Node<DocumentTreeNode, String>> itemList = null;
301                 instanceidenifiersListTemp.clear();
302                 for (Node<DocumentTreeNode, String> instanceNode : instanceList) {
303                     itemCount = 0;
304                     documentTreeNode = instanceNode.getData();
305                     instanceUUID = instanceNode.getNodeType();
306                     if (documentTreeNode.isSelect()) {
307                         instanceCount++;
308                         bibInstanceListForTree1.add(instanceUUID);
309                         bibIdentifiersListWhenInstanceSelectedForRightTree.add(bibUUID);
310                     }
311                     itemList = instanceNode.getChildren();
312                     for (Node<DocumentTreeNode, String> itemNode : itemList) {
313                         //itemCount=0;
314                         documentTreeNode = itemNode.getData();
315                         if (documentTreeNode.isSelect()) {
316                             itemCount++;
317                             String itemUUID = itemNode.getNodeType();
318                             bibItemListForTree1.add(itemUUID);
319                             instanceIdentifiersListWhenItemSelectedForRightTree.add(instanceUUID);
320                         }
321                     } //item loop end
322                     if (itemCount == itemList.size()) {
323                         instanceidenifiersListTemp.add(instanceUUID);
324                         LOG.debug("in if of no items instanceidenifiersListTemp " + instanceidenifiersListTemp);
325                         //status="Instance will remain with no items if this transfer takes place. Instance must have atleast one item. Transfer failed";
326                     }
327                 } //instance loop end
328                 if (instanceCount == instanceList.size()) {
329                     bibIdentifiersToDelete.add(bibUUID);
330                     LOG.debug("in if of no instances bibIdentifierToDelete " + bibIdentifiersToDelete);
331                     status
332                             = "Bib will remain with no instances if this transfer takes place. Bib must have atleast one instance. Transfer failed";
333                 }
334                 instanceIdentifiersToDelete.addAll(instanceidenifiersListTemp);
335                 LOG.debug("instanceIdentifiersToDelete " + instanceIdentifiersToDelete);
336                 //                else if(itemCount==itemList.size()){
337                 //                    status="Instance will remain with no items if this transfer takes place. Instance must have atleast one item. Transfer failed";
338                 //                }
339             }//bib loop end
340         }
341         return status;
342     }
343 
344     private void selectCheckedNodesLeftTree(Node<DocumentTreeNode, String> rootElement,
345                                             List<String> bibIdentifierListForTree2, StringBuffer stringBufferLeftTree,
346                                             List<String> destBibIdentifierListForTree2,
347                                             List<String> destInstanceIdentifierListForTree2) {
348 
349         DocumentTreeNode documentTreeNode;
350         if (rootElement != null) {
351             List<Node<DocumentTreeNode, String>> list = rootElement.getChildren();
352             for (Node<DocumentTreeNode, String> marcNode : list) {
353                 documentTreeNode = marcNode.getData();
354                 String bibUUID = marcNode.getNodeType();
355                 bibIdentifierListForTree2.add(bibUUID);
356                 LOG.info("documentTreeNode.isSelectTree1()-->" + documentTreeNode.isSelect());
357                 if (documentTreeNode.isSelect()) {
358                     destBibIdentifierListForTree2.add(bibUUID);
359                 }
360                 List<Node<DocumentTreeNode, String>> instanceList = marcNode.getChildren();
361                 for (Node<DocumentTreeNode, String> instanceNode : instanceList) {
362                     documentTreeNode = instanceNode.getData();
363                     if (documentTreeNode.isSelect()) {
364                         String instanceUUID = instanceNode.getNodeType();
365                         destInstanceIdentifierListForTree2.add(instanceUUID);
366                     }
367                     List<Node<DocumentTreeNode, String>> itemList = instanceNode.getChildren();
368                     for (Node<DocumentTreeNode, String> itemNode : itemList) {
369                         documentTreeNode = itemNode.getData();
370                         if (documentTreeNode.isSelect()) {
371                             stringBufferLeftTree.append("true");
372                             break;
373                         }
374                     }
375 
376                 }
377             }
378         }
379     }
380 }