View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.releaser.service;
35  
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.Map;
45  import java.util.concurrent.ExecutionException;
46  import java.util.concurrent.ExecutorService;
47  import java.util.concurrent.Executors;
48  import java.util.concurrent.Future;
49  
50  import javax.servlet.http.HttpServletRequest;
51  import javax.xml.bind.JAXBException;
52  
53  import fr.paris.lutece.plugins.releaser.business.Component;
54  import fr.paris.lutece.plugins.releaser.business.Dependency;
55  import fr.paris.lutece.plugins.releaser.business.ReleaserUser;
56  import fr.paris.lutece.plugins.releaser.business.ReleaserUser.Credential;
57  import fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model;
58  import fr.paris.lutece.plugins.releaser.business.Site;
59  import fr.paris.lutece.plugins.releaser.business.SiteHome;
60  import fr.paris.lutece.plugins.releaser.business.WorkflowReleaseContext;
61  import fr.paris.lutece.plugins.releaser.util.CVSFactoryService;
62  import fr.paris.lutece.plugins.releaser.util.ConstanteUtils;
63  import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
64  import fr.paris.lutece.plugins.releaser.util.github.GitUtils;
65  import fr.paris.lutece.plugins.releaser.util.pom.PomParser;
66  import fr.paris.lutece.plugins.releaser.util.pom.PomUpdater;
67  import fr.paris.lutece.plugins.releaser.util.version.Version;
68  import fr.paris.lutece.plugins.releaser.util.version.VersionParsingException;
69  import fr.paris.lutece.portal.business.user.AdminUser;
70  import fr.paris.lutece.portal.service.datastore.DatastoreService;
71  import fr.paris.lutece.portal.service.i18n.I18nService;
72  import fr.paris.lutece.portal.service.plugin.Plugin;
73  import fr.paris.lutece.portal.service.rbac.RBACService;
74  import fr.paris.lutece.portal.service.util.AppException;
75  import fr.paris.lutece.portal.service.util.AppLogService;
76  import fr.paris.lutece.portal.service.util.AppPropertiesService;
77  import fr.paris.lutece.util.httpaccess.HttpAccessException;
78  
79  // TODO: Auto-generated Javadoc
80  /**
81   * SiteService.
82   */
83  public class SiteService
84  {
85  
86      /** The Constant NB_POOL_REMOTE_INFORMATION. */
87      private static final int NB_POOL_REMOTE_INFORMATION = 60;
88  
89      /** The Constant MESSAGE_AVOID_SNAPSHOT. */
90      private static final String MESSAGE_AVOID_SNAPSHOT = "releaser.message.avoidSnapshot";
91  
92      /** The Constant MESSAGE_UPGRADE_SELECTED. */
93      private static final String MESSAGE_UPGRADE_SELECTED = "releaser.message.upgradeSelected";
94  
95      /** The Constant MESSAGE_TO_BE_RELEASED. */
96      private static final String MESSAGE_TO_BE_RELEASED = "releaser.message.toBeReleased";
97  
98      /** The Constant MESSAGE_MORE_RECENT_VERSION_AVAILABLE. */
99      private static final String MESSAGE_MORE_RECENT_VERSION_AVAILABLE = "releaser.message.moreRecentVersionAvailable";
100 
101     /** The Constant MESSAGE_AN_RELEASE_VERSION_ALREADY_EXIST. */
102     private static final String MESSAGE_AN_RELEASE_VERSION_ALREADY_EXIST = "releaser.message.releleaseVersionAlreadyExist";
103 
104     /** The Constant MESSAGE_WRONG_POM_PARENT_SITE_VERSION. */
105     private static final String MESSAGE_WRONG_POM_PARENT_SITE_VERSION = "releaser.message.wrongPomParentSiteVersion";
106 
107     /** The Constant MESSAGE_NOT_DEFAULT_RELEASE_BRANCH. */
108     private static final String MESSAGE_NOT_DEFAULT_RELEASE_BRANCH_FROM = "releaser.message.notDefaultReleaseBranchFrom";
109 
110     /**
111      * Load a site from its id.
112      *
113      * @param nSiteId
114      *            The site id
115      * @param request
116      *            the request
117      * @param locale
118      *            the locale
119      * @return A site object
120      */
121     public static Site getSite( int nSiteId, HttpServletRequest request, Locale locale )
122     {
123         Site site = SiteHome.findByPrimaryKey( nSiteId );
124         String strPom = null;
125         ReleaserUser user = ReleaserUtils.getReleaserUser( request, locale );
126 
127         if ( user != null )
128         {
129             Credential credential = user.getCredential( site.getRepoType( ) );
130 
131             strPom = CVSFactoryService.getService( site.getRepoType( ) ).fetchPom( site, credential.getLogin( ), credential.getPassword( ) );
132 
133             if ( strPom != null )
134             {
135                 PomParsereleaser/util/pom/PomParser.html#PomParser">PomParser parser = new PomParser( );
136                 parser.parse( site, strPom );
137                 initSite( site, request, locale );
138             }
139         }
140         else
141         {
142             throw new AppException( ConstanteUtils.ERROR_TYPE_AUTHENTICATION_ERROR );
143         }
144 
145         return site;
146     }
147 
148     /**
149      * Inits the site.
150      *
151      * @param site
152      *            the site
153      * @param request
154      *            the request
155      * @param locale
156      *            the locale
157      */
158     private static void initSite( Site site, HttpServletRequest request, Locale locale )
159     {
160 
161         ReleaserUser user = ReleaserUtils.getReleaserUser( request, locale );
162         Credential credential = user.getCredential( site.getRepoType( ) );
163 
164         // Find last release in the repository
165         String strLastReleaseVersion = CVSFactoryService.getService( site.getRepoType( ) ).getLastRelease( site, credential.getLogin( ),
166                 credential.getPassword( ) );
167         site.setLastReleaseVersion( strLastReleaseVersion );
168 
169         // To find next releases
170 
171         String strOriginVersion = getOriginVersion( strLastReleaseVersion, site.getVersion( ) );
172 
173         site.setNextReleaseVersion( Version.getReleaseVersion( strOriginVersion ) );
174         site.setNextSnapshotVersion( Version.getNextSnapshotVersion( strOriginVersion ) );
175         site.setTargetVersions( Version.getNextReleaseVersions( strOriginVersion ) );
176         site.setBranchReleaseFrom( GitUtils.DEFAULT_RELEASE_BRANCH );
177 
178         initComponents( site );
179     }
180 
181     /**
182      * Define which version between last released or current snapshot should be the origin for next release versions. Ex of cases :<br>
183      * last release : 3.2.1 current : 4.0.0-SNAPSHOT -- current <br>
184      * last release : 3.2.1 current : 3.2.2-SNAPSHOT -- last or current <br>
185      * last release : missing current : 1.0.0-SNAPSHOT -- current <br>
186      * last release : 3.2.1-RC-02 current : 3.2.1-SNAPSHOT -- last <br>
187      * 
188      * @param strLastRelease
189      *            The last release
190      * @param strCurrentVersion
191      *            The current release
192      * @return The origin version
193      */
194     public static String getOriginVersion( String strLastRelease, String strCurrentVersion )
195     {
196         String strOriginVersion = strCurrentVersion;
197         if ( ( strLastRelease != null ) && Version.isCandidate( strLastRelease ) )
198         {
199             strOriginVersion = strLastRelease;
200         }
201         return strOriginVersion;
202     }
203 
204     /**
205      * Initialize the component list for a given site.
206      *
207      * @param site
208      *            The site
209      */
210     private static void initComponents( Site site )
211     {
212         for ( Dependency dependency : site.getCurrentDependencies( ) )
213         {
214             Componentaser/business/Component.html#Component">Component component = new Component( );
215 
216             component.setIsProject( isProjectComponent( site, dependency.getArtifactId( ) ) );
217             component.setArtifactId( dependency.getArtifactId( ) );
218             component.setGroupId( dependency.getGroupId( ) );
219             component.setType( dependency.getType( ) );
220             String currentVersion = dependency.getVersion( ).replace( "[", "" ).replace( "]", "" );
221             component.setCurrentVersion( currentVersion );
222             site.addComponent( component );
223         }
224 
225         ExecutorService executor = Executors.newFixedThreadPool( NB_POOL_REMOTE_INFORMATION );
226 
227         List<Future> futures = new ArrayList<Future>( site.getCurrentDependencies( ).size( ) );
228 
229         for ( Component component : site.getComponents( ) )
230         {
231             futures.add( executor.submit( new GetRemoteInformationsTask( component ) ) );
232         }
233 
234         // wait all futures stop before continue
235         for ( Future future : futures )
236         {
237 
238             try
239             {
240                 future.get( );
241             }
242             catch( InterruptedException e )
243             {
244                 AppLogService.error( e );
245             }
246             catch( ExecutionException e )
247             {
248                 // TODO Auto-generated catch block
249                 AppLogService.error( e );
250             }
251 
252         }
253 
254         executor.shutdown( );
255 
256         for ( Component component : site.getComponents( ) )
257         {
258             ComponentService.getService( ).updateRemoteInformations( component );
259             defineTargetVersion( component );
260             defineNextSnapshotVersion( component );
261             component.setName( ReleaserUtils.getComponentName( component.getScmDeveloperConnection( ), component.getArtifactId( ) ) );
262             component.setBranchReleaseFrom( GitUtils.DEFAULT_RELEASE_BRANCH );
263         }
264 
265     }
266 
267     /**
268      * Define the target version for a given component : <br>
269      * - current version for non project component <br>
270      * - nex release for project component.
271      *
272      * @param component
273      *            The component
274      */
275     private static void defineTargetVersion( Component component )
276     {
277         if ( component.isProject( ) && component.isSnapshotVersion( ) )
278         {
279             if ( component.getLastAvailableVersion( ) != null && !component.getCurrentVersion( ).equals( component.getLastAvailableSnapshotVersion( ) )
280                     || component.isTheme( ) )
281             {
282                 component.setTargetVersion( component.getLastAvailableVersion( ) );
283             }
284             else
285             {
286                 component.setTargetVersions( Version.getNextReleaseVersions( component.getCurrentVersion( ) ) );
287                 String strTargetVersion = Version.getReleaseVersion( component.getCurrentVersion( ) );
288                 component.setTargetVersion( strTargetVersion );
289             }
290         }
291         else
292         {
293             component.setTargetVersion( component.getCurrentVersion( ) );
294         }
295     }
296 
297     /**
298      * Define the next snapshot version for a given component.
299      *
300      * @param component
301      *            The component
302      */
303     private static void defineNextSnapshotVersion( Component component )
304     {
305         String strNextSnapshotVersion = Version.NOT_AVAILABLE;
306         if ( !component.getCurrentVersion( ).equals( component.getLastAvailableSnapshotVersion( ) ) || component.isTheme( ) )
307         {
308             component.setNextSnapshotVersion( component.getLastAvailableSnapshotVersion( ) );
309         }
310         else
311         {
312             try
313             {
314                 Version version = Version.parse( component.getTargetVersion( ) );
315                 boolean bSnapshot = true;
316                 strNextSnapshotVersion = version.nextPatch( bSnapshot ).toString( );
317             }
318             catch( VersionParsingException ex )
319             {
320                 AppLogService.error( "Error parsing version for component " + component.getArtifactId( ) + " : " + ex.getMessage( ), ex );
321 
322             }
323             component.setNextSnapshotVersion( strNextSnapshotVersion );
324         }
325 
326     }
327 
328     /**
329      * Checks if is project component.
330      *
331      * @param site
332      *            the site
333      * @param strArtifactId
334      *            the str artifact id
335      * @return true, if is project component
336      */
337     private static boolean isProjectComponent( Site site, String strArtifactId )
338     {
339         return new Boolean( DatastoreService.getDataValue( getComponetIsProjectDataKey( site, strArtifactId ), Boolean.FALSE.toString( ) ) );
340     }
341 
342     /**
343      * Update component as project status.
344      *
345      * @param site
346      *            the site
347      * @param strArtifactId
348      *            the str artifact id
349      * @param bIsProject
350      *            the b is project
351      */
352     public static void updateComponentAsProjectStatus( Site site, String strArtifactId, Boolean bIsProject )
353     {
354         DatastoreService.setDataValue( getComponetIsProjectDataKey( site, strArtifactId ), bIsProject.toString( ) );
355 
356     }
357 
358     /**
359      * Removes the component as project by site.
360      *
361      * @param nIdSite
362      *            the n id site
363      */
364     public static void removeComponentAsProjectBySite( int nIdSite )
365     {
366         DatastoreService.removeDataByPrefix( getPrefixIsProjectDataKey( nIdSite ) );
367 
368     }
369 
370     /**
371      * Gets the componet is project data key.
372      *
373      * @param site
374      *            the site
375      * @param strArtifactId
376      *            the str artifact id
377      * @return the componet is project data key
378      */
379     private static String getComponetIsProjectDataKey( Site site, String strArtifactId )
380     {
381 
382         return getPrefixIsProjectDataKey( site.getId( ) ) + strArtifactId;
383 
384     }
385 
386     /**
387      * Gets the prefix is project data key.
388      *
389      * @param nIdSite
390      *            the n id site
391      * @return the prefix is project data key
392      */
393     private static String getPrefixIsProjectDataKey( int nIdSite )
394     {
395 
396         return ConstanteUtils.CONSTANTE_COMPONENT_PROJECT_PREFIX + "_" + nIdSite + "_";
397 
398     }
399 
400     /**
401      * Build release comments for a given site.
402      *
403      * @param site
404      *            The site
405      * @param locale
406      *            The locale to use for comments
407      */
408     public static void buildComments( Site site, Locale locale )
409     {
410         site.resetComments( );
411         buildReleaseComments( site, locale );
412 
413         for ( Component component : site.getComponents( ) )
414         {
415             component.resetComments( );
416             buildReleaseComments( component, locale );
417         }
418     }
419 
420     /**
421      * Build release comments for a given component.
422      *
423      * @param component
424      *            The component
425      * @param locale
426      *            The locale to use for comments
427      */
428     private static void buildReleaseComments( Component component, Locale locale )
429     {
430 
431         if ( !component.getBranchReleaseFrom( ).equals( GitUtils.DEFAULT_RELEASE_BRANCH ) )
432         {
433             String strComment = I18nService.getLocalizedString( MESSAGE_NOT_DEFAULT_RELEASE_BRANCH_FROM, locale );
434             component.addReleaseComment( strComment );
435 
436             return;
437         }
438 
439         if ( !component.isProject( ) )
440         {
441             if ( Version.isSnapshot( component.getTargetVersion( ) ) )
442             {
443                 String strComment = I18nService.getLocalizedString( MESSAGE_AVOID_SNAPSHOT, locale );
444                 component.addReleaseComment( strComment );
445             }
446             else
447                 if ( component.getLastAvailableVersion( ) != null && component.getTargetVersion( ) != null && component.getLastAvailableVersion( ) != null
448                         && ReleaserUtils.compareVersion( component.getTargetVersion( ), component.getLastAvailableVersion( ) ) < 0 )
449                 {
450                     String [ ] arguments = {
451                             component.getLastAvailableVersion( )
452                     };
453                     String strComment = I18nService.getLocalizedString( MESSAGE_MORE_RECENT_VERSION_AVAILABLE, arguments, locale );
454 
455                     component.addReleaseComment( strComment );
456                 }
457         }
458         else
459         {
460             if ( component.isSnapshotVersion( ) )
461             {
462                 if ( ReleaserUtils.compareVersion( component.getCurrentVersion( ), component.getLastAvailableSnapshotVersion( ) ) < 0 )
463                 {
464 
465                     String [ ] arguments = {
466                             component.getLastAvailableVersion( )
467                     };
468                     String strComment = I18nService.getLocalizedString( MESSAGE_UPGRADE_SELECTED, arguments, locale );
469                     component.addReleaseComment( strComment );
470                 }
471                 else
472                     if ( !component.shouldBeReleased( ) && !component.isDowngrade( ) )
473                     {
474 
475                         String [ ] arguments = {
476                                 component.getLastAvailableVersion( )
477                         };
478                         String strComment = I18nService.getLocalizedString( MESSAGE_AN_RELEASE_VERSION_ALREADY_EXIST, arguments, locale );
479                         component.addReleaseComment( strComment );
480                     }
481 
482                     else
483                         if ( component.shouldBeReleased( ) )
484                         {
485 
486                             String strComment = I18nService.getLocalizedString( MESSAGE_TO_BE_RELEASED, locale );
487                             component.addReleaseComment( strComment );
488                         }
489             }
490             else
491                 if ( ReleaserUtils.compareVersion( component.getCurrentVersion( ), component.getLastAvailableVersion( ) ) < 0 )
492                 {
493                     String [ ] arguments = {
494                             component.getLastAvailableVersion( )
495                     };
496                     String strComment = I18nService.getLocalizedString( MESSAGE_MORE_RECENT_VERSION_AVAILABLE, arguments, locale );
497 
498                     component.addReleaseComment( strComment );
499                 }
500         }
501     }
502 
503     /**
504      * Upgrade component.
505      *
506      * @param site
507      *            the site
508      * @param strArtifactId
509      *            the str artifact id
510      */
511     public static void upgradeComponent( Site site, String strArtifactId )
512     {
513         for ( Component component : site.getComponents( ) )
514         {
515             if ( component.getArtifactId( ).equals( strArtifactId ) )
516             {
517                 component.setTargetVersion( component.getLastAvailableVersion( ) );
518                 component.setUpgrade( true );
519             }
520         }
521     }
522 
523     /**
524      * Cancel upgrade component.
525      *
526      * @param site
527      *            the site
528      * @param strArtifactId
529      *            the str artifact id
530      */
531     public static void cancelUpgradeComponent( Site site, String strArtifactId )
532     {
533         for ( Component component : site.getComponents( ) )
534         {
535             if ( component.getArtifactId( ).equals( strArtifactId ) )
536             {
537 
538                 component.setTargetVersion( component.getCurrentVersion( ) );
539                 component.setUpgrade( false );
540             }
541         }
542     }
543 
544     /**
545      * Downgrade component.
546      *
547      * @param site
548      *            the site
549      * @param strArtifactId
550      *            the str artifact id
551      */
552     public static void downgradeComponent( Site site, String strArtifactId )
553     {
554         for ( Component component : site.getComponents( ) )
555         {
556             if ( component.getArtifactId( ).equals( strArtifactId ) && component.isSnapshotVersion( ) )
557             {
558                 component.setTargetVersion( component.getLastAvailableVersion( ) );
559                 component.setNextSnapshotVersion( component.getLastAvailableSnapshotVersion( ) );
560                 component.setDowngrade( true );
561             }
562         }
563     }
564 
565     /**
566      * Cancel downgrade component.
567      *
568      * @param site
569      *            the site
570      * @param strArtifactId
571      *            the str artifact id
572      */
573     public static void cancelDowngradeComponent( Site site, String strArtifactId )
574     {
575         for ( Component component : site.getComponents( ) )
576         {
577             if ( component.getArtifactId( ).equals( strArtifactId ) && component.isSnapshotVersion( ) )
578             {
579                 component.setDowngrade( false );
580                 defineTargetVersion( component );
581                 defineNextSnapshotVersion( component );
582             }
583         }
584     }
585 
586     /**
587      * Release component.
588      *
589      * @param site
590      *            the site
591      * @param strArtifactId
592      *            the str artifact id
593      * @param locale
594      *            the locale
595      * @param user
596      *            the user
597      * @param request
598      *            the request
599      * @return the int
600      */
601     public static int releaseComponent( Site site, String strArtifactId, Locale locale, AdminUser user, HttpServletRequest request )
602     {
603         for ( Component component : site.getComponents( ) )
604         {
605             if ( component.getArtifactId( ).equals( strArtifactId ) && component.shouldBeReleased( ) )
606             {
607                 // Release component
608                 return ComponentService.getService( ).release( component, locale, user, request );
609 
610             }
611         }
612         return ConstanteUtils.CONSTANTE_ID_NULL;
613     }
614 
615     /**
616      * Release site.
617      *
618      * @param site
619      *            the site
620      * @param locale
621      *            the locale
622      * @param user
623      *            the user
624      * @param request
625      *            the request
626      * @return the map
627      */
628     public static Map<String, Integer> releaseSite( Site site, Locale locale, AdminUser user, HttpServletRequest request )
629     {
630         Map<String, Integer> mapResultContext = new HashMap<String, Integer>( );
631 
632         Integer nIdWfContext;
633         // Release all snapshot compnent
634         for ( Component component : site.getComponents( ) )
635         {
636             if ( component.isProject( ) && component.shouldBeReleased( ) && !component.isTheme( ) )
637             {
638                 component.setErrorLastRelease( false );
639                 nIdWfContext = ComponentService.getService( ).release( component, locale, user, request );
640                 mapResultContext.put( component.getArtifactId( ), nIdWfContext );
641 
642             }
643         }
644 
645         WorkflowReleaseContext context = new WorkflowReleaseContext( );
646         context.setSite( site );
647         context.setReleaserUser( ReleaserUtils.getReleaserUser( request, locale ) );
648 
649         int nIdWorkflow = WorkflowReleaseContextService.getService( ).getIdWorkflow( context );
650         WorkflowReleaseContextService.getService( ).addWorkflowReleaseContext( context );
651         // start
652         WorkflowReleaseContextService.getService( ).startWorkflowReleaseContext( context, nIdWorkflow, locale, request, user );
653         // Add wf site context
654         mapResultContext.put( site.getArtifactId( ), context.getId( ) );
655 
656         return mapResultContext;
657 
658     }
659 
660     /**
661      * Add or Remove a component from the project's components list.
662      *
663      * @param site
664      *            The site
665      * @param strArtifactId
666      *            The component artifact id
667      */
668     public static void toggleProjectComponent( Site site, String strArtifactId )
669     {
670         for ( Component component : site.getComponents( ) )
671         {
672             if ( component.getArtifactId( ).equals( strArtifactId ) )
673             {
674                 component.setIsProject( !component.isProject( ) );
675                 updateComponentAsProjectStatus( site, strArtifactId, component.isProject( ) );
676 
677                 if ( component.isProject( ) )
678                 {
679                     try
680                     {
681                         ComponentService.getService( ).setRemoteInformations( component, false );
682                     }
683                     catch( HttpAccessException | IOException e )
684                     {
685                         AppLogService.error( e );
686                     }
687                     ComponentService.getService( ).updateRemoteInformations( component );
688                     defineTargetVersion( component );
689                     defineNextSnapshotVersion( component );
690                     component.setName( ReleaserUtils.getComponentName( component.getScmDeveloperConnection( ), component.getArtifactId( ) ) );
691                 }
692 
693             }
694         }
695     }
696 
697     /**
698      * Change the next release version.
699      *
700      * @param site
701      *            The site
702      * @param strArtifactId
703      *            The component artifact id
704      */
705     public static void changeNextReleaseVersion( Site site, String strArtifactId )
706     {
707         for ( Component component : site.getComponents( ) )
708         {
709             if ( component.getArtifactId( ).equals( strArtifactId ) )
710             {
711                 ComponentService.getService( ).changeNextReleaseVersion( component );
712             }
713         }
714     }
715 
716     /**
717      * Change the next release version.
718      *
719      * @param site
720      *            The site
721      */
722     public static void changeNextReleaseVersion( Site site )
723     {
724         List<String> listTargetVersions = site.getTargetVersions( );
725         int nNewIndex = ( site.getTargetVersionIndex( ) + 1 ) % listTargetVersions.size( );
726         String strTargetVersion = listTargetVersions.get( nNewIndex );
727         site.setNextReleaseVersion( strTargetVersion );
728         site.setTargetVersionIndex( nNewIndex );
729         site.setNextSnapshotVersion( Version.getNextSnapshotVersion( strTargetVersion ) );
730     }
731 
732     /**
733      * Generate the pom.xml file for a given site
734      * 
735      * @param site
736      *            The site
737      * @return The pom.xml content
738      */
739     public String generateTargetPOM( Site site )
740     {
741         throw new UnsupportedOperationException( "Not supported yet." ); // To change body of generated methods, choose Tools | Templates.
742     }
743 
744     /**
745      * Builds the release comments.
746      *
747      * @param site
748      *            the site
749      * @param locale
750      *            the locale
751      */
752     private static void buildReleaseComments( Site site, Locale locale )
753     {
754 
755         if ( !site.isTheme( ) )
756         {
757             // Check pom
758             InputStream inputStream = null;
759             String strPomPath = ReleaserUtils.getLocalSitePomPath( site );
760             String strPomParentReferenceVersion = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_POM_PARENT_SITE_VERSION );
761             try
762             {
763 
764                 inputStream = new FileInputStream( strPomPath );
765                 Model model = PomUpdater.unmarshal( Model.class, inputStream );
766                 String strParentSiteVersion = model.getParent( ).getVersion( );
767                 if ( ReleaserUtils.compareVersion( strParentSiteVersion, strPomParentReferenceVersion ) < 0 )
768                 {
769                     String [ ] arguments = {
770                             strPomParentReferenceVersion
771                     };
772                     String strComment = I18nService.getLocalizedString( MESSAGE_WRONG_POM_PARENT_SITE_VERSION, arguments, locale );
773                     site.addReleaseComment( strComment );
774                 }
775 
776             }
777             catch( FileNotFoundException e )
778             {
779                 AppLogService.error( e );
780             }
781             catch( JAXBException e )
782             {
783                 // TODO Auto-generated catch block
784                 AppLogService.error( e );
785             }
786         }
787 
788     }
789 
790     public static List<Site> getAuthorizedSites( int clusterId, AdminUser adminUser )
791     {
792 
793         List<Site> listAuthorizedSites = new ArrayList<Site>( );
794 
795         List<Site> listSite = SiteHome.findByCluster( clusterId );
796 
797         // Assign site's permissions
798         for ( Site site : listSite )
799         {
800             boolean bAutoriseViewSite = false;
801 
802             HashMap<String, Boolean> sitePermissions = new HashMap<String, Boolean>( );
803 
804             // Release site permission
805             if ( RBACService.isAuthorized( Site.RESOURCE_TYPE, site.getResourceId( ), SiteResourceIdService.PERMISSION_RELEASE, adminUser ) )
806             {
807                 sitePermissions.put( Site.PERMISSION_RELEASE_SITE, true );
808                 bAutoriseViewSite = true;
809             }
810             else
811             {
812                 sitePermissions.put( Site.PERMISSION_RELEASE_SITE, false );
813             }
814 
815             // Modify site permission
816             if ( RBACService.isAuthorized( Site.RESOURCE_TYPE, site.getResourceId( ), SiteResourceIdService.PERMISSION_MODIFY, adminUser ) )
817             {
818                 sitePermissions.put( Site.PERMISSION_MODIFY_SITE, true );
819                 bAutoriseViewSite = true;
820             }
821             else
822             {
823                 sitePermissions.put( Site.PERMISSION_MODIFY_SITE, false );
824             }
825 
826             // Delete site permission
827             if ( RBACService.isAuthorized( Site.RESOURCE_TYPE, site.getResourceId( ), SiteResourceIdService.PERMISSION_DELETE, adminUser ) )
828             {
829                 sitePermissions.put( Site.PERMISSION_DELETE_SITE, true );
830                 bAutoriseViewSite = true;
831             }
832             else
833             {
834                 sitePermissions.put( Site.PERMISSION_DELETE_SITE, false );
835             }
836 
837             // Set permissions
838             if ( bAutoriseViewSite )
839             {
840                 // Add permissions to the site
841                 site.setPermissions( sitePermissions );
842 
843                 // Add the site to list of Authorized sites
844                 listAuthorizedSites.add( site );
845             }
846         }
847 
848         return listAuthorizedSites;
849     }
850 
851     public static boolean IsUserAuthorized( AdminUser adminUser, String siteId, String permission )
852     {
853 
854         boolean bAuthorized = false;
855 
856         if ( RBACService.isAuthorized( Site.RESOURCE_TYPE, siteId, permission, adminUser ) )
857         {
858             bAuthorized = true;
859         }
860 
861         return bAuthorized;
862     }
863 
864     public static boolean IsSiteAlreadyExist( String siteName, String artifactId, String scmUrl )
865     {
866         String clusterName = SiteHome.findDuplicateSite( siteName, artifactId, scmUrl );
867         if ( clusterName != null )
868             return true;
869 
870         return false;
871     }
872 
873 }