001/*
002 * Copyright 2007 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.module.purap.businessobject;
017
018import org.joda.time.DateTime;
019import org.kuali.ole.sys.context.SpringContext;
020import org.kuali.rice.kew.api.KewApiConstants;
021import org.kuali.rice.kew.api.KewApiServiceLocator;
022import org.kuali.rice.kew.api.doctype.DocumentType;
023import org.kuali.rice.kew.api.exception.WorkflowException;
024import org.kuali.rice.kns.service.DataDictionaryService;
025import org.kuali.rice.krad.bo.Note;
026import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
027import org.kuali.rice.krad.datadictionary.exception.UnknownDocumentTypeException;
028import org.kuali.rice.krad.document.Document;
029import org.kuali.rice.krad.service.DocumentService;
030import org.kuali.rice.krad.service.NoteService;
031import org.kuali.rice.krad.util.KRADConstants;
032import org.kuali.rice.krad.util.ObjectUtils;
033
034import java.util.ArrayList;
035import java.util.LinkedHashMap;
036import java.util.List;
037
038/**
039 * Base class for Related View Business Objects.
040 */
041public abstract class AbstractRelatedView extends PersistableBusinessObjectBase {
042    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AbstractRelatedView.class);
043
044    private Integer accountsPayablePurchasingDocumentLinkIdentifier;
045    private Integer purapDocumentIdentifier;
046    private String documentNumber;
047    private String poNumberMasked;
048
049    //create date from the workflow document header...
050    private DateTime createDate;
051
052    public Integer getAccountsPayablePurchasingDocumentLinkIdentifier() {
053        return accountsPayablePurchasingDocumentLinkIdentifier;
054    }
055
056    public void setAccountsPayablePurchasingDocumentLinkIdentifier(Integer accountsPayablePurchasingDocumentLinkIdentifier) {
057        this.accountsPayablePurchasingDocumentLinkIdentifier = accountsPayablePurchasingDocumentLinkIdentifier;
058    }
059
060    public Integer getPurapDocumentIdentifier() {
061        return purapDocumentIdentifier;
062    }
063
064    public void setPurapDocumentIdentifier(Integer purapDocumentIdentifier) {
065        this.purapDocumentIdentifier = purapDocumentIdentifier;
066    }
067
068    public String getDocumentNumber() {
069        return documentNumber;
070    }
071
072    public void setDocumentNumber(String documentNumber) {
073        this.documentNumber = documentNumber;
074    }
075
076    public List<Note> getNotes() {
077        List<Note> notes = new ArrayList<Note>();
078        //reverse the order of notes only when anything exists in it..
079        NoteService noteService = SpringContext.getBean(NoteService.class);
080        String document = findDocument(this.documentNumber).getDocumentHeader().getObjectId();
081        List<Note> tmpNotes = null;
082        if (document != null)
083            tmpNotes = noteService.getByRemoteObjectId(document);
084        notes.clear();
085        // reverse the order of notes retrieved so that newest note is in the front
086        if (tmpNotes != null)
087            for (int i = tmpNotes.size() - 1; i >= 0; i--) {
088                Note note = tmpNotes.get(i);
089                notes.add(note);
090            }
091        return notes;
092    }
093
094    public String getUrl() {
095        String documentTypeName = this.getDocumentTypeName();
096        DocumentType docType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(documentTypeName);
097        String docHandlerUrl = docType.getResolvedDocumentHandlerUrl();
098        int endSubString = docHandlerUrl.lastIndexOf("/");
099        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}