1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.api.document.search;
17
18 import org.kuali.rice.core.api.CoreConstants;
19 import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
20 import org.kuali.rice.core.api.mo.ModelBuilder;
21 import org.kuali.rice.kew.api.document.Document;
22 import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
23 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeContract;
24 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeFactory;
25 import org.w3c.dom.Element;
26
27 import javax.xml.bind.annotation.XmlAccessType;
28 import javax.xml.bind.annotation.XmlAccessorType;
29 import javax.xml.bind.annotation.XmlAnyElement;
30 import javax.xml.bind.annotation.XmlElement;
31 import javax.xml.bind.annotation.XmlElementWrapper;
32 import javax.xml.bind.annotation.XmlRootElement;
33 import javax.xml.bind.annotation.XmlType;
34 import java.io.Serializable;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.List;
39
40
41
42
43
44
45
46 @XmlRootElement(name = DocumentSearchResult.Constants.ROOT_ELEMENT_NAME)
47 @XmlAccessorType(XmlAccessType.NONE)
48 @XmlType(name = DocumentSearchResult.Constants.TYPE_NAME, propOrder = {
49 DocumentSearchResult.Elements.DOCUMENT,
50 DocumentSearchResult.Elements.DOCUMENT_ATTRIBUTES,
51 CoreConstants.CommonElements.FUTURE_ELEMENTS
52 })
53 public final class DocumentSearchResult extends AbstractDataTransferObject implements DocumentSearchResultContract {
54
55 @XmlElement(name = Elements.DOCUMENT, required = false)
56 private final Document document;
57
58 @XmlElementWrapper(name = Elements.DOCUMENT_ATTRIBUTES, required = true)
59 @XmlElement(name = Elements.DOCUMENT_ATTRIBUTE, required = false)
60 private final List<DocumentAttribute> documentAttributes;
61
62 @SuppressWarnings("unused")
63 @XmlAnyElement
64 private final Collection<Element> _futureElements = null;
65
66
67
68
69 @SuppressWarnings("unused")
70 private DocumentSearchResult() {
71 this.document = null;
72 this.documentAttributes = null;
73 }
74
75 private DocumentSearchResult(Builder builder) {
76 this.document = builder.getDocument().build();
77 List<DocumentAttribute> documentAttributes = new ArrayList<DocumentAttribute>();
78 for (DocumentAttribute.AbstractBuilder<?> documentAttribute : builder.getDocumentAttributes()) {
79 documentAttributes.add(documentAttribute.build());
80 }
81 this.documentAttributes = Collections.unmodifiableList(documentAttributes);
82 }
83
84 @Override
85 public Document getDocument() {
86 return this.document;
87 }
88
89 @Override
90 public List<DocumentAttribute> getDocumentAttributes() {
91 return this.documentAttributes;
92 }
93
94
95
96
97
98
99
100
101
102 public List<DocumentAttribute> getDocumentAttributeByName(String attributeName) {
103 List<DocumentAttribute> namedAttributes = new ArrayList<DocumentAttribute>();
104 for (DocumentAttribute attribute : getDocumentAttributes()) {
105 if (attribute.getName().equals(attributeName)) {
106 namedAttributes.add(attribute);
107 }
108 }
109 return Collections.unmodifiableList(namedAttributes);
110 }
111
112
113
114
115
116
117
118
119
120
121 public DocumentAttribute getSingleDocumentAttributeByName(String attributeName) {
122 List<DocumentAttribute> namedAttributes = getDocumentAttributeByName(attributeName);
123 if (namedAttributes.isEmpty()) {
124 return null;
125 }
126 return namedAttributes.get(0);
127 }
128
129
130
131
132
133 public final static class Builder implements Serializable, ModelBuilder, DocumentSearchResultContract {
134
135 private Document.Builder document;
136 private List<DocumentAttribute.AbstractBuilder<?>> documentAttributes;
137
138 private Builder(Document.Builder document) {
139 setDocument(document);
140 setDocumentAttributes(new ArrayList<DocumentAttribute.AbstractBuilder<?>>());
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public static Builder create(Document.Builder document) {
154 return new Builder(document);
155 }
156
157
158
159
160
161
162
163
164
165
166 public static Builder create(DocumentSearchResultContract contract) {
167 if (contract == null) {
168 throw new IllegalArgumentException("contract was null");
169 }
170 Document.Builder documentBuilder = Document.Builder.create(contract.getDocument());
171 Builder builder = create(documentBuilder);
172 List<DocumentAttribute.AbstractBuilder<?>> documentAttributes = new ArrayList<DocumentAttribute.AbstractBuilder<?>>();
173 for (DocumentAttributeContract documentAttributeContract : contract.getDocumentAttributes()) {
174 documentAttributes.add(DocumentAttributeFactory.loadContractIntoBuilder(documentAttributeContract));
175 }
176 builder.setDocumentAttributes(documentAttributes);
177 return builder;
178 }
179
180 public DocumentSearchResult build() {
181 return new DocumentSearchResult(this);
182 }
183
184 @Override
185 public Document.Builder getDocument() {
186 return this.document;
187 }
188
189 @Override
190 public List<DocumentAttribute.AbstractBuilder<?>> getDocumentAttributes() {
191 return this.documentAttributes;
192 }
193
194 public void setDocument(Document.Builder document) {
195 if (document == null) {
196 throw new IllegalArgumentException("document was null");
197 }
198 this.document = document;
199 }
200
201 public void setDocumentAttributes(List<DocumentAttribute.AbstractBuilder<?>> documentAttributes) {
202 this.documentAttributes = documentAttributes;
203 }
204
205 }
206
207
208
209
210 static class Constants {
211 final static String ROOT_ELEMENT_NAME = "documentSearchResult";
212 final static String TYPE_NAME = "DocumentSearchResultType";
213 }
214
215
216
217
218 static class Elements {
219 final static String DOCUMENT = "document";
220 final static String DOCUMENT_ATTRIBUTES = "documentAttributes";
221 final static String DOCUMENT_ATTRIBUTE = "documentAttribute";
222 }
223
224 }