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.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
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
77
78 private DependenciesService( )
79 {
80 }
81
82
83
84
85
86
87
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
100
101
102
103
104
105
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
129
130
131
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
151
152
153
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
172
173
174
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
195
196
197
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
218
219
220
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
236
237
238
239
240
241 private static String getDependenciesPOM( List<Dependency> list, Site site )
242 {
243
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
260
261
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
283
284
285
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 }