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