View Javadoc

1   /**
2    * Copyright 2005-2012 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.XmlRootElement;
31  import javax.xml.bind.annotation.XmlType;
32  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
33  import java.io.Serializable;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.HashMap;
37  import java.util.Map;
38  
39  /**
40   * Defines an update to document meta-data on a particular workflow document,
41   * including title, application document id, application document status,
42   * and routing branch variables.  This structure is used to convey changes to
43   * the document meta-data on document actions, and to retrieve their state
44   * afterwards.
45   * Document titles will be truncated to to {@link KewApiConstants#TITLE_MAX_LENGTH} length.
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   * @see org.kuali.rice.kew.api.WorkflowDocumentFactory
49   * @see WorkflowDocumentImpl
50   * @see WorkflowDocumentImpl.ModifiableDocument
51   * @see WorkflowDocumentActionsServiceImpl
52   */
53  @XmlRootElement(name = DocumentUpdate.Constants.ROOT_ELEMENT_NAME)
54  @XmlAccessorType(XmlAccessType.NONE)
55  @XmlType(name = DocumentUpdate.Constants.TYPE_NAME, propOrder = {
56      DocumentUpdate.Elements.TITLE,
57      DocumentUpdate.Elements.APPLICATION_DOCUMENT_ID,
58      DocumentUpdate.Elements.APPLICATION_DOCUMENT_STATUS,
59      DocumentUpdate.Elements.VARIABLES,
60      CoreConstants.CommonElements.FUTURE_ELEMENTS
61  })
62  public final class DocumentUpdate extends AbstractDataTransferObject {
63  
64      private static final long serialVersionUID = 608839901744771499L;
65  
66      @XmlElement(name = Elements.TITLE, required = false)
67      private final String title;
68  
69      @XmlElement(name = Elements.APPLICATION_DOCUMENT_ID, required = false)
70      private final String applicationDocumentId;
71  
72      @XmlElement(name = Elements.APPLICATION_DOCUMENT_STATUS, required = false)
73      private final String applicationDocumentStatus;
74  
75      @XmlElement(name = Elements.VARIABLES, required = false)
76      @XmlJavaTypeAdapter(MapStringStringAdapter.class)
77      private final Map<String, String> variables;
78  
79      @SuppressWarnings("unused")
80      @XmlAnyElement
81      private final Collection<Element> _futureElements = null;
82  
83      private DocumentUpdate() {
84          this.title = null;
85          this.applicationDocumentId = null;
86          this.applicationDocumentStatus = null;
87          this.variables = null;
88      }
89  
90      private DocumentUpdate(Builder builder) {
91          this.title = builder.getTitle();
92          this.applicationDocumentId = builder.getApplicationDocumentId();
93          this.applicationDocumentStatus = builder.getApplicationDocumentStatus();
94          this.variables = builder.getVariables();
95      }
96  
97      public String getTitle() {
98          return title;
99      }
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 }