View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.directory.business.parameter;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceItem;
38  import fr.paris.lutece.util.ReferenceList;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  /**
42   *
43   * DirectoryParameterDAO
44   *
45   */
46  public class DirectoryParameterDAO implements IDirectoryParameterDAO
47  {
48      private static final String TRUE = "1";
49      private static final String SQL_QUERY_SELECT = " SELECT parameter_value FROM directory_directory_parameter WHERE parameter_key = ? ";
50      private static final String SQL_QUERY_UPDATE = " UPDATE directory_directory_parameter SET parameter_value = ? WHERE parameter_key = ? ";
51      private static final String SQL_QUERY_SELECT_ALL = " SELECT parameter_key, parameter_value FROM directory_directory_parameter ";
52      private static final String SQL_ORDER_BY = " ORDER BY ";
53      private static final String SQL_ASC = " ASC ";
54      private static final String SQL_PARAMETER_KEY = " parameter_key ";
55      private static final String SQL_WHERE = " WHERE ";
56      private static final String SQL_IN = " IN ";
57      private static final String SQL_NOT = " NOT ";
58      private static final String OPEN_BRACKET = " ( ";
59      private static final String CLOSED_BRACKET = " ) ";
60      private static final String SIMPLE_QUOTE = "'";
61      private static final String COMMA = ",";
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public ReferenceList selectAll( Plugin plugin )
68      {
69          ReferenceList listParams = new ReferenceList( );
70          String strSQL = SQL_QUERY_SELECT_ALL + SQL_ORDER_BY + SQL_PARAMETER_KEY + SQL_ASC;
71          DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
72          daoUtil.executeQuery( );
73  
74          while ( daoUtil.next( ) )
75          {
76              ReferenceItem param = new ReferenceItem( );
77              param.setCode( daoUtil.getString( 1 ) );
78              param.setName( daoUtil.getString( 2 ) );
79              param.setChecked( TRUE.equals( param.getName( ) ) ? true : false );
80              listParams.add( param );
81          }
82  
83          daoUtil.free( );
84  
85          return listParams;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public ReferenceItem load( String strParameterKey, Plugin plugin )
93      {
94          ReferenceItem param = null;
95          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
96          daoUtil.setString( 1, strParameterKey );
97          daoUtil.executeQuery( );
98  
99          if ( daoUtil.next( ) )
100         {
101             param = new ReferenceItem( );
102             param.setCode( strParameterKey );
103             param.setName( daoUtil.getString( 1 ) );
104             param.setChecked( TRUE.equals( param.getName( ) ) ? true : false );
105         }
106 
107         daoUtil.free( );
108 
109         return param;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public void store( ReferenceItem param, Plugin plugin )
117     {
118         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
119 
120         daoUtil.setString( 1, param.getName( ) );
121         daoUtil.setString( 2, param.getCode( ) );
122 
123         daoUtil.executeUpdate( );
124         daoUtil.free( );
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public ReferenceList selectByFilter( DirectoryParameterFilter filter, Plugin plugin )
132     {
133         // Build SQL query
134         StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_ALL );
135 
136         if ( filter.containsListParameterKeys( ) )
137         {
138             sbSQL.append( SQL_WHERE + SQL_PARAMETER_KEY );
139 
140             if ( filter.excludeParameterKeys( ) )
141             {
142                 sbSQL.append( SQL_NOT );
143             }
144 
145             sbSQL.append( SQL_IN );
146             sbSQL.append( OPEN_BRACKET );
147 
148             for ( int i = 0; i < filter.getListParameterKeys( ).size( ); i++ )
149             {
150                 String strParameterKey = filter.getListParameterKeys( ).get( i );
151                 sbSQL.append( SIMPLE_QUOTE );
152                 sbSQL.append( strParameterKey );
153                 sbSQL.append( SIMPLE_QUOTE );
154 
155                 if ( i < ( filter.getListParameterKeys( ).size( ) - 1 ) )
156                 {
157                     sbSQL.append( COMMA );
158                 }
159             }
160 
161             sbSQL.append( CLOSED_BRACKET );
162         }
163 
164         // Execute SQL query
165         ReferenceList listParams = new ReferenceList( );
166         DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin );
167         daoUtil.executeQuery( );
168 
169         while ( daoUtil.next( ) )
170         {
171             ReferenceItem param = new ReferenceItem( );
172             param.setCode( daoUtil.getString( 1 ) );
173             param.setName( daoUtil.getString( 2 ) );
174             param.setChecked( TRUE.equals( param.getName( ) ) ? true : false );
175             listParams.add( param );
176         }
177 
178         daoUtil.free( );
179 
180         return listParams;
181     }
182 }