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.web;
35
36 import java.util.ArrayList;
37 import java.util.Date;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.concurrent.ConcurrentHashMap;
41
42 import javax.servlet.http.HttpServletRequest;
43
44 import fr.paris.lutece.plugins.lutecetools.business.Component;
45 import fr.paris.lutece.plugins.lutecetools.service.AbstractGitPlatformService;
46 import fr.paris.lutece.plugins.lutecetools.service.ComponentService;
47 import fr.paris.lutece.plugins.lutecetools.service.ComponentsInfos;
48 import fr.paris.lutece.plugins.lutecetools.service.MavenRepoService;
49 import fr.paris.lutece.plugins.lutecetools.service.SonarService;
50 import fr.paris.lutece.portal.service.util.AppPropertiesService;
51 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
52 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
53 import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
54 import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
55 import fr.paris.lutece.portal.web.xpages.XPage;
56
57
58
59
60 @Controller( xpageName = "components", pageTitleI18nKey = "lutecetools.xpage.components.pageTitle", pagePathI18nKey = "lutecetools.xpage.components.pagePath" )
61 public class ComponentListApp extends MVCApplication
62 {
63
64 private static final String TEMPLATE_XPAGE = "/skin/plugins/lutecetools/components.html";
65 private static final String TEMPLATE_XPAGE_DETAIL = "/skin/plugins/lutecetools/component.html";
66
67 private static final String MARK_COMPONENTS_LIST = "components_list";
68 private static final String MARK_COMPONENT = "component";
69 private static final String MARK_GITHUB_FILTER = "github_filter";
70 private static final String MARK_GITLAB_FILTER = "gitlab_filter";
71 private static final String MARK_DISPLAY_CORE_VERSIONS = "core_versions";
72 private static final String MARK_LOGS = "logs";
73 private static final String MARK_INTEGER_SUCCESS = "rci_color_success";
74 private static final String MARK_INTEGER_WARNING = "rci_color_warning";
75 private static final String MARK_TOTAL_LINES = "total_lines";
76 private static final String MARK_TOTAL_PRS = "total_prs";
77 private static final String MARK_OLDEST_PR = "oldest_pr";
78
79 private static final String VIEW_HOME = "home";
80 private static final String VIEW_DETAIL = "detail";
81
82 private static final String ACTION_REFRESH = "refresh";
83 private static final String ACTION_CLEAR_CACHE = "clearCache";
84
85 private static final String PARAMETER_GITHUB = "github";
86 private static final String PARAMETER_GITLAB = "gitlab";
87 private static final String PARAMETER_CORE_VERSIONS = "core";
88 private static final String PARAMETER_ARTIFACT_ID = "artifact_id";
89 private static final String PARAMETER_REFRESH = "refresh";
90
91 private static final String VALUE_ON = "on";
92 private static final String PLATFORM_GITHUB = "github";
93 private static final String PLATFORM_GITLAB = "gitlab";
94 private static final long serialVersionUID = 1L;
95
96
97 private static final String PROPERTY_SONAR_RCI_SUCCESS = "lutecetools.sonar.mark.rci.success";
98 private static final String SONAR_RCI_SUCCESS = AppPropertiesService.getProperty( PROPERTY_SONAR_RCI_SUCCESS );
99 private static final String PROPERTY_SONAR_RCI_WARNING = "lutecetools.sonar.mark.rci.warning";
100 private static final String SONAR_RCI_WARNING = AppPropertiesService.getProperty( PROPERTY_SONAR_RCI_WARNING );
101
102
103
104
105
106
107
108
109 @View( value = VIEW_HOME, defaultView = true )
110 public XPage viewHome( HttpServletRequest request )
111 {
112 List<String> listFilterPlatform = new ArrayList<>( );
113 String strGitHubFilter = request.getParameter( PARAMETER_GITHUB );
114 String strGitLabFilter = request.getParameter( PARAMETER_GITLAB );
115 String strDisplayCoreVersions = request.getParameter( PARAMETER_CORE_VERSIONS );
116 boolean bDisplayCoreVersions = ( strDisplayCoreVersions != null ) && strDisplayCoreVersions.equals( VALUE_ON );
117 if ( VALUE_ON.equals( strGitHubFilter ) )
118 {
119 listFilterPlatform.add( PLATFORM_GITHUB );
120 }
121 if ( VALUE_ON.equals( strGitLabFilter ) )
122 {
123 listFilterPlatform.add( PLATFORM_GITLAB );
124 }
125 Integer nTotal = 0;
126 int nTotalPRs = 0;
127 long oldestPR = Long.MAX_VALUE;
128
129 Map<String, Object> model = getModel( );
130
131 ComponentsInfos ciInfos = MavenRepoService.instance( ).getComponents( );
132
133 if ( !listFilterPlatform.isEmpty( ) )
134 {
135 ciInfos.setListComponents( filterPlatform( ciInfos.getListComponents( ), listFilterPlatform ) );
136 }
137
138
139 for ( Component c : ciInfos.getListComponents( ) )
140 {
141 if ( c.get( SonarService.SONAR_NB_LINES ) != null )
142 {
143 nTotal += Integer.parseInt( c.get( SonarService.SONAR_NB_LINES ).replace( ",", "" ) );
144 }
145 Integer iPullRequestCount = c.getInt( AbstractGitPlatformService.PULL_REQUEST_COUNT );
146 if ( iPullRequestCount != null && iPullRequestCount > 0 )
147 {
148 nTotalPRs = nTotalPRs + c.getInt( AbstractGitPlatformService.PULL_REQUEST_COUNT );
149 if ( c.getLong( AbstractGitPlatformService.OLDEST_PULL_REQUEST ) < oldestPR )
150 {
151 oldestPR = c.getLong( AbstractGitPlatformService.OLDEST_PULL_REQUEST );
152 }
153 }
154 }
155
156 model.put( MARK_INTEGER_SUCCESS, SONAR_RCI_SUCCESS );
157 model.put( MARK_INTEGER_WARNING, SONAR_RCI_WARNING );
158 model.put( MARK_COMPONENTS_LIST, ciInfos );
159 model.put( MARK_TOTAL_LINES, nTotal );
160 if ( nTotalPRs > 0 )
161 {
162 model.put( MARK_TOTAL_PRS, nTotalPRs );
163 model.put( MARK_OLDEST_PR, new Date( oldestPR ) );
164 }
165 model.put( MARK_GITHUB_FILTER, listFilterPlatform.contains( PLATFORM_GITHUB ) );
166 model.put( MARK_GITLAB_FILTER, listFilterPlatform.contains( PLATFORM_GITLAB ) );
167 model.put( MARK_DISPLAY_CORE_VERSIONS, bDisplayCoreVersions );
168 model.put( MARK_LOGS, MavenRepoService.instance( ).getLogs( ) );
169
170 return getXPage( TEMPLATE_XPAGE, request.getLocale( ), model );
171 }
172
173
174
175
176
177
178
179
180 @View( value = VIEW_DETAIL )
181 public XPage viewDetail( HttpServletRequest request )
182 {
183
184 String strArtifactId = request.getParameter( PARAMETER_ARTIFACT_ID );
185 boolean refresh = ( request.getParameter( PARAMETER_REFRESH ) != null );
186 Map<String, Object> model = getModel( );
187
188 long nTotal = 0;
189 long nTotalPRs = 0;
190 long oldestPR = 0;
191 Component c = MavenRepoService.instance( ).getComponent( strArtifactId, true, refresh );
192
193 if ( c.get( SonarService.SONAR_NB_LINES ) != null )
194 {
195 nTotal += Integer.parseInt( c.get( SonarService.SONAR_NB_LINES ).replace( ",", "" ) );
196 }
197 Integer iPullRequestCount = c.getInt( AbstractGitPlatformService.PULL_REQUEST_COUNT );
198 if ( iPullRequestCount != null && iPullRequestCount > 0 )
199 {
200 nTotalPRs = nTotalPRs + c.getInt( AbstractGitPlatformService.PULL_REQUEST_COUNT );
201 if ( c.getLong( AbstractGitPlatformService.OLDEST_PULL_REQUEST ) < oldestPR )
202 {
203 oldestPR = c.getLong( AbstractGitPlatformService.OLDEST_PULL_REQUEST );
204 }
205 }
206
207 model.put( MARK_INTEGER_SUCCESS, SONAR_RCI_SUCCESS );
208 model.put( MARK_INTEGER_WARNING, SONAR_RCI_WARNING );
209 model.put( MARK_COMPONENT, c );
210 model.put( MARK_TOTAL_LINES, nTotal );
211 if ( nTotalPRs > 0 )
212 {
213 model.put( MARK_TOTAL_PRS, nTotalPRs );
214 model.put( MARK_OLDEST_PR, new Date( oldestPR ) );
215 }
216
217 model.put( MARK_LOGS, MavenRepoService.instance( ).getLogs( ) );
218
219 return getXPage( TEMPLATE_XPAGE_DETAIL, request.getLocale( ), model );
220 }
221
222
223
224
225
226
227
228
229 @Action( ACTION_REFRESH )
230 public XPage refresh( HttpServletRequest request )
231 {
232 return redirect( request, VIEW_HOME, getViewParameters( request ) );
233 }
234
235
236
237
238
239
240
241
242 @Action( ACTION_CLEAR_CACHE )
243 public XPage clearCache( HttpServletRequest request )
244 {
245 ComponentService.clearCache( );
246 MavenRepoService.clearLogs( );
247
248 return redirect( request, VIEW_HOME, getViewParameters( request ) );
249 }
250
251
252
253
254
255
256
257
258 private List<Component> filterPlatform( List<Component> listComponents, List<String> listPlatform )
259 {
260 List<Component> list = new ArrayList<>( );
261
262 for ( Component c : listComponents )
263 {
264 String strPlatform = c.get( AbstractGitPlatformService.GIT_PLATFORM );
265 if ( listPlatform.contains( strPlatform ) )
266 {
267 list.add( c );
268 }
269 }
270
271 return list;
272 }
273
274
275
276
277
278
279
280
281 private Map<String, String> getViewParameters( HttpServletRequest request )
282 {
283 Map<String, String> mapParameters = new ConcurrentHashMap<>( );
284 String strGitHubFilter = request.getParameter( PARAMETER_GITHUB );
285 String strGitLabFilter = request.getParameter( PARAMETER_GITLAB );
286 String strDisplayCoreVersions = request.getParameter( PARAMETER_CORE_VERSIONS );
287
288 if ( ( strGitHubFilter != null ) && ( strGitHubFilter.equals( VALUE_ON ) ) )
289 {
290 mapParameters.put( PARAMETER_GITHUB, VALUE_ON );
291 }
292 if ( ( strGitLabFilter != null ) && ( strGitLabFilter.equals( VALUE_ON ) ) )
293 {
294 mapParameters.put( PARAMETER_GITLAB, VALUE_ON );
295 }
296
297 if ( ( strDisplayCoreVersions != null ) && strDisplayCoreVersions.equals( VALUE_ON ) )
298 {
299 mapParameters.put( PARAMETER_CORE_VERSIONS, VALUE_ON );
300 }
301
302 return mapParameters;
303 }
304
305 }