001    package ezvcard.property;
002    
003    import java.util.EnumSet;
004    import java.util.List;
005    import java.util.Set;
006    
007    import ezvcard.VCard;
008    import ezvcard.VCardVersion;
009    import ezvcard.Warning;
010    
011    /*
012     Copyright (c) 2013, Michael Angstadt
013     All rights reserved.
014    
015     Redistribution and use in source and binary forms, with or without
016     modification, are permitted provided that the following conditions are met: 
017    
018     1. Redistributions of source code must retain the above copyright notice, this
019     list of conditions and the following disclaimer. 
020     2. Redistributions in binary form must reproduce the above copyright notice,
021     this list of conditions and the following disclaimer in the documentation
022     and/or other materials provided with the distribution. 
023    
024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034    
035     The views and conclusions contained in the software and documentation are those
036     of the authors and should not be interpreted as representing official policies, 
037     either expressed or implied, of the FreeBSD Project.
038     */
039    
040    /**
041     * The members that make up the group. This property can only be used if the
042     * {@link Kind} property is set to "group".
043     * 
044     * <p>
045     * <b>Adding members</b>
046     * </p>
047     * 
048     * <pre class="brush:java">
049     * VCard vcard = new VCard();
050     * 
051     * //KIND must be set to &quot;group&quot; in order to add MEMBERs
052     * vcard.setKind(Kind.group());
053     * 
054     * Member member = new Member();
055     * member.setUriEmail(&quot;funkyjoe@hotmail.com&quot;);
056     * vcard.addMember(member);
057     * member = new Member();
058     * member.setUriIM(&quot;aol&quot;, &quot;joesmoe@aol.com&quot;);
059     * vcard.addMember(member);
060     * member = new Member();
061     * member.setUriTelephone(&quot;+1-123-555-6789&quot;);
062     * vcard.addMember(member);
063     * member = new Member();
064     * member.setUri(&quot;urn:uuid:03a0e51f-d1aa-4385-8a53-e29025acd8af&quot;); //references the UID from another vCard
065     * vcard.addMember(member);
066     * </pre>
067     * 
068     * <p>
069     * <b>Getting members</b>
070     * </p>
071     * 
072     * <pre class="brush:java">
073     * VCard vcard = ...
074     * Kind kind = vcard.getKind();
075     * if (kind != null){
076     *   if (kind.isGroup()){
077     *     System.out.println("The group's members are:");
078     *     for (Member member : vcard.getMembers()){
079     *       System.out.println(member.getUri());
080     *     }
081     *   }
082     * }
083     * </pre>
084     * 
085     * <p>
086     * <b>Property name:</b> {@code MEMBER}
087     * </p>
088     * <p>
089     * <b>Supported versions:</b> {@code 4.0}
090     * </p>
091     * @author Michael Angstadt
092     */
093    public class Member extends UriProperty implements HasAltId {
094            /**
095             * Creates a member property.
096             * @param uri the URI representing the member
097             */
098            public Member(String uri) {
099                    super(uri);
100            }
101    
102            @Override
103            public Set<VCardVersion> _supportedVersions() {
104                    return EnumSet.of(VCardVersion.V4_0);
105            }
106    
107            /**
108             * Gets the URI value.
109             * @return the URI value or null if no URI value is set
110             */
111            public String getUri() {
112                    return getValue();
113            }
114    
115            /**
116             * Sets the URI to an email address.
117             * @param email the email address
118             */
119            public void setUriEmail(String email) {
120                    setUri("mailto:" + email);
121            }
122    
123            /**
124             * Sets the URI to an instant messaging handle.
125             * @param protocol the IM protocol (e.g. "aim")
126             * @param handle the handle
127             */
128            public void setUriIM(String protocol, String handle) {
129                    setUri(protocol + ":" + handle);
130            }
131    
132            /**
133             * Sets the URI to a telephone number.
134             * @param telephone the telephone number
135             */
136            public void setUriTelephone(String telephone) {
137                    setUri("tel:" + telephone);
138            }
139    
140            /**
141             * Sets the URI.
142             * @param uri the URI
143             */
144            public void setUri(String uri) {
145                    setValue(uri);
146            }
147    
148            @Override
149            public List<Integer[]> getPids() {
150                    return super.getPids();
151            }
152    
153            @Override
154            public void addPid(int localId, int clientPidMapRef) {
155                    super.addPid(localId, clientPidMapRef);
156            }
157    
158            @Override
159            public void removePids() {
160                    super.removePids();
161            }
162    
163            @Override
164            public Integer getPref() {
165                    return super.getPref();
166            }
167    
168            @Override
169            public void setPref(Integer pref) {
170                    super.setPref(pref);
171            }
172    
173            //@Override
174            public String getAltId() {
175                    return parameters.getAltId();
176            }
177    
178            //@Override
179            public void setAltId(String altId) {
180                    parameters.setAltId(altId);
181            }
182    
183            /**
184             * Gets the MEDIATYPE parameter.
185             * <p>
186             * <b>Supported versions:</b> {@code 4.0}
187             * </p>
188             * @return the media type or null if not set
189             */
190            public String getMediaType() {
191                    return parameters.getMediaType();
192            }
193    
194            /**
195             * Sets the MEDIATYPE parameter.
196             * <p>
197             * <b>Supported versions:</b> {@code 4.0}
198             * </p>
199             * @param mediaType the media type or null to remove
200             */
201            public void setMediaType(String mediaType) {
202                    parameters.setMediaType(mediaType);
203            }
204    
205            @Override
206            protected void _validate(List<Warning> warnings, VCardVersion version, VCard vcard) {
207                    super._validate(warnings, version, vcard);
208    
209                    if (vcard.getKind() == null || !vcard.getKind().isGroup()) {
210                            warnings.add(new Warning(17));
211                    }
212            }
213    }