]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/converter/landline/JobsLandLineNumberConverter.java
updated own name and resources
[jjobs-war.git] / src / java / org / mxchange / jjobs / converter / landline / JobsLandLineNumberConverter.java
1 /*
2  * Copyright (C) 2016 Roland Häder
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.converter.landline;
18
19 import java.text.MessageFormat;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.convert.Converter;
23 import javax.faces.convert.ConverterException;
24 import javax.faces.convert.FacesConverter;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
29 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
30 import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException;
31 import org.mxchange.jphone.phonenumbers.DialableNumber;
32 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
33 import org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote;
34
35 /**
36  * Converter for land-line id <-> valid land-line number instance
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @FacesConverter (value = "LandLineNumberConverter")
41 public class JobsLandLineNumberConverter implements Converter {
42
43         /**
44          * Logger instance
45          */
46         @Log
47         private LoggerBeanLocal loggerBeanLocal;
48
49         /**
50          * Phone EJB
51          */
52         private PhoneSessionBeanRemote phoneBean;
53
54         /**
55          * Initialization of this converter
56          */
57         public JobsLandLineNumberConverter () {
58                 // Try to get it
59                 try {
60                         // Get initial context
61                         Context context = new InitialContext();
62
63                         // Lookup logger
64                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
65
66                         // ... and user controller
67                         this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/jjobs-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N
68                 } catch (final NamingException ex) {
69                         // Continue to throw it
70                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
71                 }
72         }
73
74         @Override
75         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
76                 // Is the value null or empty?
77                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
78                         // Warning message
79                         this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
80
81                         // Return null
82                         return null;
83                 }
84
85                 // Init instance
86                 DialableLandLineNumber landLineNumber = null;
87
88                 try {
89                         // Try to parse the value as long
90                         Long landLineNumberId = Long.valueOf(submittedValue);
91
92                         // Try to get mobile instance from it
93                         landLineNumber = this.phoneBean.findLandLineNumberById(landLineNumberId);
94                 } catch (final NumberFormatException ex) {
95                         // Throw again
96                         throw new ConverterException(ex);
97                 } catch (final PhoneEntityNotFoundException ex) {
98                         // Debug message
99                         this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
100                 }
101
102                 // Return it
103                 return landLineNumber;
104         }
105
106         @Override
107         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
108                 // Is the object null?
109                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
110                         // Is null
111                         return ""; //NOI18N
112                 } else if (!(value instanceof DialableNumber)) {
113                         // Not same interface
114                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement DialableNumber.", value)); //NOI18N
115                 }
116
117                 // Return category id
118                 return String.valueOf(((DialableNumber) value).getPhoneId());
119         }
120
121 }