001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.kew.api.document;
017    
018    import java.io.Serializable;
019    import java.util.Collection;
020    
021    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlAnyElement;
024    import javax.xml.bind.annotation.XmlElement;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlType;
027    
028    import org.apache.commons.lang.StringUtils;
029    import org.kuali.rice.core.api.CoreConstants;
030    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
031    import org.kuali.rice.core.api.mo.ModelBuilder;
032    import org.kuali.rice.kew.api.KewApiConstants;
033    import org.w3c.dom.Element;
034    
035    @XmlRootElement(name = DocumentContent.Constants.ROOT_ELEMENT_NAME)
036    @XmlAccessorType(XmlAccessType.NONE)
037    @XmlType(name = DocumentContent.Constants.TYPE_NAME, propOrder = {
038        DocumentContent.Elements.DOCUMENT_ID,
039        DocumentContent.Elements.APPLICATION_CONTENT,
040        DocumentContent.Elements.ATTRIBUTE_CONTENT,
041        DocumentContent.Elements.SEARCHABLE_CONTENT,
042        DocumentContent.Elements.FORMAT_VERSION,
043        CoreConstants.CommonElements.FUTURE_ELEMENTS
044    })
045    public final class DocumentContent extends AbstractDataTransferObject implements DocumentContentContract {
046    
047            private static final long serialVersionUID = 6110079520547685342L;
048    
049            @XmlElement(name = Elements.DOCUMENT_ID, required = true)
050        private final String documentId;
051        
052            @XmlElement(name = Elements.APPLICATION_CONTENT, required = false)
053        private final String applicationContent;
054        
055            @XmlElement(name = Elements.ATTRIBUTE_CONTENT, required = false)
056        private final String attributeContent;
057        
058            @XmlElement(name = Elements.SEARCHABLE_CONTENT, required = false)
059        private final String searchableContent;
060        
061            @XmlElement(name = Elements.FORMAT_VERSION, required = true)
062        private final int formatVersion;
063        
064            @SuppressWarnings("unused")
065        @XmlAnyElement
066        private final Collection<Element> _futureElements = null;
067    
068        /**
069         * Private constructor used only by JAXB.
070         * 
071         */
072        private DocumentContent() {
073            this.documentId = null;
074            this.applicationContent = null;
075            this.attributeContent = null;
076            this.searchableContent = null;
077            this.formatVersion = 0;
078        }
079    
080        private DocumentContent(Builder builder) {
081            this.documentId = builder.getDocumentId();
082            this.applicationContent = builder.getApplicationContent();
083            this.attributeContent = builder.getAttributeContent();
084            this.searchableContent = builder.getSearchableContent();
085            this.formatVersion = builder.getFormatVersion();
086        }
087    
088        @Override
089        public String getDocumentId() {
090            return this.documentId;
091        }
092    
093        @Override
094        public String getApplicationContent() {
095            return this.applicationContent;
096        }
097    
098        @Override
099        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            fullContent.append(getAttributeContent());
122            fullContent.append(getSearchableContent());
123            fullContent.append("</").append(KewApiConstants.DOCUMENT_CONTENT_ELEMENT).append(">");
124            return fullContent.toString();
125        }
126    
127        /**
128         * A builder which can be used to construct {@link DocumentContent} instances.  Enforces the constraints of the {@link DocumentContentContract}.
129         */
130        public final static class Builder implements Serializable, ModelBuilder, DocumentContentContract {
131    
132                    private static final long serialVersionUID = 7549637048594326790L;
133    
134                    private String documentId;
135            private String applicationContent;
136            private String attributeContent;
137            private String searchableContent;
138            private int formatVersion;
139    
140            private Builder(String documentId) {
141                setDocumentId(documentId);
142                setFormatVersion(KewApiConstants.DocumentContentVersions.CURRENT);
143            }
144    
145            public static Builder create(String documentId) {
146                return new Builder(documentId);
147            }
148    
149            public static Builder create(DocumentContentContract contract) {
150                if (contract == null) {
151                    throw new IllegalArgumentException("contract was null");
152                }
153                Builder builder = create(contract.getDocumentId());
154                builder.setApplicationContent(contract.getApplicationContent());
155                builder.setAttributeContent(contract.getAttributeContent());
156                builder.setSearchableContent(contract.getSearchableContent());
157                builder.setFormatVersion(contract.getFormatVersion());
158                return builder;
159            }
160    
161            public DocumentContent build() {
162                return new DocumentContent(this);
163            }
164    
165            @Override
166            public String getDocumentId() {
167                return this.documentId;
168            }
169    
170            @Override
171            public String getApplicationContent() {
172                return this.applicationContent;
173            }
174    
175            @Override
176            public String getAttributeContent() {
177                return this.attributeContent;
178            }
179    
180            @Override
181            public String getSearchableContent() {
182                return this.searchableContent;
183            }
184    
185            @Override
186            public int getFormatVersion() {
187                return this.formatVersion;
188            }
189    
190            public void setDocumentId(String documentId) {
191                if (StringUtils.isBlank(documentId)) {
192                    throw new IllegalArgumentException("documentId was null or blank");
193                }
194                this.documentId = documentId;
195            }
196    
197            public void setApplicationContent(String applicationContent) {
198                this.applicationContent = applicationContent;
199            }
200    
201            public void setAttributeContent(String attributeContent) {
202                this.attributeContent = attributeContent;
203            }
204    
205            public void setSearchableContent(String searchableContent) {
206                this.searchableContent = searchableContent;
207            }
208    
209            public void setFormatVersion(int formatVersion) {
210                    if (formatVersion < 0) {
211                            throw new IllegalArgumentException("formatVersion must be a valid version, was " + formatVersion);
212                    }
213                this.formatVersion = formatVersion;
214            }
215    
216        }
217    
218        /**
219         * Defines some internal constants used on this class.
220         */
221        static class Constants {
222    
223            final static String ROOT_ELEMENT_NAME = "documentContent";
224            final static String TYPE_NAME = "DocumentContentType";
225        }
226    
227        /**
228         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
229         */
230        static class Elements {
231    
232            final static String DOCUMENT_ID = "documentId";
233            final static String APPLICATION_CONTENT = "applicationContent";
234            final static String ATTRIBUTE_CONTENT = "attributeContent";
235            final static String SEARCHABLE_CONTENT = "searchableContent";
236            final static String FORMAT_VERSION = "formatVersion";
237    
238        }
239    
240    }
241