001/**
002 * Copyright 2005-2014 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.rice.krad.web.form;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreApiServiceLocator;
020import org.kuali.rice.core.api.util.RiceConstants;
021import org.kuali.rice.kew.api.WorkflowDocument;
022import org.kuali.rice.kim.api.identity.Person;
023import org.kuali.rice.kim.api.services.KimApiServiceLocator;
024import org.kuali.rice.krad.document.Document;
025import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026
027/**
028 * Base form for all <code>DocumentView</code> screens
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class DocumentFormBase extends UifFormBase {
033        private static final long serialVersionUID = 2190268505427404480L;
034
035        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentFormBase.class);
036
037        private String annotation = "";
038        private String command;
039
040        private String docId;
041        private String docTypeName;
042
043        protected Document document;
044
045        public DocumentFormBase() {
046            super();
047
048            instantiateDocument();
049        }
050
051        public String getAnnotation() {
052                return this.annotation;
053        }
054
055        public void setAnnotation(String annotation) {
056                this.annotation = annotation;
057        }
058
059        public Document getDocument() {
060                return this.document;
061        }
062
063        public void setDocument(Document document) {
064                this.document = document;
065        }
066
067        public String getDocTypeName() {
068        if(this.docTypeName == null && !this.getDefaultDocumentTypeName().isEmpty())
069        {
070            return this.getDefaultDocumentTypeName();
071        }
072                return this.docTypeName;
073        }
074
075        public void setDocTypeName(String docTypeName) {
076                this.docTypeName = docTypeName;
077        }
078
079        public String getCommand() {
080                return this.command;
081        }
082
083        public void setCommand(String command) {
084                this.command = command;
085        }
086
087        public String getDocId() {
088                return this.docId;
089        }
090
091        public void setDocId(String docId) {
092                this.docId = docId;
093        }
094
095    protected String getDefaultDocumentTypeName() {
096        return "";
097    }
098
099    protected void instantiateDocument() {
100        if (document == null && StringUtils.isNotBlank(getDefaultDocumentTypeName())) {
101            Class<? extends Document> documentClass = KRADServiceLocatorWeb.getDataDictionaryService()
102                    .getDocumentClassByTypeName(getDefaultDocumentTypeName());
103            try {
104                Document newDocument = documentClass.newInstance();
105                setDocument(newDocument);
106            } catch (Exception e) {
107                LOG.error("Unable to instantiate document class " + documentClass.getName() + " document type "
108                        + getDefaultDocumentTypeName());
109                throw new RuntimeException(e);
110            }
111        }
112    }
113
114        /**
115         * Retrieves the principal name (network id) for the document's initiator
116         *
117         * @return String initiator name
118         */
119        public String getDocumentInitiatorNetworkId() {
120                String initiatorNetworkId = "";
121                if (getWorkflowDocument() != null) {
122                        String initiatorPrincipalId = getWorkflowDocument().getInitiatorPrincipalId();
123                        Person initiator = KimApiServiceLocator.getPersonService().getPerson(initiatorPrincipalId);
124                        if (initiator != null) {
125                                initiatorNetworkId = initiator.getPrincipalName();
126                        }
127                }
128
129                return initiatorNetworkId;
130        }
131
132        /**
133         * Retrieves the create date for the forms document and formats for
134         * presentation
135         *
136         * @return String formatted document create date
137         */
138    public String getDocumentCreateDate() {
139        String createDateStr = "";
140        if (getWorkflowDocument() != null && getWorkflowDocument().getDateCreated() != null) {
141            createDateStr = CoreApiServiceLocator.getDateTimeService().toString(
142                    getWorkflowDocument().getDateCreated().toDate(), "hh:mm a "+ RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE);
143        }
144
145        return createDateStr;
146    }
147
148        /**
149         * Retrieves the <code>WorkflowDocument</code> instance from the forms
150         * document instance
151         *
152         * @return WorkflowDocument for the forms document
153         */
154        public WorkflowDocument getWorkflowDocument() {
155                return getDocument().getDocumentHeader().getWorkflowDocument();
156        }
157
158}