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