View Javadoc
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.web;
35  
36  import fr.paris.lutece.plugins.dbpage.business.DbPageDatabase;
37  import fr.paris.lutece.plugins.dbpage.business.DbPageDatabaseHome;
38  import fr.paris.lutece.plugins.dbpage.business.DbPageDatabaseSection;
39  import fr.paris.lutece.plugins.dbpage.business.DbPageDatabaseSectionHome;
40  import fr.paris.lutece.plugins.dbpage.business.DbPageHome;
41  import fr.paris.lutece.plugins.dbpage.business.portlet.DbPagePortletHome;
42  import fr.paris.lutece.plugins.dbpage.business.section.DbSectionService;
43  import fr.paris.lutece.plugins.dbpage.service.DbPageConnectionService;
44  import fr.paris.lutece.portal.business.role.RoleHome;
45  import fr.paris.lutece.portal.service.database.AppConnectionService;
46  import fr.paris.lutece.portal.service.message.AdminMessage;
47  import fr.paris.lutece.portal.service.message.AdminMessageService;
48  import fr.paris.lutece.portal.service.template.AppTemplateService;
49  import fr.paris.lutece.portal.service.util.AppLogService;
50  import fr.paris.lutece.portal.service.util.AppPathService;
51  import fr.paris.lutece.portal.service.util.AppPropertiesService;
52  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
53  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
54  import fr.paris.lutece.portal.web.constants.Messages;
55  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
56  import fr.paris.lutece.util.ReferenceList;
57  import fr.paris.lutece.util.filesystem.UploadUtil;
58  import fr.paris.lutece.util.html.HtmlTemplate;
59  import fr.paris.lutece.util.html.Paginator;
60  import fr.paris.lutece.util.url.UrlItem;
61  
62  import java.io.File;
63  import java.io.FileFilter;
64  import java.io.FileOutputStream;
65  import java.io.IOException;
66  import java.sql.SQLException;
67  import java.util.Collection;
68  import java.util.HashMap;
69  import java.util.List;
70  import java.util.Map;
71  import java.util.Stack;
72  
73  import javax.servlet.http.HttpServletRequest;
74  
75  import org.apache.commons.fileupload.FileItem;
76  import org.apache.commons.lang.StringUtils;
77  
78  
79  /**
80   * This class provides the user interface to manage dbpage features ( manage, create, modify, remove the dbpage)
81   *
82   */
83  public class DbPageJspBean extends PluginAdminPageJspBean
84  {
85      /**
86       * The rights attributes
87       */
88      public static final String RIGHT_DBPAGE_MANAGEMENT = "DBPAGE_MANAGEMENT";
89  
90      //The Templates
91      private static final String TEMPLATE_DBPAGES = "admin/plugins/dbpage/manage_dbpages.html";
92      private static final String TEMPLATE_MODIFY_DBPAGE = "admin/plugins/dbpage/modify_dbpage.html";
93      private static final String TEMPLATE_CREATE_DBPAGE = "admin/plugins/dbpage/create_dbpage.html";
94      private static final String TEMPLATE_CREATE_SECTION_TYPE = "admin/plugins/dbpage/create_section_type.html";
95  
96      //The Bookmark
97      private static final String MARK_DBPAGE = "dbpage";
98      private static final String MARK_DBPAGE_ID = "dbpage_id";
99      private static final String MARK_DBPAGE_SECTIONS = "dbpage_sections";
100     private static final String MARK_SECTION_POOL_LIST = "pool_list";
101     private static final String MARK_SECTION_ID = "section_id";
102     private static final String MARK_TYPE_LIST = "type_list";
103     private static final String MARK_SECTION = "section";
104     private static final String MARK_SECTION_ROLE_LIST = "role_list";
105     private static final String MARK_WORKGROUP_LIST = "workgroup_list";
106 
107     //The Parameter
108     private static final String PARAMETER_DBPAGE_ID = "dbpage_id";
109     private static final String PARAMETER_DBPAGE_NAME = "dbpage_name";
110     private static final String PARAMETER_DBPAGE_TITLE = "dbpage_title";
111     private static final String PARAMETER_SECTION_ID = "section_id";
112     private static final String PARAMETER_SECTION_ORDER = "section_order";
113     private static final String PARAMETER_ITEMS_PER_PAGE = "items_per_page";
114     private static final String PARAMETER_SECTION_TYPE_ID = "type_id";
115     private static final String PARAMETER_SECTION_DESC_SQL = "section_desc_sql";
116     private static final String PARAMETER_SECTION_DESC_COLUMN = "section_desc_column";
117     private static final String PARAMETER_SECTION_POOL = "section_pool";
118     private static final String PARAMETER_SECTION_TITLE = "section_title";
119     private static final String PARAMETER_SECTION_CHECK_SQL = "section_sql_check";
120     private static final String PARAMETER_SECTION_TEMPLATE = "section_template";
121     private static final String PARAMETER_SECTION_ROLE_ID = "role";
122     private static final String PARAMETER_TEMPLATE_DEFAULT = "template_default";
123     private static final String PARAMETER_TEMPLATE_UPDATE = "section_template_update";
124     private static final String PARAMETER_PAGE_INDEX = "page_index";
125     private static final String PARAMETER_DBPAGE_WORKGROUP = "dbpage_workgroup";
126     private static final String PROPERTY_FILES_PATH = "dbpage.files.path";
127     private static final String PROPERTY_DBPAGE_PER_PAGE = "dbpage.dbPagePerPage";
128     private static final String PROPERTY_PAGE_TITLE_CREATE = "dbpage.create_dbpage.pageTitle";
129     private static final String PROPERTY_PAGE_MODIFY_SECTION = "dbpage.modify_section.pageTitle";
130     private static final String PROPERTY_PAGE_CREATE_SECTION = "dbpage.create_section.pageTitle";
131     private static final String PROPERTY_PAGE_CREATE_SECTION_TYPE = "dbpage.create_section_type.pageTitle";
132     private static final String PROPERTY_PAGE_TITLE_DBPAGES = "dbpage.manage_dbpages.pageTitle";
133     private static final String PROPERTY_DEFAULT_ITEMS_PER_PAGE = "dbpage.itemsPerPage";
134 
135     //Bookmark
136     private static final String MARK_PAGINATOR = "paginator";
137     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
138     private static final String MARK_DBPAGE_LIST = "dbpage_list";
139     private static final String MARK_SECTION_TYPE_ID_LIST = "section_type_id_list";
140     private static final String MARK_TYPE_ID = "type_id";
141 
142     //Messages
143     private static final String MESSAGE_DBPAGE_LINKED_PORTLET = "dbpage.message.dbpageLinkedPortlet";
144     private static final String MESSAGE_CONFIRM_DELETE_DBPAGE = "dbpage.message.confirmDeleteDbPage";
145     private static final String MESSAGE_CONFIRM_REMOVE_SECTION = "dbpage.message.confirmRemoveSection";
146     private static final String MESSAGE_DBPAGE_NAME_ALREADY_EXISTS = "dbpage.message.nameAlreadyExists";
147     private static final String MESSAGE_SQL_ERROR_IN_QUERY = "dbpage.message.sqlErrorInQuery";
148     private static final String MESSAGE_SECTION_TEMPLATE_CHOICE = "message.sectionTemplateModifyChoice";
149 
150     // Jsp
151     private static final String JSP_DELETE_DBPAGE = "jsp/admin/plugins/dbpage/DoDeleteDbPage.jsp";
152     private static final String JSP_DO_REMOVE_SECTION = "jsp/admin/plugins/dbpage/DoRemoveSection.jsp";
153     private static final String URL_MODIFY_DBPAGE_JSP = "jsp/admin/plugins/dbpage/ModifyDbPage.jsp";
154     private static final String URL_MODIFY_SECTION_JSP = "jsp/admin/plugins/dbpage/ModifySection.jsp";
155     private static final String JSP_MANAGE_DBPAGE = "ManageDbPages.jsp";
156     private static final String JSP_MODIFY_DBPAGE = "ModifyDbPage.jsp";
157     private static final FileFilter dirFilter = new FileFilter(  )
158         {
159             public boolean accept( File pathname )
160             {
161                 return pathname.isDirectory(  );
162             }
163         };
164 
165     //Variables
166     private int _nItemsPerPage;
167     private String _strCurrentPageIndex;
168 
169     /**
170      * Creates a new DbPageJspBean object.
171      */
172     public DbPageJspBean(  )
173     {
174     }
175 
176     /**
177      * Returns the list of dbPages
178      *
179      * @param request The Http request
180      * @return the dbPage list
181      */
182     public String getManageDbPages( HttpServletRequest request )
183     {
184         setPageTitleProperty( PROPERTY_PAGE_TITLE_DBPAGES );
185         _nItemsPerPage = getItemsPerPage( request );
186         _strCurrentPageIndex = getPageIndex( request );
187 
188         Collection<DbPageDatabase> colDbPage = DbPageDatabaseHome.findDbPageDatabasesList( getPlugin(  ) );
189         colDbPage = AdminWorkgroupService.getAuthorizedCollection( colDbPage, getUser(  ) );
190 
191         Paginator paginator = new Paginator( (List<DbPageDatabase>) colDbPage, _nItemsPerPage, getHomeUrl( request ),
192                 PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
193         Map<String, Object> model = new HashMap<String, Object>(  );
194         model.put( MARK_PAGINATOR, paginator );
195         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
196         model.put( MARK_DBPAGE_LIST, paginator.getPageItems(  ) );
197 
198         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_DBPAGES, getLocale(  ), model );
199 
200         return getAdminPage( templateList.getHtml(  ) );
201     }
202 
203     /**
204      * Returns the form to create a section
205      *
206      * @param request The Http request
207      * @return the html code of the dbPage form
208      */
209     public String getCreateSection( HttpServletRequest request )
210     {
211         setPageTitleProperty( PROPERTY_PAGE_CREATE_SECTION );
212 
213         Map<String, Object> model = new HashMap<String, Object>(  );
214         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
215         String strIdType = request.getParameter( PARAMETER_SECTION_TYPE_ID );
216 
217         Collection<DbPageDatabase> colDbPage = DbPageDatabaseHome.findDbPageDatabasesList( getPlugin(  ) );
218         colDbPage = AdminWorkgroupService.getAuthorizedCollection( colDbPage, getUser(  ) );
219 
220         if ( colDbPage.size(  ) == 0 )
221         {
222             // Return dbpage management page
223             return getManageDbPages( request );
224         }
225 
226         int nPageId = Integer.parseInt( strPageId );
227         model.put( MARK_DBPAGE_ID, nPageId );
228         model.put( MARK_TYPE_ID, Long.toString( DbSectionService.INSTANCE.findById( strIdType ).getIdType(  ) ) );
229 
230         //Add roles List
231         ReferenceList roleList = RoleHome.getRolesList(  );
232         model.put( MARK_SECTION_ROLE_LIST, roleList );
233 
234         ReferenceList listPools = new ReferenceList(  );
235         AppConnectionService.getPoolList( listPools );
236 
237         model.put( MARK_SECTION_POOL_LIST, listPools );
238         model.putAll( DbSectionService.INSTANCE.findAdditionalMap( strIdType ) );
239 
240 
241         HtmlTemplate template = AppTemplateService.getTemplate( DbSectionService.INSTANCE.findCreationTemplate( 
242                     strIdType ), getLocale(  ), model );
243 
244         return getAdminPage( template.getHtml(  ) );
245     }
246 
247     /**
248      * Returns the form to create a section
249      *
250      * @param request The Http request
251      * @return the html code of the dbPage form
252      */
253     public String getCreateSectionType( HttpServletRequest request )
254     {
255         setPageTitleProperty( PROPERTY_PAGE_CREATE_SECTION_TYPE );
256 
257         Map<String, Object> model = new HashMap<String, Object>(  );
258 
259         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
260 
261         model.put( MARK_DBPAGE_ID, strPageId );
262         model.put( MARK_SECTION_TYPE_ID_LIST, DbSectionService.INSTANCE.findDbPageDatabaseTypesList(  ) );
263 
264         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_SECTION_TYPE, getLocale(  ), model );
265 
266         return getAdminPage( template.getHtml(  ) );
267     }
268 
269     /**
270      * Returns the form to update the section
271      *
272      * @param request The Http request
273      * @return the html code of the dbPage form
274      */
275     public String getModifySection( HttpServletRequest request )
276     {
277         setPageTitleProperty( PROPERTY_PAGE_MODIFY_SECTION );
278 
279         Map<String, Object> model = new HashMap<String, Object>(  );
280         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
281         int nPageId = Integer.parseInt( strPageId );
282         DbPageDatabase dbPageDatabase = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
283 
284         if ( !AdminWorkgroupService.isAuthorized( dbPageDatabase, getUser(  ) ) )
285         {
286             // Return dbpage management page
287             return getManageDbPages( request );
288         }
289 
290         int nSectionId = Integer.parseInt( request.getParameter( PARAMETER_SECTION_ID ) );
291         DbPageDatabaseSection section = DbPageDatabaseSectionHome.findByPrimaryKey( nSectionId, getPlugin(  ) );
292         String strIdType = section.getIdType(  );
293         ReferenceList listPools = new ReferenceList(  );
294         AppConnectionService.getPoolList( listPools );
295         model.put( MARK_DBPAGE_ID, nPageId );
296         model.put( MARK_SECTION_ID, nSectionId );
297         model.put( MARK_SECTION, section );
298         // model.put( MARK_SECTION_TYPE_LIST, DbPageDatabaseTypeHome.findTypeComboList( getPlugin(  ) ) );
299         model.put( MARK_SECTION_POOL_LIST, listPools );
300         model.put( MARK_TYPE_ID, strIdType );
301 
302         //Add roles List
303         ReferenceList roleList = RoleHome.getRolesList(  );
304         model.put( MARK_SECTION_ROLE_LIST, roleList );
305         model.putAll( DbSectionService.INSTANCE.findAdditionalMap( strIdType ) );
306 
307         HtmlTemplate template = AppTemplateService.getTemplate( DbSectionService.INSTANCE.findModificationTemplate( 
308                     strIdType ), getLocale(  ), model );
309 
310         return getAdminPage( template.getHtml(  ) );
311     }
312 
313     /**
314      * Process the change form of a dbPage
315      *
316      * @param request The Http request
317      * @return The Jsp URL of the process result
318      */
319     public String doModifyDbPage( HttpServletRequest request )
320     {
321         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
322         int nPageId = Integer.parseInt( strPageId );
323         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
324 
325         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
326         {
327             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
328         }
329 
330         dbPage.setParamName( request.getParameter( PARAMETER_DBPAGE_NAME ) );
331         dbPage.setTitle( request.getParameter( PARAMETER_DBPAGE_TITLE ) );
332         dbPage.setWorkgroup( request.getParameter( PARAMETER_DBPAGE_WORKGROUP ) );
333 
334         if ( request.getParameter( PARAMETER_DBPAGE_TITLE ).equals( "" ) )
335         {
336             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
337         }
338 
339         DbPageDatabaseHome.update( dbPage, getPlugin(  ) );
340 
341         return JSP_MANAGE_DBPAGE;
342     }
343 
344     /**
345      * Creates a section
346      *
347      * @param request The Http request
348      * @return The Jsp URL of the process result
349      */
350     public String doCreateSection( HttpServletRequest request )
351     {
352         boolean bDefaultTemplate = true;
353 
354         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
355 
356         String strDefaultTemplate = multipartRequest.getParameter( PARAMETER_TEMPLATE_DEFAULT );
357         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
358         int nPageId = Integer.parseInt( strPageId );
359         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
360 
361         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
362         {
363             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
364         }
365 
366         String strPageDesc = UploadUtil.cleanFileName( dbPage.getParamName(  ) );
367         String strDirectoryPath = AppPropertiesService.getProperty( PROPERTY_FILES_PATH );
368         String strAbsolutePath = AppPathService.getWebAppPath(  ) + strDirectoryPath + strPageDesc;
369 
370         if ( !new File( strAbsolutePath ).isDirectory(  ) )
371         {
372             File fileFolder = new File( strAbsolutePath );
373             fileFolder.mkdir(  );
374         }
375 
376         String strSectionTitle = multipartRequest.getParameter( PARAMETER_SECTION_TITLE );
377 
378         if ( strSectionTitle.equals( "" ) )
379         {
380             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
381         }
382 
383         if ( ( strDefaultTemplate == null ) || !strDefaultTemplate.equals( "on" ) )
384         {
385             bDefaultTemplate = false;
386         }
387 
388         DbPageDatabaseSection section = new DbPageDatabaseSection(  );
389         section.setTitle( strSectionTitle );
390 
391         String strSql = multipartRequest.getParameter( PARAMETER_SECTION_DESC_SQL );
392         String strPoolName = multipartRequest.getParameter( PARAMETER_SECTION_POOL );
393         String strCheckSql = multipartRequest.getParameter( PARAMETER_SECTION_CHECK_SQL );
394 
395         try
396         {
397             if ( ( strCheckSql != null ) && strCheckSql.equals( "on" ) )
398             {
399                 DbPageHome.selectRows( getSqlToCheck( strSql ), DbPageConnectionService.getConnectionService( strPoolName ) );
400             }
401         }
402         catch ( SQLException e )
403         {
404             UrlItem url = new UrlItem( URL_MODIFY_DBPAGE_JSP );
405             url.addParameter( PARAMETER_DBPAGE_ID, strPageId );
406 
407             Object[] messageArgs = { e.getMessage(  ) };
408 
409             return AdminMessageService.getMessageUrl( request, MESSAGE_SQL_ERROR_IN_QUERY, messageArgs, url.getUrl(  ),
410                 AdminMessage.TYPE_STOP );
411         }
412 
413         section.setSql( strSql );
414         section.setColumn( multipartRequest.getParameter( PARAMETER_SECTION_DESC_COLUMN ) );
415         section.setPool( strPoolName );
416         section.setIdType( multipartRequest.getParameter( PARAMETER_SECTION_TYPE_ID ) );
417         section.setIdPage( nPageId );
418         section.setRole( multipartRequest.getParameter( PARAMETER_SECTION_ROLE_ID ) );
419 
420         int nIdOrder = DbPageDatabaseSectionHome.getMaxIdByOrder( nPageId, getPlugin(  ) );
421         section.setOrder( nIdOrder + 1 );
422 
423         if ( !bDefaultTemplate )
424         {
425         	FileItem fileItem = multipartRequest.getFile( PARAMETER_SECTION_TEMPLATE );
426         	
427         	try
428         	{
429         		if ( fileItem != null )
430         		{
431         			localTemplateFile( section, fileItem, strPageDesc, bDefaultTemplate, true );
432         		}
433         		else
434         		{
435         			section.setTemplatePath( StringUtils.EMPTY );
436         		}
437         	}
438         	catch ( IOException e )
439         	{
440         		AppLogService.error( e );
441         	}
442         }
443         else
444         {
445         	section.setTemplatePath( StringUtils.EMPTY );
446         }
447 
448         DbPageDatabaseSectionHome.create( section, getPlugin(  ) );
449 
450         UrlItem url = new UrlItem( JSP_MODIFY_DBPAGE );
451         url.addParameter( PARAMETER_DBPAGE_ID, nPageId );
452 
453         return url.getUrl(  );
454     }
455 
456     /**
457      * Modifies a section
458      *
459      * @param request The Http request
460      * @return The Jsp URL of the process result
461      */
462     public String doModifySection( HttpServletRequest request )
463     {
464         boolean bDefaultTemplate = true;
465         boolean bUpdateTemplate = true;
466 
467         try
468         {
469             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
470 
471             String strSectionId = multipartRequest.getParameter( PARAMETER_SECTION_ID );
472             int nSectionId = Integer.parseInt( strSectionId );
473             String strDefaultTemplate = multipartRequest.getParameter( PARAMETER_TEMPLATE_DEFAULT );
474             String strUpdateTemplate = multipartRequest.getParameter( PARAMETER_TEMPLATE_UPDATE );
475             String strSectionTitle = multipartRequest.getParameter( PARAMETER_SECTION_TITLE );
476             String strCheckSql = multipartRequest.getParameter( PARAMETER_SECTION_CHECK_SQL );
477 
478             if ( strSectionTitle.equalsIgnoreCase( "" ) )
479             {
480                 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
481             }
482 
483             if ( ( strDefaultTemplate == null ) || !strDefaultTemplate.equals( "on" ) )
484             {
485                 bDefaultTemplate = false;
486             }
487 
488             if ( ( strUpdateTemplate == null ) || !strUpdateTemplate.equals( "on" ) )
489             {
490                 bUpdateTemplate = false;
491             }
492 
493             if ( bUpdateTemplate && bDefaultTemplate )
494             {
495                 return AdminMessageService.getMessageUrl( request, MESSAGE_SECTION_TEMPLATE_CHOICE,
496                     AdminMessage.TYPE_STOP );
497             }
498 
499             DbPageDatabaseSection section = DbPageDatabaseSectionHome.findByPrimaryKey( nSectionId, getPlugin(  ) );
500             int nOrder = section.getOrder(  );
501             int nPageId = section.getIdPage(  );
502             String strPageId = Integer.toString( nPageId );
503             DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
504 
505             if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
506             {
507                 return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
508             }
509 
510             String strPageDesc = UploadUtil.cleanFileName( dbPage.getParamName(  ) );
511             section.setIdPage( nPageId );
512             section.setOrder( nOrder );
513 
514             String strSql = multipartRequest.getParameter( PARAMETER_SECTION_DESC_SQL );
515             String strPoolName = multipartRequest.getParameter( PARAMETER_SECTION_POOL );
516 
517             try
518             {
519                 if ( ( strCheckSql != null ) && strCheckSql.equals( "on" ) )
520                 {
521                     DbPageHome.selectRows( getSqlToCheck( strSql ) , DbPageConnectionService.getConnectionService( strPoolName ) );
522                 }
523             }
524             catch ( SQLException e )
525             {
526                 UrlItem url = new UrlItem( URL_MODIFY_SECTION_JSP );
527                 url.addParameter( PARAMETER_SECTION_ID, strSectionId );
528                 url.addParameter( PARAMETER_DBPAGE_ID, strPageId );
529 
530                 Object[] messageArgs = { e.getMessage(  ) };
531 
532                 return AdminMessageService.getMessageUrl( request, MESSAGE_SQL_ERROR_IN_QUERY, messageArgs,
533                     url.getUrl(  ), AdminMessage.TYPE_STOP );
534             }
535 
536             section.setIdType( multipartRequest.getParameter( PARAMETER_SECTION_TYPE_ID ) );
537             section.setColumn( multipartRequest.getParameter( PARAMETER_SECTION_DESC_COLUMN ) );
538             section.setPool( strPoolName );
539             section.setTitle( multipartRequest.getParameter( PARAMETER_SECTION_TITLE ) );
540             section.setSql( strSql );
541             section.setRole( multipartRequest.getParameter( PARAMETER_SECTION_ROLE_ID ) );
542 
543             if ( bUpdateTemplate || bDefaultTemplate )
544             {
545                 FileItem fileItem = multipartRequest.getFile( PARAMETER_SECTION_TEMPLATE );
546                 localTemplateFile( section, fileItem, strPageDesc, bDefaultTemplate, bUpdateTemplate );
547             }
548 
549             DbPageDatabaseSectionHome.update( section, getPlugin(  ) );
550 
551             UrlItem url = new UrlItem( JSP_MODIFY_DBPAGE );
552             url.addParameter( PARAMETER_DBPAGE_ID, strPageId );
553 
554             return url.getUrl(  );
555         }
556         catch ( Exception e )
557         {
558             AppLogService.error( e.getMessage(  ), e );
559 
560             return getHomeUrl( request );
561         }
562     }
563 
564     /**
565      * Returns the form to create a DbPage
566      *
567      * @param request The Http request
568      * @return the html code of the contact form
569      */
570     public String getCreateDbPage( HttpServletRequest request )
571     {
572         HashMap model = new HashMap(  );
573         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE );
574         model.put( MARK_WORKGROUP_LIST, AdminWorkgroupService.getUserWorkgroups( getUser(  ), getLocale(  ) ) );
575 
576         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_DBPAGE, getLocale(  ), model );
577 
578         return getAdminPage( template.getHtml(  ) );
579     }
580 
581     /**
582      * Process the creation of a DbPage
583      *
584      * @param request The Http Request
585      * @return The Jsp URL of the process result
586      */
587     public String doCreateDbPage( HttpServletRequest request )
588     {
589         DbPageDatabase dbPage = new DbPageDatabase(  );
590 
591         if ( request.getParameter( PARAMETER_DBPAGE_NAME ).equals( "" ) ||
592                 request.getParameter( PARAMETER_DBPAGE_TITLE ).equals( "" ) )
593         {
594             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
595         }
596 
597         if ( DbPageDatabaseHome.findByName( request.getParameter( PARAMETER_DBPAGE_NAME ), getPlugin(  ) ) != null )
598         {
599             return AdminMessageService.getMessageUrl( request, MESSAGE_DBPAGE_NAME_ALREADY_EXISTS,
600                 AdminMessage.TYPE_STOP );
601         }
602 
603         dbPage.setParamName( request.getParameter( PARAMETER_DBPAGE_NAME ) );
604         dbPage.setTitle( request.getParameter( PARAMETER_DBPAGE_TITLE ) );
605         dbPage.setWorkgroup( request.getParameter( PARAMETER_DBPAGE_WORKGROUP ) );
606 
607         DbPageDatabaseHome.create( dbPage, getPlugin(  ) );
608 
609         DbPageDatabase dbPagedb = DbPageDatabaseHome.findByName( dbPage.getParamName(  ), getPlugin(  ) );
610         UrlItem url = new UrlItem( JSP_MODIFY_DBPAGE );
611         url.addParameter( PARAMETER_DBPAGE_ID, dbPagedb.getId(  ) );
612 
613         return url.getUrl(  );
614     }
615 
616     /**
617      * Process the deletion of a DbPage
618      *
619      * @param request The Http Request
620      * @return The Jsp URL of the process result
621      */
622     public String doDeleteDbPage( HttpServletRequest request )
623     {
624         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
625         int nPageId = Integer.parseInt( strPageId );
626 
627         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
628 
629         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
630         {
631             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
632         }
633 
634         String strDirectoryPath = AppPropertiesService.getProperty( PROPERTY_FILES_PATH );
635         String strDbPageName = UploadUtil.cleanFileName( dbPage.getParamName(  ) );
636         String filePath = AppPathService.getWebAppPath(  ) + strDirectoryPath + strDbPageName;
637         File file = new File( filePath );
638         deleteDirectory( file );
639         DbPageDatabaseHome.remove( nPageId, getPlugin(  ) );
640 
641         return getHomeUrl( request );
642     }
643 
644     /**
645      * Process the deletion of a section
646      *
647      * @param request The Http Request
648      * @return The Jsp URL of the process result
649      */
650     public String doRemoveSection( HttpServletRequest request )
651     {
652         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
653         int nPageId = Integer.parseInt( strPageId );
654         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
655 
656         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
657         {
658             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
659         }
660 
661         int nSectionId = Integer.parseInt( request.getParameter( PARAMETER_SECTION_ID ) );
662         DbPageDatabaseSectionHome.remove( nSectionId, getPlugin(  ) );
663 
664         UrlItem url = new UrlItem( JSP_MODIFY_DBPAGE );
665         url.addParameter( PARAMETER_DBPAGE_ID, strPageId );
666 
667         return url.getUrl(  );
668     }
669 
670     /**
671      * Returns the form for modification of a dbpage
672      * @param request  the request
673      * @return The modification form
674      */
675     public String getModifyDbPage( HttpServletRequest request )
676     {
677         setPageTitleProperty( PROPERTY_PAGE_MODIFY_SECTION );
678 
679         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
680         int nPageId = Integer.parseInt( strPageId );
681         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
682 
683         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
684         {
685             // Return dbpage management page
686             return getManageDbPages( request );
687         }
688 
689         List<DbPageDatabaseSection> listSections = DbPageDatabaseSectionHome.findSectionsByPage( nPageId, getPlugin(  ) );
690 
691         // List<DbPageDatabaseType> listTypes = DbPageDatabaseTypeHome.findDbPageDatabaseTypesList( getPlugin(  ) );
692         ReferenceList listTypes = DbSectionService.INSTANCE.findDbPageDatabaseTypesList(  );
693         Map<String, Object> model = new HashMap<String, Object>(  );
694         model.put( MARK_DBPAGE, dbPage );
695         model.put( MARK_DBPAGE_SECTIONS, listSections );
696         model.put( MARK_TYPE_LIST, listTypes );
697         model.put( MARK_WORKGROUP_LIST, AdminWorkgroupService.getUserWorkgroups( getUser(  ), getLocale(  ) ) );
698 
699         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_DBPAGE, getLocale(  ), model );
700 
701         return getAdminPage( template.getHtml(  ) );
702     }
703 
704     /**
705      * Used by the paginator to fetch a number of items
706      * @param request The HttpRequest
707      * @return The number of items
708      */
709     private int getItemsPerPage( HttpServletRequest request )
710     {
711         int nItemsPerPage;
712         String strItemsPerPage = request.getParameter( PARAMETER_ITEMS_PER_PAGE );
713         int nDefaultItemsPerPage = Integer.parseInt( AppPropertiesService.getProperty( PROPERTY_DEFAULT_ITEMS_PER_PAGE ) );
714 
715         if ( strItemsPerPage != null )
716         {
717             nItemsPerPage = Integer.parseInt( strItemsPerPage );
718         }
719         else
720         {
721             if ( _nItemsPerPage != 0 )
722             {
723                 nItemsPerPage = _nItemsPerPage;
724             }
725             else
726             {
727                 nItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DBPAGE_PER_PAGE, nDefaultItemsPerPage );
728             }
729         }
730 
731         return nItemsPerPage;
732     }
733 
734     /**
735      * Fetches the page index
736      * @param request The HttpRequest
737      * @return The PageIndex
738      */
739     private String getPageIndex( HttpServletRequest request )
740     {
741         String strPageIndex = request.getParameter( PARAMETER_PAGE_INDEX );
742         strPageIndex = ( strPageIndex != null ) ? strPageIndex : _strCurrentPageIndex;
743 
744         return strPageIndex;
745     }
746 
747     /**
748      * Process the confirmation of the removal of a dbpage
749      *
750      * @param request The Http request
751      * @return The Jsp URL of the process result
752      */
753     public String doConfirmDelete( HttpServletRequest request )
754     {
755         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
756         int nPageId = Integer.parseInt( strPageId );
757         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
758 
759         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
760         {
761             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
762         }
763 
764         String strDeleteUrl = JSP_DELETE_DBPAGE + "?" + PARAMETER_DBPAGE_ID + "=" + strPageId;
765         String strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_DBPAGE_LINKED_PORTLET,
766                 AdminMessage.TYPE_STOP );
767 
768         // Check whether a dbpage is linked to a portlet
769         if ( !DbPagePortletHome.checkNoDbPageInPortlet( dbPage.getParamName(  ) ) )
770         {
771             //Delete all sections associated to the dbpage
772             List<DbPageDatabaseSection> listSections = DbPageDatabaseSectionHome.findSectionsByPage( nPageId,
773                     getPlugin(  ) );
774 
775             for ( DbPageDatabaseSection section : listSections )
776             {
777                 DbPageDatabaseSectionHome.remove( section.getId(  ), getPlugin(  ) );
778             }
779 
780             DbPageDatabase dbpage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
781             Object[] messageArgs = { dbpage.getTitle(  ) };
782             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_DBPAGE, messageArgs,
783                     strDeleteUrl, AdminMessage.TYPE_CONFIRMATION );
784         }
785 
786         return strUrl;
787     }
788 
789     /**
790      * Manages the removal form of a section whose identifier is in the http request
791      * @param request The Http request
792      * @return the html code to confirm
793      */
794     public String getConfirmRemoveSection( HttpServletRequest request )
795     {
796         UrlItem url = new UrlItem( JSP_DO_REMOVE_SECTION );
797         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
798         int nPageId = Integer.parseInt( strPageId );
799         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
800 
801         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
802         {
803             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
804         }
805 
806         int nIdSection = Integer.parseInt( request.getParameter( PARAMETER_SECTION_ID ) );
807         url.addParameter( PARAMETER_SECTION_ID, nIdSection );
808         url.addParameter( PARAMETER_DBPAGE_ID, strPageId );
809 
810         DbPageDatabaseSection section = DbPageDatabaseSectionHome.findByPrimaryKey( nIdSection, getPlugin(  ) );
811         Object[] messageArgs = { section.getTitle(  ) };
812 
813         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_SECTION, messageArgs, url.getUrl(  ),
814             AdminMessage.TYPE_CONFIRMATION );
815     }
816 
817     /**
818      * Create and Update the local downloaded file
819      * @param section The section
820      * @param fileItem The fileItem extracted from the request
821      * @param strDbPageName The dbpage name
822      * @param bDefaultTemplate Whether to use a default template or not
823      * @param bUpdateTemplate Whether to update template
824      * @throws java.io.IOException Exception throwed while writing the template file
825      */
826     private void localTemplateFile( DbPageDatabaseSection section, FileItem fileItem, String strDbPageName,
827         boolean bDefaultTemplate, boolean bUpdateTemplate )
828         throws IOException
829     {
830         String strFileName = fileItem.getName(  );
831         File file = new File( strFileName );
832 
833         if ( StringUtils.isNotBlank( file.getName(  ) ) && !strDbPageName.equals( null ) )
834         {
835             String strNameFile = file.getName(  );
836 
837             String strDirectoryPath = AppPropertiesService.getProperty( PROPERTY_FILES_PATH );
838             String filePath = AppPathService.getWebAppPath(  ) + strDirectoryPath + strDbPageName + "/" + strNameFile;
839 
840             //if file is not a directory and default template is off
841             if ( !bDefaultTemplate && !new File( filePath ).isDirectory(  ) && bUpdateTemplate )
842             {
843                 file = new File( filePath );
844 
845                 if ( file.exists(  ) )
846                 {
847                     file.delete(  );
848                 }
849 
850                 FileOutputStream fosFile = new FileOutputStream( file );
851                 fosFile.flush(  );
852                 fosFile.write( fileItem.get(  ) );
853                 fosFile.close(  );
854                 section.setTemplatePath( strDbPageName + "/" + strNameFile );
855             }
856         }
857         else
858         {
859             section.setTemplatePath( "" );
860         }
861     }
862 
863     /**
864      * Performs the move up action
865      * @param request The HTTP request
866      * @return The URL to go after performing the action
867      */
868     public String doSectionMoveUp( HttpServletRequest request )
869     {
870         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
871         int nPageId = Integer.parseInt( strPageId );
872         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
873 
874         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
875         {
876             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
877         }
878 
879         int nIdOrder = Integer.parseInt( request.getParameter( PARAMETER_SECTION_ORDER ) );
880 
881         if ( nIdOrder > 1 )
882         {
883             List<DbPageDatabaseSection> list = DbPageDatabaseSectionHome.findSectionsByPage( nPageId, getPlugin(  ) );
884 
885             DbPageDatabaseSection section1 = list.get( nIdOrder - 1 );
886             DbPageDatabaseSection section2 = list.get( nIdOrder - 2 );
887             DbPageDatabaseSectionHome.reorderSections( section1.getId(  ), nIdOrder - 1, section2.getId(  ), nIdOrder,
888                 getPlugin(  ) );
889         }
890 
891         UrlItem url = new UrlItem( JSP_MODIFY_DBPAGE );
892         url.addParameter( PARAMETER_DBPAGE_ID, nPageId );
893 
894         return url.getUrl(  );
895     }
896 
897     /**
898      * Performs the move down action
899      * @param request The HTTP request
900      * @return The URL to go after performing the action
901      */
902     public String doSectionMoveDown( HttpServletRequest request )
903     {
904         String strPageId = request.getParameter( PARAMETER_DBPAGE_ID );
905         int nPageId = Integer.parseInt( strPageId );
906 
907         DbPageDatabase dbPage = DbPageDatabaseHome.findByPrimaryKey( nPageId, getPlugin(  ) );
908 
909         if ( !AdminWorkgroupService.isAuthorized( dbPage, getUser(  ) ) )
910         {
911             return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR );
912         }
913 
914         int nIdOrder = Integer.parseInt( request.getParameter( PARAMETER_SECTION_ORDER ) );
915 
916         List<DbPageDatabaseSection> list = DbPageDatabaseSectionHome.findSectionsByPage( nPageId, getPlugin(  ) );
917 
918         if ( nIdOrder < list.size(  ) )
919         {
920             DbPageDatabaseSection section1 = list.get( nIdOrder - 1 );
921             DbPageDatabaseSection section2 = list.get( nIdOrder );
922             DbPageDatabaseSectionHome.reorderSections( section1.getId(  ), nIdOrder + 1, section2.getId(  ), nIdOrder,
923                 getPlugin(  ) );
924         }
925 
926         UrlItem url = new UrlItem( JSP_MODIFY_DBPAGE );
927         url.addParameter( PARAMETER_DBPAGE_ID, nPageId );
928 
929         return url.getUrl(  );
930     }
931 
932     /**
933      * Deletes a directory recursively.
934      * @param fdirectory The directory to be deleted
935      */
936     private static void deleteDirectory( File fdirectory )
937     {
938         // We use a Stack (LIFO) to keep track of the directories to delete
939         Stack<File> dirsToDelete = new Stack<File>(  );
940 
941         if ( fdirectory.exists(  ) )
942         {
943             // The stack is initialized with the main directory
944             dirsToDelete.push( fdirectory );
945 
946             // Loop until all directories have been deleted
947             while ( !dirsToDelete.empty(  ) )
948             {
949                 // Look at the directory on top of the stack (don't remove it!)
950                 File currentDir = (File) dirsToDelete.peek(  );
951 
952                 // Are there any subdirectories?
953                 File[] subDirs = currentDir.listFiles( dirFilter );
954 
955                 if ( ( subDirs.length > 0 ) && ( subDirs != null ) )
956                 {
957                     // If so, add them to the stack
958                     for ( int i = 0; i < subDirs.length; i++ )
959                     {
960                         dirsToDelete.push( subDirs[i] );
961                     }
962                 }
963                 else
964                 {
965                     // If not, delete all files in the directory
966                     File[] files = currentDir.listFiles(  );
967 
968                     for ( int i = 0; i < files.length; i++ )
969                     {
970                         files[i].delete(  );
971                     }
972 
973                     // Then delete the directory
974                     currentDir.delete(  );
975                     // Then remove the directory from the stack
976                     dirsToDelete.pop(  );
977                 }
978             }
979         }
980     }
981 
982     private String getSqlToCheck(String strSql)
983     {
984         // remove bookmarks from the SQL request
985         return strSql.replaceAll( "@value(.*)@", "1" );
986     }
987 }