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.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  
41  /**
42   * This Service manages document actions (create, move, delete, validate ...) and notify listeners.
43   */
44  public class BlogServiceSession
45  {
46  
47      private static BlogServiceSessionlogServiceSession.html#BlogServiceSession">BlogServiceSession _singleton = new BlogServiceSession( );
48      private static final String SESSION_BLOG = "blog.serviceblog";
49  
50      /**
51       * Get the unique instance of the service
52       *
53       * @return The unique instance
54       */
55      public static BlogServiceSession getInstance( )
56      {
57          return _singleton;
58      }
59  
60      /**
61       * Save an blog in the session of the user
62       *
63       * @param session
64       *            The session
65       * @param blog
66       *            The blog to save
67       */
68      public void saveBlogInSession( HttpSession session, Blog blog )
69      {
70          try
71          {
72  
73              session.setAttribute( SESSION_BLOG + blog.getId( ), blog );
74  
75          }
76          catch( IllegalStateException e )
77          {
78  
79              AppLogService.error( e.getMessage( ), e );
80              BlogSessionListner.remove( session.getId( ) );
81          }
82      }
83  
84      /**
85       * Get the current blog form from the session
86       * 
87       * @param session
88       *            The session of the user
89       * @param blog
90       * @return The blog form
91       */
92      public Blogtece/plugins/blog/business/Blog.html#Blog">Blog getBlogFromSession( HttpSession session, Blog blog )
93      {
94  
95          try
96          {
97              return (Blog) session.getAttribute( SESSION_BLOG + blog.getId( ) );
98  
99          }
100         catch( IllegalStateException e )
101         {
102 
103             AppLogService.error( e.getMessage( ), e );
104             BlogSessionListner.remove( session.getId( ) );
105             return null;
106         }
107     }
108 
109     /**
110      * Get the current blog form from the session
111      * 
112      * @param session
113      *            The session of the user
114      * @param nIdBlog
115      * @return The blog post
116      */
117     public Blog getBlogFromSession( HttpSession session, int nIdBlog )
118     {
119         try
120         {
121 
122             return (Blog) session.getAttribute( SESSION_BLOG + nIdBlog );
123 
124         }
125         catch( IllegalStateException e )
126         {
127 
128             AppLogService.error( e.getMessage( ), e );
129             BlogSessionListner.remove( session.getId( ) );
130             return null;
131         }
132     }
133 
134     /**
135      * Remove any blog form responses stored in the session of the user
136      * 
137      * @param session
138      *            The session
139      * @param blog
140      */
141     public void removeBlogFromSession( HttpSession session, Blog blog )
142     {
143         try
144         {
145 
146             session.removeAttribute( SESSION_BLOG + blog.getId( ) );
147 
148         }
149         catch( IllegalStateException e )
150         {
151 
152             AppLogService.error( e.getMessage( ), e );
153             BlogSessionListner.remove( session.getId( ) );
154         }
155     }
156 
157     /**
158      * Remove any blog form responses stored in the session of the user
159      * 
160      * @param session
161      *            The session
162      * @param idBlog
163      */
164     public void removeBlogFromSession( HttpSession session, int idBlog )
165     {
166         try
167         {
168 
169             session.removeAttribute( SESSION_BLOG + idBlog );
170 
171         }
172         catch( IllegalStateException e )
173         {
174 
175             AppLogService.error( e.getMessage( ), e );
176             BlogSessionListner.remove( session.getId( ) );
177         }
178     }
179 
180 }