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.appcenter.modules.fastdeployapplication.web;
35  
36  import java.util.Map;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import fr.paris.lutece.plugins.appcenter.business.Application;
41  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationDemand;
42  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationsData;
43  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.InvalidRepositoryUrlException;
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.RepositoryUtils;
46  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesDatas;
47  import fr.paris.lutece.plugins.appcenter.service.ApplicationService;
48  import fr.paris.lutece.plugins.appcenter.service.DemandService;
49  import fr.paris.lutece.plugins.appcenter.util.AppCenterUtils;
50  import fr.paris.lutece.plugins.appcenter.web.AppCenterDemandXPage;
51  import fr.paris.lutece.plugins.appcenter.web.Constants;
52  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
53  import fr.paris.lutece.portal.service.datastore.DatastoreService;
54  import fr.paris.lutece.portal.service.message.SiteMessageException;
55  import fr.paris.lutece.portal.service.security.UserNotSignedException;
56  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
57  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
58  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
59  import fr.paris.lutece.portal.web.xpages.XPage;
60  import fr.paris.lutece.util.ReferenceList;
61  
62  /**
63   * SourcesXPage
64   */
65  @Controller( xpageName = "fastdeployapplication", pageTitleI18nKey = "appcenter.xpage.fastdeployapplication.pageTitle", pagePathI18nKey = "appcenter.xpage.fastdeployapplication.pagePathLabel" )
66  public class FastDeployApplicationsXPage extends AppCenterDemandXPage
67  {
68      // Templates
69      private static final String TEMPLATE_MANAGE_APPLICATIONS = "/skin/plugins/appcenter/modules/fastdeployapplication/manage_applications.html";
70  
71      // VIEW
72      private static final String VIEW_MANAGE_APPLICATIONS = "manageapplications";
73  
74      // ACTION
75      private static final String ACTION_ADD_APPLICATION = "addApplication";
76      // MARK URL SITE
77      public static final String DATA_PREFIX_FAST_DEPLOY_SERVICES = "appcenter.fastdeployServices.";
78      public static final String MARK_SERVICES = "services";
79      public static final String MARK_MAP_SERVICES = "map_services";
80      private static final String MARK_DEFAULT_URL_SITE = "default_url_site";
81  
82      // PARAM
83      public static final String PARAM_LUTECE_SITE = "lutece_site";
84  
85      // CONSTANTS
86      private static final String REPOSITORY_TYPE_SITE = "site";
87  
88      @View( value = VIEW_MANAGE_APPLICATIONS, defaultView = true )
89      public XPage getManageApplications( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
90      {
91          try
92          {
93              checkPermission( request, ConstanteUtils.PERMISSION_MANAGE_FASTDEPLOY_APPLICATION, null );
94          }
95          catch( AccessDeniedException e )
96          {
97              getUnauthorizedAccessMessage( request );
98          }
99  
100         Application application = getApplication( request );
101 
102         SourcesDatas sourcesData = ApplicationService.loadApplicationDataSubset( application, SourcesDatas.class );
103 
104         Map<String, Object> model = getModel( );
105         if ( sourcesData != null && sourcesData.getListData( ) != null )
106         {
107             model.put( MARK_DEFAULT_URL_SITE, sourcesData.getListData( ).stream( ).filter( x -> x.getRepositoryType( ).equals( REPOSITORY_TYPE_SITE ) )
108                     .map( x -> x.getRepositoryUrl( ) ).findFirst( ).orElse( null ) );
109         }
110         ReferenceList refServices = DatastoreService.getDataByPrefix( DATA_PREFIX_FAST_DEPLOY_SERVICES );
111         AppCenterUtils.addEmptyItem( refServices, getLocale( request ) );
112 
113         fillAppCenterCommons( model, request );
114         fillDisplayPermission( model, request, ConstanteUtils.PERMISSION_ADD_FASTDEPLOY_APPLICATION, null );
115 
116         model.put( MARK_SERVICES, refServices );
117         model.put( MARK_MAP_SERVICES, refServices.toMap( ) );
118 
119         return getXPage( TEMPLATE_MANAGE_APPLICATIONS, request.getLocale( ), model );
120     }
121 
122     @Action( ACTION_ADD_APPLICATION )
123     public XPage doAddApplication( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
124     {
125         try
126         {
127             checkPermission( request, ConstanteUtils.PERMISSION_ADD_FASTDEPLOY_APPLICATION, null );
128         }
129         catch( AccessDeniedException e )
130         {
131             getUnauthorizedAccessMessage( request );
132         }
133 
134         int nId = Integer.parseInt( request.getParameter( Constants.PARAM_ID_APPLICATION ) );
135         Application application = getApplication( request );
136 
137         FastDeployApplicationDemandation/business/FastDeployApplicationDemand.html#FastDeployApplicationDemand">FastDeployApplicationDemand fastDeployApplicationDemand = new FastDeployApplicationDemand( );
138         fastDeployApplicationDemand.setIdApplication( application.getId( ) );
139 
140         populate( fastDeployApplicationDemand, request );
141         boolean isLuteceSite = ( request.getParameter( PARAM_LUTECE_SITE ) != null );
142         fastDeployApplicationDemand.setLuteceSite( isLuteceSite );
143 
144         // Check constraints
145         if ( !validateBean( fastDeployApplicationDemand, getLocale( request ) ) )
146         {
147             return redirect( request, VIEW_MANAGE_APPLICATIONS, Constants.PARAM_ID_APPLICATION, nId );
148         }
149 
150         // Check given site URL to see from which VCSRemoteService this URL come from
151         try
152         {
153             RepositoryUtils.checkRepository( fastDeployApplicationDemand.getUrlSite( ), fastDeployApplicationDemand );
154         }
155         catch( InvalidRepositoryUrlException e )
156         {
157             addError( "L'url du site ne designe pas un repository de sources valide" );
158             return redirect( request, VIEW_MANAGE_APPLICATIONS, Constants.PARAM_ID_APPLICATION, nId );
159         }
160 
161         DemandService.saveDemand( fastDeployApplicationDemand, application );
162 
163         return redirect( request, VIEW_MANAGE_APPLICATIONS, Constants.PARAM_ID_APPLICATION, nId );
164     }
165 
166     @Override
167     protected String getDemandType( )
168     {
169         return FastDeployApplicationDemand.DEMAND_TYPE;
170     }
171 
172     @Override
173     protected Class getDemandClass( )
174     {
175         return FastDeployApplicationDemand.class;
176     }
177 
178     @Override
179     protected String getDatasName( )
180     {
181         return FastDeployApplicationsData.DATA_FASTDEPLOY_APPLICATIONS_NAME;
182     }
183 
184     @Override
185     protected Class getDatasClass( )
186     {
187         return FastDeployApplicationsData.class;
188     }
189 
190 }