View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.util.bean;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  
38  import org.apache.commons.beanutils.BeanUtils;
39  
40  import java.lang.reflect.Field;
41  import java.lang.reflect.InvocationTargetException;
42  
43  import java.util.HashMap;
44  import java.util.Map;
45  import java.util.Map.Entry;
46  
47  import javax.servlet.http.HttpServletRequest;
48  
49  
50  /**
51   * Bean Utils
52   */
53  public final class BeanUtil
54  {
55      private static final char UNDERSCORE = '_';
56  
57      /** Private constructor */
58      private BeanUtil(  )
59      {
60      }
61  
62      /**
63       * Populate a bean using parameters in http request
64       *
65       * @param bean bean to populate
66       * @param request http request
67       */
68      public static void populate( Object bean, HttpServletRequest request )
69      {
70          for ( Field field : bean.getClass(  ).getDeclaredFields(  ) )
71          {
72              try
73              {
74                  // for all boolean field, init to false
75                  if ( field.getType(  ).getName(  ).equals( Boolean.class.getName(  ) ) ||
76                          field.getType(  ).getName(  ).equals( boolean.class.getName(  ) ) )
77                  {
78                      field.setAccessible( true );
79                      field.set( bean, false );
80                  }
81              }
82              catch ( Exception e )
83              {
84                  String error = "La valeur du champ " + field.getName(  ) + " de la classe " +
85                      bean.getClass(  ).getName(  ) + " n'a pas pu être récupéré ";
86                  AppLogService.error( error );
87                  throw new RuntimeException( error, e );
88              }
89          }
90  
91          try
92          {
93              BeanUtils.populate( bean, convertMap( request.getParameterMap(  ) ) );
94          }
95          catch ( IllegalAccessException e )
96          {
97              AppLogService.error( "Unable to fetch data from request", e );
98          }
99          catch ( InvocationTargetException e )
100         {
101             AppLogService.error( "Unable to fetch data from request", e );
102         }
103     }
104 
105     /**
106      * Convert map by casifying parameters names.
107      * @param mapInput The input map
108      * @return The output map
109      */
110     public static Map<String, Object> convertMap( Map<String, Object> mapInput )
111     {
112         Map<String, Object> mapOutput = new HashMap<String, Object>(  );
113 
114         for ( Entry<String, Object> entry : mapInput.entrySet(  ) )
115         {
116             mapOutput.put( convertUnderscores( entry.getKey(  ) ), entry.getValue(  ) );
117         }
118 
119         return mapOutput;
120     }
121 
122     /**
123      * Remove underscore and set the next letter in caps
124      * @param strSource The source
125      * @return The converted string
126      */
127     public static String convertUnderscores( String strSource )
128     {
129         StringBuilder sb = new StringBuilder(  );
130         boolean bCapitalizeNext = false;
131 
132         for ( char c : strSource.toCharArray(  ) )
133         {
134             if ( c == UNDERSCORE )
135             {
136                 bCapitalizeNext = true;
137             }
138             else
139             {
140                 if ( bCapitalizeNext )
141                 {
142                     sb.append( Character.toUpperCase( c ) );
143                     bCapitalizeNext = false;
144                 }
145                 else
146                 {
147                     sb.append( c );
148                 }
149             }
150         }
151 
152         return sb.toString(  );
153     }
154 }