]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/utils/ContactUtils.java
Continued (similar to juser-core):
[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.landline.DialableLandLineNumber;\r
25 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;\r
26 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;\r
27 \r
28 /**\r
29  * Utilities for contacts\r
30  * <p>\r
31  * @author Roland Haeder<roland@mxchange.org>\r
32  */\r
33 public class ContactUtils extends BaseFrameworkSystem {\r
34 \r
35         /**\r
36          * Updates cellphone data in contact instance. This method also removes the\r
37          * cellphone instance if no provider is selected. A bean (mostly EJB) should\r
38          * then make sure that the cellphone entry is being unlinked from contact\r
39          * instance or being removed, if no longer used.\r
40          * <p>\r
41          * @param contact           Contact instance to update\r
42          * @param cellphoneProvider New cellphone provider (or old)\r
43          * @param cellphoneNumber   New cellphone number (or old)\r
44          * <p>\r
45          * @return Whether the cellphone has been unlinked in contact object\r
46          */\r
47         public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) {\r
48                 // At least contact must be valid\r
49                 if (null == contact) {\r
50                         // Throw NPE\r
51                         throw new NullPointerException("contact is null"); //NOI18N\r
52                 }\r
53 \r
54                 // Default is not unlinked\r
55                 boolean isUnlinked = false;\r
56 \r
57                 // Is there a cellphone number?\r
58                 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {\r
59                         // Is provider null?\r
60                         if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) {\r
61                                 // Remove instance\r
62                                 contact.setContactCellphoneNumber(null);\r
63 \r
64                                 // Mark as unlinked\r
65                                 isUnlinked = true;\r
66                         } else {\r
67                                 // Yes, then update as well\r
68                                 contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);\r
69                                 contact.getContactCellphoneNumber().setPhoneNumber(cellphoneNumber);\r
70                         }\r
71                 } else if ((cellphoneProvider instanceof MobileProvider) && (cellphoneNumber > 0)) {\r
72                         // Create new instance\r
73                         DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);\r
74                 }\r
75 \r
76                 // Return status\r
77                 return isUnlinked;\r
78         }\r
79 \r
80         /**\r
81          * Updates land-line data in contact instance. This method also removes the\r
82          * land-line instance if no country is selected. A bean (mostly EJB) should\r
83          * then make sure that the land-line entry is being unlinked from contact\r
84          * instance or being removed, if no longer used.\r
85          * <p>\r
86          * @param contact       Contact instance being updated\r
87          * @param phoneCountry  New phone country or old or null\r
88          * @param phoneAreaCode New phone's area code (or old)\r
89          * @param phoneNumber   New phone number (or old)\r
90          * <p>\r
91          * @return Whether the land-line has been unlinked in contact object\r
92          */\r
93         public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {\r
94                 // At least contact must be valid\r
95                 if (null == contact) {\r
96                         // Throw NPE\r
97                         throw new NullPointerException("contact is null"); //NOI18N\r
98                 }\r
99 \r
100                 // Default is not unlinked\r
101                 boolean isUnlinked = false;\r
102 \r
103                 // Is there a land-line instance?\r
104                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {\r
105                         // Found existing land-line number, remove it?\r
106                         if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {\r
107                                 // Remove existing instance\r
108                                 contact.setContactLandLineNumber(null);\r
109 \r
110                                 // Mark it as being removed\r
111                                 isUnlinked = true;\r
112                         } else {\r
113                                 // Set all data\r
114                                 contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);\r
115                                 contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);\r
116                                 contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);\r
117                         }\r
118                 } else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {\r
119                         // Set new land-line number\r
120                         DialableLandLineNumber landLineNumber = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);\r
121 \r
122                         // Set it in contact\r
123                         contact.setContactLandLineNumber(landLineNumber);\r
124                 }\r
125 \r
126                 // Return status\r
127                 return isUnlinked;\r
128         }\r
129 \r
130         /**\r
131          * Private constructor for utilities\r
132          */\r
133         private ContactUtils () {\r
134         }\r
135 \r
136 }\r