View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.pluginwizard.service.generator;
35  
36  import fr.paris.lutece.plugins.pluginwizard.business.model.Application;
37  import fr.paris.lutece.plugins.pluginwizard.business.model.Attribute;
38  import fr.paris.lutece.plugins.pluginwizard.business.model.BusinessClass;
39  import fr.paris.lutece.plugins.pluginwizard.business.model.Feature;
40  import fr.paris.lutece.plugins.pluginwizard.business.model.PluginModel;
41  import fr.paris.lutece.plugins.pluginwizard.business.model.Portlet;
42  import fr.paris.lutece.plugins.pluginwizard.util.Utils;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  
45  import java.text.MessageFormat;
46  
47  import java.util.HashMap;
48  import java.util.Map;
49  
50  /**
51   *
52   * Class generated needed resource files for i18n implementation
53   *
54   */
55  public class ResourcesGenerator extends AbstractGenerator
56  {
57      private static final String PATH_JAVA = "src/java/fr/paris/lutece/plugins/{plugin_name}/resources/";
58      private static final String PATH_KOTLIN = "src/kotlin/fr/paris/lutece/plugins/{plugin_name}/resources/";
59      private static String [ ] _languages = {
60              "", "fr"
61      };
62      private static String [ ] _prefix = {
63              "create", "modify"
64      };
65      private static String [ ] _suffix = {
66              "created", "updated", "removed"
67      };
68  
69      /**
70       * {@inheritDoc }
71       */
72      @Override
73      public Map generate( PluginModel pm, String generationSchemeName )
74      {
75          HashMap map = new HashMap( );
76  
77          String prefixFileName = pm.getPluginNameForRessource( );
78  
79          for ( String strLanguage : _languages )
80          {
81              String strFilesPath = ( isKotlin( ) ) ? PATH_KOTLIN : PATH_JAVA;
82              String strPath = getFilePath( pm, strFilesPath,
83                      prefixFileName + "_messages" + ( strLanguage.length( ) > 0 ? "_" : "" ) + strLanguage + ".properties" );
84  
85              String strSourceCode = getCode( pm, strLanguage );
86              map.put( strPath, strSourceCode );
87          }
88  
89          return map;
90      }
91  
92      /**
93       * Build the code
94       *
95       * @param pm
96       *            The Plugin Model
97       * @param strLanguage
98       *            The language
99       * @return The code
100      */
101     private String getCode( PluginModel pm, String strLanguage )
102     {
103         StringBuilder sb = new StringBuilder( );
104         generatePluginKeys( sb, pm );
105         generateFeaturesKeys( sb, pm );
106         generateXPagesKeys( sb, pm );
107         generateBusinessClassKeys( sb, pm, strLanguage );
108         generatePortletsKeys( sb, pm );
109         generateInfosKeys( sb, pm, strLanguage );
110 
111         return sb.toString( );
112     }
113 
114     /**
115      * Writes in the buffer resources keys for the plugin
116      *
117      * @param sb
118      *            The buffer
119      * @param pm
120      *            The plugin model
121      */
122     private void generatePluginKeys( StringBuilder sb, PluginModel pm )
123     {
124         sb.append( "# Plugin's keys\n" );
125         if ( pm.isModule( ) )
126         {
127             sb.append( "module.provider=" ).append( pm.getPluginProvider( ) ).append( "\n" );
128             sb.append( "module.description=" ).append( pm.getPluginDescription( ) ).append( "\n" );
129         }
130         else
131         {
132             sb.append( "plugin.provider=" ).append( pm.getPluginProvider( ) ).append( "\n" );
133             sb.append( "plugin.description=" ).append( pm.getPluginDescription( ) ).append( "\n" );
134         }
135 
136         sb.append( "\n" );
137     }
138 
139     /**
140      * Writes in the buffer resources keys for features
141      *
142      * @param sb
143      *            The buffer
144      * @param pm
145      *            The plugin model
146      */
147     private void generateFeaturesKeys( StringBuilder sb, PluginModel pm )
148     {
149         if ( !pm.getFeatures( ).isEmpty( ) )
150         {
151             sb.append( "\n# Admin features keys\n\n" );
152 
153             for ( Feature feature : pm.getFeatures( ) )
154             {
155                 sb.append( "adminFeature." ).append( feature.getFeatureName( ) ).append( ".name=" ).append( feature.getFeatureName( ) ).append( "\n" );
156                 sb.append( "adminFeature." ).append( feature.getFeatureName( ) ).append( ".description=" ).append( feature.getFeatureDescription( ) )
157                         .append( "\n" );
158             }
159 
160             sb.append( "\n" );
161         }
162     }
163 
164     private void generateXPagesKeys( StringBuilder sb, PluginModel pm )
165     {
166         if ( !pm.getApplications( ).isEmpty( ) )
167         {
168             sb.append( "\n# XPages keys\n\n" );
169         }
170 
171         for ( Application application : pm.getApplications( ) )
172         {
173             if ( application.getIdBusinessClasses( ).isEmpty( ) )
174             {
175                 sb.append( "xpage." ).append( application.getApplicationName( ) ).append( ".pageTitle=" ).append( application.getApplicationName( ) )
176                         .append( "\n" );
177                 sb.append( "xpage." ).append( application.getApplicationName( ) ).append( ".pagePathLabel=" ).append( application.getApplicationName( ) )
178                         .append( "\n" );
179             }
180             else
181             {
182                 for ( int id : application.getIdBusinessClasses( ) )
183                 {
184                     for ( BusinessClass bc : pm.getBusinessClasses( ) )
185                     {
186                         if ( bc.getId( ) == id )
187                         {
188                             sb.append( "xpage." ).append( bc.getBusinessClass( ).toLowerCase( ) ).append( ".pageTitle=" )
189                                     .append( bc.getBusinessClass( ).toLowerCase( ) ).append( "\n" );
190                             sb.append( "xpage." ).append( bc.getBusinessClass( ).toLowerCase( ) ).append( ".pagePathLabel=" )
191                                     .append( bc.getBusinessClass( ).toLowerCase( ) ).append( "\n" );
192                         }
193                     }
194                 }
195             }
196         }
197     }
198 
199     /**
200      * Writes in the buffer resources keys for business classes
201      *
202      * @param sb
203      *            The buffer
204      * @param pm
205      *            The plugin model
206      * @param strLanguage
207      *            The language
208      */
209     private void generateBusinessClassKeys( StringBuilder sb, PluginModel pm, String strLanguage )
210     {
211         if ( !pm.getBusinessClasses( ).isEmpty( ) )
212         {
213             sb.append( "\n# Business classes keys\n\n" );
214         }
215 
216         for ( BusinessClass bc : pm.getBusinessClasses( ) )
217         {
218             sb.append( "\n# keys for business classes keys : " ).append( bc.getBusinessClass( ) ).append( "\n" );
219 
220             String strPrefix = "manage_" + bc.getBusinessClass( ).toLowerCase( ) + "s.";
221             sb.append( strPrefix ).append( "pageTitle=" ).append( bc.getBusinessClass( ) ).append( "\n" );
222             sb.append( strPrefix ).append( "title=" ).append( getLabel( "title.manage", strLanguage, bc.getBusinessClass( ) ) ).append( "\n" );
223             sb.append( strPrefix ).append( "buttonAdd=" ).append( getLabel( "buttonAdd", strLanguage, bc.getBusinessClass( ) ) ).append( "\n" );
224 
225             for ( Attribute attribute : bc.getAttributes( ) )
226             {
227                 sb.append( strPrefix ).append( "column" ).append( attribute.getName( ) ).append( "=" ).append( attribute.getLabelName( ) ).append( "\n" );
228             }
229 
230             for ( String strBasePrefix : _prefix )
231             {
232                 strPrefix = strBasePrefix + "_" + bc.getBusinessClass( ).toLowerCase( ) + ".";
233                 sb.append( strPrefix ).append( "pageTitle=" ).append( bc.getBusinessClass( ) ).append( "\n" );
234                 sb.append( strPrefix ).append( "title=" ).append( getLabel( "title." + strBasePrefix, strLanguage, bc.getBusinessClass( ) ) ).append( "\n" );
235 
236                 for ( Attribute attribute : bc.getAttributes( ) )
237                 {
238                     sb.append( strPrefix ).append( "label" ).append( attribute.getName( ) ).append( "=" ).append( attribute.getLabelName( ) ).append( "\n" );
239                     sb.append( strPrefix ).append( "label" ).append( attribute.getName( ) ).append( ".help=" ).append( attribute.getLabelName( ) )
240                             .append( " (" ).append( getLabel( "helpText", strLanguage ) ).append( ")\n" );
241                 }
242             }
243 
244             sb.append( "\nmessage.confirmRemove" ).append( bc.getBusinessClass( ) ).append( "=" )
245                     .append( getLabel( "confirmRemove", strLanguage, bc.getBusinessClass( ) ) ).append( "\n" );
246 
247             // Constraints messages
248             sb.append( "\n# JSR 303 constraint validator messages\n" );
249 
250             strPrefix = "validation." + bc.getBusinessClass( ).toLowerCase( ) + ".";
251 
252             for ( Attribute attribute : bc.getAttributes( ) )
253             {
254                 if ( !attribute.getType( ).equals( "int" ) )
255                 {
256                     if ( attribute.getNotNull( ) )
257                     {
258                         sb.append( strPrefix ).append( attribute.getName( ) ).append( ".notEmpty=" )
259                                 .append( getLabel( "validation.notEmpty", strLanguage, attribute.getLabelName( ) ) ).append( "\n" );
260                     }
261 
262                     if ( attribute.getMaxLength( ) > 0 )
263                     {
264                         sb.append( strPrefix ).append( attribute.getName( ) ).append( ".size=" )
265                                 .append( getLabel( "validation.size", strLanguage, attribute.getLabelName( ), "" + attribute.getMaxLength( ) ) ).append( "\n" );
266                     }
267                 }
268             }
269 
270             sb.append( "\n# model attributes for validation messages\n" );
271             strPrefix = "model.entity." + bc.getBusinessClass( ).toLowerCase( ) + ".attribute.";
272 
273             for ( Attribute attribute : bc.getAttributes( ) )
274             {
275                 sb.append( strPrefix ).append( Utils.firstLowerCase( attribute.getName( ) ) ).append( "=" ).append( attribute.getLabelName( ) ).append( "\n" );
276             }
277         }
278     }
279 
280     /**
281      * Writes in the buffer resources keys for portlets
282      *
283      * @param sb
284      *            The buffer
285      * @param pm
286      *            The plugin model
287      */
288     private void generatePortletsKeys( StringBuilder sb, PluginModel pm )
289     {
290         if ( !pm.getPortlets( ).isEmpty( ) )
291         {
292             sb.append( "\n# Portlets keys\n\n" );
293 
294             for ( Portlet portlet : pm.getPortlets( ) )
295             {
296                 sb.append( "portlet." ).append( pm.getPluginName( ).toLowerCase( ) ).append( portlet.getPortletClass( ) ).append( ".name=" )
297                         .append( portlet.getJspBaseName( ) ).append( "\n" );
298             }
299 
300             sb.append( "\n" );
301         }
302     }
303 
304     /**
305      * Writes in the buffer resources keys for info notifications
306      *
307      * @param sb
308      *            The buffer
309      * @param pm
310      *            The plugin model
311      */
312     private void generateInfosKeys( StringBuilder sb, PluginModel pm, String strLanguage )
313     {
314         if ( !pm.getBusinessClasses( ).isEmpty( ) )
315         {
316             sb.append( "\n# Infos keys\n\n" );
317 
318             for ( BusinessClass bc : pm.getBusinessClasses( ) )
319             {
320                 for ( String strSuffix : _suffix )
321                 {
322                     sb.append( "info." ).append( bc.getBusinessClass( ).toLowerCase( ) ).append( "." ).append( strSuffix ).append( "=" )
323                             .append( bc.getBusinessClass( ) ).append( " " ).append( getLabel( "infoKey." + strSuffix, strLanguage ) ).append( "\n" );
324                 }
325             }
326 
327             sb.append( "\n" );
328         }
329     }
330 
331     /**
332      * Gets a label for a given language from the pluginwizard.properties file
333      *
334      * @param strKey
335      *            The key of the label
336      * @param strLanguage
337      *            The language
338      * @param arguments
339      *            arguments of the label
340      * @return The value of the label
341      */
342     private String getLabel( String strKey, String strLanguage, String... arguments )
343     {
344         String strFullKey = "pluginwizard.label." + strKey + "." + strLanguage;
345         String strLabel = AppPropertiesService.getProperty( strFullKey, "Label not found for key " + strFullKey );
346 
347         return MessageFormat.format( strLabel, (Object [ ]) arguments );
348     }
349 }