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  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   * Thi class manage CodeWizard Page
55   */
56  public class CodeWizardApp implements XPageApplication
57  {
58      // Constants
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       * getPage
80       *
81       * @param request
82       *            HttpServletRequest
83       * @param nMode
84       *            int
85       * @param plugin
86       *            Plugin
87       * @return XPage
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      * Return the CodeWizard Page
116      * 
117      * @param request
118      *            The HttpServletRequest
119      * @return the html code
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      * Return the generated page
133      * 
134      * @param request
135      *            The HttpServletRequest
136      * @return strPage
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      * Parse the attributes of the list
171      * 
172      * @param strAttributesList
173      *            the list of attributes
174      * @param bo
175      *            the BusinessObject
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 }