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.chat.web;
35
36 import fr.paris.lutece.plugins.chat.business.ChatRoom;
37 import fr.paris.lutece.plugins.chat.business.RoomList;
38 import fr.paris.lutece.plugins.chat.service.ChatConstantes;
39 import fr.paris.lutece.plugins.chat.service.ChatService;
40 import fr.paris.lutece.portal.service.message.SiteMessageException;
41 import fr.paris.lutece.portal.service.plugin.Plugin;
42 import fr.paris.lutece.portal.service.security.UserNotSignedException;
43 import fr.paris.lutece.portal.service.template.AppTemplateService;
44 import fr.paris.lutece.portal.service.util.AppPropertiesService;
45 import fr.paris.lutece.portal.web.xpages.XPage;
46 import fr.paris.lutece.portal.web.xpages.XPageApplication;
47 import fr.paris.lutece.util.html.HtmlTemplate;
48
49 import java.util.Enumeration;
50
51 import javax.servlet.http.HttpServletRequest;
52
53
54
55
56
57 public class ChatApp implements XPageApplication
58 {
59 private static final String TEMPLATE_CHAT_APPLET = "skin/plugins/chat/applet.html";
60 private static final String TEMPLATE_CHAT_ENTER = "skin/plugins/chat/chat_enter.html";
61 private static final String TEMPLATE_ROOM_LIST_ROW = "skin/plugins/chat/room_list_row.html";
62 private static final String BOOKMARK_NICKNAME = "@nickname@";
63 private static final String BOOKMARK_ROOM = "@room@";
64 private static final String BOOKMARK_ROOM_DESCRIPTION = "@room_description@";
65 private static final String BOOKMARK_CHECKED = "@checked@";
66 private static final String BOOKMARK_ROWS = "@rooms_list_row@";
67 private static final String BOOKMARK_GUESTS_COUNT = "@users_count@";
68 private static final String BOOKMARK_BGCOLOR = "@bgcolor@";
69 private static final String BOOKMARK_BTBGCOLOR = "@btbgcolor@";
70 private static final String BOOKMARK_BTFGCOLOR = "@btfgcolor@";
71 private static final String BOOKMARK_FDBGCOLOR = "@fdbgcolor@";
72 private static final String PARAM_ACCESS_CODE = "access_code";
73 private static final String PROPERTY_CHAT_PAGE_TITLE = "chat.page.title";
74 private static final String PROPERTY_CHAT_PAGE_PATH_LABEL = "chat.page.pathLabel";
75 private static final String PROPERTY_CHAT_ERROR_INVALID_ROOM = "chat.msg.invalid.room";
76
77
78
79
80 public ChatApp( )
81 {
82 }
83
84
85
86
87
88
89
90
91
92
93
94 public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
95 throws SiteMessageException
96 {
97 XPage page = new XPage( );
98 String strTitle = AppPropertiesService.getProperty( PROPERTY_CHAT_PAGE_TITLE );
99 String strPathLabel = AppPropertiesService.getProperty( PROPERTY_CHAT_PAGE_PATH_LABEL );
100 page.setTitle( strTitle );
101 page.setPathLabel( strPathLabel );
102
103 String strNickname = request.getParameter( ChatConstantes.PARAM_NICKNAME );
104
105
106 if ( strNickname != null )
107 {
108 page.setContent( getAppletPage( request, strNickname ) );
109
110 return page;
111 }
112
113
114 String strAccessCode = request.getParameter( PARAM_ACCESS_CODE );
115
116
117 if ( strAccessCode != null )
118 {
119 page.setContent( getAppletPage( request, strAccessCode ) );
120
121 return page;
122 }
123
124
125 page.setContent( getLoginPage( ) );
126
127 return page;
128 }
129
130
131
132
133
134
135
136
137 private String getAppletPage( HttpServletRequest request, String strNickname )
138 {
139 ChatRoom room = ChatService.getRoom( request );
140
141 if ( room == null )
142 {
143 return AppPropertiesService.getProperty( PROPERTY_CHAT_ERROR_INVALID_ROOM );
144 }
145
146 String strRoom = request.getParameter( ChatConstantes.PARAM_ROOM );
147 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CHAT_APPLET );
148 template.substitute( BOOKMARK_NICKNAME, strNickname );
149 template.substitute( BOOKMARK_ROOM, strRoom );
150 template.substitute( BOOKMARK_BGCOLOR, room.getBgColor( ) );
151 template.substitute( BOOKMARK_BTBGCOLOR, room.getButtonBgColor( ) );
152 template.substitute( BOOKMARK_BTFGCOLOR, room.getButtonFgColor( ) );
153 template.substitute( BOOKMARK_FDBGCOLOR, room.getFieldBgColor( ) );
154
155 return template.getHtml( );
156 }
157
158
159
160
161
162
163
164 private String getLoginPage( )
165 {
166
167 String strTemplate = TEMPLATE_CHAT_ENTER;
168 HtmlTemplate template = AppTemplateService.getTemplate( strTemplate );
169
170
171 RoomList roomList = ChatService.getRoomList( );
172 Enumeration rooms = roomList.getRooms( );
173 boolean bFirst = true;
174 StringBuffer strLines = new StringBuffer( );
175 HtmlTemplate templateLine = AppTemplateService.getTemplate( TEMPLATE_ROOM_LIST_ROW );
176
177 while ( rooms.hasMoreElements( ) )
178 {
179 HtmlTemplate t = new HtmlTemplate( templateLine );
180 ChatRoom room = (ChatRoom) rooms.nextElement( );
181 t.substitute( BOOKMARK_ROOM, room.getName( ) );
182 t.substitute( BOOKMARK_ROOM_DESCRIPTION, room.getDescription( ) );
183
184
185 String strChecked = ( bFirst ) ? "checked=\"checked\"" : "";
186 t.substitute( BOOKMARK_CHECKED, strChecked );
187 bFirst = false;
188 t.substitute( BOOKMARK_GUESTS_COUNT, room.getUserCount( ) );
189 strLines.append( t.getHtml( ) );
190 }
191
192 template.substitute( BOOKMARK_ROWS, strLines.toString( ) );
193
194 return template.getHtml( );
195 }
196 }