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.XmlElementNames;
028    import microsoft.exchange.webservices.data.core.exception.misc.ArgumentOutOfRangeException;
029    
030    import java.util.Iterator;
031    
032    /**
033     * Defines the RuleOperationError class.
034     */
035    public final class RuleOperationError extends ComplexProperty implements Iterable<RuleError> {
036      /**
037       * Index of the operation mapping to the error.
038       */
039      private int operationIndex;
040    
041      /**
042       * RuleOperation object mapping to the error.
043       */
044      private RuleOperation operation;
045    
046      /**
047       * RuleError Collection.
048       */
049      private RuleErrorCollection ruleErrors;
050    
051      /**
052       * Initializes a new instance of the RuleOperationError class.
053       */
054      protected RuleOperationError() {
055        super();
056      }
057    
058      /**
059       * Gets the operation that resulted in an error.
060       *
061       * @return operation
062       */
063      public RuleOperation getOperation() {
064        return this.operation;
065      }
066    
067      /**
068       * Gets the number of rule errors in the list.
069       *
070       * @return count
071       */
072      public int getCount() {
073        return this.ruleErrors.getCount();
074      }
075    
076      /**
077       * Gets the rule error at the specified index.
078       *
079       * @return Index
080       * @throws ArgumentOutOfRangeException
081       */
082      public RuleError getRuleError(int index)
083          throws ArgumentOutOfRangeException {
084        if (index < 0 || index >= this.getCount()) {
085          throw new ArgumentOutOfRangeException("index");
086        }
087    
088        return this.ruleErrors.getPropertyAtIndex(index);
089    
090      }
091    
092    
093      /**
094       * Tries to read element from XML.
095       *
096       * @return true
097       * @throws Exception
098       */
099      @Override
100      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
101          throws Exception {
102        if (reader.getLocalName().equals(XmlElementNames.OperationIndex)) {
103          this.operationIndex = reader.readElementValue(Integer.class);
104          return true;
105        } else if (reader.getLocalName().equals(XmlElementNames.ValidationErrors)) {
106          this.ruleErrors = new RuleErrorCollection();
107          this.ruleErrors.loadFromXml(reader, reader.getLocalName());
108          return true;
109        } else {
110          return false;
111        }
112      }
113    
114      /**
115       * Set operation property by the index of a given opeation enumerator.
116       */
117      public void setOperationByIndex(Iterator<RuleOperation> operations) {
118        for (int i = 0; i <= this.operationIndex; i++) {
119          operations.next();
120        }
121        this.operation = operations.next();
122      }
123    
124      /**
125       * Gets an iterator that iterates through the elements of the collection.
126       *
127       * @return An Iterator for the collection.
128       */
129      public Iterator<RuleError> iterator() {
130        return this.ruleErrors.iterator();
131      }
132    }