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