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 org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.CoreConstants;
020    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021    import org.kuali.rice.core.api.mo.ModelBuilder;
022    import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
023    import org.kuali.rice.kew.api.KewApiConstants;
024    import org.w3c.dom.Element;
025    
026    import javax.xml.bind.annotation.XmlAccessType;
027    import javax.xml.bind.annotation.XmlAccessorType;
028    import javax.xml.bind.annotation.XmlAnyElement;
029    import javax.xml.bind.annotation.XmlElement;
030    import javax.xml.bind.annotation.XmlRootElement;
031    import javax.xml.bind.annotation.XmlType;
032    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
033    import java.io.Serializable;
034    import java.util.Collection;
035    import java.util.Collections;
036    import java.util.HashMap;
037    import java.util.Map;
038    
039    /**
040     * Defines an update to document meta-data on a particular workflow document,
041     * including title, application document id, application document status,
042     * and routing branch variables.  This structure is used to convey changes to
043     * the document meta-data on document actions, and to retrieve their state
044     * afterwards.
045     * Document titles will be truncated to to {@link KewApiConstants#TITLE_MAX_LENGTH} length.
046     *
047     * @author Kuali Rice Team (rice.collab@kuali.org)
048     * @see org.kuali.rice.kew.api.WorkflowDocumentFactory
049     * @see WorkflowDocumentImpl
050     * @see WorkflowDocumentImpl.ModifiableDocument
051     * @see WorkflowDocumentActionsServiceImpl
052     */
053    @XmlRootElement(name = DocumentUpdate.Constants.ROOT_ELEMENT_NAME)
054    @XmlAccessorType(XmlAccessType.NONE)
055    @XmlType(name = DocumentUpdate.Constants.TYPE_NAME, propOrder = {
056        DocumentUpdate.Elements.TITLE,
057        DocumentUpdate.Elements.APPLICATION_DOCUMENT_ID,
058        DocumentUpdate.Elements.APPLICATION_DOCUMENT_STATUS,
059        DocumentUpdate.Elements.VARIABLES,
060        CoreConstants.CommonElements.FUTURE_ELEMENTS
061    })
062    public final class DocumentUpdate extends AbstractDataTransferObject {
063    
064        private static final long serialVersionUID = 608839901744771499L;
065    
066        @XmlElement(name = Elements.TITLE, required = false)
067        private final String title;
068    
069        @XmlElement(name = Elements.APPLICATION_DOCUMENT_ID, required = false)
070        private final String applicationDocumentId;
071    
072        @XmlElement(name = Elements.APPLICATION_DOCUMENT_STATUS, required = false)
073        private final String applicationDocumentStatus;
074    
075        @XmlElement(name = Elements.VARIABLES, required = false)
076        @XmlJavaTypeAdapter(MapStringStringAdapter.class)
077        private final Map<String, String> variables;
078    
079        @SuppressWarnings("unused")
080        @XmlAnyElement
081        private final Collection<Element> _futureElements = null;
082    
083        private DocumentUpdate() {
084            this.title = null;
085            this.applicationDocumentId = null;
086            this.applicationDocumentStatus = null;
087            this.variables = null;
088        }
089    
090        private DocumentUpdate(Builder builder) {
091            this.title = builder.getTitle();
092            this.applicationDocumentId = builder.getApplicationDocumentId();
093            this.applicationDocumentStatus = builder.getApplicationDocumentStatus();
094            this.variables = builder.getVariables();
095        }
096    
097        public String getTitle() {
098            return title;
099        }
100    
101        public String getApplicationDocumentId() {
102            return applicationDocumentId;
103        }
104    
105        public String getApplicationDocumentStatus() {
106            return applicationDocumentStatus;
107        }
108    
109        public Map<String, String> getVariables() {
110            if (variables == null) {
111                return Collections.emptyMap();
112            }
113            return Collections.unmodifiableMap(variables);
114        }
115    
116        /**
117         * A builder which can be used to construct {@link DocumentUpdate} instances.
118         */
119        public final static class Builder implements Serializable, ModelBuilder {
120    
121            private static final long serialVersionUID = 2220000561051177421L;
122    
123            private String title;
124            private String applicationDocumentId;
125            private String applicationDocumentStatus;
126            private Map<String, String> variables;
127    
128    
129            private Builder() {
130                this.title = "";
131                this.variables = new HashMap<String, String>();
132            }
133    
134            public static Builder create() {
135                return new Builder();
136            }
137    
138            public static Builder create(Document document) {
139                if (document == null) {
140                    throw new IllegalArgumentException("document was null");
141                }
142                Builder builder = create();
143                builder.setTitle(document.getTitle());
144                builder.setApplicationDocumentId(document.getApplicationDocumentId());
145                builder.setApplicationDocumentStatus(document.getApplicationDocumentStatus());
146                builder.setVariables(document.getVariables());
147                return builder;
148            }
149    
150            public DocumentUpdate build() {
151                return new DocumentUpdate(this);
152            }
153    
154            public String getTitle() {
155                return title;
156            }
157    
158            /**
159             * Sets the document title - will be truncated to {@link KewApiConstants#TITLE_MAX_LENGTH} length.
160             */
161            public void setTitle(String title) {
162                if (title == null) {
163                    title = "";
164                }
165                if (title.length() > KewApiConstants.TITLE_MAX_LENGTH) {
166                    title = title.substring(0, KewApiConstants.TITLE_MAX_LENGTH);
167                }
168                this.title = title;
169            }
170    
171            public String getApplicationDocumentId() {
172                return applicationDocumentId;
173            }
174    
175            public void setApplicationDocumentId(String applicationDocumentId) {
176                this.applicationDocumentId = applicationDocumentId;
177            }
178    
179            public String getApplicationDocumentStatus() {
180                return applicationDocumentStatus;
181            }
182    
183            public void setApplicationDocumentStatus(String applicationDocumentStatus) {
184                this.applicationDocumentStatus = applicationDocumentStatus;
185            }
186    
187            public void setVariables(Map<String, String> variables) {
188                if (variables == null) {
189                    this.variables = new HashMap<String, String>();
190                } else {
191                    this.variables = new HashMap<String, String>(variables);
192                }
193            }
194    
195            public Map<String, String> getVariables() {
196                return variables;
197            }
198    
199            public String getVariableValue(String name) {
200                return variables.get(name);
201            }
202    
203            public void setVariable(String name, String value) {
204                if (StringUtils.isBlank(name)) {
205                    throw new IllegalArgumentException("name was null or blank");
206                }
207                variables.put(name, value);
208            }
209    
210        }
211    
212        /**
213         * Defines some internal constants used on this class.
214         */
215        static class Constants {
216            final static String ROOT_ELEMENT_NAME = "documentUpdate";
217            final static String TYPE_NAME = "DocumentUpdateType";
218        }
219    
220        /**
221         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
222         */
223        static class Elements {
224            final static String TITLE = "title";
225            final static String APPLICATION_DOCUMENT_ID = "applicationDocumentId";
226            final static String APPLICATION_DOCUMENT_STATUS = "applicationDocumentStatus";
227            final static String VARIABLES = "variables";
228        }
229    }