View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.document;
17  
18  import org.apache.commons.lang.StringEscapeUtils;
19  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
20  import org.kuali.ole.docstore.common.document.Bib;
21  import org.kuali.ole.docstore.common.document.BibMarc;
22  import org.kuali.ole.DocumentUniqueIDPrefix;
23  import org.kuali.ole.docstore.model.bo.WorkBibDocument;
24  import org.kuali.ole.docstore.model.bo.WorkInstanceDocument;
25  import org.kuali.ole.docstore.model.bo.WorkItemDocument;
26  import org.kuali.ole.docstore.model.enums.DocType;
27  import org.kuali.ole.module.purap.document.CorrectionReceivingDocument;
28  import org.kuali.ole.module.purap.document.LineItemReceivingDocument;
29  import org.kuali.ole.select.businessobject.*;
30  import org.kuali.ole.select.document.service.OleLineItemReceivingService;
31  import org.kuali.ole.select.document.service.OlePurchaseOrderDocumentHelperService;
32  import org.kuali.ole.select.document.service.impl.OleLineItemReceivingServiceImpl;
33  import org.kuali.ole.select.document.service.impl.OleReceivingServiceImpl;
34  import org.kuali.ole.select.service.BibInfoWrapperService;
35  import org.kuali.ole.select.service.FileProcessingService;
36  import org.kuali.ole.select.service.impl.BibInfoWrapperServiceImpl;
37  import org.kuali.ole.sys.OLEConstants;
38  import org.kuali.ole.sys.context.SpringContext;
39  import org.kuali.ole.sys.service.impl.OleParameterConstants;
40  import org.kuali.rice.core.api.config.property.ConfigurationService;
41  import org.kuali.rice.core.api.util.type.KualiDecimal;
42  import org.kuali.rice.core.api.util.type.KualiInteger;
43  import org.kuali.rice.coreservice.framework.parameter.ParameterConstants.COMPONENT;
44  import org.kuali.rice.coreservice.framework.parameter.ParameterConstants.NAMESPACE;
45  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
46  import org.kuali.rice.krad.service.BusinessObjectService;
47  
48  import java.lang.reflect.Method;
49  import java.util.*;
50  
51  @NAMESPACE(namespace = OleParameterConstants.PURCHASING_NAMESPACE)
52  @COMPONENT(component = "CorrectionReceivingDocument")
53  public class OleCorrectionReceivingDocument extends CorrectionReceivingDocument {
54  
55      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleCorrectionReceivingDocument.class);
56      private static transient BusinessObjectService businessObjectService;
57      private static transient OleLineItemReceivingService oleCorrectionItemReceivingService;
58      private DocstoreClientLocator docstoreClientLocator;
59  
60      public DocstoreClientLocator getDocstoreClientLocator() {
61          if (docstoreClientLocator == null) {
62              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
63          }
64          return docstoreClientLocator;
65      }
66      public static OleLineItemReceivingService getOleCorrectionItemReceivingService() {
67          if (oleCorrectionItemReceivingService == null) {
68              oleCorrectionItemReceivingService = SpringContext.getBean(OleLineItemReceivingService.class);
69          }
70          return oleCorrectionItemReceivingService;
71      }
72  
73      public static void setOleCorrectionItemReceivingService(OleLineItemReceivingService oleLineItemReceivingService) {
74          OleCorrectionReceivingDocument.oleCorrectionItemReceivingService = oleLineItemReceivingService;
75      }
76  
77      @Override
78      public BusinessObjectService getBusinessObjectService() {
79          if (businessObjectService == null) {
80              businessObjectService = SpringContext.getBean(BusinessObjectService.class);
81          }
82          return businessObjectService;
83      }
84  
85      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
86          this.businessObjectService = businessObjectService;
87      }
88  
89      /**
90       * This method sets the intial values for the Receiving document
91       */
92      public void initiateDocument() {
93          super.initiateDocument();
94          getOleCorrectionItemReceivingService().getInitialCollapseSections(this);
95  
96      }
97  
98      /**
99       * This method is overridden to populate Ole Correction Receiving Item from Receiving Line Item
100      *
101      * @see org.kuali.ole.module.purap.document.CorrectionReceivingDocument#populateCorrectionReceivingFromReceivingLine(org.kuali.ole.module.purap.document.LineItemReceivingDocument)
102      */
103     @Override
104     public void populateCorrectionReceivingFromReceivingLine(LineItemReceivingDocument rlDoc) {
105         LOG.debug("Inside populateCorrectionReceivingFromReceivingLine of OleCorrectionReceivingDocument");
106         //populate receiving line document from purchase order
107         this.setPurchaseOrderIdentifier(rlDoc.getPurchaseOrderIdentifier());
108         this.getDocumentHeader().setDocumentDescription(rlDoc.getDocumentHeader().getDocumentDescription());
109         this.getDocumentHeader().setOrganizationDocumentNumber(rlDoc.getDocumentHeader().getOrganizationDocumentNumber());
110         this.setAccountsPayablePurchasingDocumentLinkIdentifier(rlDoc.getAccountsPayablePurchasingDocumentLinkIdentifier());
111         this.setLineItemReceivingDocumentNumber(rlDoc.getDocumentNumber());
112 
113         //copy receiving line items
114         for (OleLineItemReceivingItem rli : (List<OleLineItemReceivingItem>) rlDoc.getItems()) {
115             OleLineItemReceivingService oleLineItemReceivingService = SpringContext.getBean(OleLineItemReceivingServiceImpl.class);
116             OleLineItemReceivingDoc oleLineItemReceivingDoc = oleLineItemReceivingService.getOleLineItemReceivingDoc(rli.getReceivingItemIdentifier());
117             rli.setItemTitleId(oleLineItemReceivingDoc.getItemTitleId());
118             this.getItems().add(new OleCorrectionReceivingItem(rli, this));
119         }
120         LOG.debug("Leaving populateCorrectionReceivingFromReceivingLine of OleCorrectionReceivingDocument");
121     }
122 
123     /**
124      * This method is overridden to set special processing notes and other types of receipt notes
125      * in two different lists to be displayed under Special Processing Notes Tab and Receipt Notes tab respectively.
126      */
127     @Override
128     public void processAfterRetrieve() {
129         try {
130             LOG.debug("Inside processAfterRetrieve of OleCorrectionReceivingDocument");
131             super.processAfterRetrieve();
132             BibInfoWrapperService docStore = SpringContext.getBean(BibInfoWrapperServiceImpl.class);
133             FileProcessingService fileProcessingService = SpringContext.getBean(FileProcessingService.class);
134             String itemDescription = "";
135             List<OleCorrectionReceivingItem> items = new ArrayList<OleCorrectionReceivingItem>();
136             items = this.getItems();
137             Iterator<OleCorrectionReceivingItem> iterator = items.iterator();
138             String titleId = null;
139             int itemCount = 0;
140             boolean isBibFileExist = false;
141             boolean isBibEdit = false;
142             List<OleCorrectionReceivingItemReceiptNotes> receiptNoteList;
143             List<OleCorrectionReceivingItemReceiptNotes> specialHandlingNoteList;
144             for (OleCorrectionReceivingItem item : items) {
145                 receiptNoteList = new ArrayList<OleCorrectionReceivingItemReceiptNotes>();
146                 specialHandlingNoteList = new ArrayList<OleCorrectionReceivingItemReceiptNotes>();
147                 List<OleCorrectionReceivingItemReceiptNotes> notes = new ArrayList<OleCorrectionReceivingItemReceiptNotes>();
148                 notes = item.getCorrectionNoteList();
149                 for (OleCorrectionReceivingItemReceiptNotes note : notes) {
150                     String noteType = note.getNoteType().getNoteType();
151                     if (noteType.equalsIgnoreCase(OLEConstants.SPECIAL_PROCESSING_INSTRUCTION_NOTE)) {
152                         specialHandlingNoteList.add(note);
153                     } else {
154                         receiptNoteList.add(note);
155                     }
156                     item.setCorrectionSpecialHandlingNoteList(specialHandlingNoteList);
157                     item.setCorrectionReceiptNoteList(receiptNoteList);
158                     item.setCorrectionReceiptNoteListSize(receiptNoteList.size());
159                 }
160                 while (iterator.hasNext()) {
161                     LOG.debug("###########inside processAfterRetrieve item loop###########");
162                     Object object = iterator.next();
163                     if (object instanceof OleCorrectionReceivingItem) {
164                         LOG.debug("###########inside processAfterRetrieve ole requisition item###########");
165                         OleCorrectionReceivingItem singleItem = (OleCorrectionReceivingItem) object;
166                         if (LOG.isDebugEnabled()) {
167                             LOG.debug("Title id while retriving ------>" + singleItem.getItemTitleId());
168                         }
169                         if (singleItem.getItemTitleId() == null) {
170                             singleItem.setItemTitleId(getOleCorrectionItemReceivingService().getCorrectionItemDocItemTitleId(singleItem));
171                         }
172 
173                         if (singleItem.getItemTitleId() != null) {
174                             Bib bib =  new BibMarc();
175                             if(singleItem.getItemTitleId()!=null && singleItem.getItemTitleId()!=""){
176                                 bib= getDocstoreClientLocator().getDocstoreClient().retrieveBib(singleItem.getItemTitleId());
177                                 singleItem.setBibUUID(bib.getId());
178                             }
179                             itemDescription = (bib.getTitle() != null && !bib.getTitle().isEmpty() ? bib.getTitle() : "") + (bib.getAuthor() != null &&  !bib.getAuthor().isEmpty()? "," + bib.getAuthor() : "") + (bib.getPublisher() != null && !bib.getPublisher().isEmpty()? "," + bib.getPublisher() : "") + (bib.getIsbn() != null && !bib.getIsbn().isEmpty()? "," + bib.getIsbn() : "");
180                             if (singleItem.getItemTitleId() != null) {
181                                 singleItem.setDocFormat(DocumentUniqueIDPrefix.getBibFormatType(singleItem.getItemTitleId()));
182                             }
183                             StringEscapeUtils stringEscapeUtils = new StringEscapeUtils();
184                             itemDescription = stringEscapeUtils.unescapeXml(itemDescription);
185                             singleItem.setItemDescription(itemDescription);
186                         }
187                         OleLineItemReceivingService oleLineItemReceivingService = SpringContext.getBean(OleLineItemReceivingServiceImpl.class);
188                         OlePurchaseOrderItem olePurchaseOrderItem = null;
189                         if (singleItem.getPurchaseOrderIdentifier() != null) {
190                             olePurchaseOrderItem = oleLineItemReceivingService.getOlePurchaseOrderItem(singleItem.getPurchaseOrderIdentifier());
191                         } else {
192                             olePurchaseOrderItem = oleLineItemReceivingService.getOlePurchaseOrderItem(getPurchaseOrderIdentifier());
193                         }
194                         if (olePurchaseOrderItem != null && olePurchaseOrderItem.getItemTitleId() != null) {
195                             singleItem.setItemTitleId(olePurchaseOrderItem.getItemTitleId());
196                         } else {
197                             singleItem.setItemTitleId(getOleCorrectionItemReceivingService()
198                                     .getCorrectionItemDocItemTitleId(singleItem));
199                         }
200 
201                         Integer rcvnitemIdentifier = item.getReceivingItemIdentifier();
202                         Map<String, Integer> recItemIdentifer = new HashMap<String, Integer>();
203                         recItemIdentifer.put(OLEConstants.RCV_LN_ITM_IDN, rcvnitemIdentifier);
204                         OleCorrectionReceivingItem oleCorrectionReceivingItems = new OleCorrectionReceivingItem();
205                         List<OleLineItemCorrectionReceivingDoc> oleLineItemCorrectionReceivingDocs = (List<OleLineItemCorrectionReceivingDoc>) getBusinessObjectService().findMatching(OleLineItemCorrectionReceivingDoc.class, recItemIdentifer);
206                         if (oleLineItemCorrectionReceivingDocs != null && oleLineItemCorrectionReceivingDocs.size() > 0) {
207                             for (int i = 0; i < oleLineItemCorrectionReceivingDocs.size(); i++) {
208                                 Integer rcvnItemIdentifier = oleLineItemCorrectionReceivingDocs.get(i).getReceivingLineItemIdentifier();
209                                 if (!rcvnItemIdentifier.equals(item.getReceivingItemIdentifier())) {
210                                     OleLineItemCorrectionReceivingDoc corr = new OleLineItemCorrectionReceivingDoc();
211                                     corr.setReceivingLineItemIdentifier(item.getReceivingItemIdentifier());
212                                     corr.setItemTitleId(item.getItemTitleId());
213                                     oleLineItemReceivingService.saveOleLineItemReceivingCorrection(corr);
214                                 }
215                             }
216                         } else {
217                             OleLineItemCorrectionReceivingDoc corr = new OleLineItemCorrectionReceivingDoc();
218                             corr.setReceivingLineItemIdentifier(item.getReceivingItemIdentifier());
219                             corr.setItemTitleId(item.getItemTitleId());
220                             oleLineItemReceivingService.saveOleLineItemReceivingCorrection(corr);
221                         }
222                         Map cpySection = new HashMap();
223                         cpySection.put("itemIdentifier", singleItem.getPurchaseOrderIdentifier());
224 
225                         List<OlePurchaseOrderItem> olePurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, cpySection);
226                         if (olePurchaseOrderItems.size() > 0) {
227                             for (OlePurchaseOrderItem purItem : olePurchaseOrderItems) {
228                                 singleItem.setCopyList(purItem.getCopyList());
229                             }
230                         }
231                     }
232                 }
233                 LOG.debug("Leaving processAfterRetrieve of OleCorrectionReceivingDocument");
234             }
235         } catch (Exception e) {
236             LOG.error("Exception in processAfterRetrieve of OleCorrectionReceivingDocument"+e);
237             throw new RuntimeException(e);
238         }
239     }
240 
241     /**
242      * This method is used to get the bibedtior creat url from propertie file
243      *
244      * @return Bibeditor creat url string
245      */
246     public String getBibeditorCreateURL() {
247         String bibeditorCreateURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.BIBEDITOR_CREATE_URL_KEY);
248         return bibeditorCreateURL;
249     }
250 
251     public String getBibSearchURL() {
252         String bibSearchURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.BIBEDITOR_SEARCH_URL_KEY);
253         return bibSearchURL;
254     }
255 
256     /**
257      * This method is used to get the bibedtior edit url from propertie file
258      *
259      * @return Bibeditor edit url string
260      */
261     public String getBibeditorEditURL() {
262         String bibeditorEditURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.BIBEDITOR_URL_KEY);
263         return bibeditorEditURL;
264     }
265 
266     /**
267      * This method is used to get the bibedtior view url from propertie file
268      *
269      * @return Bibeditor view url string
270      */
271     public String getBibeditorViewURL() {
272         String bibeditorViewURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.DOCSTORE_APP_URL_KEY);
273         return bibeditorViewURL;
274     }
275 
276     /**
277      * This method is used to get the dublinedtior edit url from propertie file
278      *
279      * @return Dublineditor edit url string
280      */
281     public String getDublinEditorEditURL() {
282         return SpringContext.getBean(OlePurchaseOrderDocumentHelperService.class).getDublinEditorEditURL();
283     }
284 
285     /**
286      * This method is used to get the dublinedtior view url from propertie file
287      *
288      * @return dublineditor view url string
289      */
290     public String getDublinEditorViewURL() {
291         return SpringContext.getBean(OlePurchaseOrderDocumentHelperService.class).getDublinEditorViewURL();
292     }
293 
294     /**
295      * This method is used to get the directory path where the marc xml files need to be created
296      *
297      * @return Directory path string
298      */
299     public String getMarcXMLFileDirLocation() throws Exception {
300         FileProcessingService fileProcessingService = SpringContext.getBean(FileProcessingService.class);
301         String externaleDirectory = fileProcessingService.getMarcXMLFileDirLocation();
302         return externaleDirectory;
303     }
304 
305     /**
306      * This method is used to check the status of the document for displaying view and edit buttons in line item
307      *
308      * @return boolean
309      */
310     public boolean getIsSaved() {
311         if (this.getDocumentHeader().getWorkflowDocument().isSaved() || this.getDocumentHeader().getWorkflowDocument().isInitiated()) {
312             return true;
313         }
314         return false;
315     }
316 
317     @Override
318     public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
319 
320         if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isProcessed()) {
321             OleReceivingServiceImpl service = new OleReceivingServiceImpl();
322             service.completeCorrectionReceivingDocument(this);
323         }
324     }
325 
326     /**
327      * This method takes List of UUids as parameter and creates a LinkedHashMap with instance as key and id as value. and calls
328      * Docstore's QueryServiceImpl class getWorkBibRecords method and return workBibDocument for passed instance Id.
329      *
330      * @param instanceIdsList
331      * @return List<WorkBibDocument>
332      */
333 //    private List<WorkBibDocument> getWorkBibDocuments(List<String> instanceIdsList) {
334 //        List<LinkedHashMap<String, String>> instanceIdMapList = new ArrayList<LinkedHashMap<String, String>>();
335 //        for (String instanceId : instanceIdsList) {
336 //            LinkedHashMap<String, String> instanceIdMap = new LinkedHashMap<String, String>();
337 //            instanceIdMap.put(DocType.INSTANCE.getDescription(), instanceId);
338 //            instanceIdMapList.add(instanceIdMap);
339 //        }
340 //
341 //        QueryService queryService = QueryServiceImpl.getInstance();
342 //        List<WorkBibDocument> workBibDocuments = new ArrayList<WorkBibDocument>();
343 //        try {
344 //            workBibDocuments = queryService.getWorkBibRecords(instanceIdMapList);
345 //        } catch (Exception ex) {
346 //            // TODO Auto-generated catch block
347 //            ex.printStackTrace();
348 //        }
349 //        return workBibDocuments;
350 //    }
351 
352     // Added for Jira OLE-1900 Ends
353 
354     // Added for Jira OLE-1900 Starts
355 
356     /**
357      * This method will set copies into list of copies for LineItem.
358      *
359      * @param singleItem
360      * @param workBibDocument
361      * @return
362      */
363     public List<OleCopies> setCopiesToLineItem(OleCorrectionReceivingItem singleItem, WorkBibDocument workBibDocument) {
364         List<WorkInstanceDocument> instanceDocuments = workBibDocument.getWorkInstanceDocumentList();
365         List<OleCopies> copies = new ArrayList<OleCopies>();
366         for (WorkInstanceDocument workInstanceDocument : instanceDocuments) {
367             List<WorkItemDocument> itemDocuments = workInstanceDocument.getItemDocumentList();
368             StringBuffer enumeration = new StringBuffer();
369             for (int itemDocs = 0; itemDocs < itemDocuments.size(); itemDocs++) {
370                 if (itemDocs + 1 == itemDocuments.size()) {
371                     enumeration = enumeration.append(itemDocuments.get(itemDocs).getEnumeration());
372                 } else {
373                     enumeration = enumeration.append(itemDocuments.get(itemDocs).getEnumeration() + ",");
374                 }
375 
376             }
377             int startingCopy = 0;
378             if (singleItem.getItemReceivedTotalParts().intValue() != 0 && null != enumeration) {
379                 String enumerationSplit = enumeration.substring(1, 2);
380                 boolean isint = checkIsEnumerationSplitIsIntegerOrNot(enumerationSplit);
381                 if (isint) {
382                     startingCopy = Integer.parseInt(enumerationSplit);
383                 }
384             }
385             int parts = 1;
386             if (null != singleItem.getPurchaseOrderIdentifier()) {
387                 parts = getPONoOfPartsOrdered(singleItem.getPurchaseOrderIdentifier());
388             } else {
389                 parts = singleItem.getItemReceivedTotalParts().intValue();
390             }
391             /*
392              * if (singleItem.getItemReceivedTotalQuantity().isGreaterThan(new KualiDecimal(1)) ||
393              * singleItem.getItemReceivedTotalParts().isGreaterThan(new KualiDecimal(1))) {
394              */
395             int noOfCopies = workInstanceDocument.getItemDocumentList().size()
396                     / parts;
397             OleRequisitionCopies copy = new OleRequisitionCopies();
398             copy.setParts(new KualiInteger(parts));
399             copy.setLocationCopies(workInstanceDocument.getHoldingsDocument().getLocationName());
400             copy.setItemCopies(new KualiDecimal(noOfCopies));
401             copy.setPartEnumeration(enumeration.toString());
402             copy.setStartingCopyNumber(new KualiInteger(startingCopy));
403             copies.add(copy);
404             // }
405         }
406         return copies;
407     }
408 
409     public int getPONoOfPartsOrdered(int poId) {
410         OleLineItemReceivingService oleLineItemReceivingService = SpringContext
411                 .getBean(OleLineItemReceivingServiceImpl.class);
412         OlePurchaseOrderItem olePurchaseOrderItem = oleLineItemReceivingService.getOlePurchaseOrderItem(poId);
413         return olePurchaseOrderItem.getItemNoOfParts().intValue();
414     }
415 
416     public boolean checkIsEnumerationSplitIsIntegerOrNot(String enumerationSplit) {
417         try {
418             int startingCopy = Integer.parseInt(enumerationSplit);
419         } catch (NumberFormatException e) {
420             return false;
421         }
422         return true;
423     }
424 
425     public boolean getIsATypeOfRCVGDoc() {
426         return false;
427     }
428 
429     public boolean getIsATypeOfCORRDoc() {
430         return true;
431     }
432 
433     public boolean getIsFinalReqs() {
434         if (this.getDocumentHeader().getWorkflowDocument().isFinal()) {
435             return true;
436         }
437         return false;
438     }
439 
440 }