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      /**
59       * 
60       */
61      private static final long serialVersionUID = 2220490166227831286L;
62      // Constants
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       * getPage
84       *
85       * @param request
86       *            HttpServletRequest
87       * @param nMode
88       *            int
89       * @param plugin
90       *            Plugin
91       * @return XPage
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      * Return the CodeWizard Page
120      * 
121      * @param request
122      *            The HttpServletRequest
123      * @return the html code
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      * Return the generated page
137      * 
138      * @param request
139      *            The HttpServletRequest
140      * @return strPage
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      * Parse the attributes of the list
175      * 
176      * @param strAttributesList
177      *            the list of attributes
178      * @param bo
179      *            the BusinessObject
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 }