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.Collection;
45  import java.util.HashMap;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Map;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  
53  /**
54   * This class represents business object DbPageSectionForm
55   */
56  public class DbPageSectionForm extends DbPageSection
57  {
58      private static final long serialVersionUID = 2087550201069955806L;
59      private static final String PROPERTY_ERROR_MESSAGE = "dbpage.section.properties.message.error";
60      private static final String MARK_SECTION_TITLE = "section_title";
61      private static final String MARK_SECTION_CONTENT = "section_content";
62      private static final String PROPERTY_FILES_PATH = "dbpage.files.path";
63      private static final String TEMPLATE_DEFAULT_FORM = "skin/plugins/dbpage/default_form.html";
64      private static final String TEMPLATE_CREATION_FORM = "admin/plugins/dbpage/create_section_form.html";
65      private static final String TEMPLATE_MODIFICATION_FORM = "admin/plugins/dbpage/modify_section_form.html";
66  
67      /**
68       * Constructor
69       * @param strDescType The type of the section
70       */
71      public DbPageSectionForm( String strDescType )
72      {
73          this.setIdTypeSignature( serialVersionUID );
74          this.setDescType( strDescType );
75      }
76  
77      /**
78       * Returns the Html Section Form
79       * @return the html code of the html Section Form
80       * @param request The request
81       * @param listValues The list of id values to substitute in the SQL request
82       */
83      public String getHtmlSection( List listValues, HttpServletRequest request )
84      {
85          HashMap<String, Object> rootModel = new HashMap<String, Object>(  );
86  
87          rootModel.put( MARK_SECTION_TITLE, getTitle(  ) );
88  
89          if ( getTemplatePath(  ).equals( "" ) )
90          {
91              HtmlTemplate tForm = AppTemplateService.getTemplate( TEMPLATE_DEFAULT_FORM, request.getLocale(  ), rootModel );
92              rootModel.put( MARK_SECTION_CONTENT, buildForm( tForm, listValues ) );
93  
94              return tForm.getHtml(  );
95          }
96          else
97          {
98              String strFilePath = AppPropertiesService.getProperty( PROPERTY_FILES_PATH );
99              HtmlTemplate tForm = AppTemplateService.getTemplate( getTemplatePath(  ), strFilePath,
100                     ( request == null ) ? null : request.getLocale(  ), rootModel );
101             rootModel.put( MARK_SECTION_CONTENT, buildForm( tForm, listValues ) );
102 
103             return tForm.getHtml(  );
104         }
105     }
106 
107     /**
108      * Returns the Html of the filled form
109      * @param template The html template
110      * @return the Html code of the filled form
111      * @param listValues The list of values substitute in the SQL request
112      */
113     public String buildForm( HtmlTemplate template, List listValues )
114     {
115         String strQuery = getValuatedQuery( listValues );
116 
117         if ( ( strQuery != null ) && !strQuery.equals( "" ) )
118         {
119             
120 
121             try
122             {
123                 List<List<String>> listRows = DbPageHome.selectRows( strQuery, getConnectionService( getDbPool(  ) ) );
124 
125                 if ( ( listRows != null ) && !listRows.isEmpty(  ) )
126                 {
127                     // Form values should be in the first row of the resultset 
128                     List<String> formRow = listRows.get( 0 );
129                     
130                     int nIndex = 0;
131                     for( String strColumnName : getColumnNames(  ) )
132                     {
133                         if( nIndex < formRow.size() )
134                         {
135                             String strElement = (String) formRow.get( nIndex++ );
136                             String strBookmark = "@" + strColumnName + "@";
137                             template.substitute( strBookmark, strElement );
138                         }
139                     }
140 
141                     return template.getHtml(  );
142                 }
143                 else
144                 {
145                     return AppPropertiesService.getProperty( PROPERTY_ERROR_MESSAGE );
146                 }
147             }
148             catch ( SQLException e )
149             {
150                 AppLogService.error( e );
151 
152                 return AppPropertiesService.getProperty( PROPERTY_ERROR_MESSAGE );
153             }
154         }
155         else
156         {
157             return template.getHtml(  );
158         }
159     }
160 
161     /**
162      * The type of the section
163      * @return The type of the section
164      */
165     public long getIdType(  )
166     {
167         return getIdTypeSignature(  );
168     }
169 
170     /**
171      * The description of the section type
172      * @return The description of the section type
173      */
174     public String getTypeDescription(  )
175     {
176         return getDescType(  );
177     }
178 
179     /**
180      * The path of the template for creation
181      * @return the path of the creation template
182      */
183     public String getCreationTemplate(  )
184     {
185         return TEMPLATE_CREATION_FORM;
186     }
187 
188     /**
189      * The path of the template for modification
190      * @return the path of the modification template
191      */
192     public String getModificationTemplate(  )
193     {
194         return TEMPLATE_MODIFICATION_FORM;
195     }
196 
197     /**
198      * Returns a map with additional markers
199      */
200     public Map<String, Object> getMarkMap(  )
201     {
202         Map map = new HashMap(  );
203 
204         return map;
205     }
206 }