View Javadoc
1   /*
2    * Copyright 2007 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.module.purap.businessobject;
17  
18  import org.joda.time.DateTime;
19  import org.kuali.ole.sys.context.SpringContext;
20  import org.kuali.rice.kew.api.KewApiConstants;
21  import org.kuali.rice.kew.api.KewApiServiceLocator;
22  import org.kuali.rice.kew.api.doctype.DocumentType;
23  import org.kuali.rice.kew.api.exception.WorkflowException;
24  import org.kuali.rice.kns.service.DataDictionaryService;
25  import org.kuali.rice.krad.bo.Note;
26  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
27  import org.kuali.rice.krad.datadictionary.exception.UnknownDocumentTypeException;
28  import org.kuali.rice.krad.document.Document;
29  import org.kuali.rice.krad.service.DocumentService;
30  import org.kuali.rice.krad.service.NoteService;
31  import org.kuali.rice.krad.util.KRADConstants;
32  import org.kuali.rice.krad.util.ObjectUtils;
33  
34  import java.util.ArrayList;
35  import java.util.LinkedHashMap;
36  import java.util.List;
37  
38  /**
39   * Base class for Related View Business Objects.
40   */
41  public abstract class AbstractRelatedView extends PersistableBusinessObjectBase {
42      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AbstractRelatedView.class);
43  
44      private Integer accountsPayablePurchasingDocumentLinkIdentifier;
45      private Integer purapDocumentIdentifier;
46      private String documentNumber;
47      private String poNumberMasked;
48  
49      //create date from the workflow document header...
50      private DateTime createDate;
51  
52      public Integer getAccountsPayablePurchasingDocumentLinkIdentifier() {
53          return accountsPayablePurchasingDocumentLinkIdentifier;
54      }
55  
56      public void setAccountsPayablePurchasingDocumentLinkIdentifier(Integer accountsPayablePurchasingDocumentLinkIdentifier) {
57          this.accountsPayablePurchasingDocumentLinkIdentifier = accountsPayablePurchasingDocumentLinkIdentifier;
58      }
59  
60      public Integer getPurapDocumentIdentifier() {
61          return purapDocumentIdentifier;
62      }
63  
64      public void setPurapDocumentIdentifier(Integer purapDocumentIdentifier) {
65          this.purapDocumentIdentifier = purapDocumentIdentifier;
66      }
67  
68      public String getDocumentNumber() {
69          return documentNumber;
70      }
71  
72      public void setDocumentNumber(String documentNumber) {
73          this.documentNumber = documentNumber;
74      }
75  
76      public List<Note> getNotes() {
77          List<Note> notes = new ArrayList<Note>();
78          //reverse the order of notes only when anything exists in it..
79          NoteService noteService = SpringContext.getBean(NoteService.class);
80          String document = findDocument(this.documentNumber).getDocumentHeader().getObjectId();
81          List<Note> tmpNotes = null;
82          if (document != null)
83              tmpNotes = noteService.getByRemoteObjectId(document);
84          notes.clear();
85          // reverse the order of notes retrieved so that newest note is in the front
86          if (tmpNotes != null)
87              for (int i = tmpNotes.size() - 1; i >= 0; i--) {
88                  Note note = tmpNotes.get(i);
89                  notes.add(note);
90              }
91          return notes;
92      }
93  
94      public String getUrl() {
95          String documentTypeName = this.getDocumentTypeName();
96          DocumentType docType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(documentTypeName);
97          String docHandlerUrl = docType.getResolvedDocumentHandlerUrl();
98          int endSubString = docHandlerUrl.lastIndexOf("/");
99          String serverName = docHandlerUrl.substring(0, endSubString);
100         String handler = docHandlerUrl.substring(endSubString + 1, docHandlerUrl.lastIndexOf("?"));
101         if (handler.equals("OLEInvoice"))
102             return docHandlerUrl + "&" + KRADConstants.PARAMETER_DOC_ID + "=" + this.getDocumentNumber() + "&" + KRADConstants.PARAMETER_COMMAND + "=" + KewApiConstants.DOCSEARCH_COMMAND;
103         else
104             return serverName + "/" + KRADConstants.PORTAL_ACTION + "?channelTitle=" + docType.getName() + "&channelUrl=" + handler + "?" + KRADConstants.DISPATCH_REQUEST_PARAMETER + "=" + KRADConstants.DOC_HANDLER_METHOD + "&" + KRADConstants.PARAMETER_DOC_ID + "=" + this.getDocumentNumber() + "&" + KRADConstants.PARAMETER_COMMAND + "=" + KewApiConstants.DOCSEARCH_COMMAND;
105     }
106 
107     public String getDocumentIdentifierString() {
108         if (purapDocumentIdentifier != null) {
109             return purapDocumentIdentifier.toString();
110         } else {
111             return documentNumber;
112         }
113     }
114 
115 
116     /**
117      * Returns the document label according to the label specified in the data dictionary.
118      *
119      * @return
120      * @throws org.kuali.rice.kew.api.exception.WorkflowException
121      *
122      */
123     public String getDocumentLabel() throws WorkflowException {
124         return SpringContext.getBean(DataDictionaryService.class).getDocumentLabelByTypeName(getDocumentTypeName());
125     }
126 
127     /**
128      * @see org.kuali.rice.kns.bo.BusinessObjectBase#toStringMapper()
129      */
130     public abstract String getDocumentTypeName();
131 
132     /**
133      * @see org.kuali.rice.krad.bo.BusinessObjectBase#toStringMapper()
134      */
135     protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
136         LinkedHashMap m = new LinkedHashMap();
137         if (this.accountsPayablePurchasingDocumentLinkIdentifier != null) {
138             m.put("accountsPayablePurchasingDocumentLinkIdentifier", this.accountsPayablePurchasingDocumentLinkIdentifier.toString());
139         }
140         return m;
141     }
142 
143     /**
144      * Gets the poNumberMasked attribute.
145      *
146      * @return Returns the poNumberMasked
147      */
148 
149     public String getPoNumberMasked() {
150         return poNumberMasked;
151     }
152 
153     /**
154      * Sets the poNumberMasked attribute.
155      *
156      * @param poNumberMasked The poNumberMasked to set.
157      */
158     public void setPoNumberMasked(String poNumberMasked) {
159         this.poNumberMasked = poNumberMasked;
160     }
161 
162     public String getApplicationDocumentStatus() {
163         Document document = findDocument(this.getDocumentNumber());
164         if (ObjectUtils.isNotNull(document)) {
165             return document.getDocumentHeader().getWorkflowDocument().getApplicationDocumentStatus();
166         }
167         return "";
168     }
169 
170     /**
171      * This method finds the document for the given document header id
172      *
173      * @param documentHeaderId
174      * @return document The document in the workflow that matches the document header id.
175      */
176     protected Document findDocument(String documentHeaderId) {
177         Document document = null;
178 
179         try {
180             document = SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(documentHeaderId);
181         } catch (WorkflowException ex) {
182             LOG.error("Exception encountered on finding the document: " + documentHeaderId, ex);
183         } catch (UnknownDocumentTypeException ex) {
184             // don't blow up just because a document type is not installed (but don't return it either)
185             LOG.error("Exception encountered on finding the document: " + documentHeaderId, ex);
186         }
187 
188         return document;
189     }
190 
191     public void setAppDocStatus(String appDocStatus) {
192         Document document = findDocument(this.getDocumentNumber());
193         if (ObjectUtils.isNotNull(document)) {
194             document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(appDocStatus);
195         }
196     }
197 
198     /**
199      * Gets the createDate attribute.
200      *
201      * @return Returns the createDate
202      */
203     public DateTime getCreateDate() {
204         Document document = findDocument(this.getDocumentNumber());
205 
206         return document.getDocumentHeader().getWorkflowDocument().getDateCreated();
207     }
208 }