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.service.DbPageConnectionService;
37  import fr.paris.lutece.portal.service.database.PluginConnectionService;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.util.string.StringUtil;
40  
41  import java.util.List;
42  
43  
44  /**
45   * This class represents business object DbPageSection
46   */
47  public abstract class DbPageSection implements IDbPageSection, Cloneable
48  {
49      private static final String EMPTY_STRING = "";
50  
51      /////////////////////////////////////////////////////////////////////////////////
52      // Constants
53      private long _lIdType;
54      private String _strDescType;
55      private String _strType;
56      private String _strTitle;
57      private String _strTemplatePath;
58      private List<String> _listColumnNames;
59      private String _strSQL;
60      private String _strDbPool;
61      private String _strRole;
62  
63      /**
64       * Returns the type of this DbPageSection.
65       *
66       * @return the DbPageSection type
67       */
68      public String getType(  )
69      {
70          return _strType;
71      }
72  
73      /**
74       * Sets the type of the DbPageSection to the specified string.
75       *
76       * @param strType the new type
77       */
78      public void setType( String strType )
79      {
80          _strType = ( strType == null ) ? EMPTY_STRING : strType;
81      }
82  
83      /**
84      * Returns the template path
85      *
86      * @return the relative path of the template
87      */
88      public String getTemplatePath(  )
89      {
90          return _strTemplatePath;
91      }
92  
93      /**
94      * Sets the template path of the DbPageSection to the specified string.
95      * @param strTemplatePath The new template path
96      */
97      public void setTemplatePath( String strTemplatePath )
98      {
99          _strTemplatePath = ( strTemplatePath == null ) ? EMPTY_STRING : strTemplatePath;
100     }
101 
102     /**
103      * Returns the ColumnNames
104      *
105      * @return The list of the Columns' name
106      */
107     public List<String> getColumnNames(  )
108     {
109         return _listColumnNames;
110     }
111 
112     /**
113      * Sets the listColumnNames
114      *
115      * @param listColumnNames the list of columns names
116      */
117     public void setColumnNames( List listColumnNames )
118     {
119         _listColumnNames = listColumnNames;
120     }
121 
122     /**
123      * Returns the title of the section
124      *
125      * @return The title
126      */
127     public String getTitle(  )
128     {
129         return _strTitle;
130     }
131 
132     /**
133      * Sets the title of the section
134      *
135      * @param strTitle The new title
136      */
137     public void setTitle( String strTitle )
138     {
139         _strTitle = ( strTitle == null ) ? EMPTY_STRING : strTitle;
140     }
141 
142     /**
143      * Returns the sql request of the section
144      *
145      * @return The SQL request
146      */
147     public String getSql(  )
148     {
149         return _strSQL;
150     }
151 
152     /**
153      * Sets the SQL request
154      *
155      * @param strSql The SQL request
156      */
157     public void setSql( String strSql )
158     {
159         _strSQL = ( strSql == null ) ? EMPTY_STRING : strSql;
160     }
161 
162     /**
163      * Returns the DbPool of the section
164      *
165      * @return The DbPool
166      */
167     public String getDbPool(  )
168     {
169         return _strDbPool;
170     }
171 
172     /**
173      * Sets the DbPool
174      *
175      * @param strDbPool The DbPool
176      */
177     public void setDbPool( String strDbPool )
178     {
179         _strDbPool = ( strDbPool == null ) ? EMPTY_STRING : strDbPool;
180     }
181 
182     /**
183      * Get a connection service corresponding to the given poolname. If the pool
184      * name is empty, the default connection service of the current plugin is returned
185      * @return A Connection Service
186      * @param strPoolName The Poolname
187      */
188     public PluginConnectionService getConnectionService( String strPoolName )
189     {
190         return DbPageConnectionService.getConnectionService( strPoolName );
191     }
192 
193     /**
194      * Returns the SQL query associated to the section filled by extra values
195      * @param listValues A list of extra values to substitute in the query
196      * @return The SQL query
197      */
198     protected String getValuatedQuery( List listValues )
199     {
200         return substituteValues( getSql(  ), listValues );
201     }
202 
203     /**
204      *
205      * @param strQuery the request
206      * @param listValues The list of values
207      * @return the query
208      */
209     private String substituteValues( String strQuery, List listValues )
210     {
211         String strResult = strQuery;
212 
213         for ( int i = 0; i < listValues.size(  ); i++ )
214         {
215             String strValue = (String) listValues.get( i );
216             String strBookmark = "@value" + ( i + 1 ) + "@";
217             strResult = StringUtil.substitute( strResult, strValue, strBookmark );
218         }
219 
220         return strResult;
221     }
222 
223     /**
224      * Returns the Role of the section
225      *
226      * @return The Role
227      */
228     public String getRole(  )
229     {
230         return _strRole;
231     }
232 
233     /**
234      * Sets the Role
235      *
236      * @param strRole The Role
237      */
238     public void setRole( String strRole )
239     {
240         _strRole = ( strRole == null ) ? EMPTY_STRING : strRole;
241     }
242 
243     /**
244      * A DbPageSection object is produced
245      * @return Gets a section
246      */
247     public DbPageSection getDbSection(  )
248     {
249         DbPageSection sect = null;
250 
251         try
252         {
253             sect = (DbPageSection) this.clone(  );
254         }
255         catch ( CloneNotSupportedException e )
256         {
257             AppLogService.error( e );
258         }
259 
260         return sect;
261     }
262 
263     /**
264      * Sets the signature of the section
265      * @param lIdType The type of the section id
266      */
267     public void setIdTypeSignature( long lIdType )
268     {
269         this._lIdType = lIdType;
270     }
271 
272     /**
273      * Gets the signature of the section
274      * @return The long value representing the type of the section
275      */
276     public long getIdTypeSignature(  )
277     {
278         return _lIdType;
279     }
280 
281     /**
282      * Sets the type of the section
283      * @param strDescType The description of the type of the section
284      */
285     public void setDescType( String strDescType )
286     {
287         this._strDescType = strDescType;
288     }
289 
290     /**
291      * The type of the section
292      * @return The description of the type of the section
293      */
294     public String getDescType(  )
295     {
296         return _strDescType;
297     }
298 }