1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
60
61
62
63 public static BlogServiceSession getInstance( )
64 {
65 return _singleton;
66 }
67
68
69
70
71
72
73
74
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
94
95
96
97
98
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
119
120
121
122
123
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
144
145
146
147
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
167
168
169
170
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
190
191
192
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
204
205
206
207
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
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
248
249
250
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
279
280
281
282
283
284
285
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 }