View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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  
35  package fr.paris.lutece.plugins.codewizard.service;
36  
37  import fr.paris.lutece.plugins.codewizard.business.JavaType;
38  import fr.paris.lutece.plugins.codewizard.business.ObjectAttribute;
39  import java.text.MessageFormat;
40  import java.util.HashMap;
41  import java.util.Map;
42  
43  /**
44   * JavaTypeService
45   */
46  public class JavaTypeService
47  {
48      private static Map<String, JavaType> _mapJavaType = new HashMap<>( );
49      private static boolean _bInit;
50  
51      private static void init( )
52      {
53          // constructor parameters : Java type | prefix | use in DAO | required import | test value format
54          _mapJavaType.put( "int", new JavaType( "int", "n", true , null , "{0}" ) );
55          _mapJavaType.put( "float", new JavaType( "float", "f", true  , null , "{0}.0") );
56          _mapJavaType.put( "long", new JavaType( "long", "l", true , null , "{0}L" ) );
57          _mapJavaType.put( "double", new JavaType( "double", "d", true  , null , "{0}.0") );
58          _mapJavaType.put( "boolean", new JavaType( "boolean", "b", true , null , "true" ) );
59          _mapJavaType.put( "string", new JavaType( "String", "str", true , null , "\"{1}{0}\"" ) );
60          _mapJavaType.put( "date", new JavaType( "Date", "date", true , "java.sql.Date" , "new Date( {0}L )" ) );
61          _mapJavaType.put( "timestamp", new JavaType( "Timestamp", "date", true , "java.sql.Timestamp"  , "new Timestamp( {0}L )" ) );
62          _mapJavaType.put( "map", new JavaType( "Map", "map", false , "java.util.Map" , "" ) );
63          _mapJavaType.put( "list", new JavaType( "List", "list", false , "java.util.List" , ""  ) );
64          _bInit = true;
65      }
66  
67      public static ObjectAttribute getAttribute( String strColumnName, String strJavaType )
68      {
69          ObjectAttribute attribute = new ObjectAttribute( );
70          attribute.setColumnName( strColumnName );
71  
72          if ( !_bInit )
73          {
74              init( );
75          }
76          String strName = getProperName( strColumnName );
77          attribute.setName( strName );
78          String strJavaTypeKey = strJavaType.toLowerCase( );
79          JavaType jt = _mapJavaType.get( strJavaTypeKey );
80          if ( jt != null )
81          {
82              attribute.setType( jt.getName( ) );
83              attribute.setVariableName( jt.getPrefix( ) + strName );
84              attribute.setDaoType( jt.isDaoType( ) );
85              Object[] args1 = { "1" , strName };
86              Object[] args2 = { "2" , strName };
87              attribute.setTestInitValue1( MessageFormat.format( jt.getTestInitValueFormat(), args1));
88              attribute.setTestInitValue2( MessageFormat.format( jt.getTestInitValueFormat(), args2));
89          }
90          else
91          {
92              attribute.setType( strJavaType );
93              attribute.setVariableName( decapitalize( strName ) );
94              attribute.setDaoType( false );
95          }
96          return attribute;
97      }
98  
99      /**
100      * Return an import for a given data type if needed
101      * @param strJavaType The java type
102      * @return  The import or null
103      */
104     public static String getImport( String strJavaType )
105     {
106         String strImport = null;
107         String strJavaTypeKey = strJavaType.toLowerCase( );
108         JavaType jt = _mapJavaType.get( strJavaTypeKey );
109         if( jt != null )
110         {
111             strImport = jt.getImport();
112         }
113         return strImport;
114     }
115 
116     
117     /**
118      * Returns the Proper Name
119      *
120      * @param strSource
121      *            the source
122      * @return source
123      */
124     private static String getProperName( String strSource )
125     {
126         int nIndex = 0;
127         boolean bUpper = true;
128         StringBuilder sbBuffer = new StringBuilder( );
129 
130         while ( nIndex < strSource.length( ) )
131         {
132             char c = strSource.charAt( nIndex );
133 
134             if ( c == '_' )
135             {
136                 // skip by reading the next char
137                 nIndex++;
138                 bUpper = true;
139             }
140 
141             if ( bUpper )
142             {
143                 String strChar = strSource.substring( nIndex, nIndex + 1 );
144                 c = strChar.toUpperCase( ).charAt( 0 );
145                 bUpper = false;
146             }
147 
148             sbBuffer.append( c );
149             nIndex++;
150         }
151 
152         return sbBuffer.toString( );
153     }
154 
155     /**
156      * Set the first character to lower case
157      * 
158      * @param strSource
159      *            The source
160      * @return The new string
161      */
162     private static String decapitalize( String strSource )
163     {
164         if ( strSource == null || strSource.length( ) == 0 )
165         {
166             return strSource;
167         }
168         char c [ ] = strSource.toCharArray( );
169         c [0] = Character.toLowerCase( c [0] );
170         return new String( c );
171     }
172 
173 }