]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/utils/ContactUtils.java
Added method updateFaxNumber() to add/update/delete fax number
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / utils / ContactUtils.java
1 /*\r
2  * Copyright (C) 2016 Roland Haeder\r
3  *\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
8  *\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
13  *\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
16  */\r
17 package org.mxchange.jcontacts.contact.utils;\r
18 \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
29 \r
30 /**\r
31  * Utilities for contacts\r
32  * <p>\r
33  * @author Roland Haeder<roland@mxchange.org>\r
34  */\r
35 public class ContactUtils extends BaseFrameworkSystem {\r
36 \r
37         /**\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
42          * <p>\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
46          * <p>\r
47          * @return Whether the cellphone has been unlinked in contact object\r
48          */\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
52                         // Throw NPE\r
53                         throw new NullPointerException("contact is null"); //NOI18N\r
54                 }\r
55 \r
56                 // Default is not unlinked\r
57                 boolean isUnlinked = false;\r
58 \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
63                                 // Remove instance\r
64                                 contact.setContactCellphoneNumber(null);\r
65 \r
66                                 // Mark as unlinked\r
67                                 isUnlinked = true;\r
68                         } else {\r
69                                 // Yes, then update as well\r
70                                 contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);\r
71                                 contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber);\r
72                         }\r
73                 } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) {\r
74                         // Create new instance\r
75                         DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);\r
76 \r
77                         // Set it in contact\r
78                         contact.setContactCellphoneNumber(cellphone);\r
79                 }\r
80 \r
81                 // Return status\r
82                 return isUnlinked;\r
83         }\r
84 \r
85         /**\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
90          * <p>\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
95          * <p>\r
96          * @return Whether the fax number has been unlinked in contact object\r
97          */\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
101                         // Throw NPE\r
102                         throw new NullPointerException("contact is null"); //NOI18N\r
103                 }\r
104 \r
105                 // Default is not unlinked\r
106                 boolean isUnlinked = false;\r
107 \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
114 \r
115                                 // Mark it as being removed\r
116                                 isUnlinked = true;\r
117                         } else {\r
118                                 // Set all data\r
119                                 contact.getContactFaxNumber().setPhoneCountry(faxCountry);\r
120                                 contact.getContactFaxNumber().setPhoneAreaCode(faxAreaCode);\r
121                                 contact.getContactFaxNumber().setPhoneNumber(faxNumber);\r
122                         }\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
126 \r
127                         // Set it in contact\r
128                         contact.setContactFaxNumber(fax);\r
129                 }\r
130 \r
131                 // Return status\r
132                 return isUnlinked;\r
133         }\r
134 \r
135         /**\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
140          * <p>\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
145          * <p>\r
146          * @return Whether the land-line number has been unlinked in contact object\r
147          */\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
151                         // Throw NPE\r
152                         throw new NullPointerException("contact is null"); //NOI18N\r
153                 }\r
154 \r
155                 // Default is not unlinked\r
156                 boolean isUnlinked = false;\r
157 \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
164 \r
165                                 // Mark it as being removed\r
166                                 isUnlinked = true;\r
167                         } else {\r
168                                 // Set all data\r
169                                 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);\r
170                                 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);\r
171                                 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);\r
172                         }\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
176 \r
177                         // Set it in contact\r
178                         contact.setContactLandLineNumber(landLine);\r
179                 }\r
180 \r
181                 // Return status\r
182                 return isUnlinked;\r
183         }\r
184 \r
185         /**\r
186          * Private constructor for utilities\r
187          */\r
188         private ContactUtils () {\r
189         }\r
190 \r
191 }\r