View Javadoc
1   /**
2    * Copyright 2005-2016 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.uif.view;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.util.RiceKeyConstants;
21  import org.kuali.rice.kew.api.KewApiServiceLocator;
22  import org.kuali.rice.kew.api.doctype.DocumentType;
23  import org.kuali.rice.krad.bo.Note;
24  import org.kuali.rice.krad.datadictionary.DocumentEntry;
25  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
26  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
27  import org.kuali.rice.krad.document.Document;
28  import org.kuali.rice.krad.document.DocumentRequestAuthorizationCache;
29  import org.kuali.rice.krad.document.DocumentViewAuthorizerBase;
30  import org.kuali.rice.krad.document.DocumentViewPresentationControllerBase;
31  import org.kuali.rice.krad.keyvalues.KeyValuesFinder;
32  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
33  import org.kuali.rice.krad.uif.UifConstants;
34  import org.kuali.rice.krad.uif.util.LifecycleElement;
35  import org.kuali.rice.krad.util.KRADConstants;
36  
37  /**
38   * View type for KRAD documents.
39   *
40   * <p>
41   * Provides commons configuration and default behavior applicable to documents
42   * in the KRAD module.
43   * </p>
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  @BeanTag(name = "documentView", parent = "Uif-DocumentView")
48  public class DocumentView extends FormView {
49  	private static final long serialVersionUID = 2251983409572774175L;
50  
51  	private Class<? extends Document> documentClass;
52  
53  	private boolean allowsNoteAttachments = true;
54  	private boolean allowsNoteFYI = false;
55  	private boolean displayTopicFieldInNotes = false;
56      private boolean superUserView = false;
57  
58      private Class<? extends KeyValuesFinder> attachmentTypesValuesFinderClass;
59  
60  	public DocumentView() {
61  		super();
62  
63          setRequestAuthorizationCacheClass(DocumentRequestAuthorizationCache.class);
64  	}
65  
66      /**
67       * The following initialization is performed:
68       *
69       * <ul>
70       * <li>Retrieve the document entry</li>
71       * <li>Makes sure that the header is set.</li>
72       * <li>Set up the document view authorizer and presentation controller</li>
73       * </ul>
74       *
75       * {@inheritDoc}
76       */
77      @Override
78      public void performInitialization(Object model) {
79          super.performInitialization(model);
80  
81          // get document entry
82          DocumentEntry documentEntry = getDocumentEntryForView();
83          pushObjectToContext(UifConstants.ContextVariableNames.DOCUMENT_ENTRY, documentEntry);
84  
85          // default document type on the header
86          String documentTypeName = documentEntry.getDocumentTypeName();
87          DocumentType documentType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(documentTypeName);
88  
89          if (getHeader() != null && StringUtils.isBlank(getHeaderText())) {
90              setHeaderText(documentType.getLabel());
91          }
92  
93          // setup authorizer and presentation controller using the configured authorizer and pc for document
94          if (getAuthorizer() == null) {
95              setAuthorizer(new DocumentViewAuthorizerBase());
96          }
97  
98          if (getAuthorizer() instanceof DocumentViewAuthorizerBase) {
99              DocumentViewAuthorizerBase documentViewAuthorizerBase = (DocumentViewAuthorizerBase) getAuthorizer();
100             if (documentViewAuthorizerBase.getDocumentAuthorizer() == null) {
101                 documentViewAuthorizerBase.setDocumentAuthorizerClass(documentEntry.getDocumentAuthorizerClass());
102             }
103         }
104 
105         if (getPresentationController() == null) {
106             setPresentationController(new DocumentViewPresentationControllerBase());
107         }
108 
109         if (getPresentationController() instanceof DocumentViewPresentationControllerBase) {
110             DocumentViewPresentationControllerBase documentViewPresentationControllerBase =
111                     (DocumentViewPresentationControllerBase) getPresentationController();
112             if (documentViewPresentationControllerBase.getDocumentPresentationController() == null) {
113                 documentViewPresentationControllerBase.setDocumentPresentationControllerClass(
114                         documentEntry.getDocumentPresentationControllerClass());
115             }
116         }
117 
118         getObjectPathToConcreteClassMapping().put(getDefaultBindingObjectPath(), getDocumentClass());
119     }
120 
121     /**
122      * Retrieves the associated {@link DocumentEntry} for the document view
123      *
124      * @return DocumentEntry entry (exception thrown if one is not found)
125      */
126     protected DocumentEntry getDocumentEntryForView() {
127         DocumentEntry documentEntry = KRADServiceLocatorWeb.getDocumentDictionaryService().getDocumentEntryByClass(
128                 getDocumentClass());
129 
130         if (documentEntry == null) {
131             throw new RuntimeException(
132                     "Unable to find document entry for document class: " + getDocumentClass().getName());
133         }
134 
135         return documentEntry;
136     }
137 
138     /**
139      * Returns the maximum allowed length for explanation notes within the document
140      *
141      * <p>
142      *     The max length for the explanation data is calculated as the difference between the max length of
143      *     the entire note and the length of the introduction message plus a whitespace.
144      * </p>
145      *
146      * @return int
147      */
148     @BeanTagAttribute(name = "explanationDataMaxLength")
149     public int getExplanationDataMaxLength() {
150         return KRADServiceLocatorWeb.getDataDictionaryService().getAttributeMaxLength(Note.class,
151                 KRADConstants.NOTE_TEXT_PROPERTY_NAME) -
152                 (CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
153                         RiceKeyConstants.MESSAGE_DISAPPROVAL_NOTE_TEXT_INTRO) + KRADConstants.BLANK_SPACE).length();
154     }
155 
156     /**
157      * Gets the document class
158      *
159      * @return Class<? extends Document> the document class.
160      */
161     @BeanTagAttribute
162 	public Class<? extends Document> getDocumentClass() {
163 		return this.documentClass;
164 	}
165 
166     /**
167      * Sets the document class
168      *
169      * @param documentClass
170      */
171 	public void setDocumentClass(Class<? extends Document> documentClass) {
172 		this.documentClass = documentClass;
173 	}
174 
175     /**
176      * Gets boolean that indicates if the document view allows note attachments
177      *
178      * @return true if the document view allows note attachments
179      */
180     @BeanTagAttribute
181 	public boolean isAllowsNoteAttachments() {
182 		return this.allowsNoteAttachments;
183 	}
184 
185     /**
186      * Sets boolean that indicates if the document view allows note attachments
187      *
188      * @param allowsNoteAttachments
189      */
190 	public void setAllowsNoteAttachments(boolean allowsNoteAttachments) {
191 		this.allowsNoteAttachments = allowsNoteAttachments;
192 	}
193 
194     /**
195      * Gets boolean that indicates if the document view allows note FYI
196      *
197      * @return true if the document view allows note FYI
198      */
199     @BeanTagAttribute
200 	public boolean isAllowsNoteFYI() {
201 		return this.allowsNoteFYI;
202 	}
203 
204     /**
205      * Sets boolean that indicates if the document view allows note FYI
206      *
207      * @param allowsNoteFYI
208      */
209 	public void setAllowsNoteFYI(boolean allowsNoteFYI) {
210 		this.allowsNoteFYI = allowsNoteFYI;
211 	}
212 
213     /**
214      * Gets boolean that indicates if the document view displays the topic field in notes
215      *
216      * @return true if the document view displays the topic field in notes
217      */
218     @BeanTagAttribute
219 	public boolean isDisplayTopicFieldInNotes() {
220 		return this.displayTopicFieldInNotes;
221 	}
222 
223     /**
224      * Sets boolean that indicates if the document view displays the topic field in notes
225      *
226      * @param displayTopicFieldInNotes
227      */
228 	public void setDisplayTopicFieldInNotes(boolean displayTopicFieldInNotes) {
229 		this.displayTopicFieldInNotes = displayTopicFieldInNotes;
230 	}
231 
232     /**
233      * Gets attachment types values finder classs
234      *
235      * @return attachment types values finder class
236      */
237     @BeanTagAttribute
238 	public Class<? extends KeyValuesFinder> getAttachmentTypesValuesFinderClass() {
239 		return this.attachmentTypesValuesFinderClass;
240 	}
241 
242     /**
243      * Sets attachment types values finder classs
244      *
245      * @param attachmentTypesValuesFinderClass
246      */
247 	public void setAttachmentTypesValuesFinderClass(Class<? extends KeyValuesFinder> attachmentTypesValuesFinderClass) {
248 		this.attachmentTypesValuesFinderClass = attachmentTypesValuesFinderClass;
249 	}
250 
251     /**
252      * Indicates whether the view is a super user view, used for
253      * KEW functionality
254      *
255      * @return true if the view is a super user viwe
256      */
257     public boolean isSuperUserView() {
258         return superUserView;
259     }
260 
261     /**
262      * @see DocumentView#isSuperUserView()
263      */
264     public void setSuperUserView(boolean superUserView) {
265         checkMutable(true);
266         this.superUserView = superUserView;
267     }
268 
269 }