001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.api.document;
017
018import java.io.Serializable;
019import java.util.Collection;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAnyElement;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlType;
027
028import org.apache.commons.lang.StringUtils;
029import org.kuali.rice.core.api.CoreConstants;
030import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
031import org.kuali.rice.core.api.mo.ModelBuilder;
032import org.kuali.rice.kew.api.KewApiConstants;
033import 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})
045public 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        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