View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.lutecetools.service;
35  
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import fr.paris.lutece.plugins.lutecetools.business.AbstractComponent;
43  import fr.paris.lutece.plugins.lutecetools.business.Dependency;
44  import fr.paris.lutece.plugins.lutecetools.business.Site;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.portal.web.l10n.LocaleService;
47  import fr.paris.lutece.util.html.HtmlTemplate;
48  import fr.paris.lutece.util.xml.XmlUtil;
49  
50  /**
51   * Dependencies Service
52   */
53  public final class DependenciesService
54  {
55      private static final String TEMPLATE_POM = "skin/plugins/lutecetools/pom_template.html";
56      private static final String MARK_SITE = "site";
57      private static final String MARK_DEPENDENCIES = "dependencies";
58      private static final String LUTECE_CORE = "lutece-core";
59      private static final String TAG_DEPENDENCY = "dependency";
60      private static final String TAG_GROUP_ID = "groupId";
61      private static final String GROUP_ID_CORE = "fr.paris.lutece";
62      private static final String GROUP_ID_PLUGINS = GROUP_ID_CORE + ".plugins";
63      private static final String TAG_ARTIFACT_ID = "artifactId";
64      private static final String TAG_VERSION = "version";
65      private static final String TAG_TYPE = "type";
66      private static final String TYPE_LUTECE_CORE = "lutece-core";
67      private static final String TYPE_LUTECE_SITE = "lutece-site";
68      private static final String TYPE_LUTECE_PLUGIN = "lutece-plugin";
69      private static final String TYPE_LUTECE_LIBRARY = "jar";
70      private static final String FORMAT_TEXT = "text";
71      private static final String FORMAT_XML = "xml";
72      private static final String FORMAT_POM = "pom";
73      private static final String INDENT = "    ";
74  
75      /**
76       * private constructor
77       */
78      private DependenciesService( )
79      {
80      }
81  
82      /**
83       * Process dependencies generation
84       * 
85       * @param strSource The source
86       * @param strFormat The output
87       * @return The dependencies
88       */
89      public static String process( String strSource, String strFormat )
90      {
91          String[] components = strSource.split( "\\s+" );
92  
93          List<Dependency> list = getDependenciesList( components );
94  
95          return process( list, strFormat, null );
96      }
97  
98      /**
99       * Process dependencies generation
100      * 
101      * @param listComponents The list of components for building the pom
102      * @param strFormat      The output
103      * @param site           The site to build
104      * @param strArtifactId  The artifact Id
105      * @return The dependencies
106      */
107     public static String process( List<? extends AbstractComponent> listComponents, String strFormat, Site site )
108     {
109         List<Dependency> listDependencies = getDependenciesList( listComponents );
110 
111         if ( ( strFormat != null ) && strFormat.equals( FORMAT_TEXT ) )
112         {
113             return getDependenciesText( listDependencies );
114         }
115         else if ( ( strFormat != null ) && strFormat.equals( FORMAT_XML ) )
116         {
117             return getDependenciesXML( listDependencies );
118         }
119         else if ( ( strFormat != null ) && strFormat.equals( FORMAT_POM ) )
120         {
121             return getDependenciesPOM( listDependencies, site );
122         }
123 
124         return "Invalid format";
125     }
126 
127     /**
128      * Get the dependency list from a list of artifact id
129      * 
130      * @param components a list of artifact id
131      * @return the list of dependency
132      */
133     private static List<Dependency> getDependenciesList( String[] components )
134     {
135         List<Dependency> list = new ArrayList<>( );
136         for ( String name : components )
137         {
138             Dependency dependency = new Dependency( );
139             dependency.setArtifactId( name );
140             dependency.setGroupId( "fr.paris.lutece.plugins" );
141             dependency.setComponentType( TYPE_LUTECE_PLUGIN );
142             MavenRepoService.instance( ).setReleaseVersion( dependency );
143             list.add( dependency );
144         }
145         return list;
146 
147     }
148 
149     /**
150      * Gets the dependencies list from a component list
151      * 
152      * @param components The array of components
153      * @return The list
154      */
155     private static List<Dependency> getDependenciesList( List<? extends AbstractComponent> listComponents )
156     {
157         List<Dependency> list = new ArrayList<>( );
158 
159         for ( AbstractComponent component : listComponents )
160         {
161             Dependency dependency = getDependency( component );
162             list.add( dependency );
163         }
164 
165         Collections.sort( list );
166 
167         return list;
168     }
169 
170     /**
171      * Get a dependency from an abstractComponent
172      * 
173      * @param component The component
174      * @return a dependency
175      */
176     private static Dependency getDependency( AbstractComponent component )
177     {
178         Dependency dep = new Dependency( );
179         dep.setArtifactId( component.getArtifactId( ) );
180         dep.setVersion( component.getVersion( ) );
181         dep.setComponentType( provideDependencyType( component.getComponentType( ) ) );
182         if ( component.getComponentType( ).equals( LUTECE_CORE ) )
183         {
184             dep.setGroupId( GROUP_ID_CORE );
185         }
186         else
187         {
188             dep.setGroupId( GROUP_ID_PLUGINS );
189         }
190         return dep;
191     }
192 
193     /**
194      * Returns the dependencies formatted with XML format
195      * 
196      * @param list The dependencies list
197      * @return The output
198      */
199     private static String getDependenciesXML( List<Dependency> list )
200     {
201         StringBuffer sb = new StringBuffer( );
202 
203         for ( Dependency dependency : list )
204         {
205             XmlUtil.beginElement( sb, TAG_DEPENDENCY );
206             XmlUtil.addElement( sb.append( INDENT ), TAG_GROUP_ID, dependency.getGroupId( ) );
207             XmlUtil.addElement( sb.append( INDENT ), TAG_ARTIFACT_ID, dependency.getArtifactId( ) );
208             XmlUtil.addElement( sb.append( INDENT ), TAG_VERSION, dependency.getVersion( ) );
209             XmlUtil.addElement( sb.append( INDENT ), TAG_TYPE, dependency.getComponentType( ) );
210             XmlUtil.endElement( sb, TAG_DEPENDENCY );
211         }
212 
213         return sb.toString( );
214     }
215 
216     /**
217      * Returns the dependencies formatted with Text format
218      * 
219      * @param list The dependencies list
220      * @return The output
221      */
222     private static String getDependenciesText( List<Dependency> list )
223     {
224         StringBuilder sb = new StringBuilder( );
225 
226         for ( Dependency dependency : list )
227         {
228             sb.append( dependency.getArtifactId( ) ).append( "\t" ).append( dependency.getVersion( ) ).append( "\n" );
229         }
230 
231         return sb.toString( );
232     }
233 
234     /**
235      * Get the site POM from a list of dependency
236      * 
237      * @param list          the dependency list
238      * @param strArtifactId the artifact id
239      * @return the site POM. the site name
240      */
241     private static String getDependenciesPOM( List<Dependency> list, Site site )
242     {
243         // Check if core is in given dependency list
244         provideCoreDependency( list );
245 
246         if ( site == null )
247         {
248             site = new Site( );
249         }
250         Map<String, Object> model = new HashMap<>( );
251         model.put( MARK_SITE, site );
252         model.put( MARK_DEPENDENCIES, list );
253         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_POM, LocaleService.getDefault( ), model );
254 
255         return template.getHtml( );
256     }
257 
258     /**
259      * Provide core dependency
260      * 
261      * @param list the list of dependencies
262      */
263     private static void provideCoreDependency( List<Dependency> list )
264     {
265         for ( Dependency dependency : list )
266         {
267             if ( dependency.getArtifactId( ).equals( LUTECE_CORE ) )
268             {
269                 return;
270             }
271         }
272         String strLatestCoreVersion = MavenRepoService.instance( ).getLatestCoreVersion( );
273         Dependency coreDependency = new Dependency( );
274         coreDependency.setArtifactId( LUTECE_CORE );
275         coreDependency.setComponentType( TYPE_LUTECE_CORE );
276         coreDependency.setGroupId( GROUP_ID_CORE );
277         coreDependency.setVersion( strLatestCoreVersion );
278         list.add( 0, coreDependency );
279     }
280 
281     /**
282      * provide Dependency Type
283      *
284      * @param componentType
285      * @return the type
286      */
287     public static String provideDependencyType( String componentType )
288     {
289     	if ( componentType.contains("library") )
290     	{
291     		return TYPE_LUTECE_LIBRARY;
292     	}
293     	else if ( componentType.contains("site") )
294     	{
295     		return TYPE_LUTECE_SITE;
296     	}
297     	else if ( componentType.contains("core") )
298     	{
299     		return TYPE_LUTECE_CORE;
300     	}
301     	else
302     	{
303     		return TYPE_LUTECE_PLUGIN;
304     	}
305     }
306 
307 }