001    package ezvcard.property;
002    
003    import java.util.List;
004    
005    import ezvcard.VCard;
006    import ezvcard.VCardVersion;
007    import ezvcard.Warning;
008    import ezvcard.util.GeoUri;
009    
010    /*
011     Copyright (c) 2013, Michael Angstadt
012     All rights reserved.
013    
014     Redistribution and use in source and binary forms, with or without
015     modification, are permitted provided that the following conditions are met: 
016    
017     1. Redistributions of source code must retain the above copyright notice, this
018     list of conditions and the following disclaimer. 
019     2. Redistributions in binary form must reproduce the above copyright notice,
020     this list of conditions and the following disclaimer in the documentation
021     and/or other materials provided with the distribution. 
022    
023     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
030     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033    
034     The views and conclusions contained in the software and documentation are those
035     of the authors and should not be interpreted as representing official policies, 
036     either expressed or implied, of the FreeBSD Project.
037     */
038    
039    /**
040     * <p>
041     * A set of latitude/longitude coordinates. There is no rule for what these
042     * coordinates must represent, but the meaning could vary depending on the value
043     * of {@link Kind}:
044     * </p>
045     * 
046     * <table border="1">
047     * <tr>
048     * <th>KIND value</th>
049     * <th>GEO meaning</th>
050     * </tr>
051     * <tr>
052     * <td>"individual"</td>
053     * <td>the location of the person's home or workplace.</td>
054     * </tr>
055     * <tr>
056     * <td>"group"</td>
057     * <td>the location of the group's meeting place.</td>
058     * </tr>
059     * <tr>
060     * <td>"org"</td>
061     * <td>the coordinates of the organization's headquarters.</td>
062     * </tr>
063     * <tr>
064     * <td>"location"</td>
065     * <td>the coordinates of the location itself.</td>
066     * </tr>
067     * </table>
068     * 
069     * <p>
070     * <b>Code sample</b>
071     * </p>
072     * 
073     * <pre class="brush:java">
074     * VCard vcard = new VCard();
075     * Geo geo = new Geo(-123.456, 12.54);
076     * vcard.setGeo(geo);
077     * </pre>
078     * 
079     * <p>
080     * <b>Property name:</b> {@code GEO}
081     * </p>
082     * <p>
083     * <b>Supported versions:</b> {@code 2.1, 3.0, 4.0}
084     * </p>
085     * @author Michael Angstadt
086     */
087    public class Geo extends VCardProperty implements HasAltId {
088            private GeoUri uri;
089    
090            /**
091             * Creates a geo property.
092             * @param latitude the latitude
093             * @param longitude the longitude
094             */
095            public Geo(Double latitude, Double longitude) {
096                    this(new GeoUri.Builder(latitude, longitude).build());
097            }
098    
099            /**
100             * Creates a geo property.
101             * @param uri the geo URI
102             */
103            public Geo(GeoUri uri) {
104                    this.uri = uri;
105            }
106    
107            /**
108             * Gets the latitude.
109             * @return the latitude
110             */
111            public Double getLatitude() {
112                    return (uri == null) ? null : uri.getCoordA();
113            }
114    
115            /**
116             * Sets the latitude.
117             * @param latitude the latitude
118             */
119            public void setLatitude(Double latitude) {
120                    if (uri == null) {
121                            uri = new GeoUri.Builder(latitude, null).build();
122                    } else {
123                            uri = new GeoUri.Builder(uri).coordA(latitude).build();
124                    }
125            }
126    
127            /**
128             * Gets the longitude.
129             * @return the longitude
130             */
131            public Double getLongitude() {
132                    return (uri == null) ? null : uri.getCoordB();
133            }
134    
135            /**
136             * Sets the longitude.
137             * @param longitude the longitude
138             */
139            public void setLongitude(Double longitude) {
140                    if (uri == null) {
141                            uri = new GeoUri.Builder(null, longitude).build();
142                    } else {
143                            uri = new GeoUri.Builder(uri).coordB(longitude).build();
144                    }
145            }
146    
147            /**
148             * Gets the raw object used for storing the GEO information. This can be
149             * used to supplement the GEO value with additional information (such as
150             * altitude or level of accuracy). Geo URIs are only supported by vCard
151             * version 4.0. Only latitude and longitude values are used when marshalling
152             * to earlier vCard versions.
153             * @return the geo URI object or null if not set
154             * @see <a href="http://tools.ietf.org/html/rfc5870">RFC 5870</a>
155             */
156            public GeoUri getGeoUri() {
157                    return uri;
158            }
159    
160            /**
161             * Sets the raw object used for storing the GEO information. This can be
162             * used to supplement the GEO value with additional information (such as
163             * altitude or level of accuracy). Geo URIs are only supported by vCard
164             * version 4.0. Only latitude and longitude values are used when marshalling
165             * to earlier vCard versions.
166             * @param uri the geo URI object
167             * @see <a href="http://tools.ietf.org/html/rfc5870">RFC 5870</a>
168             */
169            public void setGeoUri(GeoUri uri) {
170                    this.uri = uri;
171            }
172    
173            /**
174             * Gets the TYPE parameter.
175             * <p>
176             * <b>Supported versions:</b> {@code 4.0}
177             * </p>
178             * @return the TYPE value (typically, this will be either "work" or "home")
179             * or null if it doesn't exist
180             */
181            public String getType() {
182                    return parameters.getType();
183            }
184    
185            /**
186             * Sets the TYPE parameter.
187             * <p>
188             * <b>Supported versions:</b> {@code 4.0}
189             * </p>
190             * @param type the TYPE value (this should be either "work" or "home") or
191             * null to remove
192             */
193            public void setType(String type) {
194                    parameters.setType(type);
195            }
196    
197            /**
198             * Gets the MEDIATYPE parameter.
199             * <p>
200             * <b>Supported versions:</b> {@code 4.0}
201             * </p>
202             * @return the media type or null if not set
203             */
204            public String getMediaType() {
205                    return parameters.getMediaType();
206            }
207    
208            /**
209             * Sets the MEDIATYPE parameter.
210             * <p>
211             * <b>Supported versions:</b> {@code 4.0}
212             * </p>
213             * @param mediaType the media type or null to remove
214             */
215            public void setMediaType(String mediaType) {
216                    parameters.setMediaType(mediaType);
217            }
218    
219            @Override
220            public List<Integer[]> getPids() {
221                    return super.getPids();
222            }
223    
224            @Override
225            public void addPid(int localId, int clientPidMapRef) {
226                    super.addPid(localId, clientPidMapRef);
227            }
228    
229            @Override
230            public void removePids() {
231                    super.removePids();
232            }
233    
234            @Override
235            public Integer getPref() {
236                    return super.getPref();
237            }
238    
239            @Override
240            public void setPref(Integer pref) {
241                    super.setPref(pref);
242            }
243    
244            //@Override
245            public String getAltId() {
246                    return parameters.getAltId();
247            }
248    
249            //@Override
250            public void setAltId(String altId) {
251                    parameters.setAltId(altId);
252            }
253    
254            @Override
255            protected void _validate(List<Warning> warnings, VCardVersion version, VCard vcard) {
256                    if (getLatitude() == null) {
257                            warnings.add(new Warning(13));
258                    }
259                    if (getLongitude() == null) {
260                            warnings.add(new Warning(14));
261                    }
262            }
263    }