View Javadoc
1   package org.kuali.ole.select.document.service.impl;
2   
3   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
4   import org.kuali.ole.docstore.common.document.BibTree;
5   import org.kuali.ole.docstore.common.document.Holdings;
6   import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
7   import org.kuali.ole.docstore.common.document.ids.BibId;
8   import org.kuali.ole.docstore.common.document.ids.HoldingsId;
9   import org.kuali.ole.docstore.model.bo.WorkBibDocument;
10  import org.kuali.ole.docstore.model.bo.WorkItemDocument;
11  import org.kuali.ole.module.purap.PurapConstants;
12  import org.kuali.ole.select.bo.OLELinkPurapDonor;
13  import org.kuali.ole.select.businessobject.*;
14  import org.kuali.ole.select.document.service.OleCopyHelperService;
15  import org.kuali.ole.select.document.service.OleDocstoreHelperService;
16  import org.kuali.ole.sys.OLEConstants;
17  import org.kuali.ole.sys.context.SpringContext;
18  import org.kuali.ole.util.DocstoreUtil;
19  import org.kuali.rice.core.api.util.type.KualiDecimal;
20  import org.kuali.rice.core.api.util.type.KualiInteger;
21  import org.kuali.rice.krad.service.KRADServiceLocator;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  import org.kuali.ole.docstore.common.document.Item;
24  
25  import java.util.*;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  /**
30   * Created with IntelliJ IDEA.
31   * User: vivekb
32   * Date: 11/13/13
33   * Time: 1:06 PM
34   * To change this template use File | Settings | File Templates.
35   */
36  public class OleCopyHelperServiceImpl implements OleCopyHelperService {
37  
38     private DocstoreClientLocator docstoreClientLocator;
39      private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
40      private DocstoreUtil docstoreUtil=new DocstoreUtil();
41      private OleDocstoreHelperService oleDocstoreHelperService = SpringContext
42              .getBean(OleDocstoreHelperService.class);
43      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
44              .getLogger(OleDocstoreHelperServiceImpl.class);
45  
46      public DocstoreClientLocator getDocstoreClientLocator() {
47          if (null == docstoreClientLocator) {
48              return SpringContext.getBean(DocstoreClientLocator.class);
49          }
50          return docstoreClientLocator;
51      }
52  
53      @Override
54      public HashMap<String, List<OleCopy>> getCopyListBasedOnLocation(List<OleCopy> copyList, String bibId) {
55          HashMap<String, List<OleCopy>> copyMap = new HashMap<>();
56          for (OleCopy oleCopy : copyList) {
57              Map map = new HashMap();
58              map.put(OLEConstants.OleCopy.BIB_ID, bibId);
59              map.put(OLEConstants.OleCopy.LOC, oleCopy.getLocation());
60              if (oleCopy.getInstanceId() != null)
61                  map.put(OLEConstants.OleCopy.INSTANCE_ID, oleCopy.getInstanceId());
62              if (oleCopy.getReqItemId() != null)
63                  map.put(OLEConstants.OleCopy.REQ_ITM_ID, oleCopy.getReqItemId());
64              if (oleCopy.getPoDocNum() != null && oleCopy.getPoItemId() != null) {
65                  map.put(OLEConstants.OleCopy.PO_DOC_NUM, oleCopy.getPoDocNum());
66                  map.put(OLEConstants.OleCopy.PO_ITM_ID, oleCopy.getPoItemId());
67              }
68              List<OleCopy> oleCopyList = (List<OleCopy>) KRADServiceLocator.getBusinessObjectService().findMatching(OleCopy.class, map);
69              List<OleCopy>  modCopyList = new ArrayList<>();
70              for(OleCopy copy : copyList){
71                  for(OleCopy modCopy : oleCopyList){
72                      if(modCopy.getCopyId().equals(copy.getCopyId())){
73                          if(!modCopyList.contains(modCopy)){
74                              modCopyList.add(modCopy);
75                          }
76  
77                      }
78                  }
79              }
80              if (copyMap.containsKey(oleCopy.getLocation())) {
81                  List<OleCopy> oleCopiesList = copyMap.get(oleCopy.getLocation());
82                  for (int modCopies=0;modCopies<modCopyList.size();modCopies++) {
83                      if (!oleCopiesList.contains(modCopyList.get(modCopies))) {
84                          copyMap.get(oleCopy.getLocation()).add(modCopyList.get(modCopies));
85                      }
86                  }
87              } else {
88                  copyMap.put(oleCopy.getLocation(), modCopyList);
89              }
90          }
91          return copyMap;
92      }
93  
94      @Override
95      public HashMap<String, List<OleCopy>> getCopyListBasedOnCopyNumber(List<OleCopy> copyList,Integer noOfParts) {
96          HashMap<String, List<OleCopy>> copyMap = new HashMap<>();
97          for (OleCopy oleCopy : copyList) {
98              Map map = new HashMap();
99              if(oleCopy.getCopyNumber()!=null)
100                 map.put(OLEConstants.OleCopy.COPY_NUM, oleCopy.getCopyNumber());
101             map.put(OLEConstants.OleCopy.LOC, oleCopy.getLocation());
102             map.put(OLEConstants.OleCopy.INSTANCE_ID, oleCopy.getInstanceId());
103             if (oleCopy.getReqItemId() != null)
104                 map.put(OLEConstants.OleCopy.REQ_ITM_ID, oleCopy.getReqItemId());
105             if (oleCopy.getPoDocNum() != null && oleCopy.getPoItemId() != null) {
106                 map.put(OLEConstants.OleCopy.PO_DOC_NUM, oleCopy.getPoDocNum());
107                 map.put(OLEConstants.OleCopy.PO_ITM_ID, oleCopy.getPoItemId());
108             }
109             List<OleCopy> oleCopyList = (List<OleCopy>) KRADServiceLocator.getBusinessObjectService().findMatching(OleCopy.class, map);
110             String location = oleCopy.getLocation()!=null?oleCopy.getLocation():"";
111             String copyNum = oleCopy.getCopyNumber()!=null?"-"+oleCopy.getCopyNumber():"";
112             if(copyNum.isEmpty()){
113                 int i=0;
114                 List<OleCopy> copies = new ArrayList<>();
115                 for(OleCopy copy : oleCopyList){
116                     copies.add(copy);
117                     if(noOfParts!=null &&( noOfParts==1 || copy.getPartNumber().equalsIgnoreCase(noOfParts.toString()))){
118                         copyMap.put(location+i++, copies);
119                         copies = new ArrayList<>();
120                     }
121                 }
122             }else{
123                 copyMap.put(location+copyNum, oleCopyList);
124             }
125         }
126         return copyMap;
127     }
128 
129     /**
130      * This method will set copies into list of copies for LineItem.
131      *
132      * @param copyList
133      * @param noOfParts
134      * @return
135      */
136     @Override
137     public List<OleCopies> setCopiesToLineItem(List<OleCopy> copyList, KualiInteger noOfParts, String bibId) {
138         List<OleCopies> copies = new ArrayList<OleCopies>();
139         HashMap<String, List<OleCopy>> copyListBasedOnLocation = getCopyListBasedOnLocation(copyList, bibId);
140         Iterator<Map.Entry<String, List<OleCopy>>> entries = copyListBasedOnLocation.entrySet().iterator();
141         while (entries.hasNext()) {
142             Map.Entry<String, List<OleCopy>> entry = entries.next();
143             List<OleCopy> oleCopyList = entry.getValue();
144             String caption = "";
145             LinkedHashMap<String, String> volumeNumbers = new LinkedHashMap<>();
146             String location = "";
147             int i = 0;
148             KualiInteger staringCopyNumber = oleCopyList != null && oleCopyList.size() > 0 && oleCopyList.get(0).getCopyNumber() != null ?
149                     new KualiInteger(oleCopyList.get(0).getCopyNumber()) : null;
150             for (OleCopy copy : oleCopyList) {
151                 i++;
152                 location = copy.getLocation();
153                 String[] enums = copy.getEnumeration() != null ? copy.getEnumeration().split("\\s") : new String[0];
154                 if (enums != null && enums.length > 0) {
155                     caption = enums[0];
156                     if (caption.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING)){
157                         caption="";
158                     }
159                     if( enums.length > 1) {
160                         volumeNumbers.put(enums[1], enums[1]);
161                     }
162                     if(enums.length==1 && isNumber(enums[0])){
163                         volumeNumbers.put(enums[0], enums[0]);
164                         caption="";
165                     }
166                 }
167             }
168             if (i > 0) {
169                 OleRequisitionCopies oleRequisitionCopies = new OleRequisitionCopies();
170                 StringBuffer volumeNumber = new StringBuffer();
171                 Iterator<Map.Entry<String, String>> volumeEntries = volumeNumbers.entrySet().iterator();
172                 int j = 0;
173                 while (volumeEntries.hasNext()) {
174                     Map.Entry<String, String> volumeEntry = volumeEntries.next();
175                     if (j > 0)
176                         volumeNumber.append(",");
177                     j++;
178                     volumeNumber.append(volumeEntry.getValue());
179                 }
180                 KualiDecimal copyCount = noOfParts.isGreaterThan(KualiInteger.ZERO) ? new KualiDecimal(new KualiInteger(i).divide(noOfParts)) : new KualiDecimal(oleCopyList.size());
181                 oleRequisitionCopies.setItemCopies(copyCount);
182                 oleRequisitionCopies.setParts(noOfParts);
183                 oleRequisitionCopies.setLocationCopies(location);
184                 oleRequisitionCopies.setCaption(caption);
185                 oleRequisitionCopies.setVolumeNumber(volumeNumber.toString());
186                 oleRequisitionCopies.setStartingCopyNumber(staringCopyNumber);
187                 copies.add(oleRequisitionCopies);
188             }
189         }
190         return copies;
191     }
192 
193     public static boolean isNumber(String value) {
194         for (int i = 0; i < value.length(); i++) {
195             if (!Character.isDigit(value.charAt(i)))
196                 return false;
197         }
198         return true;
199     }
200 
201     /**
202      * This method takes RequisitionItem as parameter, it will calculate and set copyList
203      * lineItem
204      *
205      * @param itemCopy
206      * @return OleRequisitionCopies
207      */
208     public List<OleCopy> setCopyValues(OleRequisitionCopies itemCopy, String bibId, List<String> volChar) {
209         KualiInteger noOfItems = KualiInteger.ZERO;
210         KualiInteger noOfCopies = new KualiInteger(itemCopy.getItemCopies().intValue());
211         if (noOfCopies.isGreaterThan(KualiInteger.ZERO)) {
212             if (itemCopy.getParts().isGreaterThan(KualiInteger.ZERO)) {
213                 noOfItems = itemCopy.getParts().multiply(noOfCopies);
214             } else {
215                 noOfItems = new KualiInteger(itemCopy.getItemCopies().intValue());
216             }
217         }
218         int itemCount = volChar.size();
219         if(itemCopy.getParts().equals(new KualiInteger(1)) && volChar.size()==0){
220             itemCount = 1;
221         }
222         List<OleCopy> copyList = new ArrayList<>();
223         Integer copyNum = itemCopy.getStartingCopyNumber() != null ? itemCopy.getStartingCopyNumber().intValue() : null;
224         if (itemCount == itemCopy.getParts().intValue()) {
225             String volNum = "";
226             for (int i = 0, partNumCount = 1, enumCount = 0; i < noOfItems.intValue(); i++) {
227                 OleCopy oleCopy = new OleCopy();
228                 oleCopy.setLocation(itemCopy.getLocationCopies());
229                 volNum = volChar.size()>enumCount ? volChar.get(enumCount):"";
230                 oleCopy.setEnumeration(itemCopy.getCaption() + " " + volNum);
231                 oleCopy.setReceiptStatus(OLEConstants.OleLineItemReceiving.NOT_RECEIVED_STATUS);
232                 oleCopy.setCopyNumber(copyNum!=null?copyNum.toString():null);
233                 oleCopy.setPartNumber(partNumCount + "");
234                 oleCopy.setBibId(bibId);
235                 if (partNumCount == itemCopy.getParts().intValue()) {
236                     partNumCount = 1;
237                     if(copyNum != null){
238                         copyNum++;
239                     }
240                     enumCount = 0;
241                 } else {
242                     enumCount++;
243                     partNumCount++;
244                 }
245                 copyList.add(oleCopy);
246             }
247         }
248         return copyList;
249     }
250 
251 
252     /**
253      * This method takes RequisitionItem as parameter, it will calculate and set copyList
254      * lineItem
255      *
256      * @param itemCopies
257      * @return OleRequisitionCopies
258      */
259     public List<OleCopy> setCopyValuesForList(List<OleCopies> itemCopies, String bibId, BibId bibTree, String oleERSIdentifier) {
260         List<OleCopy> copyList = new ArrayList<>();
261         for (OleCopies itemCopy : itemCopies) {
262             KualiInteger noOfItems = KualiInteger.ZERO;
263             KualiInteger noOfCopies = new KualiInteger(itemCopy.getItemCopies().intValue());
264             if (noOfCopies.isGreaterThan(KualiInteger.ZERO)) {
265                 if (itemCopy.getParts().isGreaterThan(KualiInteger.ZERO)) {
266                     noOfItems = itemCopy.getParts().multiply(noOfCopies);
267                 } else {
268                     noOfItems = new KualiInteger(itemCopy.getItemCopies().intValue());
269                 }
270             }
271             List<String> volChar = new ArrayList<>();
272             String[] volNumbers = itemCopy.getVolumeNumber() != null ? itemCopy.getVolumeNumber().split(",") : new String[0];
273             for (String volStr : volNumbers) {
274                 volChar.add(volStr);
275             }
276             int itemCount = volChar.size();
277             if(itemCopy.getParts().equals(new KualiInteger(1)) && volChar.size()==0){
278                 itemCount = 1;
279             }
280             Integer copyNum = itemCopy.getStartingCopyNumber() != null ? itemCopy.getStartingCopyNumber().intValue() : null;
281             if (itemCount == itemCopy.getParts().intValue()) {
282                 String volNum="";
283                 for (int i = 0, partNumCount = 1, enumCount = 0; i < noOfItems.intValue(); i++) {
284                     OleCopy oleCopy = new OleCopy();
285                     String caption = itemCopy.getCaption()!=null?itemCopy.getCaption():"";
286                     oleCopy.setLocation(itemCopy.getLocationCopies());
287                     volNum = volChar.size()>enumCount ? volChar.get(enumCount):"";
288                     oleCopy.setEnumeration(caption + " " + volNum);
289                     oleCopy.setReceiptStatus(OLEConstants.OleLineItemReceiving.NOT_RECEIVED_STATUS);
290                     oleCopy.setCopyNumber(copyNum!=null?copyNum.toString():null);
291                     oleCopy.setPartNumber(partNumCount + "");
292                     oleCopy.setBibId(bibId);
293                     if (oleERSIdentifier != null) {
294                         oleCopy.setOleERSIdentifier(oleERSIdentifier);
295                     }
296                     if (partNumCount == itemCopy.getParts().intValue()) {
297                         partNumCount = 1;
298                         if(copyNum != null){
299                             copyNum++;
300                         }
301                         enumCount = 0;
302                     } else {
303                         enumCount++;
304                         partNumCount++;
305                     }
306                     copyList.add(oleCopy);
307                 }
308             }
309         }
310         if (bibTree != null) {
311             for (int size = 0; size < copyList.size(); size++) {
312                 for (HoldingsId holdingsId : bibTree.getHoldingsIds()) {
313                     if (holdingsId.getItems() != null && holdingsId.getItems().size() > 0) {
314                         for (String itemId : holdingsId.getItems()) {
315                             if (copyList.size() > size) {
316                                 copyList.get(size).setInstanceId(holdingsId.getId());
317                                 setLocationToCopy(copyList.get(size));
318                                 copyList.get(size).setItemUUID(itemId);
319                                 size++;
320                             }
321                         }
322                     } else {
323                         if (copyList.size() > size) {
324                             copyList.get(size).setInstanceId(holdingsId.getId());
325                             setLocationToCopy(copyList.get(size));
326                             size++;
327                         }
328                     }
329                 }
330             }
331         }
332         return copyList;
333     }
334 
335     private void setLocationToCopy(OleCopy oleCopy){
336         try{
337             if (oleCopy.getInstanceId()!=null){
338                 Holdings holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(oleCopy.getInstanceId());
339                 org.kuali.ole.docstore.common.document.content.instance.OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdings.getContent());
340                 StringBuffer locationName=new StringBuffer("");
341                 String location=null;
342                 location=docstoreUtil.getLocation(oleHoldings.getLocation(),locationName);
343                 if (org.apache.commons.lang3.StringUtils.isNotBlank(location) && oleDocstoreHelperService.isValidLocation(location)){
344                     oleCopy.setLocation(location);
345                 }
346             }
347         }catch (Exception e){
348             LOG.error("Exception "+e);
349         }
350     }
351 
352     public boolean checkCopyEntry(KualiDecimal noOfCopies, String location, Integer itemCount, KualiDecimal noOfCopiesOrdered,
353                                   KualiInteger noOfPartsOrdered, List<OleCopies> copiesList, String volumeNumber, boolean isRoute) {
354         boolean isValid = true;
355         isValid &= checkForCopiesAndLocation(noOfCopies, location);
356         isValid &= checkForItemCopiesGreaterThanQuantity(noOfCopies, noOfCopiesOrdered);
357         if (!isRoute)
358             isValid &= checkForTotalCopiesGreaterThanQuantity(copiesList, noOfCopies, noOfCopiesOrdered);
359         isValid &= volumeNumberValidation(itemCount, noOfPartsOrdered, volumeNumber, copiesList, isRoute);
360         return isValid;
361     }
362 
363     public boolean checkForCopiesAndLocation(KualiDecimal noOfCopies, String location) {
364         boolean isValid = true;
365         if (null == noOfCopies || null == location) {
366             GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
367                     OLEConstants.ITEM_ITEMCOPIES_OR_LOCATIONCOPIES_SHOULDNOT_BE_NULL, new String[]{});
368             isValid = false;
369         }
370         return isValid;
371     }
372 
373     public boolean checkForItemCopiesGreaterThanQuantity(KualiDecimal noOfCopies, KualiDecimal noOfCopiesOrdered) {
374         boolean isValid = true;
375         if (noOfCopies != null && noOfCopiesOrdered != null && noOfCopies.isGreaterThan(noOfCopiesOrdered)) {
376             GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
377                     OLEConstants.ITEM_COPIES_ITEMCOPIES_GREATERTHAN_ITEMCOPIESORDERED, new String[]{});
378             isValid = false;
379         }
380         return isValid;
381     }
382 
383 
384     public boolean checkForTotalCopiesGreaterThanQuantity(List<OleCopies> copyList, KualiDecimal noOfCopies, KualiDecimal noOfCopiesOrdered) {
385         boolean isValid = true;
386         int copies = 0;
387         if (copyList.size() > 0) {
388             for (int itemCopies = 0; itemCopies < copyList.size(); itemCopies++) {
389                 copies = copies + copyList.get(itemCopies).getItemCopies().intValue();
390             }
391             if (noOfCopies != null && noOfCopiesOrdered != null && noOfCopiesOrdered.isLessThan(noOfCopies.add(new KualiDecimal(copies)))) {
392                 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
393                         OLEConstants.TOTAL_OF_ITEM_COPIES_ITEMCOPIES_GREATERTHAN_ITEMCOPIESORDERED, new String[]{});
394                 isValid = false;
395             }
396         }
397         return isValid;
398     }
399 
400     public boolean checkForTotalCopiesGreaterThanQuantityAtSubmit(List<OleCopies> copyList, KualiDecimal noOfCopiesOrdered) {
401         boolean isValid = true;
402         int copies = 0;
403         if (copyList.size() > 0) {
404             for (int itemCopies = 0; itemCopies < copyList.size(); itemCopies++) {
405                 copies = copies + copyList.get(itemCopies).getItemCopies().intValue();
406             }
407             if (noOfCopiesOrdered != null && !noOfCopiesOrdered.equals(new KualiDecimal(copies))) {
408                 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
409                         OLEConstants.TOTAL_OF_ITEM_COPIES_ITEMCOPIES_GREATERTHAN_ITEMCOPIESORDERED, new String[]{});
410                 isValid = false;
411             }
412         } else {
413             GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
414                     OLEConstants.TOTAL_OF_ITEM_COPIES_ITEMCOPIES_GREATERTHAN_ITEMCOPIESORDERED, new String[]{});
415             isValid = false;
416         }
417         return isValid;
418     }
419 
420     public boolean volumeNumberValidation(Integer itemCount, KualiInteger noOfPartsOrdered, String volumeNumber, List<OleCopies> copiesList, boolean isRoute) {
421         boolean isValid = true;
422         int noOfParts = noOfPartsOrdered != null ? noOfPartsOrdered.intValue() : 0;
423         if(noOfParts > 1){
424             if (isRoute) {
425                 for (OleCopies oleCopies : copiesList) {
426                     if (!isValidVolumeNumber(oleCopies.getVolumeNumber(), OLEConstants.VOLUME_NUMBER_PATTERN)) {
427                         GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
428                                 OLEConstants.VOLUME_NUMBER_REGEX_VALIDATIONS, new String[]{});
429                         isValid = false;
430                     }
431                 }
432             } else {
433                 if (!isValidVolumeNumber(volumeNumber, OLEConstants.VOLUME_NUMBER_PATTERN)) {
434                     GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
435                             OLEConstants.VOLUME_NUMBER_REGEX_VALIDATIONS, new String[]{});
436                     isValid = false;
437                 }
438             }
439             if (itemCount != noOfParts) {
440                 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
441                         OLEConstants.VOLUME_NUMBER_VALIDATIONS, new String[]{});
442                 isValid = false;
443             }
444         }
445         return isValid;
446     }
447 
448     public void updateRequisitionAndPOItems(OlePurchaseOrderItem olePurchaseOrderItem,
449                                             OleLineItemReceivingItem oleLineItemReceivingItem, OleCorrectionReceivingItem oleCorrectionReceivingItem, boolean isReceiving) {
450 
451         List<OleCopy> copyList = null;
452         try{
453             if (!isReceiving && oleCorrectionReceivingItem != null) {
454                 copyList = oleCorrectionReceivingItem.getCopyList() != null ? oleCorrectionReceivingItem.getCopyList() : new ArrayList<OleCopy>();
455                 olePurchaseOrderItem.setOleDonors(oleCorrectionReceivingItem.getOleDonors());
456             } else {
457                 copyList = oleLineItemReceivingItem.getCopyList() != null ? oleLineItemReceivingItem.getCopyList() : new ArrayList<OleCopy>();
458                 olePurchaseOrderItem.setOleDonors(oleLineItemReceivingItem.getOleDonors());
459             }
460         }
461         catch (Exception e){
462             throw new RuntimeException(e);
463         }
464         Integer receivedCount = 0;
465         for (OleCopy oleCopy : copyList) {
466             if (!isReceiving && oleCorrectionReceivingItem != null)
467                 oleCopy.setCorrectionItemId(oleCorrectionReceivingItem.getReceivingItemIdentifier());
468             if (oleCopy.getReceiptStatus().equalsIgnoreCase(OLEConstants.OleLineItemReceiving.RECEIVED_STATUS)) {
469                 receivedCount++;
470             }
471         }
472         if (receivedCount == 0) {
473             oleLineItemReceivingItem
474                     .setReceiptStatusId(getReceiptStatusDetails(OLEConstants.PO_RECEIPT_STATUS_NOT_RECEIVED));
475         } else if (receivedCount == copyList.size()) {
476             oleLineItemReceivingItem
477                     .setReceiptStatusId(getReceiptStatusDetails(OLEConstants.PO_RECEIPT_STATUS_FULLY_RECEIVED));
478         } else {
479             oleLineItemReceivingItem
480                     .setReceiptStatusId(getReceiptStatusDetails(OLEConstants.PO_RECEIPT_STATUS_PARTIALLY_RECEIVED));
481         }
482         olePurchaseOrderItem.setReceiptStatusId(oleLineItemReceivingItem.getReceiptStatusId());
483         if (olePurchaseOrderItem.getItemQuantity().equals(new KualiDecimal(1)) && olePurchaseOrderItem.getItemNoOfParts().isGreaterThan(new KualiInteger(1))) {
484             olePurchaseOrderItem.setNoOfCopiesReceived(OLEConstants.OleLineItemReceiving.NOT_APPLICABLE);
485             olePurchaseOrderItem.setNoOfPartsReceived(receivedCount.toString());
486         } else if (olePurchaseOrderItem.getItemQuantity().isGreaterThan(new KualiDecimal(1)) && olePurchaseOrderItem.getItemNoOfParts().equals(new KualiInteger(1))) {
487             olePurchaseOrderItem.setNoOfCopiesReceived(receivedCount.toString());
488             olePurchaseOrderItem.setNoOfPartsReceived(OLEConstants.OleLineItemReceiving.NOT_APPLICABLE);
489         } else if (olePurchaseOrderItem.getItemQuantity().isGreaterThan(new KualiDecimal(1)) && olePurchaseOrderItem.getItemNoOfParts().isGreaterThan(new KualiInteger(1))) {
490             olePurchaseOrderItem.setNoOfCopiesReceived(OLEConstants.OleLineItemReceiving.SEE_COPIES_SECTION);
491             olePurchaseOrderItem.setNoOfPartsReceived(OLEConstants.OleLineItemReceiving.SEE_COPIES_SECTION);
492         } else {
493             olePurchaseOrderItem.setNoOfCopiesReceived(receivedCount.toString());
494             olePurchaseOrderItem.setNoOfPartsReceived(receivedCount.toString());
495         }
496 
497         Integer reqsItemId = copyList.size() > 0 ? copyList.get(0).getReqItemId() : null;
498         Map reqItemMap = new HashMap();
499         reqItemMap.put(OLEConstants.LN_ITM_IDN, reqsItemId);
500         OleRequisitionItem oleRequisitionItem = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleRequisitionItem.class, reqItemMap);
501         if (oleRequisitionItem != null) {
502             oleRequisitionItem.setNoOfCopiesReceived(olePurchaseOrderItem.getNoOfCopiesReceived());
503             oleRequisitionItem.setNoOfPartsReceived(olePurchaseOrderItem.getNoOfPartsReceived());
504             oleRequisitionItem.setOleDonors(olePurchaseOrderItem.getOleDonors());
505             oleRequisitionItem.setReceiptStatusId(olePurchaseOrderItem.getReceiptStatusId());
506             KRADServiceLocator.getBusinessObjectService().save(oleRequisitionItem);
507         }
508         if (isReceiving) {
509             HashMap<Integer, String> receiptStatusMap = new HashMap<Integer, String>();
510             for (OleCopy oleCopy : oleLineItemReceivingItem.getCopyList()) {
511                 receiptStatusMap.put(oleCopy.getCopyId(), oleCopy.getReceiptStatus());
512             }
513             updateReceivedCountFromRCVL(oleLineItemReceivingItem, receiptStatusMap);
514             for (OleCopy purCopy : olePurchaseOrderItem.getCopyList()) {
515                 if (oleLineItemReceivingItem.getItemReceivedTotalQuantity().compareTo(KualiDecimal.ZERO) > 0) {
516                     purCopy.setReceiptStatus(receiptStatusMap.get(purCopy.getCopyId()));
517                     purCopy.setReceivingItemId(oleLineItemReceivingItem.getReceivingItemIdentifier());
518                 }
519             }
520         } else {
521             if (oleCorrectionReceivingItem != null) {
522                 HashMap<Integer, String> receiptStatusMap = new HashMap<Integer, String>();
523                 for (OleCopy oleCopy : oleCorrectionReceivingItem.getCopyList()) {
524                     receiptStatusMap.put(oleCopy.getCopyId(), oleCopy.getReceiptStatus());
525                 }
526                 updateReceivedCountFromRCVC(oleLineItemReceivingItem, receiptStatusMap);
527                 oleCorrectionReceivingItem.setItemReceivedTotalQuantity(oleLineItemReceivingItem.getItemReceivedTotalQuantity());
528                 oleCorrectionReceivingItem.setItemReceivedTotalParts(oleLineItemReceivingItem.getItemReceivedTotalParts());
529                 oleCorrectionReceivingItem.setItemReturnedTotalQuantity(oleLineItemReceivingItem.getItemReturnedTotalQuantity());
530                 oleCorrectionReceivingItem.setItemReturnedTotalParts(oleLineItemReceivingItem.getItemReturnedTotalParts());
531                 for (OleCopy purCopy : olePurchaseOrderItem.getCopyList()) {
532                     purCopy.setReceiptStatus(receiptStatusMap.get(purCopy.getCopyId()));
533                     purCopy.setCorrectionItemId(oleCorrectionReceivingItem.getReceivingItemIdentifier());
534                 }
535             }
536         }
537 
538         KRADServiceLocator.getBusinessObjectService().save(olePurchaseOrderItem);
539     }
540 
541     public int getReceiptStatusDetails(String receiptStatusCd) {
542         int receiptStatusId = 0;
543         Map<String, String> receiptStatusCdMap = new HashMap<String, String>();
544         receiptStatusCdMap.put(OLEConstants.RCPT_STATUS_CD, receiptStatusCd);
545         List<OleReceiptStatus> oleReceiptStatusList = (List) KRADServiceLocator.getBusinessObjectService().findMatching(
546                 OleReceiptStatus.class, receiptStatusCdMap);
547         for (OleReceiptStatus oleReceiptStatus : oleReceiptStatusList) {
548             receiptStatusId = oleReceiptStatus.getReceiptStatusId().intValue();
549         }
550         return receiptStatusId;
551     }
552 
553     private void updateReceivedCountFromRCVL(OleLineItemReceivingItem oleLineItemReceivingItem, HashMap<Integer, String> receiptStatusMap) {
554         HashMap<String, List<OleCopy>> copyListBasedOnCopyNumber = getCopyListBasedOnCopyNumber(oleLineItemReceivingItem.getCopyList(),oleLineItemReceivingItem.getItemOrderedParts().intValue());
555         Iterator<Map.Entry<String, List<OleCopy>>> entries = copyListBasedOnCopyNumber.entrySet().iterator();
556         Integer receivedCopyCount = 0;
557         Integer receivedParts=0;
558         while (entries.hasNext()) {
559             Map.Entry<String, List<OleCopy>> entry = entries.next();
560             List<OleCopy> copyMap = entry.getValue();
561             Integer copyCount = 0;
562             for (OleCopy copy : copyMap) {
563                 if (receiptStatusMap.get(copy.getCopyId()) != null &&
564                         receiptStatusMap.get(copy.getCopyId()).equalsIgnoreCase(OLEConstants.OleLineItemReceiving.RECEIVED_STATUS)) {
565                     copyCount++;
566                     receivedParts++;
567                 }
568             }
569             if (copyCount != 0 &&copyCount <=oleLineItemReceivingItem.getItemOrderedParts().intValue()) {
570                 receivedCopyCount++;
571             }
572         }
573         if (receivedCopyCount > 0) {
574             oleLineItemReceivingItem.setItemReceivedTotalQuantity(new KualiDecimal(receivedCopyCount));
575             oleLineItemReceivingItem.setItemReceivedTotalParts(new KualiDecimal(receivedParts));
576         } else {
577             oleLineItemReceivingItem.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
578             oleLineItemReceivingItem.setItemReceivedTotalParts(KualiDecimal.ZERO);
579         }
580     }
581 
582     private void updateReceivedCountFromRCVC(OleLineItemReceivingItem oleLineItemReceivingItem, HashMap<Integer, String> receiptStatusMap) {
583         HashMap<String, List<OleCopy>> copyListBasedOnCopyNumber = getCopyListBasedOnCopyNumber(oleLineItemReceivingItem.getCopyList(),oleLineItemReceivingItem.getItemOrderedParts().intValue());
584         Iterator<Map.Entry<String, List<OleCopy>>> entries = copyListBasedOnCopyNumber.entrySet().iterator();
585         Integer receivedCopyCount = 0;
586         Integer unReceivedCopyCount = 0;
587         while (entries.hasNext()) {
588             Map.Entry<String, List<OleCopy>> entry = entries.next();
589             List<OleCopy> copyMap = entry.getValue();
590             Integer copyCount = 0;
591             for (OleCopy copy : copyMap) {
592                 if (receiptStatusMap.get(copy.getCopyId()) != null &&
593                         receiptStatusMap.get(copy.getCopyId()).equalsIgnoreCase(OLEConstants.OleLineItemReceiving.RECEIVED_STATUS)) {
594                     copyCount++;
595                 }
596             }
597             if (copyCount == oleLineItemReceivingItem.getItemOrderedParts().intValue()) {
598                 receivedCopyCount++;
599             } else {
600                 unReceivedCopyCount++;
601             }
602         }
603         if (receivedCopyCount > 0) {
604             oleLineItemReceivingItem.setItemReceivedTotalQuantity(new KualiDecimal(receivedCopyCount));
605             oleLineItemReceivingItem.setItemReceivedTotalParts(new KualiDecimal(oleLineItemReceivingItem.getItemOrderedParts().intValue()));
606         } else {
607             oleLineItemReceivingItem.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
608             oleLineItemReceivingItem.setItemReceivedTotalParts(KualiDecimal.ZERO);
609         }
610         if (unReceivedCopyCount > 0) {
611             oleLineItemReceivingItem.setItemReturnedTotalQuantity(new KualiDecimal(unReceivedCopyCount));
612             oleLineItemReceivingItem.setItemReturnedTotalParts(new KualiDecimal(oleLineItemReceivingItem.getItemOrderedParts().intValue()));
613         } else {
614             oleLineItemReceivingItem.setItemReturnedTotalQuantity(KualiDecimal.ZERO);
615             oleLineItemReceivingItem.setItemReturnedTotalParts(KualiDecimal.ZERO);
616         }
617     }
618 
619     public boolean isValidVolumeNumber(String volumeNumber, String pattern) {
620         //  pattern = "^([0-9][,][0-9]*)$";
621         boolean valid = false;
622         //"^[0-9]{1}(([0-9]*-[0-9]*)*|([0-9]* [0-9]*)*|[0-9]*)[0-9]{1}$"
623         if (pattern != null && volumeNumber != null) {
624             Pattern p = Pattern.compile(pattern);
625             Matcher m = p.matcher(volumeNumber);
626             StringBuffer sb = new StringBuffer();
627             boolean result = m.matches();
628 
629             if (result) {
630                 valid = true;
631             }
632         }
633         return valid;
634     }
635 
636 }