1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
101
102
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
119
120
121
122
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
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
157
158
159
160
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 }