View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.ctv.util;
35  
36  import java.lang.reflect.Field;
37  import java.lang.reflect.ParameterizedType;
38  import java.lang.reflect.Type;
39  import java.util.HashMap;
40  import java.util.HashSet;
41  import java.util.Map;
42  import java.util.Set;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise;
50  
51  public class EmpriseMappingUtils
52  {
53  
54      private final static Log LOGGER = LogFactory.getLog( EmpriseMappingUtils.class );
55  
56      public static void map( HttpServletRequest request, DetailsEmprise details )
57      {
58          for ( Field field : details.getClass( ).getDeclaredFields( ) )
59          {
60              String fieldName = field.getName( );
61              Class<?> fieldType = field.getType( );
62              if ( fieldType.isAssignableFrom( Map.class ) )
63              {
64                  ParameterizedType pType = (ParameterizedType) field.getGenericType( );
65                  Type type = pType.getActualTypeArguments( ) [0];
66                  Type data = pType.getActualTypeArguments( ) [1];
67                  Map map = new HashMap<>( );
68                  if ( request.getParameterValues( fieldName ) != null && request.getParameterValues( fieldName ) [0] != null )
69                  {
70                      for ( String key : request.getParameterValues( fieldName ) )
71                      {
72                          String value = request.getParameter( fieldName + "{'" + key + "'}" );
73                          Class<Enum> e = (Class<Enum>) type;
74                          map.put( Enum.valueOf( e, key ), getValue( (Class) data, value ) );
75  
76                      }
77                  }
78                  if ( !map.isEmpty( ) )
79                  {
80                      setValue( details, field, map );
81                  }
82              }
83              else
84                  if ( fieldType.isAssignableFrom( Set.class ) )
85                  {
86                      Set set = new HashSet<>( );
87                      if ( request.getParameterValues( fieldName ) != null && request.getParameterValues( fieldName ) [0] != null )
88                      {
89                          ParameterizedType pType = (ParameterizedType) field.getGenericType( );
90                          Type type = pType.getActualTypeArguments( ) [0];
91                          for ( String key : request.getParameterValues( fieldName ) )
92                          {
93                              Class<Enum> e = (Class<Enum>) type;
94                              set.add( Enum.valueOf( e, key ) );
95                          }
96                      }
97                      if ( !set.isEmpty( ) )
98                      {
99                          setValue( details, field, set );
100                     }
101                 }
102         }
103     }
104 
105     private static void setValue( DetailsEmprise details, Field field, Object obj )
106     {
107         try
108         {
109             field.setAccessible( true );
110             field.set( details, obj );
111             field.setAccessible( false );
112         }
113         catch( IllegalArgumentException | IllegalAccessException e )
114         {
115             LOGGER.debug( "erreur lors de l'introspection", e );
116         }
117     }
118 
119     private static Object getValue( Class type, String value )
120     {
121 
122         if ( value == null || value.isEmpty( ) )
123         {
124             return null;
125         }
126         if ( type.isAssignableFrom( Double.class ) )
127         {
128             value = value.replace( ",", "." );
129             try
130             {
131                 return Double.parseDouble( value );
132             }
133             catch( NumberFormatException e )
134             {
135                 return 0;
136             }
137         }
138         else
139             if ( type.isAssignableFrom( Integer.class ) )
140             {
141                 try
142                 {
143                     return Integer.parseInt( value );
144                 }
145                 catch( NumberFormatException e )
146                 {
147                     return 0;
148                 }
149             }
150         return value;
151     }
152 
153 }