1 /*
2 * Copyright (c) 2002-2014, 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.dbpage.business;
35
36 import fr.paris.lutece.plugins.dbpage.business.section.DbPageSection;
37 import fr.paris.lutece.portal.business.page.Page;
38 import fr.paris.lutece.portal.service.resource.Resource;
39 import fr.paris.lutece.portal.service.security.SecurityService;
40 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupResource;
41 import fr.paris.lutece.portal.service.workgroup.WorkgroupRemovalListenerService;
42
43 import java.util.List;
44
45 import javax.servlet.http.HttpServletRequest;
46
47
48 /**
49 * This class represents business object Page
50 */
51 public class DbPage implements Resource, AdminWorkgroupResource
52 {
53 public static final String RESOURCE_TYPE = "DBPAGE";
54 private static final String EMPTY_STRING = "";
55 private static DbPageWorkgroupRemovalListener _listenerWorkgroup;
56
57 /////////////////////////////////////////////////////////////////////////////////
58 // Constants
59 private int _nNbSection;
60 private String _strName;
61 private String _strTitle;
62 private String _strWorkgroup;
63 private List<DbPageSection> _listSections;
64
65 /**
66 * Initialize the DbPage
67 */
68 public static void init( )
69 {
70 // Create removal listeners and register them
71 if ( _listenerWorkgroup == null )
72 {
73 _listenerWorkgroup = new DbPageWorkgroupRemovalListener( );
74 WorkgroupRemovalListenerService.getService( ).registerListener( _listenerWorkgroup );
75 }
76 }
77
78 /**
79 * Returns the number of sections of this dbpage.
80 *
81 * @return nNbSection the number of sections
82 */
83 public int getNbSection( )
84 {
85 return _nNbSection;
86 }
87
88 /**
89 * Sets the number of section of the dbpage to the specified integer.
90 *
91 * @param nNbSection the new number of section
92 */
93 public void setNbSection( int nNbSection )
94 {
95 _nNbSection = nNbSection;
96 }
97
98 /**
99 * Returns the name of this dbpage.
100 *
101 * @return the dbpage name
102 */
103 public String getName( )
104 {
105 return _strName;
106 }
107
108 /**
109 * Sets the name of the dbpage to the specified string.
110 *
111 * @param strName the new name
112 */
113 public void setName( String strName )
114 {
115 _strName = ( strName == null ) ? EMPTY_STRING : strName;
116 }
117
118 /**
119 * Returns the title of this dbpage.
120 *
121 * @return the dbpage title
122 */
123 public String getTitle( )
124 {
125 return _strTitle;
126 }
127
128 /**
129 * Sets the title of the dbpage to the specified string.
130 *
131 * @param strTitle the new email
132 */
133 public void setTitle( String strTitle )
134 {
135 _strTitle = ( strTitle == null ) ? EMPTY_STRING : strTitle;
136 }
137
138 /**
139 * Returns the sections of this dbpage.
140 *
141 * @return the dbpage sections
142 */
143 public List<DbPageSection> getListSection( )
144 {
145 return _listSections;
146 }
147
148 /**
149 * Sets the sections of the dbpage to the specified list.
150 * @param listSections Sets a list of sections
151 */
152 public void setListSection( List<DbPageSection> listSections )
153 {
154 _listSections = listSections;
155 }
156
157 /**
158 * The workgroup
159 * @return The workgroup
160 */
161 public String getWorkgroup( )
162 {
163 return _strWorkgroup;
164 }
165
166 /**
167 * Sets the workgroup
168 * @param strWorkgroup The workgroup
169 */
170 public void setWorkgroup( String strWorkgroup )
171 {
172 _strWorkgroup = ( strWorkgroup == null ) ? EMPTY_STRING : strWorkgroup;
173 }
174
175 /**
176 * Returns the DbPage Content
177 * @param request The request
178 * @param listValues The list of Values of parameters
179 * @return The HTML code of the DbPage content
180 */
181 public String getContent( List<String> listValues, HttpServletRequest request )
182 {
183 StringBuffer sbSections = new StringBuffer( );
184
185 for ( DbPageSection section : _listSections )
186 {
187 if ( isVisible( request, section.getRole( ) ) )
188 {
189 sbSections.append( section.getHtmlSection( listValues, request ) );
190 }
191 }
192
193 return sbSections.toString( );
194 }
195
196 /**
197 * Checks if the page is visible for the current user
198 * @param request The HTTP request
199 * @param strRole The role
200 * @return true if the page could be shown to the user
201 * @since v1.3.1
202 */
203 private boolean isVisible( HttpServletRequest request, String strRole )
204 {
205 if ( ( strRole == null ) || strRole.trim( ).equals( EMPTY_STRING ) )
206 {
207 return true;
208 }
209
210 if ( !strRole.equals( Page.ROLE_NONE ) && SecurityService.isAuthenticationEnable( ) )
211 {
212 return SecurityService.getInstance( ).isUserInRole( request, strRole );
213 }
214
215 return true;
216 }
217
218 /**
219 * Checks if the page is visible for the current user
220 * @param request The HTTP request
221 * @return true if the page could be shown to the user
222 * @since v1.3.1
223 */
224 public boolean isVisible( HttpServletRequest request )
225 {
226 return isVisible( request, getWorkgroup( ) );
227 }
228 }