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