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 package fr.paris.lutece.plugins.codewizard.web;
35
36 import fr.paris.lutece.plugins.codewizard.business.BusinessObject;
37 import fr.paris.lutece.plugins.codewizard.business.ObjectAttribute;
38 import fr.paris.lutece.plugins.codewizard.service.GeneratorService;
39 import fr.paris.lutece.plugins.codewizard.service.JavaTypeService;
40 import fr.paris.lutece.portal.service.i18n.I18nService;
41 import fr.paris.lutece.portal.service.plugin.Plugin;
42 import fr.paris.lutece.portal.service.template.AppTemplateService;
43 import fr.paris.lutece.portal.web.xpages.XPage;
44 import fr.paris.lutece.portal.web.xpages.XPageApplication;
45 import fr.paris.lutece.util.html.HtmlTemplate;
46
47 import java.util.HashMap;
48 import java.util.Map;
49 import java.util.StringTokenizer;
50
51 import javax.servlet.http.HttpServletRequest;
52
53
54
55
56 public class CodeWizardApp implements XPageApplication
57 {
58
59 private static final String TEMPLATE_CODE_WIZARD = "/skin/plugins/codewizard/code_wizard.html";
60 private static final String TEMPLATE_SOURCE_CODE = "/skin/plugins/codewizard/code_source.html";
61
62 private static final String MARK_COMBO_GENERATORS = "combo_generators";
63 private static final String MARK_SOURCE_CODE = "source_code";
64
65 private static final String PARAM_ACTION = "action";
66 private static final String PARAM_CLASSNAME = "class";
67 private static final String PARAM_PACKAGE = "package";
68 private static final String PARAM_TABLE = "table";
69 private static final String PARAM_PLUGIN = "plugin";
70 private static final String PARAM_GENERATION_TYPE = "generation_type";
71 private static final String PARAM_ATTRIBUTES = "attributes";
72
73 private static final String ACTION_GENERATE = "generate";
74
75 private static final String PROPERTY_PAGE_TITLE = "codewizard.pageTitle";
76 private static final String PROPERTY_PAGE_PATH_LABEL = "codewizard.pagePathLabel";
77
78
79
80
81
82
83
84
85
86
87
88
89 @Override
90 public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
91 {
92 XPage page = new XPage( );
93
94 page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGE_TITLE, request.getLocale( ) ) );
95 page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGE_PATH_LABEL, request.getLocale( ) ) );
96
97 String strContent;
98 String strAction = request.getParameter( PARAM_ACTION );
99
100 if ( ( strAction != null ) && ( strAction.equals( ACTION_GENERATE ) ) )
101 {
102 strContent = getGeneratePage( request );
103 }
104 else
105 {
106 strContent = getCodeWizardPage( request );
107 }
108
109 page.setContent( strContent );
110
111 return page;
112 }
113
114
115
116
117
118
119
120
121 private String getCodeWizardPage( HttpServletRequest request )
122 {
123 Map<String, Object> model = new HashMap<>( );
124 model.put( MARK_COMBO_GENERATORS, GeneratorService.getGeneratorsList( ) );
125
126 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CODE_WIZARD, request.getLocale( ), model );
127
128 return template.getHtml( );
129 }
130
131
132
133
134
135
136
137
138 private String getGeneratePage( HttpServletRequest request )
139 {
140 String strSourceCode = "";
141 String strPackageName = request.getParameter( PARAM_PACKAGE );
142 String strClassName = request.getParameter( PARAM_CLASSNAME );
143 String strTable = request.getParameter( PARAM_TABLE );
144 String strPlugin = request.getParameter( PARAM_PLUGIN );
145 String strGenerationType = request.getParameter( PARAM_GENERATION_TYPE );
146 String strAttributesList = request.getParameter( PARAM_ATTRIBUTES );
147 BusinessObject bo = new BusinessObject( );
148
149 bo.setClassName( strClassName );
150 bo.setPackageName( strPackageName );
151 bo.setTable( strTable );
152 bo.setPluginName( strPlugin );
153 parseAttributesList( strAttributesList, bo );
154
155 if ( strGenerationType != null )
156 {
157 int nIndex = Integer.parseInt( strGenerationType );
158 strSourceCode = GeneratorService.generate( bo, nIndex );
159 }
160
161 Map<String, Object> model = new HashMap<>( );
162 model.put( MARK_SOURCE_CODE, strSourceCode );
163
164 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SOURCE_CODE, request.getLocale( ), model );
165
166 return template.getHtml( );
167 }
168
169
170
171
172
173
174
175
176
177 private void parseAttributesList( String strAttributesList, BusinessObject bo )
178 {
179 StringTokenizer stLines = new StringTokenizer( strAttributesList.trim( ), "\r\n" );
180 boolean bFirst = true;
181
182 while ( stLines.hasMoreTokens( ) )
183 {
184 String strLine = stLines.nextToken( );
185 strLine = strLine.trim( );
186
187 String[] tokens = strLine.split( "\\s+" );
188
189 if ( tokens.length >= 2 )
190 {
191 String strColumnName = tokens[0];
192 String strJavaType = tokens[1];
193 ObjectAttribute attribute = JavaTypeService.getAttribute( strColumnName, strJavaType );
194 bo.addAttribute( attribute );
195 if( tokens.length >= 3 )
196 {
197 attribute.setInputType( tokens[2] );
198 }
199
200 if ( bFirst )
201 {
202 bo.setIdColumnName( strColumnName );
203 bFirst = false;
204 }
205 }
206 }
207 }
208
209 }