]> git.mxchange.org Git - jjobs-war.git/blobdiff - src/java/org/mxchange/jjobs/converter/business/basicdata/JobsBasicCompanyDataConverter.java
Don't cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / converter / business / basicdata / JobsBasicCompanyDataConverter.java
diff --git a/src/java/org/mxchange/jjobs/converter/business/basicdata/JobsBasicCompanyDataConverter.java b/src/java/org/mxchange/jjobs/converter/business/basicdata/JobsBasicCompanyDataConverter.java
new file mode 100644 (file)
index 0000000..3dc4c72
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2016 - 2018 Free Software Foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jjobs.converter.business.basicdata;
+
+import javax.enterprise.inject.spi.CDI;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+import javax.faces.convert.FacesConverter;
+import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicDataNotFoundException;
+import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
+import org.mxchange.jjobs.beans.business.basicdata.list.JobsBasicDataListWebViewBean;
+import org.mxchange.jjobs.beans.business.basicdata.list.JobsBasicDataListWebViewController;
+
+/**
+ * Converter for basic company data id <-> valid basic company data instance
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@FacesConverter ("BasicCompanyDataConverter")
+public class JobsBasicCompanyDataConverter implements Converter<BasicData> {
+
+       /**
+        * Basic company data backing bean
+        */
+       private static JobsBasicDataListWebViewController BASIC_DATA_LIST_CONTROLLER;
+
+       @Override
+       public BasicData getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
+               // Is the instance there?
+               if (null == BASIC_DATA_LIST_CONTROLLER) {
+                       // Get bean from CDI directly
+                       BASIC_DATA_LIST_CONTROLLER = CDI.current().select(JobsBasicDataListWebViewBean.class).get();
+               }
+
+               // Is the value null or empty?
+               if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
+                       // Warning message
+                       // @TODO Not working with JNDI (no remote interface) this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
+
+                       // Return null
+                       return null;
+               }
+
+               // Init instance
+               BasicData basicData = null;
+
+               try {
+                       // Try to parse the value as long
+                       final Long basicDataId = Long.valueOf(submittedValue);
+
+                       // Try to get user instance from it
+                       basicData = BASIC_DATA_LIST_CONTROLLER.findBasicDataById(basicDataId);
+               } catch (final NumberFormatException ex) {
+                       // Throw again
+                       throw new ConverterException(ex);
+               } catch (final BasicDataNotFoundException ex) {
+                       // Debug message
+                       // @TODO Not working with JNDI (no remote interface) this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
+               }
+
+               // Return it
+               return basicData;
+       }
+
+       @Override
+       public String getAsString (final FacesContext context, final UIComponent component, final BasicData value) {
+               // Is the object null?
+               if ((null == value) || (String.valueOf(value).isEmpty())) {
+                       // Is null
+                       return ""; //NOI18N
+               }
+
+               // Return id number
+               return String.valueOf(value.getBasicDataId());
+       }
+
+}