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.property.complex;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.EwsUtilities;
029    import microsoft.exchange.webservices.data.core.XmlElementNames;
030    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031    
032    /**
033     * Represents a rule that automatically handles incoming messages.
034     * A rule consists of a set of conditions
035     * and exception that determine whether or
036     * not a set of actions should be executed on incoming messages.
037     */
038    public final class Rule extends ComplexProperty {
039    
040      /**
041       * The rule ID.
042       */
043      private String ruleId;
044    
045      /**
046       * The rule display name.
047       */
048      private String displayName;
049    
050      /**
051       * The rule priority.
052       */
053      private int priority;
054    
055      /**
056       * The rule status of enabled or not.
057       */
058      private boolean isEnabled;
059    
060      /**
061       * The rule status of is supported or not.
062       */
063      private boolean isNotSupported;
064    
065      /**
066       * The rule status of in error or not.
067       */
068      private boolean isInError;
069    
070      /**
071       * The rule conditions.
072       */
073      private RulePredicates conditions;
074    
075      /**
076       * The rule actions.
077       */
078      private RuleActions actions;
079    
080      /**
081       * The rule exception.
082       */
083      private RulePredicates exceptions;
084    
085      /**
086       * Initializes a new instance of the Rule class.
087       */
088      public Rule() {
089        super();
090    
091        /**
092         * New rule has priority as 0 by default
093         */
094        this.priority = 1;
095    
096        /**
097         * New rule is enabled by default
098         */
099        this.isEnabled = true;
100        this.conditions = new RulePredicates();
101        this.actions = new RuleActions();
102        this.exceptions = new RulePredicates();
103      }
104    
105    
106      /**
107       * Gets or sets the Id of this rule.
108       */
109      public String getId() {
110    
111        return this.ruleId;
112      }
113    
114      public void setId(String value) {
115        if (this.canSetFieldValue(this.ruleId, value)) {
116          this.ruleId = value;
117          this.changed();
118        }
119      }
120    
121      /**
122       * Gets or sets the name of this rule as it should be displayed to the user.
123       */
124      public String getDisplayName() {
125        return this.displayName;
126      }
127    
128      public void setDisplayName(String value) {
129        if (this.canSetFieldValue(this.displayName, value)) {
130          this.displayName = value;
131          this.changed();
132        }
133      }
134    
135    
136      /**
137       * Gets or sets the priority of this rule,
138       * which determines its execution order.
139       */
140      public int getPriority() {
141        return this.priority;
142      }
143    
144      public void setPriority(int value) {
145        if (this.canSetFieldValue(this.priority, value)) {
146          this.priority = value;
147          this.changed();
148        }
149      }
150    
151    
152      /**
153       * Gets or sets a value indicating whether this rule is enabled.
154       */
155      public boolean getIsEnabled() {
156        return this.isEnabled;
157      }
158    
159      public void setIsEnabled(boolean value) {
160        if (this.canSetFieldValue(this.isEnabled, value)) {
161          this.isEnabled = value;
162          this.changed();
163        }
164      }
165    
166      /**
167       * Gets a value indicating whether this rule can be modified via EWS.
168       * If IsNotSupported is true, the rule cannot be modified via EWS.
169       */
170      public boolean getIsNotSupported() {
171        return this.isNotSupported;
172    
173      }
174    
175      /**
176       * Gets or sets a value indicating whether
177       * this rule has errors. A rule that is in error
178       * cannot be processed unless it is updated and the error is corrected.
179       */
180      public boolean getIsInError() {
181        return this.isInError;
182      }
183    
184      public void setIsInError(boolean value) {
185        if (this.canSetFieldValue(this.isInError, value)) {
186          this.isInError = value;
187          this.changed();
188        }
189      }
190    
191      /**
192       * Gets the conditions that determine whether or not this rule should be
193       * executed against incoming messages.
194       */
195      public RulePredicates getConditions() {
196        return this.conditions;
197      }
198    
199      /**
200       * Gets the actions that should be executed against incoming messages if the
201       * conditions evaluate as true.
202       */
203      public RuleActions getActions() {
204        return this.actions;
205    
206      }
207    
208      /**
209       * Gets the exception that determine
210       * if this rule should be skipped even if
211       * its conditions evaluate to true.
212       */
213      public RulePredicates getExceptions() {
214        return this.exceptions;
215      }
216    
217      /**
218       * Tries to read element from XML.
219       *
220       * @param reader The reader.
221       * @return True if element was read.
222       * @throws Exception
223       */
224      @Override
225      public boolean tryReadElementFromXml(EwsServiceXmlReader
226          reader) throws Exception {
227    
228        if (reader.getLocalName().equals(XmlElementNames.DisplayName)) {
229          this.displayName = reader.readElementValue();
230          return true;
231        } else if (reader.getLocalName().equals(XmlElementNames.RuleId)) {
232          this.ruleId = reader.readElementValue();
233          return true;
234        } else if (reader.getLocalName().equals(XmlElementNames.Priority)) {
235          this.priority = reader.readElementValue(Integer.class);
236          return true;
237        } else if (reader.getLocalName().equals(XmlElementNames.IsEnabled)) {
238          this.isEnabled = reader.readElementValue(Boolean.class);
239          return true;
240        } else if (reader.getLocalName().equals(XmlElementNames.IsNotSupported)) {
241          this.isNotSupported = reader.readElementValue(Boolean.class);
242          return true;
243        } else if (reader.getLocalName().equals(XmlElementNames.IsInError)) {
244          this.isInError = reader.readElementValue(Boolean.class);
245          return true;
246        } else if (reader.getLocalName().equals(XmlElementNames.Conditions)) {
247          this.conditions.loadFromXml(reader, reader.getLocalName());
248          return true;
249        } else if (reader.getLocalName().equals(XmlElementNames.Actions)) {
250          this.actions.loadFromXml(reader, reader.getLocalName());
251          return true;
252        } else if (reader.getLocalName().equals(XmlElementNames.Exceptions)) {
253          this.exceptions.loadFromXml(reader, reader.getLocalName());
254          return true;
255        } else {
256          return false;
257        }
258      }
259    
260      /**
261       * Writes elements to XML.
262       *
263       * @param writer The writer.
264       * @throws Exception
265       */
266      @Override
267      public void writeElementsToXml(EwsServiceXmlWriter writer)
268          throws Exception {
269        if (!(getId() == null || getId().isEmpty())) {
270          writer.writeElementValue(
271              XmlNamespace.Types,
272              XmlElementNames.RuleId,
273              this.getId());
274        }
275    
276        writer.writeElementValue(
277            XmlNamespace.Types,
278            XmlElementNames.DisplayName,
279            this.getDisplayName());
280        writer.writeElementValue(
281            XmlNamespace.Types,
282            XmlElementNames.Priority,
283            this.getPriority());
284        writer.writeElementValue(
285            XmlNamespace.Types,
286            XmlElementNames.IsEnabled,
287            this.getIsEnabled());
288        writer.writeElementValue(
289            XmlNamespace.Types,
290            XmlElementNames.IsInError,
291            this.getIsInError());
292        this.getConditions().writeToXml(writer, XmlElementNames.Conditions);
293        this.getExceptions().writeToXml(writer, XmlElementNames.Exceptions);
294        this.getActions().writeToXml(writer, XmlElementNames.Actions);
295      }
296    
297    
298      /**
299       * Validates this instance.
300       */
301      @Override
302      protected void internalValidate() throws Exception {
303        super.internalValidate();
304        EwsUtilities.validateParam(this.displayName, "DisplayName");
305        EwsUtilities.validateParam(this.conditions, "Conditions");
306        EwsUtilities.validateParam(this.exceptions, "Exceptions");
307        EwsUtilities.validateParam(this.actions, "Actions");
308      }
309    }
310