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    
031    /**
032     * Represents an operation to update an existing rule.
033     */
034    public class SetRuleOperation extends RuleOperation {
035      /**
036       * Inbox rule to be updated.
037       */
038      private Rule rule;
039    
040      /**
041       * Initializes a new instance of the SetRuleOperation class.
042       */
043      public SetRuleOperation() {
044        super();
045      }
046    
047      /**
048       * Initializes a new instance of the SetRuleOperation class.
049       *
050       * @param rule The rule
051       *             The inbox rule to update.
052       */
053      public SetRuleOperation(Rule rule) {
054        super();
055        this.rule = rule;
056      }
057    
058      /**
059       * Gets the rule to be updated.
060       */
061      public Rule getRule() {
062        return this.rule;
063      }
064    
065      /**
066       * Sets the rule to be updated.
067       */
068      public void setRule(Rule value) {
069        if (this.canSetFieldValue(this.rule, value)) {
070          this.rule = value;
071          this.changed();
072        }
073      }
074    
075      /**
076       * Tries to read element from XML.
077       *
078       * @param reader The reader
079       * @return True if element was read.
080       */
081      @Override
082      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
083          throws Exception {
084        if (reader.getLocalName().equals(XmlElementNames.Rule)) {
085          this.rule = new Rule();
086          this.rule.loadFromXml(reader, reader.getLocalName());
087          return true;
088        } else {
089          return false;
090        }
091      }
092    
093      /**
094       * Writes elements to XML.
095       *
096       * @param writer The writer.
097       */
098      @Override
099      public void writeElementsToXml(EwsServiceXmlWriter writer)
100          throws Exception {
101        this.rule.writeToXml(writer, XmlElementNames.Rule);
102      }
103    
104      /**
105       * Validates this instance.
106       *
107       * @throws Exception
108       */
109      @Override
110      protected void internalValidate() throws Exception {
111        EwsUtilities.validateParam(this.rule, "Rule");
112      }
113    
114      /**
115       * Gets the Xml element name of the SetRuleOperation object.
116       */
117      @Override public String getXmlElementName() {
118        return XmlElementNames.SetRuleOperation;
119      }
120    }