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.blog.service;
35  
36  import javax.servlet.http.HttpSession;
37  
38  import fr.paris.lutece.plugins.blog.business.Blog;
39  import fr.paris.lutece.portal.service.util.AppLogService;
40  import  fr.paris.lutece.plugins.blog.business.DocContent;
41  import fr.paris.lutece.api.user.User;
42  import fr.paris.lutece.plugins.blog.business.DocContentHome;
43  import java.util.List;
44  import  java.util.Enumeration;
45  import java.util.HashMap;
46  
47  /**
48   * This Service manages document actions (create, move, delete, validate ...) and notify listeners.
49   */
50  public class BlogServiceSession
51  {
52  
53      private static BlogServiceSessionlogServiceSession.html#BlogServiceSession">BlogServiceSession _singleton = new BlogServiceSession( );
54      private static final String SESSION_BLOG = "blog.serviceblog";
55      private static final String SESSION_KEY_ID_DOCCONTENT = "docContentId";
56      private static final String SESSION_KEY_PRIORITY_DOCCONTENT = "docContentPriority";
57  
58      /**
59       * Get the unique instance of the service
60       *
61       * @return The unique instance
62       */
63      public static BlogServiceSession getInstance( )
64      {
65          return _singleton;
66      }
67  
68      /**
69       * Save an blog in the session of the user
70       *
71       * @param session
72       *            The session
73       * @param blog
74       *            The blog to save
75       */
76      public void saveBlogInSession( HttpSession session, Blog blog )
77      {
78          try
79          {
80  
81              session.setAttribute( SESSION_BLOG + blog.getId( ), blog );
82  
83          }
84          catch( IllegalStateException e )
85          {
86  
87              AppLogService.error( e.getMessage( ), e );
88              BlogSessionListner.remove( session.getId( ) );
89          }
90      }
91  
92      /**
93       * Get the current blog form from the session
94       * 
95       * @param session
96       *            The session of the user
97       * @param blog
98       * @return The blog form
99       */
100     public Blogtece/plugins/blog/business/Blog.html#Blog">Blog getBlogFromSession( HttpSession session, Blog blog )
101     {
102 
103         try
104         {
105             return (Blog) session.getAttribute( SESSION_BLOG + blog.getId( ) );
106 
107         }
108         catch( IllegalStateException e )
109         {
110 
111             AppLogService.error( e.getMessage( ), e );
112             BlogSessionListner.remove( session.getId( ) );
113             return null;
114         }
115     }
116 
117     /**
118      * Get the current blog form from the session
119      * 
120      * @param session
121      *            The session of the user
122      * @param nIdBlog
123      * @return The blog post
124      */
125     public Blog getBlogFromSession( HttpSession session, int nIdBlog )
126     {
127         try
128         {
129 
130             return (Blog) session.getAttribute( SESSION_BLOG + nIdBlog );
131 
132         }
133         catch( IllegalStateException e )
134         {
135 
136             AppLogService.error( e.getMessage( ), e );
137             BlogSessionListner.remove( session.getId( ) );
138             return null;
139         }
140     }
141 
142     /**
143      * Remove any blog form responses stored in the session of the user
144      * 
145      * @param session
146      *            The session
147      * @param blog
148      */
149     public void removeBlogFromSession( HttpSession session, Blog blog )
150     {
151         try
152         {
153 
154             session.removeAttribute( SESSION_BLOG + blog.getId( ) );
155 
156         }
157         catch( IllegalStateException e )
158         {
159 
160             AppLogService.error( e.getMessage( ), e );
161             BlogSessionListner.remove( session.getId( ) );
162         }
163     }
164 
165     /**
166      * Remove any blog form responses stored in the session of the user
167      * 
168      * @param session
169      *            The session
170      * @param idBlog
171      */
172     public void removeBlogFromSession( HttpSession session, int idBlog )
173     {
174         try
175         {
176 
177             session.removeAttribute( SESSION_BLOG + idBlog );
178 
179         }
180         catch( IllegalStateException e )
181         {
182 
183             AppLogService.error( e.getMessage( ), e );
184             BlogSessionListner.remove( session.getId( ) );
185         }
186     }
187 
188     /**
189      * Save docContent in session when the user is in the process of creating a new blog
190      *
191      * @param session
192      *            The session
193      */
194     public void saveBlogInSession( HttpSession session, DocContent docContent, User user)
195     {
196         HashMap<String, Integer> mapDocContentKeys = new HashMap<>( );
197         mapDocContentKeys.put( SESSION_KEY_ID_DOCCONTENT, docContent.getId( ) );
198         mapDocContentKeys.put( SESSION_KEY_PRIORITY_DOCCONTENT, docContent.getPriority( ) );
199 
200         session.setAttribute( user.getAccessCode( ) + "-" + user.getEmail() + "-" + docContent.getTextValue( ),  mapDocContentKeys);
201     }
202     /**
203      * Get the current list of docContent form from the session that contains the user's access code
204      *
205      * @param session
206      *            The session of the user
207      * @return The docContent form
208      */
209     public List<DocContent> getDocContentFromSession( HttpSession session, User user )
210     {
211         String strAccessCode = user.getAccessCode( );
212         String strEmail = user.getEmail( );
213         List<DocContent> listDocContent = new java.util.ArrayList<>( );
214         try
215         {
216           Enumeration<String> e = session.getAttributeNames( );
217             while ( e.hasMoreElements( ) )
218             {
219                 String strAttributeName = e.nextElement( );
220                 if ( strAttributeName.startsWith( strAccessCode + "-" + strEmail ) )
221                 {
222                     if( session.getAttribute( strAttributeName ) instanceof HashMap)
223                     {
224                         HashMap<String, Integer> mapDocContentKeys = (HashMap) session.getAttribute( strAttributeName );
225                         // Get the docContent from the database
226                         DocContent docContent = DocContentHome.getDocsContent( mapDocContentKeys.get( SESSION_KEY_ID_DOCCONTENT ) );
227                         docContent.setPriority( mapDocContentKeys.get( SESSION_KEY_PRIORITY_DOCCONTENT ) );
228                         listDocContent.add( docContent );
229 
230                     }
231                     }
232             }
233             listDocContent.sort( ( d1, d2 ) -> d1.getPriority( ) );
234             return listDocContent;
235 
236         }
237         catch( IllegalStateException e )
238         {
239 
240             AppLogService.error( e.getMessage( ), e );
241             BlogSessionListner.remove( session.getId( ) );
242         }
243         return null;
244     }
245 
246     /**
247      * Remove any docContent form responses stored in the session of the user
248      *
249      * @param session
250      *            The session
251      */
252     public void removeDocContentFromSession( HttpSession session, User user )
253     {
254         String strAccessCode = user.getAccessCode( );
255         String strEmail = user.getEmail( );
256         try
257         {
258             Enumeration<String> e = session.getAttributeNames( );
259             while ( e.hasMoreElements( ) )
260             {
261                 String strAttributeName = e.nextElement( );
262                 if ( strAttributeName.startsWith( strAccessCode + "-" + strEmail ) )
263                 {
264                     session.removeAttribute( strAttributeName );
265                 }
266             }
267 
268         }
269         catch( IllegalStateException e )
270         {
271 
272             AppLogService.error( e.getMessage( ), e );
273             BlogSessionListner.remove( session.getId( ) );
274         }
275     }
276 
277     /**
278      * Remove a docContent form response stored in the session of the user
279      *
280      * @param session
281      *           The session
282      * @param docContent
283      *          The docContent to remove
284      * @param user
285      *         The user
286      */
287     public void removeDocContentFromSession( HttpSession session, DocContent docContent, User user )
288     {
289         session.removeAttribute( user.getAccessCode( ) + "-" + user.getEmail() + "-" + docContent.getTextValue( ) );
290     }
291 
292 
293     }