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.kew.api.document;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.w3c.dom.Element;
25  
26  import javax.xml.bind.annotation.XmlAccessType;
27  import javax.xml.bind.annotation.XmlAccessorType;
28  import javax.xml.bind.annotation.XmlAnyElement;
29  import javax.xml.bind.annotation.XmlElement;
30  import javax.xml.bind.annotation.XmlElementWrapper;
31  import javax.xml.bind.annotation.XmlRootElement;
32  import javax.xml.bind.annotation.XmlType;
33  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
34  import java.io.Serializable;
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * Defines an update to document meta-data on a particular workflow document,
44   * including title, application document id, application document status,
45   * and routing branch variables.  This structure is used to convey changes to
46   * the document meta-data on document actions, and to retrieve their state
47   * afterwards.
48   * Document titles will be truncated to to {@link KewApiConstants#TITLE_MAX_LENGTH} length.
49   *
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   * @see org.kuali.rice.kew.api.WorkflowDocumentFactory
52   * @see WorkflowDocumentImpl
53   * @see WorkflowDocumentImpl.ModifiableDocument
54   * @see WorkflowDocumentActionsServiceImpl
55   */
56  @XmlRootElement(name = DocumentUpdate.Constants.ROOT_ELEMENT_NAME)
57  @XmlAccessorType(XmlAccessType.NONE)
58  @XmlType(name = DocumentUpdate.Constants.TYPE_NAME, propOrder = {
59      DocumentUpdate.Elements.TITLE,
60      DocumentUpdate.Elements.APPLICATION_DOCUMENT_ID,
61      DocumentUpdate.Elements.APPLICATION_DOCUMENT_STATUS,
62      DocumentUpdate.Elements.VARIABLES,
63      DocumentUpdate.Elements.DIRTY_FIELDS,
64      CoreConstants.CommonElements.FUTURE_ELEMENTS
65  })
66  public final class DocumentUpdate extends AbstractDataTransferObject {
67  
68      private static final long serialVersionUID = 608839901744771499L;
69  
70      @XmlElement(name = Elements.TITLE, required = false)
71      private final String title;
72  
73      @XmlElement(name = Elements.APPLICATION_DOCUMENT_ID, required = false)
74      private final String applicationDocumentId;
75  
76      @XmlElement(name = Elements.APPLICATION_DOCUMENT_STATUS, required = false)
77      private final String applicationDocumentStatus;
78  
79      @XmlElement(name = Elements.VARIABLES, required = false)
80      @XmlJavaTypeAdapter(MapStringStringAdapter.class)
81      private final Map<String, String> variables;
82  
83      @XmlElementWrapper(name = Elements.DIRTY_FIELDS, required = false)
84      @XmlElement(name = "field", required = false)
85      private final List<String> dirtyFields;
86  
87      @SuppressWarnings("unused")
88      @XmlAnyElement
89      private final Collection<Element> _futureElements = null;
90  
91      private DocumentUpdate() {
92          this.title = null;
93          this.applicationDocumentId = null;
94          this.applicationDocumentStatus = null;
95          this.variables = null;
96          this.dirtyFields = new ArrayList<String>();
97      }
98  
99      private DocumentUpdate(Builder builder) {
100         this.title = builder.getTitle();
101         this.applicationDocumentId = builder.getApplicationDocumentId();
102         this.applicationDocumentStatus = builder.getApplicationDocumentStatus();
103         this.variables = builder.getVariables();
104         this.dirtyFields = builder.getDirtyFields();
105     }
106 
107     public String getTitle() {
108         return title;
109     }
110 
111     public String getApplicationDocumentId() {
112         return applicationDocumentId;
113     }
114 
115     public String getApplicationDocumentStatus() {
116         return applicationDocumentStatus;
117     }
118 
119     public Map<String, String> getVariables() {
120         if (variables == null) {
121             return Collections.emptyMap();
122         }
123         return Collections.unmodifiableMap(variables);
124     }
125     public List<String> getDirtyFields() {
126         return dirtyFields;
127     }
128     /**
129      * A builder which can be used to construct {@link DocumentUpdate} instances.
130      */
131     public final static class Builder implements Serializable, ModelBuilder {
132 
133         private static final long serialVersionUID = 2220000561051177421L;
134 
135         private String title;
136         private String applicationDocumentId;
137         private String applicationDocumentStatus;
138         private Map<String, String> variables;
139         private List<String> dirtyFields;
140 
141         private Builder() {
142             this.title = "";
143             this.variables = new HashMap<String, String>();
144         }
145 
146         public static Builder create() {
147             return new Builder();
148         }
149 
150         public static Builder create(Document document) {
151             if (document == null) {
152                 throw new IllegalArgumentException("document was null");
153             }
154             Builder builder = create();
155             builder.setTitle(document.getTitle());
156             builder.setApplicationDocumentId(document.getApplicationDocumentId());
157             builder.setApplicationDocumentStatus(document.getApplicationDocumentStatus());
158             builder.setVariables(document.getVariables());
159             return builder;
160         }
161 
162         public DocumentUpdate build() {
163             return new DocumentUpdate(this);
164         }
165 
166         public String getTitle() {
167             return title;
168         }
169 
170         /**
171          * Sets the document title - will be truncated to {@link KewApiConstants#TITLE_MAX_LENGTH} length.
172          */
173         public void setTitle(String title) {
174             if (title == null) {
175                 title = "";
176             }
177             if (title.length() > KewApiConstants.TITLE_MAX_LENGTH) {
178                 title = title.substring(0, KewApiConstants.TITLE_MAX_LENGTH);
179             }
180             this.title = title;
181         }
182 
183         public String getApplicationDocumentId() {
184             return applicationDocumentId;
185         }
186 
187         public void setApplicationDocumentId(String applicationDocumentId) {
188             this.applicationDocumentId = applicationDocumentId;
189         }
190 
191         public String getApplicationDocumentStatus() {
192             return applicationDocumentStatus;
193         }
194 
195         public void setApplicationDocumentStatus(String applicationDocumentStatus) {
196             this.applicationDocumentStatus = applicationDocumentStatus;
197         }
198 
199         public void setVariables(Map<String, String> variables) {
200             if (variables == null) {
201                 this.variables = new HashMap<String, String>();
202             } else {
203                 this.variables = new HashMap<String, String>(variables);
204             }
205         }
206 
207         public Map<String, String> getVariables() {
208             return variables;
209         }
210 
211         public String getVariableValue(String name) {
212             return variables.get(name);
213         }
214 
215         public void setVariable(String name, String value) {
216             if (StringUtils.isBlank(name)) {
217                 throw new IllegalArgumentException("name was null or blank");
218             }
219             variables.put(name, value);
220         }
221 
222         public List<String> getDirtyFields() {
223             return dirtyFields;
224         }
225 
226         public void setDirtyFields(List<String> dirtyFields) {
227             this.dirtyFields = dirtyFields;
228         }
229 
230         public void addDirtyField(String field) {
231             if(this.getDirtyFields() == null) {
232                 this.setDirtyFields(new ArrayList<String>());
233             }
234             this.getDirtyFields().add(field);
235         }
236 
237     }
238 
239     /**
240      * Defines some internal constants used on this class.
241      */
242     static class Constants {
243         final static String ROOT_ELEMENT_NAME = "documentUpdate";
244         final static String TYPE_NAME = "DocumentUpdateType";
245     }
246 
247     /**
248      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
249      */
250     static class Elements {
251         final static String TITLE = "title";
252         final static String APPLICATION_DOCUMENT_ID = "applicationDocumentId";
253         final static String APPLICATION_DOCUMENT_STATUS = "applicationDocumentStatus";
254         final static String VARIABLES = "variables";
255         final static String DIRTY_FIELDS = "dirtyFields";
256     }
257 }