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.business.section;
35  
36  import fr.paris.lutece.plugins.dbpage.business.DbPageHome;
37  import fr.paris.lutece.portal.service.template.AppTemplateService;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.portal.service.util.AppPropertiesService;
40  import fr.paris.lutece.util.html.HtmlTemplate;
41  
42  import java.sql.SQLException;
43  
44  import java.util.ArrayList;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /**
53   * This class represents business object DbPageSectionTable
54   */
55  public class DbPageSectionTable extends DbPageSection
56  {
57      private static final long serialVersionUID = 3589380186475889829L;
58      private static final String MARK_RESULT_DATASET = "result_dataset";
59      private static final String MARK_SECTION_TITLE = "section_title";
60      private static final String MARK_COLUMN_NAMES = "column_names";
61      private static final String PROPERTY_FILES_PATH = "dbpage.files.path";
62      private static final String TEMPLATE_DEFAULT_TABLE = "skin/plugins/dbpage/default_table.html";
63      private static final String TEMPLATE_CREATION_TABLE = "admin/plugins/dbpage/create_section_table.html";
64      private static final String TEMPLATE_MODIFICATION_TABLE = "admin/plugins/dbpage/modify_section_table.html";
65  
66      /**
67       * Creates a section which of type table
68       * @param strDescType The description of the type of the section
69       */
70      public DbPageSectionTable( String strDescType )
71      {
72          this.setIdTypeSignature( serialVersionUID );
73          this.setDescType( strDescType );
74      }
75  
76      /**
77       * Returns the Html Section Table form
78       * @return the html code of the html Section Table form
79       * @param request The request
80       * @param listValues the list of id values substitute in the SQL request
81       */
82      public String getHtmlSection( List listValues, HttpServletRequest request )
83      {
84          HashMap rootModel = new HashMap(  );
85  
86          rootModel.put( MARK_SECTION_TITLE, getTitle(  ) );
87          rootModel.put( MARK_COLUMN_NAMES, getTableHeader(  ) );
88          rootModel.put( MARK_RESULT_DATASET, getTableDataset( getValuatedQuery( listValues ) ) );
89  
90          if ( getTemplatePath(  ).equals( "" ) )
91          {
92              HtmlTemplate tTable = AppTemplateService.getTemplate( TEMPLATE_DEFAULT_TABLE, request.getLocale(  ),
93                      rootModel );
94  
95              return tTable.getHtml(  );
96          }
97          else
98          {
99              String strFilePath = AppPropertiesService.getProperty( PROPERTY_FILES_PATH );
100             HtmlTemplate tSelect = AppTemplateService.getTemplate( getTemplatePath(  ), strFilePath,
101                     ( request == null ) ? null : request.getLocale(  ), rootModel );
102 
103             return tSelect.getHtml(  );
104         }
105     }
106 
107     /**
108      *  Returns the table headers as a list of string values
109      * @return The table headers
110      */
111     private List<String> getTableHeader(  )
112     {
113         List<String> listColumnNames = new ArrayList<String>(  );
114 
115         for ( String strColumnName : getColumnNames(  ) )
116         {
117             listColumnNames.add( strColumnName );
118         }
119 
120         return listColumnNames;
121     }
122 
123     /**
124      * Returns the table dataset
125      * @param strRequest The request
126      * @return A list of list of string values
127      */
128     private List<List<String>> getTableDataset( String strRequest )
129     {
130         List<List<String>> listDataset = null;
131 
132         try
133         {
134             listDataset = DbPageHome.selectRows( strRequest, getConnectionService( getDbPool(  ) ) );
135         }
136         catch ( SQLException e )
137         {
138             AppLogService.error( e );
139         }
140 
141         return listDataset;
142     }
143 
144     /**
145      * The type of the section
146      * @return The type of the section
147      */
148     public long getIdType(  )
149     {
150         return getIdTypeSignature(  );
151     }
152 
153     /**
154      * The description of the section type
155      * @return The description of the section type
156      */
157     public String getTypeDescription(  )
158     {
159         return getDescType(  );
160     }
161 
162     /**
163      * The path of the template for creation
164      * @return the path of the creation template
165      */
166     public String getCreationTemplate(  )
167     {
168         return TEMPLATE_CREATION_TABLE;
169     }
170 
171     /**
172      * The path of the template for modification
173      * @return the path of the modification template
174      */
175     public String getModificationTemplate(  )
176     {
177         return TEMPLATE_MODIFICATION_TABLE;
178     }
179 
180     /**
181      * Returns a map with additional markers
182      */
183     public Map<String, Object> getMarkMap(  )
184     {
185         Map map = new HashMap(  );
186 
187         return map;
188     }
189 }