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) {
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 (partNumCount == itemCopy.getParts().intValue()) {
294                         partNumCount = 1;
295                         if(copyNum != null){
296                             copyNum++;
297                         }
298                         enumCount = 0;
299                     } else {
300                         enumCount++;
301                         partNumCount++;
302                     }
303                     copyList.add(oleCopy);
304                 }
305             }
306         }
307         if (bibTree != null) {
308             for (int size = 0; size < copyList.size(); size++) {
309                 for (HoldingsId holdingsId : bibTree.getHoldingsIds()) {
310                     if (holdingsId.getItems() != null && holdingsId.getItems().size() > 0) {
311                         for (String itemId : holdingsId.getItems()) {
312                             if (copyList.size() > size) {
313                                 copyList.get(size).setInstanceId(holdingsId.getId());
314                                 setLocationToCopy(copyList.get(size));
315                                 copyList.get(size).setItemUUID(itemId);
316                                 size++;
317                             }
318                         }
319                     } else {
320                         if (copyList.size() > size) {
321                             copyList.get(size).setInstanceId(holdingsId.getId());
322                             setLocationToCopy(copyList.get(size));
323                             size++;
324                         }
325                     }
326                 }
327             }
328         }
329         return copyList;
330     }
331 
332     private void setLocationToCopy(OleCopy oleCopy){
333         try{
334             if (oleCopy.getInstanceId()!=null){
335                 Holdings holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(oleCopy.getInstanceId());
336                 org.kuali.ole.docstore.common.document.content.instance.OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdings.getContent());
337                 StringBuffer locationName=new StringBuffer("");
338                 String location=null;
339                 location=docstoreUtil.getLocation(oleHoldings.getLocation(),locationName);
340                 if (org.apache.commons.lang3.StringUtils.isNotBlank(location) && oleDocstoreHelperService.isValidLocation(location)){
341                     oleCopy.setLocation(location);
342                 }
343             }
344         }catch (Exception e){
345             LOG.error("Exception "+e);
346         }
347     }
348 
349     public boolean checkCopyEntry(KualiDecimal noOfCopies, String location, Integer itemCount, KualiDecimal noOfCopiesOrdered,
350                                   KualiInteger noOfPartsOrdered, List<OleCopies> copiesList, String volumeNumber, boolean isRoute) {
351         boolean isValid = true;
352         isValid &= checkForCopiesAndLocation(noOfCopies, location);
353         isValid &= checkForItemCopiesGreaterThanQuantity(noOfCopies, noOfCopiesOrdered);
354         if (!isRoute)
355             isValid &= checkForTotalCopiesGreaterThanQuantity(copiesList, noOfCopies, noOfCopiesOrdered);
356         isValid &= volumeNumberValidation(itemCount, noOfPartsOrdered, volumeNumber, copiesList, isRoute);
357         return isValid;
358     }
359 
360     public boolean checkForCopiesAndLocation(KualiDecimal noOfCopies, String location) {
361         boolean isValid = true;
362         if (null == noOfCopies || null == location) {
363             GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
364                     OLEConstants.ITEM_ITEMCOPIES_OR_LOCATIONCOPIES_SHOULDNOT_BE_NULL, new String[]{});
365             isValid = false;
366         }
367         return isValid;
368     }
369 
370     public boolean checkForItemCopiesGreaterThanQuantity(KualiDecimal noOfCopies, KualiDecimal noOfCopiesOrdered) {
371         boolean isValid = true;
372         if (noOfCopies != null && noOfCopiesOrdered != null && noOfCopies.isGreaterThan(noOfCopiesOrdered)) {
373             GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
374                     OLEConstants.ITEM_COPIES_ITEMCOPIES_GREATERTHAN_ITEMCOPIESORDERED, new String[]{});
375             isValid = false;
376         }
377         return isValid;
378     }
379 
380 
381     public boolean checkForTotalCopiesGreaterThanQuantity(List<OleCopies> copyList, KualiDecimal noOfCopies, KualiDecimal noOfCopiesOrdered) {
382         boolean isValid = true;
383         int copies = 0;
384         if (copyList.size() > 0) {
385             for (int itemCopies = 0; itemCopies < copyList.size(); itemCopies++) {
386                 copies = copies + copyList.get(itemCopies).getItemCopies().intValue();
387             }
388             if (noOfCopies != null && noOfCopiesOrdered != null && noOfCopiesOrdered.isLessThan(noOfCopies.add(new KualiDecimal(copies)))) {
389                 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
390                         OLEConstants.TOTAL_OF_ITEM_COPIES_ITEMCOPIES_GREATERTHAN_ITEMCOPIESORDERED, new String[]{});
391                 isValid = false;
392             }
393         }
394         return isValid;
395     }
396 
397     public boolean checkForTotalCopiesGreaterThanQuantityAtSubmit(List<OleCopies> copyList, KualiDecimal noOfCopiesOrdered) {
398         boolean isValid = true;
399         int copies = 0;
400         if (copyList.size() > 0) {
401             for (int itemCopies = 0; itemCopies < copyList.size(); itemCopies++) {
402                 copies = copies + copyList.get(itemCopies).getItemCopies().intValue();
403             }
404             if (noOfCopiesOrdered != null && !noOfCopiesOrdered.equals(new KualiDecimal(copies))) {
405                 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
406                         OLEConstants.TOTAL_OF_ITEM_COPIES_ITEMCOPIES_GREATERTHAN_ITEMCOPIESORDERED, new String[]{});
407                 isValid = false;
408             }
409         } else {
410             GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
411                     OLEConstants.TOTAL_OF_ITEM_COPIES_ITEMCOPIES_GREATERTHAN_ITEMCOPIESORDERED, new String[]{});
412             isValid = false;
413         }
414         return isValid;
415     }
416 
417     public boolean volumeNumberValidation(Integer itemCount, KualiInteger noOfPartsOrdered, String volumeNumber, List<OleCopies> copiesList, boolean isRoute) {
418         boolean isValid = true;
419         int noOfParts = noOfPartsOrdered != null ? noOfPartsOrdered.intValue() : 0;
420         if(noOfParts > 1){
421             if (isRoute) {
422                 for (OleCopies oleCopies : copiesList) {
423                     if (!isValidVolumeNumber(oleCopies.getVolumeNumber(), OLEConstants.VOLUME_NUMBER_PATTERN)) {
424                         GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
425                                 OLEConstants.VOLUME_NUMBER_REGEX_VALIDATIONS, new String[]{});
426                         isValid = false;
427                     }
428                 }
429             } else {
430                 if (!isValidVolumeNumber(volumeNumber, OLEConstants.VOLUME_NUMBER_PATTERN)) {
431                     GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
432                             OLEConstants.VOLUME_NUMBER_REGEX_VALIDATIONS, new String[]{});
433                     isValid = false;
434                 }
435             }
436             if (itemCount != noOfParts) {
437                 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
438                         OLEConstants.VOLUME_NUMBER_VALIDATIONS, new String[]{});
439                 isValid = false;
440             }
441         }
442         return isValid;
443     }
444 
445     public void updateRequisitionAndPOItems(OlePurchaseOrderItem olePurchaseOrderItem,
446                                             OleLineItemReceivingItem oleLineItemReceivingItem, OleCorrectionReceivingItem oleCorrectionReceivingItem, boolean isReceiving) {
447 
448         List<OleCopy> copyList = null;
449         try{
450             if (!isReceiving && oleCorrectionReceivingItem != null) {
451                 copyList = oleCorrectionReceivingItem.getCopyList() != null ? oleCorrectionReceivingItem.getCopyList() : new ArrayList<OleCopy>();
452                 olePurchaseOrderItem.setOleDonors(oleCorrectionReceivingItem.getOleDonors());
453             } else {
454                 copyList = oleLineItemReceivingItem.getCopyList() != null ? oleLineItemReceivingItem.getCopyList() : new ArrayList<OleCopy>();
455                 olePurchaseOrderItem.setOleDonors(oleLineItemReceivingItem.getOleDonors());
456             }
457         }
458         catch (Exception e){
459             throw new RuntimeException(e);
460         }
461         Integer receivedCount = 0;
462         for (OleCopy oleCopy : copyList) {
463             if (!isReceiving && oleCorrectionReceivingItem != null)
464                 oleCopy.setCorrectionItemId(oleCorrectionReceivingItem.getReceivingItemIdentifier());
465             if (oleCopy.getReceiptStatus().equalsIgnoreCase(OLEConstants.OleLineItemReceiving.RECEIVED_STATUS)) {
466                 receivedCount++;
467             }
468         }
469         if (receivedCount == 0) {
470             oleLineItemReceivingItem
471                     .setReceiptStatusId(getReceiptStatusDetails(OLEConstants.PO_RECEIPT_STATUS_NOT_RECEIVED));
472         } else if (receivedCount == copyList.size()) {
473             oleLineItemReceivingItem
474                     .setReceiptStatusId(getReceiptStatusDetails(OLEConstants.PO_RECEIPT_STATUS_FULLY_RECEIVED));
475         } else {
476             oleLineItemReceivingItem
477                     .setReceiptStatusId(getReceiptStatusDetails(OLEConstants.PO_RECEIPT_STATUS_PARTIALLY_RECEIVED));
478         }
479         olePurchaseOrderItem.setReceiptStatusId(oleLineItemReceivingItem.getReceiptStatusId());
480         if (olePurchaseOrderItem.getItemQuantity().equals(new KualiDecimal(1)) && olePurchaseOrderItem.getItemNoOfParts().isGreaterThan(new KualiInteger(1))) {
481             olePurchaseOrderItem.setNoOfCopiesReceived(OLEConstants.OleLineItemReceiving.NOT_APPLICABLE);
482             olePurchaseOrderItem.setNoOfPartsReceived(receivedCount.toString());
483         } else if (olePurchaseOrderItem.getItemQuantity().isGreaterThan(new KualiDecimal(1)) && olePurchaseOrderItem.getItemNoOfParts().equals(new KualiInteger(1))) {
484             olePurchaseOrderItem.setNoOfCopiesReceived(receivedCount.toString());
485             olePurchaseOrderItem.setNoOfPartsReceived(OLEConstants.OleLineItemReceiving.NOT_APPLICABLE);
486         } else if (olePurchaseOrderItem.getItemQuantity().isGreaterThan(new KualiDecimal(1)) && olePurchaseOrderItem.getItemNoOfParts().isGreaterThan(new KualiInteger(1))) {
487             olePurchaseOrderItem.setNoOfCopiesReceived(OLEConstants.OleLineItemReceiving.SEE_COPIES_SECTION);
488             olePurchaseOrderItem.setNoOfPartsReceived(OLEConstants.OleLineItemReceiving.SEE_COPIES_SECTION);
489         } else {
490             olePurchaseOrderItem.setNoOfCopiesReceived(receivedCount.toString());
491             olePurchaseOrderItem.setNoOfPartsReceived(receivedCount.toString());
492         }
493 
494         Integer reqsItemId = copyList.size() > 0 ? copyList.get(0).getReqItemId() : null;
495         Map reqItemMap = new HashMap();
496         reqItemMap.put(OLEConstants.LN_ITM_IDN, reqsItemId);
497         OleRequisitionItem oleRequisitionItem = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleRequisitionItem.class, reqItemMap);
498         if (oleRequisitionItem != null) {
499             oleRequisitionItem.setNoOfCopiesReceived(olePurchaseOrderItem.getNoOfCopiesReceived());
500             oleRequisitionItem.setNoOfPartsReceived(olePurchaseOrderItem.getNoOfPartsReceived());
501             oleRequisitionItem.setOleDonors(olePurchaseOrderItem.getOleDonors());
502             oleRequisitionItem.setReceiptStatusId(olePurchaseOrderItem.getReceiptStatusId());
503             KRADServiceLocator.getBusinessObjectService().save(oleRequisitionItem);
504         }
505         if (isReceiving) {
506             HashMap<Integer, String> receiptStatusMap = new HashMap<Integer, String>();
507             for (OleCopy oleCopy : oleLineItemReceivingItem.getCopyList()) {
508                 receiptStatusMap.put(oleCopy.getCopyId(), oleCopy.getReceiptStatus());
509             }
510             updateReceivedCountFromRCVL(oleLineItemReceivingItem, receiptStatusMap);
511             for (OleCopy purCopy : olePurchaseOrderItem.getCopyList()) {
512                 if (oleLineItemReceivingItem.getItemReceivedTotalQuantity().compareTo(KualiDecimal.ZERO) > 0) {
513                     purCopy.setReceiptStatus(receiptStatusMap.get(purCopy.getCopyId()));
514                     purCopy.setReceivingItemId(oleLineItemReceivingItem.getReceivingItemIdentifier());
515                 }
516             }
517         } else {
518             if (oleCorrectionReceivingItem != null) {
519                 HashMap<Integer, String> receiptStatusMap = new HashMap<Integer, String>();
520                 for (OleCopy oleCopy : oleCorrectionReceivingItem.getCopyList()) {
521                     receiptStatusMap.put(oleCopy.getCopyId(), oleCopy.getReceiptStatus());
522                 }
523                 updateReceivedCountFromRCVC(oleLineItemReceivingItem, receiptStatusMap);
524                 oleCorrectionReceivingItem.setItemReceivedTotalQuantity(oleLineItemReceivingItem.getItemReceivedTotalQuantity());
525                 oleCorrectionReceivingItem.setItemReceivedTotalParts(oleLineItemReceivingItem.getItemReceivedTotalParts());
526                 oleCorrectionReceivingItem.setItemReturnedTotalQuantity(oleLineItemReceivingItem.getItemReturnedTotalQuantity());
527                 oleCorrectionReceivingItem.setItemReturnedTotalParts(oleLineItemReceivingItem.getItemReturnedTotalParts());
528                 for (OleCopy purCopy : olePurchaseOrderItem.getCopyList()) {
529                     purCopy.setReceiptStatus(receiptStatusMap.get(purCopy.getCopyId()));
530                     purCopy.setCorrectionItemId(oleCorrectionReceivingItem.getReceivingItemIdentifier());
531                 }
532             }
533         }
534 
535         KRADServiceLocator.getBusinessObjectService().save(olePurchaseOrderItem);
536     }
537 
538     public int getReceiptStatusDetails(String receiptStatusCd) {
539         int receiptStatusId = 0;
540         Map<String, String> receiptStatusCdMap = new HashMap<String, String>();
541         receiptStatusCdMap.put(OLEConstants.RCPT_STATUS_CD, receiptStatusCd);
542         List<OleReceiptStatus> oleReceiptStatusList = (List) KRADServiceLocator.getBusinessObjectService().findMatching(
543                 OleReceiptStatus.class, receiptStatusCdMap);
544         for (OleReceiptStatus oleReceiptStatus : oleReceiptStatusList) {
545             receiptStatusId = oleReceiptStatus.getReceiptStatusId().intValue();
546         }
547         return receiptStatusId;
548     }
549 
550     private void updateReceivedCountFromRCVL(OleLineItemReceivingItem oleLineItemReceivingItem, HashMap<Integer, String> receiptStatusMap) {
551         HashMap<String, List<OleCopy>> copyListBasedOnCopyNumber = getCopyListBasedOnCopyNumber(oleLineItemReceivingItem.getCopyList(),oleLineItemReceivingItem.getItemOrderedParts().intValue());
552         Iterator<Map.Entry<String, List<OleCopy>>> entries = copyListBasedOnCopyNumber.entrySet().iterator();
553         Integer receivedCopyCount = 0;
554         Integer receivedParts=0;
555         while (entries.hasNext()) {
556             Map.Entry<String, List<OleCopy>> entry = entries.next();
557             List<OleCopy> copyMap = entry.getValue();
558             Integer copyCount = 0;
559             for (OleCopy copy : copyMap) {
560                 if (receiptStatusMap.get(copy.getCopyId()) != null &&
561                         receiptStatusMap.get(copy.getCopyId()).equalsIgnoreCase(OLEConstants.OleLineItemReceiving.RECEIVED_STATUS)) {
562                     copyCount++;
563                     receivedParts++;
564                 }
565             }
566             if (copyCount != 0 &&copyCount <=oleLineItemReceivingItem.getItemOrderedParts().intValue()) {
567                 receivedCopyCount++;
568             }
569         }
570         if (receivedCopyCount > 0) {
571             oleLineItemReceivingItem.setItemReceivedTotalQuantity(new KualiDecimal(receivedCopyCount));
572             oleLineItemReceivingItem.setItemReceivedTotalParts(new KualiDecimal(receivedParts));
573         } else {
574             oleLineItemReceivingItem.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
575             oleLineItemReceivingItem.setItemReceivedTotalParts(KualiDecimal.ZERO);
576         }
577     }
578 
579     private void updateReceivedCountFromRCVC(OleLineItemReceivingItem oleLineItemReceivingItem, HashMap<Integer, String> receiptStatusMap) {
580         HashMap<String, List<OleCopy>> copyListBasedOnCopyNumber = getCopyListBasedOnCopyNumber(oleLineItemReceivingItem.getCopyList(),oleLineItemReceivingItem.getItemOrderedParts().intValue());
581         Iterator<Map.Entry<String, List<OleCopy>>> entries = copyListBasedOnCopyNumber.entrySet().iterator();
582         Integer receivedCopyCount = 0;
583         Integer unReceivedCopyCount = 0;
584         while (entries.hasNext()) {
585             Map.Entry<String, List<OleCopy>> entry = entries.next();
586             List<OleCopy> copyMap = entry.getValue();
587             Integer copyCount = 0;
588             for (OleCopy copy : copyMap) {
589                 if (receiptStatusMap.get(copy.getCopyId()) != null &&
590                         receiptStatusMap.get(copy.getCopyId()).equalsIgnoreCase(OLEConstants.OleLineItemReceiving.RECEIVED_STATUS)) {
591                     copyCount++;
592                 }
593             }
594             if (copyCount == oleLineItemReceivingItem.getItemOrderedParts().intValue()) {
595                 receivedCopyCount++;
596             } else {
597                 unReceivedCopyCount++;
598             }
599         }
600         if (receivedCopyCount > 0) {
601             oleLineItemReceivingItem.setItemReceivedTotalQuantity(new KualiDecimal(receivedCopyCount));
602             oleLineItemReceivingItem.setItemReceivedTotalParts(new KualiDecimal(oleLineItemReceivingItem.getItemOrderedParts().intValue()));
603         } else {
604             oleLineItemReceivingItem.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
605             oleLineItemReceivingItem.setItemReceivedTotalParts(KualiDecimal.ZERO);
606         }
607         if (unReceivedCopyCount > 0) {
608             oleLineItemReceivingItem.setItemReturnedTotalQuantity(new KualiDecimal(unReceivedCopyCount));
609             oleLineItemReceivingItem.setItemReturnedTotalParts(new KualiDecimal(oleLineItemReceivingItem.getItemOrderedParts().intValue()));
610         } else {
611             oleLineItemReceivingItem.setItemReturnedTotalQuantity(KualiDecimal.ZERO);
612             oleLineItemReceivingItem.setItemReturnedTotalParts(KualiDecimal.ZERO);
613         }
614     }
615 
616     public boolean isValidVolumeNumber(String volumeNumber, String pattern) {
617         //  pattern = "^([0-9][,][0-9]*)$";
618         boolean valid = false;
619         //"^[0-9]{1}(([0-9]*-[0-9]*)*|([0-9]* [0-9]*)*|[0-9]*)[0-9]{1}$"
620         if (pattern != null && volumeNumber != null) {
621             Pattern p = Pattern.compile(pattern);
622             Matcher m = p.matcher(volumeNumber);
623             StringBuffer sb = new StringBuffer();
624             boolean result = m.matches();
625 
626             if (result) {
627                 valid = true;
628             }
629         }
630         return valid;
631     }
632 
633 }