View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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  
35  package fr.paris.lutece.plugins.appcenter.modules.sources.web;
36  
37  import static fr.paris.lutece.plugins.appcenter.web.Constants.MARK_USER;
38  
39  import java.util.ArrayList;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import com.rometools.rome.feed.rss.Source;
47  
48  import fr.paris.lutece.plugins.appcenter.business.Application;
49  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourceUserDemand;
50  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesDatas;
51  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesDemand;
52  import fr.paris.lutece.plugins.appcenter.modules.sources.service.SourcesUtil;
53  import fr.paris.lutece.plugins.appcenter.service.ApplicationService;
54  import fr.paris.lutece.plugins.appcenter.service.DemandService;
55  import fr.paris.lutece.plugins.appcenter.service.UserService;
56  import fr.paris.lutece.plugins.appcenter.web.AppCenterDemandXPage;
57  import fr.paris.lutece.plugins.appcenter.web.Constants;
58  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
59  import fr.paris.lutece.portal.service.message.SiteMessageException;
60  import fr.paris.lutece.portal.service.security.UserNotSignedException;
61  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
62  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
63  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
64  import fr.paris.lutece.portal.web.xpages.XPage;
65  import fr.paris.lutece.util.ReferenceList;
66  
67  /**
68   * SourcesXPage
69   */
70  @Controller( xpageName = "sources", pageTitleI18nKey = "module.appcenter.sources.xpage.sources.pageTitle", pagePathI18nKey = "module.appcenter.sources.xpage.sources.pagePathLabel" )
71  public class SourcesXPage extends AppCenterDemandXPage
72  {
73      // Templates
74      private static final String TEMPLATE_MANAGE_SOURCES = "/skin/plugins/appcenter/modules/sources/manage_sources.html";
75   
76      private static final String VIEW_MANAGE_SOURCES = "sources";
77      private static final String ACTION_ADD_REPOSITORY = "addRepository";
78      private static final String PARAM_USER_NAME = "user_name";
79      private static final String PARAM_MAIL = "email";
80      
81      
82      
83      private static final int NB_MAX_USER=100;
84      
85      private static final String PERMISSION_VIEW_SOURCES_DEMANDS = "PERMISSION_VIEW_SOURCES_DEMANDS";
86      private static final String PERMISSION_ADD_REPO= "PERMISSION_ADD_REPO";
87  
88  
89      /**
90       * Maganage sources view
91       * @param request The HttpServletRequest
92       * @return the view for managing sources
93       * @throws UserNotSignedException
94       * @throws SiteMessageException 
95       */
96      @View( value = VIEW_MANAGE_SOURCES, defaultView = true )
97      public XPage getManageSources( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
98      {
99          try
100         {
101             checkPermission( request, PERMISSION_VIEW_SOURCES_DEMANDS, null );
102         }
103         catch ( AccessDeniedException e )
104         {
105             getUnauthorizedAccessMessage( request );
106         }
107         
108         Map<String, Object> model = getModel( );
109         fillAppCenterCommons( model, request );
110         fillDisplayPermission( model, request, PERMISSION_ADD_REPO , null );
111        
112         ReferenceList refListRepoType=SourcesUtil.getAllRepositoryType(getLocale(request));
113         model.put(SourcesUtil.MARK_REPOSITORY_TYPES, refListRepoType);
114         model.put(SourcesUtil.MARK_REPOSITORY_TYPES_MAP, refListRepoType.toMap());
115         
116        
117         return getXPage( TEMPLATE_MANAGE_SOURCES, request.getLocale( ), model );
118     }
119 
120     /**
121      * Action add site repository
122      * @param request The HttpServletRequest
123      * @return the manage view after processing action
124      * @throws UserNotSignedException
125      * @throws SiteMessageException 
126      */
127     @Action( ACTION_ADD_REPOSITORY )
128     public XPage doAddRepository (HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
129     {
130         try
131         {
132             checkPermission( request, PERMISSION_ADD_REPO, null);
133         }
134         catch ( AccessDeniedException e )
135         {
136             getUnauthorizedAccessMessage( request );
137         }
138         int nId = Integer.parseInt( request.getParameter(Constants.PARAM_ID_APPLICATION ) );
139         Application application = getApplication( request );
140         SourcesDemandter/modules/sources/business/SourcesDemand.html#SourcesDemand">SourcesDemand sourcesDemand = new SourcesDemand( );
141         sourcesDemand.setIdApplication( application.getId( ) );
142         populate( sourcesDemand, request );
143         //Add Users Authorized
144         String strUserName=null;
145         String strMail=null;
146         SourceUserDemand sourceUserDemand;
147         sourcesDemand.setListSourceUserDemand( new ArrayList<>( ) );
148         for ( int i = 0; i < NB_MAX_USER; i++ )
149         {
150             strUserName=request.getParameter( PARAM_USER_NAME+"_"+i );
151             if(!StringUtils.isEmpty( strUserName ))
152             {
153                 
154                 strMail=request.getParameter( PARAM_MAIL+"_"+i );
155                 sourceUserDemand=new SourceUserDemand( );
156                 sourceUserDemand.setUserName( strUserName );
157                 sourceUserDemand.setEmail( strMail );
158                 sourcesDemand.getListSourceUserDemand( ).add( sourceUserDemand );
159                 
160             }
161         }
162         // Check constraints
163         if ( !validateBean( sourcesDemand, getLocale( request ) ) )
164         {
165             return redirectView( request, VIEW_MANAGE_SOURCES );
166         }
167         DemandService.saveDemand( sourcesDemand, application );
168 
169         return redirect(request, VIEW_MANAGE_SOURCES, Constants.PARAM_ID_APPLICATION, nId );
170     }
171 
172    
173 
174     @Override
175     protected String getDemandType()
176     {
177         return SourcesDemand.DEMAND_TYPE;
178     }
179 
180     @Override
181     protected Class getDemandClass()
182     {
183         return SourcesDemand.class;
184     }
185 
186     @Override
187     protected String getDatasName()
188     {
189         return SourcesDatas.DATA_SOURCES_NAME;
190     }
191 
192     @Override
193     protected Class getDatasClass()
194     {
195         return SourcesDatas.class;
196     }
197 }