001    /*
002     * The MIT License
003     * Copyright (c) 2012 Microsoft Corporation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a copy
006     * of this software and associated documentation files (the "Software"), to deal
007     * in the Software without restriction, including without limitation the rights
008     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009     * copies of the Software, and to permit persons to whom the Software is
010     * furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021     * THE SOFTWARE.
022     */
023    
024    package microsoft.exchange.webservices.data.search;
025    
026    import microsoft.exchange.webservices.data.ISelfValidate;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.EwsUtilities;
029    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030    import microsoft.exchange.webservices.data.core.XmlElementNames;
031    import microsoft.exchange.webservices.data.core.enumeration.search.AggregateType;
032    import microsoft.exchange.webservices.data.core.enumeration.search.SortDirection;
033    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
034    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
035    import microsoft.exchange.webservices.data.property.definition.PropertyDefinitionBase;
036    import org.apache.commons.logging.Log;
037    import org.apache.commons.logging.LogFactory;
038    
039    import javax.xml.stream.XMLStreamException;
040    
041    /**
042     * Represents grouping options in item search operations.
043     */
044    public final class Grouping implements ISelfValidate {
045    
046      private static final Log LOG = LogFactory.getLog(Grouping.class);
047    
048      /**
049       * The sort direction.
050       */
051      private SortDirection sortDirection = SortDirection.Ascending;
052    
053      /**
054       * The group on.
055       */
056      private PropertyDefinitionBase groupOn;
057    
058      /**
059       * The aggregate on.
060       */
061      private PropertyDefinitionBase aggregateOn;
062    
063      /**
064       * The aggregate type.
065       */
066      private AggregateType aggregateType = AggregateType.Minimum;
067    
068      /**
069       * Validates this grouping.
070       *
071       * @throws Exception the exception
072       */
073      private void internalValidate() throws Exception {
074        EwsUtilities.validateParam(this.groupOn, "GroupOn");
075        EwsUtilities.validateParam(this.aggregateOn, "AggregateOn");
076      }
077    
078      /**
079       * Initializes a new instance of the "Grouping" class.
080       */
081      public Grouping() {
082    
083      }
084    
085      /**
086       * Initializes a new instance of the "Grouping" class.
087       *
088       * @param groupOn       The property to group on
089       * @param sortDirection The sort direction.
090       * @param aggregateOn   The property to aggregate on.
091       * @param aggregateType The type of aggregate to calculate.
092       * @throws Exception the exception
093       */
094      public Grouping(PropertyDefinitionBase groupOn,
095          SortDirection sortDirection, PropertyDefinitionBase aggregateOn,
096          AggregateType aggregateType) throws Exception {
097        this();
098        EwsUtilities.validateParam(groupOn, "groupOn");
099        EwsUtilities.validateParam(aggregateOn, "aggregateOn");
100    
101        this.groupOn = groupOn;
102        this.sortDirection = sortDirection;
103        this.aggregateOn = aggregateOn;
104        this.aggregateType = aggregateType;
105      }
106    
107      /**
108       * Writes to XML.
109       *
110       * @param writer the writer
111       * @throws XMLStreamException the XML stream exception
112       * @throws ServiceXmlSerializationException the service xml serialization exception
113       */
114      protected void writeToXml(EwsServiceXmlWriter writer)
115          throws XMLStreamException, ServiceXmlSerializationException {
116        writer
117            .writeStartElement(XmlNamespace.Messages,
118                XmlElementNames.GroupBy);
119        writer.writeAttributeValue(XmlAttributeNames.Order, this.sortDirection);
120    
121        this.groupOn.writeToXml(writer);
122    
123        writer.writeStartElement(XmlNamespace.Types,
124            XmlElementNames.AggregateOn);
125        writer.writeAttributeValue(XmlAttributeNames.Aggregate,
126            this.aggregateType);
127    
128        this.aggregateOn.writeToXml(writer);
129    
130        writer.writeEndElement(); // AggregateOn
131    
132        writer.writeEndElement(); // GroupBy
133      }
134    
135      /**
136       * Gets the Sort Direction.
137       *
138       * @return the sort direction
139       */
140      public SortDirection getSortDirection() {
141        return sortDirection;
142      }
143    
144      /**
145       * Sets the Sort Direction.
146       *
147       * @param sortDirection the new sort direction
148       */
149      public void setSortDirection(SortDirection sortDirection) {
150        this.sortDirection = sortDirection;
151      }
152    
153      /**
154       * Gets the property to group on.
155       *
156       * @return the group on
157       */
158      public PropertyDefinitionBase getGroupOn() {
159        return groupOn;
160      }
161    
162      /**
163       * sets the property to group on.
164       *
165       * @param groupOn the new group on
166       */
167      public void setGroupOn(PropertyDefinitionBase groupOn) {
168        this.groupOn = groupOn;
169      }
170    
171      /**
172       * Gets the property to aggregateOn.
173       *
174       * @return the aggregate on
175       */
176      public PropertyDefinitionBase getAggregateOn() {
177        return aggregateOn;
178      }
179    
180      /**
181       * Sets the property to aggregateOn.
182       *
183       * @param aggregateOn the new aggregate on
184       */
185      public void setAggregateOn(PropertyDefinitionBase aggregateOn) {
186        this.aggregateOn = aggregateOn;
187      }
188    
189      /**
190       * Gets the types of aggregate to calculate.
191       *
192       * @return the aggregate type
193       */
194      public AggregateType getAggregateType() {
195        return aggregateType;
196      }
197    
198      /**
199       * Sets the types of aggregate to calculate.
200       *
201       * @param aggregateType the new aggregate type
202       */
203      public void setAggregateType(AggregateType aggregateType) {
204        this.aggregateType = aggregateType;
205      }
206    
207      /**
208       * Implements ISelfValidate.Validate. Validates this grouping.
209       */
210      @Override
211      public void validate() {
212        try {
213          this.internalValidate();
214        } catch (Exception e) {
215          LOG.error(e);
216        }
217    
218      }
219    }