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.XmlElementNames;
029    import microsoft.exchange.webservices.data.core.enumeration.permission.folder.DelegateFolderPermissionLevel;
030    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
032    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
033    
034    import javax.xml.stream.XMLStreamException;
035    
036    import java.util.HashMap;
037    import java.util.Map;
038    
039    /**
040     * Represents the permissions of a delegate user.
041     */
042    public final class DelegatePermissions extends ComplexProperty {
043    
044      private Map<String, DelegateFolderPermission> delegateFolderPermissions;
045    
046      /**
047       * Initializes a new instance of the class.
048       */
049    
050      protected DelegatePermissions() {
051        super();
052        this.delegateFolderPermissions = new HashMap<String,
053            DelegateFolderPermission>();
054    
055        delegateFolderPermissions.put(
056            XmlElementNames.CalendarFolderPermissionLevel,
057            new DelegateFolderPermission());
058        delegateFolderPermissions.put(
059            XmlElementNames.TasksFolderPermissionLevel,
060            new DelegateFolderPermission());
061        delegateFolderPermissions.put(
062            XmlElementNames.InboxFolderPermissionLevel,
063            new DelegateFolderPermission());
064        delegateFolderPermissions.put(
065            XmlElementNames.ContactsFolderPermissionLevel,
066            new DelegateFolderPermission());
067        delegateFolderPermissions.put(
068            XmlElementNames.NotesFolderPermissionLevel,
069            new DelegateFolderPermission());
070        delegateFolderPermissions.put(
071            XmlElementNames.JournalFolderPermissionLevel,
072            new DelegateFolderPermission());
073      }
074    
075      /**
076       * Gets the delegate user's permission on the principal's calendar.
077       *
078       * @return the calendar folder permission level
079       */
080      public DelegateFolderPermissionLevel getCalendarFolderPermissionLevel() {
081        return this.delegateFolderPermissions.get(XmlElementNames.
082            CalendarFolderPermissionLevel).getPermissionLevel();
083    
084      }
085    
086      /**
087       * sets the delegate user's permission on the principal's calendar.
088       *
089       * @param value the new calendar folder permission level
090       */
091      public void setCalendarFolderPermissionLevel(
092          DelegateFolderPermissionLevel value) {
093        this.delegateFolderPermissions.get(XmlElementNames.
094            CalendarFolderPermissionLevel).setPermissionLevel(value);
095      }
096    
097      /**
098       * Gets  the delegate user's permission on the principal's tasks
099       * folder.
100       *
101       * @return the tasks folder permission level
102       */
103      public DelegateFolderPermissionLevel getTasksFolderPermissionLevel() {
104        return this.delegateFolderPermissions.get(XmlElementNames.
105            TasksFolderPermissionLevel).getPermissionLevel();
106    
107      }
108    
109      /**
110       * Sets the tasks folder permission level.
111       *
112       * @param value the new tasks folder permission level
113       */
114      public void setTasksFolderPermissionLevel(
115          DelegateFolderPermissionLevel value) {
116    
117        this.delegateFolderPermissions.get(XmlElementNames.
118            TasksFolderPermissionLevel).setPermissionLevel(value);
119      }
120    
121      /**
122       * Gets the delegate user's permission on the principal's inbox.
123       *
124       * @return the inbox folder permission level
125       */
126      public DelegateFolderPermissionLevel getInboxFolderPermissionLevel() {
127        return this.delegateFolderPermissions.get(XmlElementNames.
128            InboxFolderPermissionLevel).
129            getPermissionLevel();
130      }
131    
132      /**
133       * Sets the inbox folder permission level.
134       *
135       * @param value the new inbox folder permission level
136       */
137      public void setInboxFolderPermissionLevel(
138          DelegateFolderPermissionLevel value) {
139        this.delegateFolderPermissions.get(XmlElementNames.
140            InboxFolderPermissionLevel).
141            setPermissionLevel(value);
142      }
143    
144      /**
145       * Gets  the delegate user's permission on the principal's contacts
146       * folder.
147       *
148       * @return the contacts folder permission level
149       */
150      public DelegateFolderPermissionLevel getContactsFolderPermissionLevel() {
151        return this.delegateFolderPermissions.get(
152            XmlElementNames.ContactsFolderPermissionLevel).
153            getPermissionLevel();
154      }
155    
156      /**
157       * Sets the contacts folder permission level.
158       *
159       * @param value the new contacts folder permission level
160       */
161      public void setContactsFolderPermissionLevel(
162          DelegateFolderPermissionLevel value) {
163        this.delegateFolderPermissions.get(
164            XmlElementNames.ContactsFolderPermissionLevel).
165            setPermissionLevel(value);
166      }
167    
168      /**
169       * Gets the delegate user's permission on the principal's notes
170       * folder.
171       *
172       * @return the notes folder permission level
173       */
174      public DelegateFolderPermissionLevel getNotesFolderPermissionLevel() {
175        return this.delegateFolderPermissions.get(XmlElementNames.
176            NotesFolderPermissionLevel).
177            getPermissionLevel();
178      }
179    
180      /**
181       * Sets the notes folder permission level.
182       *
183       * @param value the new notes folder permission level
184       */
185      public void setNotesFolderPermissionLevel(
186          DelegateFolderPermissionLevel value) {
187        this.delegateFolderPermissions.get(XmlElementNames.
188            NotesFolderPermissionLevel).
189            setPermissionLevel(value);
190      }
191    
192      /**
193       * Gets  the delegate user's permission on the principal's journal
194       * folder.
195       *
196       * @return the journal folder permission level
197       */
198      public DelegateFolderPermissionLevel getJournalFolderPermissionLevel() {
199        return this.delegateFolderPermissions.get(XmlElementNames.
200            JournalFolderPermissionLevel).
201            getPermissionLevel();
202      }
203    
204      /**
205       * Sets the journal folder permission level.
206       *
207       * @param value the new journal folder permission level
208       */
209      public void setJournalFolderPermissionLevel(
210          DelegateFolderPermissionLevel value) {
211        this.delegateFolderPermissions.get(XmlElementNames.
212            JournalFolderPermissionLevel).
213            setPermissionLevel(value);
214      }
215    
216      /**
217       * Reset.
218       */
219      protected void reset() {
220        for (DelegateFolderPermission delegateFolderPermission : this.delegateFolderPermissions.values()) {
221          delegateFolderPermission.reset();
222        }
223      }
224    
225      /**
226       * Tries to read element from XML.
227       *
228       * @param reader the reader
229       * @return Returns true if element was read.
230       * @throws Exception the exception
231       */
232      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
233          throws Exception {
234        DelegateFolderPermission delegateFolderPermission = null;
235    
236        if (this.delegateFolderPermissions.containsKey(reader.getLocalName())) {
237          delegateFolderPermission = this.delegateFolderPermissions.
238              get(reader.getLocalName());
239          delegateFolderPermission.initialize(reader.
240              readElementValue(DelegateFolderPermissionLevel.class));
241        }
242    
243    
244        return delegateFolderPermission != null;
245      }
246    
247      /**
248       * Writes elements to XML.
249       *
250       * @param writer the writer
251       * @throws Exception the exception
252       */
253      public void writeElementsToXml(EwsServiceXmlWriter writer)
254          throws Exception {
255        this.writePermissionToXml(writer,
256            XmlElementNames.CalendarFolderPermissionLevel);
257    
258        this.writePermissionToXml(writer,
259            XmlElementNames.TasksFolderPermissionLevel);
260    
261        this.writePermissionToXml(writer,
262            XmlElementNames.InboxFolderPermissionLevel);
263    
264        this.writePermissionToXml(writer,
265            XmlElementNames.ContactsFolderPermissionLevel);
266    
267        this.writePermissionToXml(writer,
268            XmlElementNames.NotesFolderPermissionLevel);
269    
270        this.writePermissionToXml(writer,
271            XmlElementNames.JournalFolderPermissionLevel);
272      }
273    
274      /**
275       * Write permission to Xml.
276       *
277       * @param writer         the writer
278       * @param xmlElementName the element name
279       * @throws XMLStreamException the XML stream exception
280       */
281      private void writePermissionToXml(
282          EwsServiceXmlWriter writer,
283          String xmlElementName) throws ServiceXmlSerializationException,
284          XMLStreamException {
285        DelegateFolderPermissionLevel delegateFolderPermissionLevel =
286            this.delegateFolderPermissions.
287                get(xmlElementName).getPermissionLevel();
288        // E14 Bug 298307: UpdateDelegate fails if
289        //Custom permission level is round tripped
290        //
291        if (delegateFolderPermissionLevel != DelegateFolderPermissionLevel.Custom) {
292          writer.writeElementValue(
293              XmlNamespace.Types,
294              xmlElementName,
295              delegateFolderPermissionLevel);
296        }
297      }
298    
299      /**
300       * Validates this instance for AddDelegate.
301       *
302       * @throws ServiceValidationException
303       */
304      protected void validateAddDelegate() throws ServiceValidationException {
305        for (DelegateFolderPermission delegateFolderPermission : this.delegateFolderPermissions.values()) {
306          if (delegateFolderPermission.getPermissionLevel() == DelegateFolderPermissionLevel.Custom) {
307            throw new ServiceValidationException("This operation can't be performed because one or more folder "
308                                                 + "permission levels were set to Custom.");
309          }
310        }
311      }
312    
313      /**
314       * Validates this instance for UpdateDelegate.
315       *
316       * @throws ServiceValidationException
317       */
318      protected void validateUpdateDelegate() throws ServiceValidationException {
319        for (DelegateFolderPermission delegateFolderPermission : this.delegateFolderPermissions.values()) {
320          if (delegateFolderPermission.getPermissionLevel() == DelegateFolderPermissionLevel.Custom &&
321              !delegateFolderPermission.isExistingPermissionLevelCustom) {
322            throw new ServiceValidationException("This operation can't be performed because one or more folder "
323                                                 + "permission levels were set to Custom.");
324          }
325        }
326      }
327    
328      /**
329       * Represents a folder's DelegateFolderPermissionLevel
330       */
331      private static class DelegateFolderPermission {
332    
333        /**
334         * Initializes this DelegateFolderPermission.
335         *
336         * @param permissionLevel The DelegateFolderPermissionLevel
337         */
338        protected void initialize(
339            DelegateFolderPermissionLevel permissionLevel) {
340          this.setPermissionLevel(permissionLevel);
341          this.setIsExistingPermissionLevelCustom(permissionLevel ==
342              DelegateFolderPermissionLevel.Custom);
343        }
344    
345        /**
346         * Resets this DelegateFolderPermission.
347         */
348        protected void reset() {
349          this.initialize(DelegateFolderPermissionLevel.None);
350        }
351    
352    
353        private DelegateFolderPermissionLevel permissionLevel = DelegateFolderPermissionLevel.None;
354    
355        /**
356         * Gets the delegate user's permission on a principal's folder.
357         */
358        protected DelegateFolderPermissionLevel getPermissionLevel() {
359          return this.permissionLevel;
360        }
361    
362        /**
363         * Sets the delegate user's permission on a principal's folder.
364         */
365        protected void setPermissionLevel(
366            DelegateFolderPermissionLevel value) {
367          this.permissionLevel = value;
368        }
369    
370    
371        private boolean isExistingPermissionLevelCustom;
372    
373        /**
374         * Gets IsExistingPermissionLevelCustom.
375         */
376        protected boolean getIsExistingPermissionLevelCustom() {
377          return this.isExistingPermissionLevelCustom;
378        }
379    
380        /**
381         * Sets IsExistingPermissionLevelCustom.
382         */
383        private void setIsExistingPermissionLevelCustom(Boolean value) {
384          this.isExistingPermissionLevelCustom = value;
385        }
386    
387      }
388    }