View Javadoc
1   /**
2    * Copyright 2005-2014 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.framework.document.search;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlAnyElement;
25  import javax.xml.bind.annotation.XmlElement;
26  import javax.xml.bind.annotation.XmlElementWrapper;
27  import javax.xml.bind.annotation.XmlRootElement;
28  import javax.xml.bind.annotation.XmlType;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.kuali.rice.core.api.CoreConstants;
32  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
33  import org.kuali.rice.core.api.mo.ModelBuilder;
34  import org.kuali.rice.core.api.mo.ModelObjectUtils;
35  import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
36  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeContract;
37  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeFactory;
38  import org.w3c.dom.Element;
39  
40  /**
41   * An immutable data transfer object implementation of the {@link DocumentSearchResultValueContract}.
42   * Instances of this class should be constructed using the nested {@link Builder} class.
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  @XmlRootElement(name = DocumentSearchResultValue.Constants.ROOT_ELEMENT_NAME)
47  @XmlAccessorType(XmlAccessType.NONE)
48  @XmlType(name = DocumentSearchResultValue.Constants.TYPE_NAME, propOrder = {
49      DocumentSearchResultValue.Elements.DOCUMENT_ID,
50      DocumentSearchResultValue.Elements.DOCUMENT_ATTRIBUTES,
51      CoreConstants.CommonElements.FUTURE_ELEMENTS
52  })
53  public final class DocumentSearchResultValue extends AbstractDataTransferObject
54          implements DocumentSearchResultValueContract {
55  
56      @XmlElement(name = Elements.DOCUMENT_ID, required = true)
57      private final String documentId;
58  
59      @XmlElementWrapper(name = Elements.DOCUMENT_ATTRIBUTES, required = false)
60      @XmlElement(name = Elements.DOCUMENT_ATTRIBUTE, required = false)
61      private final List<DocumentAttribute> documentAttributes;
62      
63      @SuppressWarnings("unused")
64      @XmlAnyElement
65      private final Collection<Element> _futureElements = null;
66  
67      /**
68       * Private constructor used only by JAXB.
69       */
70      @SuppressWarnings("unused")
71      private DocumentSearchResultValue() {
72          this.documentId = null;
73          this.documentAttributes = null;
74      }
75  
76      private DocumentSearchResultValue(Builder builder) {
77          this.documentId = builder.getDocumentId();
78          this.documentAttributes = ModelObjectUtils.buildImmutableCopy(builder.getDocumentAttributes());
79      }
80  
81      @Override
82      public String getDocumentId() {
83          return this.documentId;
84      }
85  
86      @Override
87      public List<DocumentAttribute> getDocumentAttributes() {
88          return this.documentAttributes;
89      }
90  
91      /**
92       * A builder which can be used to construct {@link DocumentSearchResultValue} instances.  Enforces the constraints
93       * of the {@link DocumentSearchResultValueContract}.
94       */
95      public final static class Builder implements Serializable, ModelBuilder, DocumentSearchResultValueContract {
96  
97          private String documentId;
98          private List<DocumentAttribute.AbstractBuilder<?>> documentAttributes;
99  
100         private Builder(String documentId) {
101             setDocumentId(documentId);
102             setDocumentAttributes(new ArrayList<DocumentAttribute.AbstractBuilder<?>>());
103         }
104 
105         /**
106          * Creates a new builder instance initialized with the given document id.  The list of document attributes on
107          * this builder is initialized to an empty list.
108          *
109          * @param documentId the id of the document with which to initialize this builder, must not be a null or blank
110          * value
111          *
112          * @return a new builder instance initialized with the given document id
113          */
114         public static Builder create(String documentId) {
115             return new Builder(documentId);
116         }
117 
118         /**
119          * Creates a new builder instance initialized with copies of the properties from the given contract.
120          *
121          * @param contract the contract from which to copy properties
122          *
123          * @return a builder instance initialized with properties from the given contract
124          *
125          * @throws IllegalArgumentException if the given contract is null
126          */
127         public static Builder create(DocumentSearchResultValueContract contract) {
128             if (contract == null) {
129                 throw new IllegalArgumentException("contract was null");
130             }
131             Builder builder = create(contract.getDocumentId());
132             if (contract.getDocumentAttributes() != null) {
133                 for (DocumentAttributeContract documentAttribute : contract.getDocumentAttributes()) {
134                     builder.getDocumentAttributes().add(DocumentAttributeFactory.loadContractIntoBuilder(documentAttribute));
135                 }
136             }
137             return builder;
138         }
139 
140         @Override
141         public DocumentSearchResultValue build() {
142             return new DocumentSearchResultValue(this);
143         }
144 
145         @Override
146         public String getDocumentId() {
147             return this.documentId;
148         }
149 
150         @Override
151         public List<DocumentAttribute.AbstractBuilder<?>> getDocumentAttributes() {
152             return this.documentAttributes;
153         }
154 
155         /**
156          * Sets the document id on this builder to the given value.  The given document id must not be a null or blank
157          * value.
158          *
159          * @param documentId the id of the document to set on this builder, must not be a null or blank value
160          *
161          * @throws IllegalArgumentException if documentId is a null or blank value
162          */
163         public void setDocumentId(String documentId) {
164             if (StringUtils.isBlank(documentId)) {
165                 throw new IllegalArgumentException("documentId was null or blank");
166             }
167             this.documentId = documentId;
168         }
169 
170         /**
171          * Sets the list of document attribute builders on this builder to the given list.
172          *
173          * @param documentAttributes the list of document attribute builders to set on this builder
174          */
175         public void setDocumentAttributes(List<DocumentAttribute.AbstractBuilder<?>> documentAttributes) {
176             this.documentAttributes = documentAttributes;
177         }
178 
179     }
180 
181     /**
182      * Defines some internal constants used on this class.
183      */
184     static class Constants {
185         final static String ROOT_ELEMENT_NAME = "documentSearchResultValue";
186         final static String TYPE_NAME = "DocumentSearchResultValueType";
187     }
188 
189     /**
190      * A private class which exposes constants which define the XML element names to use when this object is marshalled
191      * to XML.
192      */
193     static class Elements {
194         final static String DOCUMENT_ID = "documentId";
195         final static String DOCUMENT_ATTRIBUTES = "documentAttributes";
196         final static String DOCUMENT_ATTRIBUTE = "documentAttribute";
197     }
198 
199 }