001    /**
002     * Copyright 2005-2012 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.framework.document.search;
017    
018    import org.kuali.rice.core.api.CoreConstants;
019    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
020    import org.kuali.rice.core.api.mo.ModelBuilder;
021    import org.kuali.rice.core.api.uif.RemotableAttributeField;
022    import org.w3c.dom.Element;
023    
024    import javax.xml.bind.annotation.XmlAccessType;
025    import javax.xml.bind.annotation.XmlAccessorType;
026    import javax.xml.bind.annotation.XmlAnyElement;
027    import javax.xml.bind.annotation.XmlElement;
028    import javax.xml.bind.annotation.XmlElementWrapper;
029    import javax.xml.bind.annotation.XmlRootElement;
030    import javax.xml.bind.annotation.XmlType;
031    import java.io.Serializable;
032    import java.util.ArrayList;
033    import java.util.Collection;
034    import java.util.Collections;
035    import java.util.List;
036    
037    /**
038     * An immutable data transfer object implementation of the {@link DocumentSearchCriteriaConfigurationContract}.
039     * Instances of this class should be constructed using the nested {@link Builder} class.
040     *
041     * @author Kuali Rice Team (rice.collab@kuali.org)
042     */
043    @XmlRootElement(name = DocumentSearchCriteriaConfiguration.Constants.ROOT_ELEMENT_NAME)
044    @XmlAccessorType(XmlAccessType.NONE)
045    @XmlType(name = DocumentSearchCriteriaConfiguration.Constants.TYPE_NAME, propOrder = {
046        DocumentSearchCriteriaConfiguration.Elements.SEARCH_ATTRIBUTE_FIELDS,
047        CoreConstants.CommonElements.FUTURE_ELEMENTS
048    })
049    public final class DocumentSearchCriteriaConfiguration extends AbstractDataTransferObject implements DocumentSearchCriteriaConfigurationContract {
050    
051        private static final long serialVersionUID = -5764134034667636217L;
052    
053        @XmlElementWrapper(name = Elements.SEARCH_ATTRIBUTE_FIELDS, required = false)
054        @XmlElement(name = Elements.ATTRIBUTE_FIELDS, required = false)
055        private final List<AttributeFields> searchAttributeFields;
056    
057        @SuppressWarnings("unused")
058        @XmlAnyElement
059        private final Collection<Element> _futureElements = null;
060    
061        /**
062         * Private constructor used only by JAXB.
063         */
064        @SuppressWarnings("unused")
065        private DocumentSearchCriteriaConfiguration() {
066            this.searchAttributeFields = null;
067        }
068    
069        private DocumentSearchCriteriaConfiguration(Builder builder) {
070            if (builder.getSearchAttributeFields() == null) {
071                this.searchAttributeFields = Collections.emptyList();
072            } else {
073                this.searchAttributeFields = Collections.unmodifiableList(new ArrayList<AttributeFields>(builder.getSearchAttributeFields()));
074            }
075        }
076    
077        @Override
078        public List<AttributeFields> getSearchAttributeFields() {
079            return this.searchAttributeFields;
080        }
081    
082        public List<RemotableAttributeField> getFlattenedSearchAttributeFields() {
083            List<RemotableAttributeField> searchAttributeFields = new ArrayList<RemotableAttributeField>();
084            for (AttributeFields attributeFields : getSearchAttributeFields()) {
085                searchAttributeFields.addAll(attributeFields.getRemotableAttributeFields());
086            }
087            return searchAttributeFields;
088        }
089    
090        /**
091         * A builder which can be used to construct {@link DocumentSearchCriteriaConfiguration} instances.  Enforces the
092         * constraints of the {@link DocumentSearchCriteriaConfigurationContract}.
093         */
094        public final static class Builder implements Serializable, ModelBuilder, DocumentSearchCriteriaConfigurationContract {
095    
096            private List<AttributeFields> searchAttributeFields;
097    
098            private Builder() {
099                setSearchAttributeFields(new ArrayList<AttributeFields>());
100            }
101    
102            /**
103             * Creates new empty builder instance.  The list of search attributes on this builder is intialized to an empty
104             * list.
105             *
106             * @return a new empty builder instance
107             */
108            public static Builder create() {
109                return new Builder();
110            }
111    
112            /**
113             * Creates a new builder instance initialized with copies of the properties from the given contract.
114             *
115             * @param contract the contract from which to copy properties
116             *
117             * @return a builder instance initialized with properties from the given contract
118             *
119             * @throws IllegalArgumentException if the given contract is null
120             */
121            public static Builder create(DocumentSearchCriteriaConfigurationContract contract) {
122                if (contract == null) {
123                    throw new IllegalArgumentException("contract was null");
124                }
125                Builder builder = create();
126                builder.setSearchAttributeFields(contract.getSearchAttributeFields());
127                return builder;
128            }
129    
130            @Override
131            public DocumentSearchCriteriaConfiguration build() {
132                return new DocumentSearchCriteriaConfiguration(this);
133            }
134    
135            @Override
136            public List<AttributeFields> getSearchAttributeFields() {
137                return this.searchAttributeFields;
138            }
139    
140            /**
141             * Sets the search attribute fields on this builder to the given list of attribute fields.
142             *
143             * @param searchAttributeFields the list of search attribute fields to set
144             */
145            public void setSearchAttributeFields(List<AttributeFields> searchAttributeFields) {
146                this.searchAttributeFields = searchAttributeFields;
147            }
148    
149        }
150    
151        /**
152         * Defines some internal constants used on this class.
153         */
154        static class Constants {
155            final static String ROOT_ELEMENT_NAME = "documentSearchCriteriaConfiguration";
156            final static String TYPE_NAME = "DocumentSearchCriteriaConfigurationType";
157        }
158    
159        /**
160         * A private class which exposes constants which define the XML element names to use when this object is marshalled
161         * to XML.
162         */
163        static class Elements {
164            final static String SEARCH_ATTRIBUTE_FIELDS = "searchAttributeFields";
165            final static String ATTRIBUTE_FIELDS = "attributeFields";
166        }
167    
168    }