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.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   * This class provides a simple implementation of an XPage
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      // RCI color mark
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      * Returns the content of the page lutecetools.
104      *
105      * @param request
106      *            The HTTP request
107      * @return The view
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         // FIXME
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      * Returns the content of the page lutecetools.
175      *
176      * @param request
177      *            The HTTP request
178      * @return The view
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      * Refresh action processing
224      *
225      * @param request
226      *            The HTTP request
227      * @return The page
228      */
229     @Action( ACTION_REFRESH )
230     public XPage refresh( HttpServletRequest request )
231     {
232         return redirect( request, VIEW_HOME, getViewParameters( request ) );
233     }
234 
235     /**
236      * Clear Cache action processing
237      *
238      * @param request
239      *            The HTTP request
240      * @return The page
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      * Filter a list of component to keep only github hosted ones
253      *
254      * @param listComponents
255      *            The list to filter
256      * @return The filtered list
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      * Get the parameters send with the action to resend to the view
276      *
277      * @param request
278      *            The HTTP Request
279      * @return The parameters
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 }