View Javadoc
1   package fr.ifremer.allegro.obsdeb.ui.swing.util.table;
2   
3   /*
4    * #%L
5    * SIH Allegro ObsDeb :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2013 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.base.Preconditions;
27  import fr.ifremer.allegro.obsdeb.service.ObsdebTechnicalException;
28  import org.apache.commons.beanutils.NestedNullException;
29  import org.apache.commons.beanutils.PropertyUtils;
30  import org.apache.commons.lang3.StringUtils;
31  import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
32  
33  import java.lang.reflect.InvocationTargetException;
34  
35  /**
36   * override version of ColumnIdentifier with property type
37   *
38   * @param <R>
39   * @author Ludovic Pecquot (ludovic.pecquot@e-is.pro)
40   */
41  public class ObsdebColumnIdentifier<R> extends ColumnIdentifier<R> {
42  
43      private static final long serialVersionUID = 1L;
44  
45      private final Class<?> propertyType;
46      private final String decoratorName;
47      private final boolean mandatory;
48  
49      public static <R> ObsdebColumnIdentifier<R> newId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType) {
50          return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, null, false);
51      }
52  
53      public static <R> ObsdebColumnIdentifier<R> newId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType,
54                                                        String decoratorName) {
55          return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, decoratorName, false);
56      }
57  
58      public static <R> ObsdebColumnIdentifier<R> newId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType,
59                                                        boolean mandatory) {
60          return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, null, mandatory);
61      }
62  
63      public static <R> ObsdebColumnIdentifier<R> newId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType,
64                                                        String decoratorName, boolean mandatory) {
65          return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, decoratorName, mandatory);
66      }
67  
68      public static <R> ObsdebColumnIdentifier<R> newReadOnlyId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType) {
69          return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, null, false) {
70  
71              private static final long serialVersionUID = 1L;
72  
73              @Override
74              public void setValue(R entry, Object value) {
75                  // no set
76              }
77          };
78      }
79  
80      protected ObsdebColumnIdentifier(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType, String decoratorName, boolean mandatory) {
81          super(propertyName, headerI18nKey, headerTipI18nKey);
82          this.propertyType = propertyType;
83          this.decoratorName = decoratorName;
84          this.mandatory = mandatory;
85      }
86  
87      public Class<?> getPropertyType() {
88          return propertyType;
89      }
90  
91      public String getDecoratorName() {
92          return decoratorName;
93      }
94  
95      public boolean isMandatory() {
96          return mandatory;
97      }
98  
99      @Override
100     public Object getValue(R entry) {
101         Object result = null;
102         if (StringUtils.isNotBlank(getPropertyName()) && entry != null) {
103             try {
104                 result = PropertyUtils.getProperty(entry, getPropertyName());
105             } catch (NestedNullException e) {
106                 result = null;
107             } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
108                 throw new ObsdebTechnicalException(String.format("Property %s not found on object of type %s", getPropertyName(), entry.getClass().getName()), e);
109             }
110         }
111         return result;
112     }
113 
114     @Override
115     public void setValue(R entry, Object value) {
116         Preconditions.checkNotNull(entry);
117         if (StringUtils.isNotBlank(getPropertyName())) {
118             try {
119                 PropertyUtils.setProperty(entry, getPropertyName(), value);
120             } catch (NestedNullException e) {
121                 throw new ObsdebTechnicalException(String.format("Nested property %s is null on object of type %s", getPropertyName(), entry.getClass().getName()), e);
122             } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
123                 throw new ObsdebTechnicalException(String.format("Property %s not found on object of type %s", getPropertyName(), entry.getClass().getName()), e);
124             }
125         }
126     }
127 }