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.misc;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027    import microsoft.exchange.webservices.data.core.service.folder.Folder;
028    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
029    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
030    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
031    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException;
032    import microsoft.exchange.webservices.data.property.complex.FolderId;
033    
034    import java.util.ArrayList;
035    import java.util.Iterator;
036    import java.util.List;
037    
038    /**
039     * Represents a list a abstracted folder Ids.
040     */
041    public class FolderIdWrapperList implements Iterable<AbstractFolderIdWrapper> {
042    
043      /**
044       * The ids.
045       */
046      private List<AbstractFolderIdWrapper> ids = new
047          ArrayList<AbstractFolderIdWrapper>();
048    
049      /**
050       * Adds the specified folder.
051       *
052       * @param folder the folder
053       * @throws ServiceLocalException the service local exception
054       */
055      public void add(Folder folder) throws ServiceLocalException {
056        this.ids.add(new FolderWrapper(folder));
057      }
058    
059      /**
060       * Adds the range.
061       *
062       * @param folders the folder
063       * @throws ServiceLocalException the service local exception
064       */
065      protected void addRangeFolder(Iterable<Folder> folders)
066          throws ServiceLocalException {
067        if (folders != null) {
068          for (Folder folder : folders) {
069            this.add(folder);
070          }
071        }
072      }
073    
074      /**
075       * Adds the specified folder id.
076       *
077       * @param folderId the folder id
078       */
079      public void add(FolderId folderId) {
080        this.ids.add(new FolderIdWrapper(folderId));
081      }
082    
083      /**
084       * Adds the range of folder ids.
085       *
086       * @param folderIds the folder ids
087       */
088      public void addRangeFolderId(Iterable<FolderId> folderIds) {
089        if (folderIds != null) {
090          for (FolderId folderId : folderIds) {
091            this.add(folderId);
092          }
093        }
094      }
095    
096      /**
097       * Writes to XML.
098       *
099       * @param writer         the writer
100       * @param ewsNamesapce   the ews namesapce
101       * @param xmlElementName the xml element name
102       * @throws Exception the exception
103       */
104      public void writeToXml(EwsServiceXmlWriter writer, XmlNamespace ewsNamesapce, String xmlElementName) throws Exception {
105        if (this.getCount() > 0) {
106          writer.writeStartElement(ewsNamesapce, xmlElementName);
107    
108          for (AbstractFolderIdWrapper folderIdWrapper : this.ids) {
109            folderIdWrapper.writeToXml(writer);
110          }
111    
112          writer.writeEndElement();
113        }
114      }
115    
116      /**
117       * Gets the id count.
118       *
119       * @return the count
120       */
121      public int getCount() {
122        return this.ids.size();
123      }
124    
125      /**
126       * Gets the <see
127       * cref="Microsoft.Exchange.WebServices.Data.AbstractFolderIdWrapper"/> at
128       * the specified index.
129       *
130       * @param i the i
131       * @return the index
132       */
133      public AbstractFolderIdWrapper getFolderIdWrapperList(int i) {
134        return this.ids.get(i);
135      }
136    
137      /**
138       * Validates list of folderIds against a specified request version.
139       *
140       * @param version the version
141       * @throws ServiceVersionException the service version exception
142       */
143      public void validate(ExchangeVersion version)
144          throws ServiceVersionException {
145        for (AbstractFolderIdWrapper folderIdWrapper : this.ids) {
146          folderIdWrapper.validate(version);
147        }
148      }
149    
150      /*
151       * (non-Javadoc)
152       *
153       * @see java.lang.Iterable#iterator()
154       */
155      @Override
156      public Iterator<AbstractFolderIdWrapper> iterator() {
157        return ids.iterator();
158      }
159    
160    }