View Javadoc
1   /*
2    * Copyright (c) 2002-2015, 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  package fr.paris.lutece.plugins.adminwall.web;
35  
36  import fr.paris.lutece.plugins.adminwall.business.Hashtag;
37  import fr.paris.lutece.plugins.adminwall.business.HashtagHome;
38  import fr.paris.lutece.plugins.adminwall.business.Link;
39  import fr.paris.lutece.plugins.adminwall.business.LinkHome;
40  import fr.paris.lutece.plugins.adminwall.business.Post;
41  import fr.paris.lutece.plugins.adminwall.business.PostHome;
42  import fr.paris.lutece.plugins.adminwall.service.AdminWallService;
43  import fr.paris.lutece.portal.business.user.AdminUser;
44  import fr.paris.lutece.portal.service.admin.AdminUserService;
45  import fr.paris.lutece.portal.service.message.AdminMessage;
46  import fr.paris.lutece.portal.service.message.AdminMessageService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
49  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
50  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
51  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
52  import fr.paris.lutece.util.html.Paginator;
53  import fr.paris.lutece.util.url.UrlItem;
54  
55  import java.sql.Timestamp;
56  
57  import java.util.ArrayList;
58  import java.util.Collection;
59  import java.util.Collections;
60  import java.util.Date;
61  import java.util.List;
62  import java.util.Map;
63  
64  import javax.servlet.http.HttpServletRequest;
65  
66  
67  /**
68   * This class provides the user interface to manage Wall features ( manage,
69   * create, modify, remove )
70   */
71  @Controller( controllerJsp = "ManageWall.jsp", controllerPath = "jsp/admin/plugins/adminwall/", right = "ADMINWALL_ACCESS" )
72  public class WallJspBean extends AdminWallJspBean
73  {
74      ////////////////////////////////////////////////////////////////////////////
75      // Constants
76      // templates
77      private static final String TEMPLATE_MANAGE_POST = "/admin/plugins/adminwall/manage_posts.html";
78      private static final String TEMPLATE_CREATE_POST = "/admin/plugins/adminwall/create_post.html";
79  
80      // Parameters
81      private static final String PARAMETER_ID_POST = "idPost";
82      private static final String PARAMETER_TAG = "tag";
83      private static final String PARAMETER_ID_AUTEUR = "idAuteur";
84  
85      // Properties for page titles
86      private static final String PROPERTY_PAGE_TITLE_MANAGE_POSTS = "adminwall.manage_posts.pageTitle";
87      private static final String PROPERTY_PAGE_TITLE_CREATE_POST = "adminwall.create_post.pageTitle";
88  
89      // Markers
90      private static final String MARK_POST_LIST = "post_list";
91      private static final String MARK_POST = "post";
92      private static final String MARK_USERID = "userId";
93      private static final String JSP_MANAGE_WALL = "jsp/admin/plugins/adminwall/ManageWall.jsp";
94  
95      // Properties
96      private static final String MESSAGE_CONFIRM_REMOVE_POST = "adminwall.message.confirmRemovePost";
97      private static final String PROPERTY_DEFAULT_LIST_POST_PER_PAGE = "adminwall.listPosts.itemsPerPage";
98      private static final String VALIDATION_ATTRIBUTES_PREFIX = "adminwall.model.entity.post.attribute.";
99  
100     // Views
101     private static final String VIEW_MANAGE_POSTS = "managePosts";
102     private static final String VIEW_CREATE_POST = "createPost";
103 
104     // Actions
105     private static final String ACTION_CREATE_POST = "createPost";
106     private static final String ACTION_REMOVE_POST = "removePost";
107     private static final String ACTION_CONFIRM_REMOVE_POST = "confirmRemovePost";
108 
109     // Infos
110     private static final String INFO_POST_CREATED = "adminwall.info.post.created";
111     private static final String INFO_POST_REMOVED = "adminwall.info.post.removed";
112 
113     // Session variables to store working values
114     private Post _post;
115     private Hashtag _hashtag;
116     private Link _link;
117 
118     /**
119      * Returns the page with posts management
120      *
121      * @param request The Http request
122      * @return the html code of the page
123      */
124     @View( value = VIEW_MANAGE_POSTS, defaultView = true )
125     public String getManagePosts( HttpServletRequest request )
126     {
127         Collection<Post> collectionPosts = new ArrayList<Post>(  );
128         List<Post> listPosts = (List<Post>) collectionPosts;
129 
130         //PAGINATOR
131         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
132         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_POST_PER_PAGE, 50 );
133         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
134                 _nDefaultItemsPerPage );
135 
136         //URL
137         UrlItem url = new UrlItem( JSP_MANAGE_WALL );
138         String strUrl = url.getUrl(  );
139 
140         //TAG
141         String paramTag = request.getParameter( PARAMETER_TAG );
142 
143         if ( paramTag == null )
144         { //SANS PARAMETRE/FILTRAGE
145             listPosts = (List<Post>) PostHome.getPostsList(  );
146         }
147         else
148         { //AVEC PARAMETRE/FILTRAGE
149 
150             int idHashtag = HashtagHome.getId( paramTag );
151             List<Link> listLinks = (List<Link>) LinkHome.getLinksListTag( idHashtag );
152 
153             //Creation de la liste de la liste de Posts avec la liste de Links
154             for ( Link link : listLinks )
155             {
156                 int idPost = link.getPost(  );
157                 Post pos = PostHome.findByPrimaryKey( idPost );
158                 listPosts.add( pos );
159             }
160 
161             Collections.reverse( listPosts );
162         }
163 
164         //AUTEUR
165         String paramIdAuteur = request.getParameter( PARAMETER_ID_AUTEUR );
166 
167         if ( paramIdAuteur != null )
168         { //AVEC PARAMETRE/FILTRAGE
169 
170             int nIdAuteur = Integer.parseInt( paramIdAuteur );
171             listPosts = (List<Post>) PostHome.getPostsListIdAuteur( nIdAuteur );
172         }
173 
174         for ( Post pos : listPosts )
175         {
176             //Insertion des URL
177             AdminWallService.activateURL( pos );
178             //Insertion des liens de filtrages sur les hashtags
179             AdminWallService.activateHashtag( pos );
180         }
181 
182         // PAGINATOR
183         LocalizedPaginator paginator = new LocalizedPaginator( listPosts, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX,
184                 _strCurrentPageIndex, getLocale(  ) );
185 
186         //Infos User
187         AdminUser currentUser = AdminUserService.getAdminUser( request );
188         int userId = currentUser.getUserId(  );
189 
190         //Model
191         Map<String, Object> model = getModel(  );
192         model.put( MARK_USERID, userId );
193         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
194         model.put( MARK_PAGINATOR, paginator );
195         model.put( MARK_POST_LIST, paginator.getPageItems(  ) );
196 
197         return getPage( PROPERTY_PAGE_TITLE_MANAGE_POSTS, TEMPLATE_MANAGE_POST, model );
198     }
199 
200     /**
201      * Returns the form to create a post
202      *
203      * @param request The Http request
204      * @return the html code of the post form
205      */
206     @View( VIEW_CREATE_POST )
207     public String getCreatePost( HttpServletRequest request )
208     {
209         _post = ( _post != null ) ? _post : new Post(  );
210 
211         Map<String, Object> model = getModel(  );
212         model.put( MARK_POST, _post );
213 
214         return getPage( PROPERTY_PAGE_TITLE_CREATE_POST, TEMPLATE_CREATE_POST, model );
215     }
216 
217     /**
218      * Process the data capture form of a new post
219      *
220      * @param request The Http Request
221      * @return The Jsp URL of the process result
222      */
223     @Action( ACTION_CREATE_POST )
224     public String doCreatePost( HttpServletRequest request )
225     {
226         _post = ( _post != null ) ? _post : new Post(  );
227 
228         /*Date Automatique*/
229         Date dDate = new Date(  );
230         Timestamp time = new Timestamp( dDate.getTime(  ) );
231         _post.setTimestamp( time );
232 
233         /*Auteur Automatique*/
234         AdminUser currentUser = AdminUserService.getAdminUser( request );
235 
236         String prenom = currentUser.getFirstName(  );
237         String nom = currentUser.getLastName(  );
238         _post.setAuteur( prenom + " " + nom );
239 
240         int idAuteur = currentUser.getUserId(  );
241         _post.setIdAuteur( idAuteur );
242 
243         populate( _post, request );
244 
245         // Check constraints
246         if ( !validateBean( _post, VALIDATION_ATTRIBUTES_PREFIX ) )
247         {
248             return redirectView( request, VIEW_MANAGE_POSTS );
249         }
250 
251         PostHome.create( _post );
252 
253         /*Detection Hashtags+ajout BDD*/
254         AdminWallService.detectHashtag( _post );
255 
256         _post = null;
257         addInfo( INFO_POST_CREATED, getLocale(  ) );
258 
259         return redirectView( request, VIEW_MANAGE_POSTS );
260     }
261 
262     /**
263      * Manages the removal form of a post whose identifier is in the http
264      * request
265      *
266      * @param request The Http request
267      * @return the html code to confirm
268      */
269     @Action( ACTION_CONFIRM_REMOVE_POST )
270     public String getConfirmRemovePost( HttpServletRequest request )
271     {
272         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_POST ) );
273         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_POST ) );
274         url.addParameter( PARAMETER_ID_POST, nId );
275 
276         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_POST, url.getUrl(  ),
277                 AdminMessage.TYPE_CONFIRMATION );
278 
279         return redirect( request, strMessageUrl );
280     }
281 
282     /**
283      * Handles the removal form of a post
284      *
285      * @param request The Http request
286      * @return the jsp URL to display the form to manage posts
287      */
288     @Action( ACTION_REMOVE_POST )
289     public String doRemovePost( HttpServletRequest request )
290     {
291         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_POST ) );
292         PostHome.remove( nId );
293         LinkHome.remove( nId );
294         addInfo( INFO_POST_REMOVED, getLocale(  ) );
295 
296         return redirectView( request, VIEW_MANAGE_POSTS );
297     }
298 }