]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/phone/JobsAdminContactPhoneWebSessionBean.java
Refactured a lot:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / phone / JobsAdminContactPhoneWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.beans.phone;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import javax.enterprise.context.RequestScoped;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Named;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jcontacts.contact.Contact;
29 import org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote;
30 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
31 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
32 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
33
34 /**
35  * Administrative bean (controller) for contact's phone numbers
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @Named ("adminContactPhoneController")
40 @RequestScoped
41 public class JobsAdminContactPhoneWebSessionBean implements JobsAdminContactPhoneWebSessionController {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 184_598_175_371_269_016L;
47
48         /**
49          * Remote EJB for phone number (administrative)
50          */
51         private AdminContactsPhoneSessionBeanRemote adminRemoteBean;
52
53         /**
54          * Cell phone number
55          */
56         private DialableCellphoneNumber cellPhone;
57
58         /**
59          * Instance of linked contact account
60          */
61         private Contact contact;
62
63         /**
64          * "Cache" for contact lists, mostly only one is assigned. So this cache
65          * shouldn't grow beyond control.
66          */
67         private final Map<Long, List<Contact>> contacts;
68
69         /**
70          * Fax number
71          */
72         private DialableFaxNumber fax;
73
74         /**
75          * Land-line number
76          */
77         private DialableLandLineNumber landLine;
78
79         /**
80          * Default constructor
81          */
82         public JobsAdminContactPhoneWebSessionBean () {
83                 // Try it
84                 try {
85                         // Get initial context
86                         Context context = new InitialContext();
87
88                         // Try to lookup the beans
89                         this.adminRemoteBean = (AdminContactsPhoneSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/admincontactphone!org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote"); //NOI18N
90                 } catch (final NamingException e) {
91                         // Throw it again
92                         throw new FaceletException(e);
93                 }
94
95                 // Init map
96                 this.contacts = new HashMap<>(10);
97         }
98
99         @Override
100         public List<Contact> allCellphoneContacts () {
101                 // Get id
102                 Long phoneId = this.getCellPhone().getPhoneId();
103
104                 // Is cache there?
105                 if (this.contacts.containsKey(phoneId)) {
106                         // Return cached version
107                         return this.contacts.get(phoneId);
108                 } else {
109                         // Ask bean
110                         List<Contact> list = this.adminRemoteBean.allContacts(this.getCellPhone());
111
112                         // Store result in cache
113                         this.contacts.put(phoneId, list);
114
115                         // Return now-cached list
116                         return list;
117                 }
118         }
119
120         @Override
121         public DialableCellphoneNumber getCellPhone () {
122                 return this.cellPhone;
123         }
124
125         @Override
126         public void setCellPhone (final DialableCellphoneNumber cellPhone) {
127                 this.cellPhone = cellPhone;
128         }
129
130         @Override
131         public Contact getContact () {
132                 return this.contact;
133         }
134
135         @Override
136         public void setContact (final Contact contact) {
137                 this.contact = contact;
138         }
139
140         @Override
141         public DialableFaxNumber getFax () {
142                 return this.fax;
143         }
144
145         @Override
146         public void setFax (final DialableFaxNumber fax) {
147                 this.fax = fax;
148         }
149
150         @Override
151         public DialableLandLineNumber getLandLine () {
152                 return this.landLine;
153         }
154
155         @Override
156         public void setLandLine (final DialableLandLineNumber landLine) {
157                 this.landLine = landLine;
158         }
159
160 }