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.kew.api.document;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlAnyElement;
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.XmlType;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.rice.core.api.CoreConstants;
30  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
31  import org.kuali.rice.core.api.mo.ModelBuilder;
32  import org.kuali.rice.kew.api.KewApiConstants;
33  import org.w3c.dom.Element;
34  
35  @XmlRootElement(name = DocumentContent.Constants.ROOT_ELEMENT_NAME)
36  @XmlAccessorType(XmlAccessType.NONE)
37  @XmlType(name = DocumentContent.Constants.TYPE_NAME, propOrder = {
38      DocumentContent.Elements.DOCUMENT_ID,
39      DocumentContent.Elements.APPLICATION_CONTENT,
40      DocumentContent.Elements.ATTRIBUTE_CONTENT,
41      DocumentContent.Elements.SEARCHABLE_CONTENT,
42      DocumentContent.Elements.FORMAT_VERSION,
43      CoreConstants.CommonElements.FUTURE_ELEMENTS
44  })
45  public final class DocumentContent extends AbstractDataTransferObject implements DocumentContentContract {
46  
47  	private static final long serialVersionUID = 6110079520547685342L;
48  
49  	@XmlElement(name = Elements.DOCUMENT_ID, required = true)
50      private final String documentId;
51      
52  	@XmlElement(name = Elements.APPLICATION_CONTENT, required = false)
53      private final String applicationContent;
54      
55  	@XmlElement(name = Elements.ATTRIBUTE_CONTENT, required = false)
56      private final String attributeContent;
57      
58  	@XmlElement(name = Elements.SEARCHABLE_CONTENT, required = false)
59      private final String searchableContent;
60      
61  	@XmlElement(name = Elements.FORMAT_VERSION, required = true)
62      private final int formatVersion;
63      
64  	@SuppressWarnings("unused")
65      @XmlAnyElement
66      private final Collection<Element> _futureElements = null;
67  
68      /**
69       * Private constructor used only by JAXB.
70       * 
71       */
72      private DocumentContent() {
73          this.documentId = null;
74          this.applicationContent = null;
75          this.attributeContent = null;
76          this.searchableContent = null;
77          this.formatVersion = 0;
78      }
79  
80      private DocumentContent(Builder builder) {
81          this.documentId = builder.getDocumentId();
82          this.applicationContent = builder.getApplicationContent();
83          this.attributeContent = builder.getAttributeContent();
84          this.searchableContent = builder.getSearchableContent();
85          this.formatVersion = builder.getFormatVersion();
86      }
87  
88      @Override
89      public String getDocumentId() {
90          return this.documentId;
91      }
92  
93      @Override
94      public String getApplicationContent() {
95          return this.applicationContent;
96      }
97  
98      @Override
99      public String getAttributeContent() {
100         return this.attributeContent;
101     }
102 
103     @Override
104     public String getSearchableContent() {
105         return this.searchableContent;
106     }
107 
108     @Override
109     public int getFormatVersion() {
110         return this.formatVersion;
111     }
112     
113     public String getFullContent() {
114         StringBuilder fullContent = new StringBuilder();
115         fullContent.append("<").append(KewApiConstants.DOCUMENT_CONTENT_ELEMENT).append(">");
116         if (!StringUtils.isBlank(getApplicationContent())) {
117             fullContent.append("<").append(KewApiConstants.APPLICATION_CONTENT_ELEMENT).append(">");
118             fullContent.append(getApplicationContent());
119             fullContent.append("</").append(KewApiConstants.APPLICATION_CONTENT_ELEMENT).append(">");        	
120         }
121         if (!StringUtils.isBlank(getAttributeContent())) {
122             fullContent.append(getAttributeContent());
123         }
124         if (!StringUtils.isBlank(getSearchableContent())) {
125             fullContent.append(getSearchableContent());
126         }
127         fullContent.append("</").append(KewApiConstants.DOCUMENT_CONTENT_ELEMENT).append(">");
128         return fullContent.toString();
129     }
130 
131     /**
132      * A builder which can be used to construct {@link DocumentContent} instances.  Enforces the constraints of the {@link DocumentContentContract}.
133      */
134     public final static class Builder implements Serializable, ModelBuilder, DocumentContentContract {
135 
136 		private static final long serialVersionUID = 7549637048594326790L;
137 
138 		private String documentId;
139         private String applicationContent;
140         private String attributeContent;
141         private String searchableContent;
142         private int formatVersion;
143 
144         private Builder(String documentId) {
145             setDocumentId(documentId);
146             setFormatVersion(KewApiConstants.DocumentContentVersions.CURRENT);
147         }
148 
149         public static Builder create(String documentId) {
150             return new Builder(documentId);
151         }
152 
153         public static Builder create(DocumentContentContract contract) {
154             if (contract == null) {
155                 throw new IllegalArgumentException("contract was null");
156             }
157             Builder builder = create(contract.getDocumentId());
158             builder.setApplicationContent(contract.getApplicationContent());
159             builder.setAttributeContent(contract.getAttributeContent());
160             builder.setSearchableContent(contract.getSearchableContent());
161             builder.setFormatVersion(contract.getFormatVersion());
162             return builder;
163         }
164 
165         public DocumentContent build() {
166             return new DocumentContent(this);
167         }
168 
169         @Override
170         public String getDocumentId() {
171             return this.documentId;
172         }
173 
174         @Override
175         public String getApplicationContent() {
176             return this.applicationContent;
177         }
178 
179         @Override
180         public String getAttributeContent() {
181             return this.attributeContent;
182         }
183 
184         @Override
185         public String getSearchableContent() {
186             return this.searchableContent;
187         }
188 
189         @Override
190         public int getFormatVersion() {
191             return this.formatVersion;
192         }
193 
194         public void setDocumentId(String documentId) {
195             if (StringUtils.isBlank(documentId)) {
196             	throw new IllegalArgumentException("documentId was null or blank");
197             }
198             this.documentId = documentId;
199         }
200 
201         public void setApplicationContent(String applicationContent) {
202             this.applicationContent = applicationContent;
203         }
204 
205         public void setAttributeContent(String attributeContent) {
206             this.attributeContent = attributeContent;
207         }
208 
209         public void setSearchableContent(String searchableContent) {
210             this.searchableContent = searchableContent;
211         }
212 
213         public void setFormatVersion(int formatVersion) {
214         	if (formatVersion < 0) {
215         		throw new IllegalArgumentException("formatVersion must be a valid version, was " + formatVersion);
216         	}
217             this.formatVersion = formatVersion;
218         }
219 
220     }
221 
222     /**
223      * Defines some internal constants used on this class.
224      */
225     static class Constants {
226 
227         final static String ROOT_ELEMENT_NAME = "documentContent";
228         final static String TYPE_NAME = "DocumentContentType";
229     }
230 
231     /**
232      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
233      */
234     static class Elements {
235 
236         final static String DOCUMENT_ID = "documentId";
237         final static String APPLICATION_CONTENT = "applicationContent";
238         final static String ATTRIBUTE_CONTENT = "attributeContent";
239         final static String SEARCHABLE_CONTENT = "searchableContent";
240         final static String FORMAT_VERSION = "formatVersion";
241 
242     }
243 
244 }
245