1    /*
2     * Copyright 2009 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.component.util.model;
19   
20   import javax.persistence.Basic;
21   import javax.persistence.Column;
22   import javax.persistence.MappedSuperclass;
23   import javax.xml.bind.annotation.XmlAccessOrder;
24   import javax.xml.bind.annotation.XmlAccessType;
25   import javax.xml.bind.annotation.XmlAccessorOrder;
26   import javax.xml.bind.annotation.XmlAccessorType;
27   import javax.xml.bind.annotation.XmlElement;
28   import javax.xml.bind.annotation.XmlRootElement;
29   
30   import net.sf.json.JSONObject;
31   
32   import org.jdom.Element;
33   import org.torweg.pulse.bundle.JDOMable;
34   import org.torweg.pulse.util.INameable;
35   import org.torweg.pulse.util.entity.AbstractNamableEntity;
36   
37   /**
38    * A utility-class to derive from.
39    * 
40    * @author Daniel Dietz
41    * @version $Revision: 1577 $
42    */
43   @MappedSuperclass
44   @XmlRootElement(name = "abstract-extended-address")
45   @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
46   @XmlAccessorType(XmlAccessType.FIELD)
47   public abstract class AbstractExtendedAddress extends AbstractNamableEntity
48           implements INameable, JDOMable {
49   
50       /**
51        * The serialVersionUID.
52        */
53       private static final long serialVersionUID = -3347213603937082774L;
54   
55       /**
56        * The country of the {@code AbstractExtendedAddress}.
57        */
58       @Basic
59       @Column(nullable = false)
60       @XmlElement(name = "country")
61       private String country = null;
62   
63       /**
64        * The contact person of the {@code AbstractExtendedAddress}.
65        */
66       @Basic
67       @XmlElement(name = "contact-person")
68       private String contactPerson = null;
69   
70       /**
71        * The street of the {@code AbstractExtendedAddress}.
72        */
73       @Basic
74       @XmlElement(name = "street")
75       private String street = null;
76   
77       /**
78        * The postal code of the {@code AbstractExtendedAddress}.
79        */
80       @Basic
81       @XmlElement(name = "postal-code")
82       private String postalCode = null;
83   
84       /**
85        * The city of the {@code AbstractExtendedAddress}.
86        */
87       @Basic
88       @XmlElement(name = "city")
89       private String city = null;
90   
91       /**
92        * The phone number of the {@code AbstractExtendedAddress}.
93        */
94       @Basic
95       @XmlElement(name = "phone-number")
96       private String phoneNumber = null;
97   
98       /**
99        * The phone number of the {@code AbstractExtendedAddress}.
100       */
101      @Basic
102      @XmlElement(name = "mobile-phone-number")
103      private String mobilePhoneNumber = null;
104  
105      /**
106       * The fax number of the {@code AbstractExtendedAddress}.
107       */
108      @Basic
109      @XmlElement(name = "fax-number")
110      private String faxNumber = null;
111  
112      /**
113       * The email address of the {@code AbstractExtendedAddress}.
114       */
115      @Basic
116      @XmlElement(name = "email")
117      private String email = null;
118  
119      /**
120       * The URL of the {@code AbstractExtendedAddress}.
121       */
122      @Basic
123      @XmlElement(name = "url")
124      private String url = null;
125  
126      /**
127       * The state of the {@code Retailer}.
128       */
129      @Basic
130      @XmlElement(name = "state")
131      private String state = null;
132  
133      /**
134       * Returns the country of the {@code AbstractExtendedAddress}.
135       * 
136       * @return the country of the {@code AbstractExtendedAddress}
137       */
138      public final String getCountry() {
139          return this.country;
140      }
141  
142      /**
143       * Sets the country of the {@code AbstractExtendedAddress}.
144       * 
145       * @param c
146       *            the country to be set
147       */
148      public final void setCountry(final String c) {
149          if (c == null) {
150              throw new NullPointerException(
151                      "AbstractExtendedAddress.country must not be set to null.");
152          }
153          this.country = c;
154      }
155  
156      /**
157       * Returns the contact person of the {@code AbstractExtendedAddress}.
158       * 
159       * @return the contact person of the {@code AbstractExtendedAddress}
160       */
161      public final String getContactPerson() {
162          return this.contactPerson;
163      }
164  
165      /**
166       * Sets the contactPerson of the {@code AbstractExtendedAddress}.
167       * 
168       * @param cp
169       *            the contact person to be set
170       */
171      public final void setContactPerson(final String cp) {
172          this.contactPerson = cp;
173      }
174  
175      /**
176       * Returns the street of the {@code AbstractExtendedAddress}.
177       * 
178       * @return the street of the {@code AbstractExtendedAddress}
179       */
180      public final String getStreet() {
181          return this.street;
182      }
183  
184      /**
185       * Sets the street of the {@code AbstractExtendedAddress}.
186       * 
187       * @param s
188       *            the street to be set
189       */
190      public final void setStreet(final String s) {
191          this.street = s;
192      }
193  
194      /**
195       * Returns the postal code of the {@code AbstractExtendedAddress}.
196       * 
197       * @return the postal code of the {@code AbstractExtendedAddress}
198       */
199      public final String getPostalCode() {
200          return this.postalCode;
201      }
202  
203      /**
204       * Sets the postal code of the {@code AbstractExtendedAddress}.
205       * 
206       * @param pc
207       *            the postal code to be set
208       */
209      public final void setPostalCode(final String pc) {
210          this.postalCode = pc;
211      }
212  
213      /**
214       * Returns the city of the {@code AbstractExtendedAddress}.
215       * 
216       * @return the city of the {@code AbstractExtendedAddress}
217       */
218      public final String getCity() {
219          return this.city;
220      }
221  
222      /**
223       * Sets the city of the {@code AbstractExtendedAddress}.
224       * 
225       * @param c
226       *            the city to be set
227       */
228      public final void setCity(final String c) {
229          this.city = c;
230      }
231  
232      /**
233       * Returns the phone number of the {@code AbstractExtendedAddress}.
234       * 
235       * @return the phone number of the {@code AbstractExtendedAddress}
236       */
237      public final String getPhoneNumber() {
238          return this.phoneNumber;
239      }
240  
241      /**
242       * Sets the phoneNumber of the {@code AbstractExtendedAddress}.
243       * 
244       * @param pn
245       *            the phone number to be set
246       */
247      public final void setPhoneNumber(final String pn) {
248          this.phoneNumber = pn;
249      }
250  
251      /**
252       * Returns the mobile phone number of the
253       * {@code AbstractExtendedAddress}.
254       * 
255       * @return the mobile phone number of the
256       *         {@code AbstractExtendedAddress}
257       */
258      public final String getMobilePhoneNumber() {
259          return this.mobilePhoneNumber;
260      }
261  
262      /**
263       * Sets the mobile phone number of the {@code AbstractExtendedAddress}.
264       * 
265       * @param mpn
266       *            the mobile phone number to be set
267       */
268      public final void setMobilePhoneNumber(final String mpn) {
269          this.mobilePhoneNumber = mpn;
270      }
271  
272      /**
273       * Returns the fax number of the {@code AbstractExtendedAddress}.
274       * 
275       * @return the fax number of the {@code AbstractExtendedAddress}
276       */
277      public final String getFaxNumber() {
278          return this.faxNumber;
279      }
280  
281      /**
282       * Sets the faxNumber of the {@code AbstractExtendedAddress}.
283       * 
284       * @param fn
285       *            the fax number to be set
286       */
287      public final void setFaxNumber(final String fn) {
288          this.faxNumber = fn;
289      }
290  
291      /**
292       * Returns the email address of the {@code AbstractExtendedAddress}.
293       * 
294       * @return the email address of the {@code AbstractExtendedAddress}
295       */
296      public final String getEmail() {
297          return this.email;
298      }
299  
300      /**
301       * Sets the email address of the {@code AbstractExtendedAddress}.
302       * 
303       * @param e
304       *            the email address to be set
305       */
306      public final void setEmail(final String e) {
307          this.email = e;
308      }
309  
310      /**
311       * Returns the URL of the {@code AbstractExtendedAddress}.
312       * 
313       * @return the URL of the {@code AbstractExtendedAddress}
314       */
315      public final String getUrl() {
316          return this.url;
317      }
318  
319      /**
320       * Sets the URL of the {@code AbstractExtendedAddress}.
321       * 
322       * @param u
323       *            the URL to be set
324       */
325      public final void setUrl(final String u) {
326          this.url = u;
327      }
328  
329      /**
330       * De-serialises the state of the {@code AbstractExtendedAddress} as a
331       * JDOM {@code Element}.
332       * 
333       * @return the state of the {@code AbstractExtendedAddress} as a JDOM
334       *         {@code Element}.
335       */
336      public Element deserializeToJDOM() {
337  
338          Element retailerEl = new Element(this.getClass().getSimpleName());
339  
340          // id
341          if (getId() != null) {
342              retailerEl.setAttribute("id", getId().toString());
343          }
344  
345          // url
346          if (getUrl() != null) {
347              retailerEl.addContent(new Element("url").setText(getUrl()));
348          }
349  
350          // name
351          if (getName() != null) {
352              retailerEl.setAttribute("name", getName());
353          }
354  
355          // contact-person
356          if (getContactPerson() != null) {
357              retailerEl.addContent(new Element("contact-person")
358                      .setText(getContactPerson()));
359          }
360  
361          addAddressInfo(retailerEl);
362  
363          addContactData(retailerEl);
364  
365          return retailerEl;
366      }
367  
368      /**
369       * Adds the address-information of the {@code AbstractExtendedAddress}
370       * to the given Element.
371       * 
372       * @param retailerEl
373       *            the current retailer-{@code Element}
374       */
375      private void addAddressInfo(final Element retailerEl) {
376  
377          // street
378          if (getStreet() != null) {
379              retailerEl.addContent(new Element("street").setText(getStreet()));
380          }
381  
382          // postal-code
383          if (getPostalCode() != null) {
384              retailerEl.addContent(new Element("postal-code")
385                      .setText(getPostalCode()));
386          }
387  
388          // city
389          if (getCity() != null) {
390              retailerEl.addContent(new Element("city").setText(getCity()));
391          }
392  
393          // state
394          if (getState() != null) {
395              retailerEl.addContent(new Element("state").setText(getState()));
396          }
397  
398          // country
399          if (getCountry() != null) {
400              retailerEl.addContent(new Element("country").setText(getCountry()));
401          }
402  
403      }
404  
405      /**
406       * Adds the contact-information of the {@code AbstractExtendedAddress}
407       * to the given Element.
408       * 
409       * @param retailerEl
410       *            the current retailer-{@code Element}
411       */
412      private void addContactData(final Element retailerEl) {
413  
414          // phone-number
415          if (getPhoneNumber() != null) {
416              retailerEl.addContent(new Element("phone-number")
417                      .setText(getPhoneNumber()));
418          }
419  
420          // phone-number
421          if (getMobilePhoneNumber() != null) {
422              retailerEl.addContent(new Element("mobile-phone-number")
423                      .setText(getMobilePhoneNumber()));
424          }
425  
426          // fax-number
427          if (getFaxNumber() != null) {
428              retailerEl.addContent(new Element("fax-number")
429                      .setText(getFaxNumber()));
430          }
431  
432          // email-address
433          if (getEmail() != null) {
434              retailerEl.addContent(new Element("email").setText(getEmail()));
435          }
436  
437      }
438  
439      /**
440       * Returns a JSON-representation of the {@code AbstractExtendedAddress}
441       * .
442       * 
443       * @return a {@code JSONObject}
444       */
445      public JSONObject toJSON() {
446          JSONObject jsonObj = new JSONObject();
447          jsonObj.put("id", getId());
448          jsonObj.put("country", getCountry());
449          jsonObj.put("name", getName());
450          jsonObj.put("contactPerson", getContactPerson());
451          jsonObj.put("street", getStreet());
452          jsonObj.put("postalCode", getPostalCode());
453          jsonObj.put("city", getCity());
454          jsonObj.put("state", getState());
455          jsonObj.put("phoneNumber", getPhoneNumber());
456          jsonObj.put("mobilePhoneNumber", getPhoneNumber());
457          jsonObj.put("faxNumber", getFaxNumber());
458          jsonObj.put("email", getEmail());
459          jsonObj.put("url", getUrl());
460          return jsonObj;
461      }
462  
463      /**
464       * Returns the state of the {@code Retailer}.
465       * 
466       * @return the state of the {@code Retailer}
467       */
468      public final String getState() {
469          return this.state;
470      }
471  
472      /**
473       * Sets the state of the {@code Retailer}.
474       * 
475       * @param s
476       *            the state to be set
477       */
478      public final void setState(final String s) {
479          this.state = s;
480      }
481  
482  }
483