View Javadoc

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