2 * Copyright (C) 2016 Roland Haeder
\r
4 * This program is free software: you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation, either version 3 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
17 package org.mxchange.jcontacts.contact.utils;
\r
19 import org.mxchange.jcontacts.contact.Contact;
\r
20 import org.mxchange.jcore.BaseFrameworkSystem;
\r
21 import org.mxchange.jcountry.data.Country;
\r
22 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
\r
23 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
\r
24 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
\r
25 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
\r
26 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
\r
27 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
\r
28 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
\r
31 * Utilities for contacts
\r
33 * @author Roland Haeder<roland@mxchange.org>
\r
35 public class ContactUtils extends BaseFrameworkSystem {
\r
38 * Updates cellphone data in contact instance. This method also removes the
\r
39 * cellphone instance if no provider is selected. A bean (mostly EJB) should
\r
40 * then make sure that the cellphone entry is being unlinked from contact
\r
41 * instance or being removed, if no longer used.
\r
43 * @param contact Contact instance to update
\r
44 * @param cellphoneProvider New cellphone provider (or old)
\r
45 * @param cellphoneNumber New cellphone number (or old)
\r
47 * @return Whether the cellphone has been unlinked in contact object
\r
49 public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) {
\r
50 // At least contact must be valid
\r
51 if (null == contact) {
\r
53 throw new NullPointerException("contact is null"); //NOI18N
\r
56 // Default is not unlinked
\r
57 boolean isUnlinked = false;
\r
59 // Is there a cellphone number?
\r
60 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
\r
61 // Is provider null?
\r
62 if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) {
\r
64 contact.setContactCellphoneNumber(null);
\r
69 // Yes, then update as well
\r
70 contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);
\r
71 contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber);
\r
73 } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) {
\r
74 // Create new instance
\r
75 DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);
\r
77 // Set it in contact
\r
78 contact.setContactCellphoneNumber(cellphone);
\r
86 * Updates land-line data in contact instance. This method also removes the
\r
87 * land-line instance if no country is selected. A bean (mostly EJB) should
\r
88 * then make sure that the land-line entry is being unlinked from contact
\r
89 * instance or being removed, if no longer used.
\r
91 * @param contact Contact instance being updated
\r
92 * @param faxCountry Updated fax number or null
\r
93 * @param faxAreaCode Updated fax area code or null
\r
94 * @param faxNumber Updated fax number
\r
96 * @return Whether the fax number has been unlinked in contact object
\r
98 public static boolean updateFaxNumber (final Contact contact, final Country faxCountry, final Integer faxAreaCode, final Long faxNumber) {
\r
99 // At least contact must be valid
\r
100 if (null == contact) {
\r
102 throw new NullPointerException("contact is null"); //NOI18N
\r
105 // Default is not unlinked
\r
106 boolean isUnlinked = false;
\r
108 // Is there a fax instance?
\r
109 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
\r
110 // Found existing fax number, remove it?
\r
111 if ((null == faxCountry) || (null == faxAreaCode) || (null == faxNumber)) {
\r
112 // Remove existing instance
\r
113 contact.setContactFaxNumber(null);
\r
115 // Mark it as being removed
\r
119 contact.getContactFaxNumber().setPhoneCountry(faxCountry);
\r
120 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);
\r
121 contact.getContactFaxNumber().setPhoneNumber(faxNumber);
\r
123 } else if ((faxCountry instanceof Country) && (faxAreaCode > 0) && (faxNumber > 0)) {
\r
124 // Set new land-line number
\r
125 DialableFaxNumber fax = new FaxNumber(faxCountry, faxAreaCode, faxNumber);
\r
127 // Set it in contact
\r
128 contact.setContactFaxNumber(fax);
\r
136 * Updates land-line data in contact instance. This method also removes the
\r
137 * land-line instance if no country is selected. A bean (mostly EJB) should
\r
138 * then make sure that the land-line entry is being unlinked from contact
\r
139 * instance or being removed, if no longer used.
\r
141 * @param contact Contact instance being updated
\r
142 * @param phoneCountry New phone country or old or null
\r
143 * @param phoneAreaCode New phone's area code (or old)
\r
144 * @param phoneNumber New phone number (or old)
\r
146 * @return Whether the land-line number has been unlinked in contact object
\r
148 public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
\r
149 // At least contact must be valid
\r
150 if (null == contact) {
\r
152 throw new NullPointerException("contact is null"); //NOI18N
\r
155 // Default is not unlinked
\r
156 boolean isUnlinked = false;
\r
158 // Is there a land-line instance?
\r
159 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
\r
160 // Found existing land-line number, remove it?
\r
161 if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
\r
162 // Remove existing instance
\r
163 contact.setContactLandLineNumber(null);
\r
165 // Mark it as being removed
\r
169 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
\r
170 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
\r
171 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
\r
173 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
\r
174 // Set new land-line number
\r
175 DialableLandLineNumber landLine = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
\r
177 // Set it in contact
\r
178 contact.setContactLandLineNumber(landLine);
\r
186 * Private constructor for utilities
\r
188 private ContactUtils () {
\r