View Javadoc
1   /**
2    * Copyright 2005-2015 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.rice.krad.web.form;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.kew.api.WorkflowDocument;
21  import org.kuali.rice.kim.api.identity.Person;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.krad.document.Document;
24  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25  import org.kuali.rice.krad.web.bind.RequestAccessible;
26  
27  /**
28   * Base form for all <code>DocumentView</code> screens
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class DocumentFormBase extends UifFormBase {
33  	private static final long serialVersionUID = 2190268505427404480L;
34  
35  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentFormBase.class);
36  
37  	private String annotation = "";
38  
39      @RequestAccessible
40  	private String command;
41  
42      @RequestAccessible
43  	private String docId;
44  
45      @RequestAccessible
46  	private String docTypeName;
47  
48  	protected Document document;
49  
50      public DocumentFormBase() {
51  	    super();
52  
53  	    instantiateDocument();
54  	}
55  
56  	public String getAnnotation() {
57  		return this.annotation;
58  	}
59  
60  	public void setAnnotation(String annotation) {
61  		this.annotation = annotation;
62  	}
63  
64  	public Document getDocument() {
65  		return this.document;
66  	}
67  
68  	public void setDocument(Document document) {
69  		this.document = document;
70  	}
71  
72      public String getDocTypeName() {
73          if (this.docTypeName == null && !this.getDefaultDocumentTypeName().isEmpty()) {
74              return this.getDefaultDocumentTypeName();
75          }
76          return this.docTypeName;
77      }
78  
79  	public void setDocTypeName(String docTypeName) {
80  		this.docTypeName = docTypeName;
81  	}
82  
83  	public String getCommand() {
84  		return this.command;
85  	}
86  
87  	public void setCommand(String command) {
88  		this.command = command;
89  	}
90  
91  	public String getDocId() {
92  		return this.docId;
93  	}
94  
95  	public void setDocId(String docId) {
96  		this.docId = docId;
97  	}
98  
99      protected String getDefaultDocumentTypeName() {
100         return "";
101     }
102 
103     protected void instantiateDocument() {
104         if (document == null && StringUtils.isNotBlank(getDefaultDocumentTypeName())) {
105             Class<? extends Document> documentClass = KRADServiceLocatorWeb.getDataDictionaryService()
106                     .getValidDocumentClassByTypeName(getDefaultDocumentTypeName());
107             try {
108                 Document newDocument = documentClass.newInstance();
109                 setDocument(newDocument);
110             } catch (Exception e) {
111                 LOG.error("Unable to instantiate document class " + documentClass + " document type "
112                         + getDefaultDocumentTypeName());
113                 throw new RuntimeException("Unable to instantiate document class " + documentClass + " document type "
114                         + getDefaultDocumentTypeName(),e);
115             }
116         }
117     }
118 
119 	/**
120 	 * Retrieves the principal name (network id) for the document's initiator
121 	 *
122 	 * @return String initiator name
123 	 */
124 	public String getDocumentInitiatorNetworkId() {
125 		String initiatorNetworkId = "";
126 		if (getWorkflowDocument() != null) {
127 			String initiatorPrincipalId = getWorkflowDocument().getInitiatorPrincipalId();
128 			Person initiator = KimApiServiceLocator.getPersonService().getPerson(initiatorPrincipalId);
129 			if (initiator != null) {
130 				initiatorNetworkId = initiator.getPrincipalName();
131 			}
132 		}
133 
134 		return initiatorNetworkId;
135 	}
136 
137 	/**
138 	 * Retrieves the create date for the forms document and formats for
139 	 * presentation
140 	 *
141 	 * @return String formatted document create date
142 	 */
143     public String getDocumentCreateDate() {
144         String createDateStr = "";
145         if (getWorkflowDocument() != null && getWorkflowDocument().getDateCreated() != null) {
146             createDateStr = CoreApiServiceLocator.getDateTimeService().toString(
147                     getWorkflowDocument().getDateCreated().toDate(), "hh:mm a MM/dd/yyyy");
148         }
149 
150         return createDateStr;
151     }
152 
153 	/**
154 	 * Retrieves the <code>WorkflowDocument</code> instance from the forms
155 	 * document instance
156 	 *
157 	 * @return WorkflowDocument for the forms document
158 	 */
159 	public WorkflowDocument getWorkflowDocument() {
160 		return getDocument().getDocumentHeader().getWorkflowDocument();
161 	}
162 
163 }