Commit 5146fc14f0703e8bd2d16fff0bfc085aa39a9dcb

Authored by Thiago Soares
1 parent 8f2f1f04
Exists in master

Retirado do framework demoiselle a dependencia com a classe

impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/template/AbstractEditPageBean.java
1   -/*
2   - * Demoiselle Framework
3   - * Copyright (C) 2010 SERPRO
4   - * ----------------------------------------------------------------------------
5   - * This file is part of Demoiselle Framework.
6   - *
7   - * Demoiselle Framework is free software; you can redistribute it and/or
8   - * modify it under the terms of the GNU Lesser General Public License version 3
9   - * as published by the Free Software Foundation.
10   - *
11   - * This program is distributed in the hope that it will be useful,
12   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   - * GNU General Public License for more details.
15   - *
16   - * You should have received a copy of the GNU Lesser General Public License version 3
17   - * along with this program; if not, see <http://www.gnu.org/licenses/>
18   - * or write to the Free Software Foundation, Inc., 51 Franklin Street,
19   - * Fifth Floor, Boston, MA 02110-1301, USA.
20   - * ----------------------------------------------------------------------------
21   - * Este arquivo é parte do Framework Demoiselle.
22   - *
23   - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
24   - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
25   - * do Software Livre (FSF).
26   - *
27   - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
28   - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
29   - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
30   - * para maiores detalhes.
31   - *
32   - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
33   - * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
34   - * ou escreva para a Fundação do Software Livre (FSF) Inc.,
35   - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
36   - */
37   -package br.gov.frameworkdemoiselle.template;
38   -
39   -import javax.faces.context.FacesContext;
40   -import javax.faces.convert.Converter;
41   -import javax.inject.Inject;
42   -
43   -import br.gov.frameworkdemoiselle.DemoiselleException;
44   -import br.gov.frameworkdemoiselle.annotation.Name;
45   -import br.gov.frameworkdemoiselle.util.Beans;
46   -import br.gov.frameworkdemoiselle.util.Parameter;
47   -import br.gov.frameworkdemoiselle.util.Reflections;
48   -import br.gov.frameworkdemoiselle.util.ResourceBundle;
49   -
50   -import com.sun.faces.util.Util;
51   -
52   -public abstract class AbstractEditPageBean<T, I> extends AbstractPageBean implements EditPageBean<T> {
53   -
54   - private static final long serialVersionUID = 1L;
55   -
56   - @Inject
57   - private Parameter<String> id;
58   -
59   - private T bean;
60   -
61   - private Class<T> beanClass;
62   -
63   - private Class<I> idClass;
64   -
65   - @Inject
66   - @Name("demoiselle-jsf-bundle")
67   - private ResourceBundle bundle;
68   -
69   - @Inject
70   - private FacesContext facesContext;
71   -
72   - protected void clear() {
73   - this.id = null;
74   - this.bean = null;
75   - }
76   -
77   - protected T createBean() {
78   - return Beans.getReference(getBeanClass());
79   - }
80   -
81   - @Override
82   - public T getBean() {
83   - if (this.bean == null) {
84   - initBean();
85   - }
86   -
87   - return this.bean;
88   - }
89   -
90   - protected Class<T> getBeanClass() {
91   - if (this.beanClass == null) {
92   - this.beanClass = Reflections.getGenericTypeArgument(this.getClass(), 0);
93   - }
94   -
95   - return this.beanClass;
96   - }
97   -
98   - protected Class<I> getIdClass() {
99   - if (this.idClass == null) {
100   - this.idClass = Reflections.getGenericTypeArgument(this.getClass(), 1);
101   - }
102   -
103   - return this.idClass;
104   - }
105   -
106   - @Override
107   - @SuppressWarnings("unchecked")
108   - public I getId() {
109   - Converter converter = getIdConverter();
110   -
111   - if(converter == null && String.class.equals(getIdClass())) {
112   - return (I) id.getValue();
113   -
114   - } else if (converter == null) {
115   - throw new DemoiselleException(bundle.getString("id-converter-not-found", getIdClass().getCanonicalName()));
116   -
117   - } else {
118   - return (I) converter.getAsObject(facesContext, facesContext.getViewRoot(), id.getValue());
119   - }
120   - }
121   -
122   - private Converter getIdConverter() {
123   - return Util.getConverterForClass(getIdClass(), facesContext);
124   - }
125   -
126   - protected abstract void handleLoad();
127   -
128   - private void initBean() {
129   - if (isUpdateMode()) {
130   - this.bean = this.loadBean();
131   - } else {
132   - setBean(createBean());
133   - }
134   - }
135   -
136   - @Override
137   - public boolean isUpdateMode() {
138   - return getId() != null;
139   - }
140   -
141   - private T loadBean() {
142   - this.handleLoad();
143   - return this.bean;
144   - }
145   -
146   - protected void setBean(final T bean) {
147   - this.bean = bean;
148   - }
149   -
150   - // protected void setId(final I id) {
151   - // clear();
152   - // String value = getIdConverter().getAsString(getFacesContext(), getFacesContext().getViewRoot(), id);
153   - // this.id.setValue(value);
154   - // }
155   -
156   -}
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.template;
  38 +
  39 +import javax.faces.context.FacesContext;
  40 +import javax.faces.convert.Converter;
  41 +import javax.inject.Inject;
  42 +
  43 +import br.gov.frameworkdemoiselle.DemoiselleException;
  44 +import br.gov.frameworkdemoiselle.annotation.Name;
  45 +import br.gov.frameworkdemoiselle.util.Beans;
  46 +import br.gov.frameworkdemoiselle.util.Faces;
  47 +import br.gov.frameworkdemoiselle.util.Parameter;
  48 +import br.gov.frameworkdemoiselle.util.Reflections;
  49 +import br.gov.frameworkdemoiselle.util.ResourceBundle;
  50 +
  51 +public abstract class AbstractEditPageBean<T, I> extends AbstractPageBean implements EditPageBean<T> {
  52 +
  53 + private static final long serialVersionUID = 1L;
  54 +
  55 + @Inject
  56 + private Parameter<String> id;
  57 +
  58 + private T bean;
  59 +
  60 + private Class<T> beanClass;
  61 +
  62 + private Class<I> idClass;
  63 +
  64 + @Inject
  65 + @Name("demoiselle-jsf-bundle")
  66 + private ResourceBundle bundle;
  67 +
  68 + @Inject
  69 + private FacesContext facesContext;
  70 +
  71 + protected void clear() {
  72 + this.id = null;
  73 + this.bean = null;
  74 + }
  75 +
  76 + protected T createBean() {
  77 + return Beans.getReference(getBeanClass());
  78 + }
  79 +
  80 + @Override
  81 + public T getBean() {
  82 + if (this.bean == null) {
  83 + initBean();
  84 + }
  85 +
  86 + return this.bean;
  87 + }
  88 +
  89 + protected Class<T> getBeanClass() {
  90 + if (this.beanClass == null) {
  91 + this.beanClass = Reflections.getGenericTypeArgument(this.getClass(), 0);
  92 + }
  93 +
  94 + return this.beanClass;
  95 + }
  96 +
  97 + protected Class<I> getIdClass() {
  98 + if (this.idClass == null) {
  99 + this.idClass = Reflections.getGenericTypeArgument(this.getClass(), 1);
  100 + }
  101 +
  102 + return this.idClass;
  103 + }
  104 +
  105 + @Override
  106 + @SuppressWarnings("unchecked")
  107 + public I getId() {
  108 + Converter converter = getIdConverter();
  109 +
  110 + if(converter == null && String.class.equals(getIdClass())) {
  111 + return (I) id.getValue();
  112 +
  113 + } else if (converter == null) {
  114 + throw new DemoiselleException(bundle.getString("id-converter-not-found", getIdClass().getCanonicalName()));
  115 +
  116 + } else {
  117 + return (I) converter.getAsObject(facesContext, facesContext.getViewRoot(), id.getValue());
  118 + }
  119 + }
  120 +
  121 + private Converter getIdConverter() {
  122 + return Faces.getConverter(getIdClass());
  123 + }
  124 +
  125 + protected abstract void handleLoad();
  126 +
  127 + private void initBean() {
  128 + if (isUpdateMode()) {
  129 + this.bean = this.loadBean();
  130 + } else {
  131 + setBean(createBean());
  132 + }
  133 + }
  134 +
  135 + @Override
  136 + public boolean isUpdateMode() {
  137 + return getId() != null;
  138 + }
  139 +
  140 + private T loadBean() {
  141 + this.handleLoad();
  142 + return this.bean;
  143 + }
  144 +
  145 + protected void setBean(final T bean) {
  146 + this.bean = bean;
  147 + }
  148 +
  149 + // protected void setId(final I id) {
  150 + // clear();
  151 + // String value = getIdConverter().getAsString(getFacesContext(), getFacesContext().getViewRoot(), id);
  152 + // this.id.setValue(value);
  153 + // }
  154 +
  155 +}
... ...
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/Faces.java
1   -/*
2   - * Demoiselle Framework
3   - * Copyright (C) 2010 SERPRO
4   - * ----------------------------------------------------------------------------
5   - * This file is part of Demoiselle Framework.
6   - *
7   - * Demoiselle Framework is free software; you can redistribute it and/or
8   - * modify it under the terms of the GNU Lesser General Public License version 3
9   - * as published by the Free Software Foundation.
10   - *
11   - * This program is distributed in the hope that it will be useful,
12   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   - * GNU General Public License for more details.
15   - *
16   - * You should have received a copy of the GNU Lesser General Public License version 3
17   - * along with this program; if not, see <http://www.gnu.org/licenses/>
18   - * or write to the Free Software Foundation, Inc., 51 Franklin Street,
19   - * Fifth Floor, Boston, MA 02110-1301, USA.
20   - * ----------------------------------------------------------------------------
21   - * Este arquivo é parte do Framework Demoiselle.
22   - *
23   - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
24   - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
25   - * do Software Livre (FSF).
26   - *
27   - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
28   - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
29   - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
30   - * para maiores detalhes.
31   - *
32   - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
33   - * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
34   - * ou escreva para a Fundação do Software Livre (FSF) Inc.,
35   - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
36   - */
37   -package br.gov.frameworkdemoiselle.util;
38   -
39   -import static javax.faces.application.FacesMessage.SEVERITY_ERROR;
40   -import static javax.faces.application.FacesMessage.SEVERITY_FATAL;
41   -import static javax.faces.application.FacesMessage.SEVERITY_INFO;
42   -import static javax.faces.application.FacesMessage.SEVERITY_WARN;
43   -
44   -import java.util.List;
45   -import java.util.Map;
46   -
47   -import javax.faces.application.FacesMessage;
48   -import javax.faces.application.FacesMessage.Severity;
49   -import javax.faces.component.UIViewRoot;
50   -import javax.faces.context.FacesContext;
51   -import javax.faces.convert.Converter;
52   -
53   -import br.gov.frameworkdemoiselle.exception.ApplicationException;
54   -import br.gov.frameworkdemoiselle.message.Message;
55   -import br.gov.frameworkdemoiselle.message.SeverityType;
56   -
57   -import com.sun.faces.util.Util;
58   -
59   -public class Faces {
60   -
61   - public static void addMessages(final List<Message> messages) {
62   - if (messages != null) {
63   - for (Message m : messages) {
64   - addMessage(m);
65   - }
66   - }
67   - }
68   -
69   - public static void addMessage(final Message message) {
70   - getFacesContext().addMessage(null, parse(message));
71   - }
72   -
73   - public static void addMessage(final String clientId, final Message message) {
74   - getFacesContext().addMessage(clientId, parse(message));
75   - }
76   -
77   - public static void addMessage(final String clientId, final Throwable throwable) {
78   - getFacesContext().addMessage(clientId, parse(throwable));
79   - }
80   -
81   - public static void addMessage(final Throwable throwable) {
82   - addMessage(null, throwable);
83   - }
84   -
85   - private static FacesContext getFacesContext() {
86   - return Beans.getReference(FacesContext.class);
87   - }
88   -
89   - public static Severity parse(final SeverityType severityType) {
90   - Severity result = null;
91   -
92   - switch (severityType) {
93   - case INFO:
94   - result = SEVERITY_INFO;
95   - break;
96   - case WARN:
97   - result = SEVERITY_WARN;
98   - break;
99   - case ERROR:
100   - result = SEVERITY_ERROR;
101   - break;
102   - case FATAL:
103   - result = SEVERITY_FATAL;
104   - }
105   -
106   - return result;
107   - }
108   -
109   - public static FacesMessage parse(final Throwable throwable) {
110   - FacesMessage facesMessage = new FacesMessage();
111   - ApplicationException annotation = throwable.getClass().getAnnotation(ApplicationException.class);
112   -
113   - if (annotation != null) {
114   - facesMessage.setSeverity(parse(annotation.severity()));
115   - } else {
116   - facesMessage.setSeverity(SEVERITY_ERROR);
117   - }
118   -
119   - if (throwable.getMessage() != null) {
120   - facesMessage.setSummary(throwable.getMessage());
121   - } else {
122   - facesMessage.setSummary(throwable.toString());
123   - }
124   -
125   - return facesMessage;
126   - }
127   -
128   - public static FacesMessage parse(final Message message) {
129   - FacesMessage facesMessage = new FacesMessage();
130   - facesMessage.setSeverity(parse(message.getSeverity()));
131   - facesMessage.setSummary(message.getText());
132   - return facesMessage;
133   - }
134   -
135   - public static Object convert(final String value, final Converter converter) {
136   - Object result = null;
137   -
138   - if (!Strings.isEmpty(value)) {
139   - if (converter != null) {
140   - result = converter.getAsObject(getFacesContext(), getFacesContext().getViewRoot(), value);
141   - } else {
142   - result = new String(value);
143   - }
144   - }
145   -
146   - return result;
147   - }
148   -
149   - public static Converter getConverter(Class<?> clazz) {
150   - return Util.getConverterForClass(clazz, getFacesContext());
151   - }
152   -
153   - public static Map<String, Object> getViewMap() {
154   - UIViewRoot viewRoot = getFacesContext().getViewRoot();
155   - return viewRoot.getViewMap(true);
156   - }
157   -
158   -}
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.util;
  38 +
  39 +import static javax.faces.application.FacesMessage.SEVERITY_ERROR;
  40 +import static javax.faces.application.FacesMessage.SEVERITY_FATAL;
  41 +import static javax.faces.application.FacesMessage.SEVERITY_INFO;
  42 +import static javax.faces.application.FacesMessage.SEVERITY_WARN;
  43 +
  44 +import java.util.List;
  45 +import java.util.Map;
  46 +
  47 +import javax.faces.application.Application;
  48 +import javax.faces.application.FacesMessage;
  49 +import javax.faces.application.FacesMessage.Severity;
  50 +import javax.faces.component.UIViewRoot;
  51 +import javax.faces.context.FacesContext;
  52 +import javax.faces.convert.Converter;
  53 +
  54 +import br.gov.frameworkdemoiselle.exception.ApplicationException;
  55 +import br.gov.frameworkdemoiselle.message.Message;
  56 +import br.gov.frameworkdemoiselle.message.SeverityType;
  57 +
  58 +public class Faces {
  59 +
  60 + public static void addMessages(final List<Message> messages) {
  61 + if (messages != null) {
  62 + for (Message m : messages) {
  63 + addMessage(m);
  64 + }
  65 + }
  66 + }
  67 +
  68 + public static void addMessage(final Message message) {
  69 + getFacesContext().addMessage(null, parse(message));
  70 + }
  71 +
  72 + public static void addMessage(final String clientId, final Message message) {
  73 + getFacesContext().addMessage(clientId, parse(message));
  74 + }
  75 +
  76 + public static void addMessage(final String clientId, final Throwable throwable) {
  77 + getFacesContext().addMessage(clientId, parse(throwable));
  78 + }
  79 +
  80 + public static void addMessage(final Throwable throwable) {
  81 + addMessage(null, throwable);
  82 + }
  83 +
  84 + private static FacesContext getFacesContext() {
  85 + return Beans.getReference(FacesContext.class);
  86 + }
  87 +
  88 + public static Severity parse(final SeverityType severityType) {
  89 + Severity result = null;
  90 +
  91 + switch (severityType) {
  92 + case INFO:
  93 + result = SEVERITY_INFO;
  94 + break;
  95 + case WARN:
  96 + result = SEVERITY_WARN;
  97 + break;
  98 + case ERROR:
  99 + result = SEVERITY_ERROR;
  100 + break;
  101 + case FATAL:
  102 + result = SEVERITY_FATAL;
  103 + }
  104 +
  105 + return result;
  106 + }
  107 +
  108 + public static FacesMessage parse(final Throwable throwable) {
  109 + FacesMessage facesMessage = new FacesMessage();
  110 + ApplicationException annotation = throwable.getClass().getAnnotation(ApplicationException.class);
  111 +
  112 + if (annotation != null) {
  113 + facesMessage.setSeverity(parse(annotation.severity()));
  114 + } else {
  115 + facesMessage.setSeverity(SEVERITY_ERROR);
  116 + }
  117 +
  118 + if (throwable.getMessage() != null) {
  119 + facesMessage.setSummary(throwable.getMessage());
  120 + } else {
  121 + facesMessage.setSummary(throwable.toString());
  122 + }
  123 +
  124 + return facesMessage;
  125 + }
  126 +
  127 + public static FacesMessage parse(final Message message) {
  128 + FacesMessage facesMessage = new FacesMessage();
  129 + facesMessage.setSeverity(parse(message.getSeverity()));
  130 + facesMessage.setSummary(message.getText());
  131 + return facesMessage;
  132 + }
  133 +
  134 + public static Object convert(final String value, final Converter converter) {
  135 + Object result = null;
  136 +
  137 + if (!Strings.isEmpty(value)) {
  138 + if (converter != null) {
  139 + result = converter.getAsObject(getFacesContext(), getFacesContext().getViewRoot(), value);
  140 + } else {
  141 + result = new String(value);
  142 + }
  143 + }
  144 +
  145 + return result;
  146 + }
  147 +
  148 + public static Converter getConverter(Class<?> clazz) {
  149 + FacesContext context = getFacesContext();
  150 + if (clazz == null) {
  151 + return null;
  152 + }
  153 + try {
  154 + Application application = context.getApplication();
  155 + Converter converter = (application.createConverter(clazz));
  156 + return converter;
  157 + } catch (Exception e) {
  158 + return (null);
  159 + }
  160 + }
  161 +
  162 + public static Map<String, Object> getViewMap() {
  163 + UIViewRoot viewRoot = getFacesContext().getViewRoot();
  164 + return viewRoot.getViewMap(true);
  165 + }
  166 +
  167 +}
... ...