001    /**
002     * Copyright 2005-2013 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.location.api.postalcode;
017    
018    import org.kuali.rice.core.api.CoreConstants;
019    import org.kuali.rice.core.api.criteria.QueryResults;
020    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021    import org.kuali.rice.core.api.mo.ModelBuilder;
022    import org.kuali.rice.location.api.county.County;
023    import org.w3c.dom.Element;
024    
025    import javax.xml.bind.annotation.XmlAccessType;
026    import javax.xml.bind.annotation.XmlAccessorType;
027    import javax.xml.bind.annotation.XmlAnyElement;
028    import javax.xml.bind.annotation.XmlElement;
029    import javax.xml.bind.annotation.XmlElementWrapper;
030    import javax.xml.bind.annotation.XmlRootElement;
031    import javax.xml.bind.annotation.XmlType;
032    import java.util.ArrayList;
033    import java.util.Collection;
034    import java.util.Collections;
035    import java.util.List;
036    
037    /**
038     * @author Kuali Rice Team (rice.collab@kuali.org)
039     */
040    @XmlRootElement(name = PostalCodeQueryResults.Constants.ROOT_ELEMENT_NAME)
041    @XmlAccessorType(XmlAccessType.NONE)
042    @XmlType(name = PostalCodeQueryResults.Constants.TYPE_NAME, propOrder = {
043                    PostalCodeQueryResults.Elements.RESULTS,
044                    PostalCodeQueryResults.Elements.TOTAL_ROW_COUNT,
045                    PostalCodeQueryResults.Elements.MORE_RESULTS_AVAILALBE,
046                    CoreConstants.CommonElements.FUTURE_ELEMENTS })
047    public class PostalCodeQueryResults extends AbstractDataTransferObject implements QueryResults<PostalCode> {
048    
049            @XmlElementWrapper(name = Elements.RESULTS, required = false)
050            @XmlElement(name = Elements.RESULT_ELEM, required = false)
051            private final List<PostalCode> results;
052    
053            @XmlElement(name = Elements.TOTAL_ROW_COUNT, required = false)
054            private final Integer totalRowCount;
055    
056            @XmlElement(name = Elements.MORE_RESULTS_AVAILALBE, required = true)
057            private final boolean moreResultsAvailable;
058    
059            @SuppressWarnings("unused")
060        @XmlAnyElement
061        private final Collection<Element> _futureElements = null;
062    
063            private PostalCodeQueryResults() {
064                    this.results = null;
065                    this.totalRowCount = null;
066                    this.moreResultsAvailable = false;
067            }
068    
069            private PostalCodeQueryResults(Builder builder) {
070                    final List<PostalCode> temp = new ArrayList<PostalCode>();
071            for (PostalCode.Builder b : builder.getResults()) {
072                if (b != null) {
073                    temp.add(b.build());
074                }
075            }
076    
077            this.results = Collections.unmodifiableList(temp);
078                    this.totalRowCount = builder.getTotalRowCount();
079                    this.moreResultsAvailable = builder.isMoreResultsAvailable();
080            }
081    
082            @Override
083            public List<PostalCode> getResults() {
084                    return results;
085            }
086            
087            @Override
088            public Integer getTotalRowCount() {
089                    return totalRowCount;
090            }
091    
092            @Override
093            public boolean isMoreResultsAvailable() {
094                    return moreResultsAvailable;
095            }
096    
097            public static class Builder implements ModelBuilder, QueryResults<PostalCode.Builder> {
098    
099                    private List<PostalCode.Builder> results;
100                    private Integer totalRowCount;
101                    private boolean moreResultsAvailable;
102    
103            public static Builder create() {
104                return new Builder();
105            }
106    
107                    private Builder() {
108                            this.results = new ArrayList<PostalCode.Builder>();
109                            this.moreResultsAvailable = false;
110                    }
111    
112            @Override
113                    public PostalCodeQueryResults build() {
114                            return new PostalCodeQueryResults(this);
115                    }
116    
117            @Override
118                    public List<PostalCode.Builder> getResults() {
119                            return Collections.unmodifiableList(this.results);
120                    }
121    
122                    public void setResults(List<PostalCode.Builder> results) {
123                            this.results = new ArrayList<PostalCode.Builder>(results);
124                    }
125    
126            @Override
127                    public Integer getTotalRowCount() {
128                            return this.totalRowCount;
129                    }
130    
131                    public void setTotalRowCount(Integer totalRowCount) {
132                            this.totalRowCount = totalRowCount;
133                    }
134    
135            @Override
136                    public boolean isMoreResultsAvailable() {
137                            return this.moreResultsAvailable;
138                    }
139    
140                    public void setMoreResultsAvailable(boolean moreResultsAvailable) {
141                            this.moreResultsAvailable = moreResultsAvailable;
142                    }
143                    
144            }
145            
146            /**
147             * Defines some internal constants used on this class.
148             */
149            public static class Constants {
150                    public final static String ROOT_ELEMENT_NAME = "PostalCodeQueryResults";
151                    public final static String TYPE_NAME = "PostalCodeQueryResultsType";
152            }
153    
154            /**
155             * A private class which exposes constants which define the XML element
156             * names to use when this object is marshaled to XML.
157             */
158            public static class Elements {
159                    public final static String RESULTS = "results";
160                    public final static String RESULT_ELEM = "PostalCode";
161                    public final static String TOTAL_ROW_COUNT = "totalRowCount";
162                    public final static String MORE_RESULTS_AVAILALBE = "moreResultsAvailable";
163            }
164            
165    }