View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.portal.business.prefs;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  /**
42   * User Preferences DAO
43   */
44  public abstract class AbstractUserPreferencesDAO implements IPreferencesDAO
45  {
46      private static final String SQL_COUNT = "SELECT COUNT(*) FROM ";
47      private final String _strSqlSelect = "SELECT pref_value FROM " + getPreferencesTable( ) + " WHERE id_user = ? AND pref_key = ?";
48      private final String _strSqlInsert = "INSERT INTO " + getPreferencesTable( ) + " ( pref_value , id_user, pref_key ) VALUES ( ?, ?, ? ) ";
49      private final String _strSqlUpdate = "UPDATE " + getPreferencesTable( ) + " SET pref_value = ? WHERE id_user = ? AND pref_key = ?";
50      private final String _strSqlDelete = "DELETE FROM " + getPreferencesTable( ) + " WHERE id_user = ? ";
51      private final String _strSqlSelectAll = "SELECT pref_key FROM " + getPreferencesTable( ) + " WHERE id_user = ?";
52      private final String _strSqlSelectByValue = "SELECT id_user FROM " + getPreferencesTable( ) + " WHERE pref_key = ? AND pref_value = ? ";
53      private final String _strSqlDeleteKey = _strSqlDelete + " AND pref_key = ? ";
54      private final String _strSqlDeleteKeyPrefix = _strSqlDelete + " AND pref_key LIKE ? ";
55      private final String _strSqlSelectCount = SQL_COUNT + getPreferencesTable( ) + " WHERE id_user = ? AND pref_key = ?";
56      private final String _strSqlSelectCountPrefValue = SQL_COUNT + getPreferencesTable( ) + " WHERE pref_key = ? AND pref_value = ?";
57  
58      /**
59       * Gets the preferences table
60       * 
61       * @return The table name that stores preferences
62       */
63      abstract String getPreferencesTable( );
64  
65      /**
66       * {@inheritDoc }
67       */
68      @Override
69      public String load( String strUserId, String strKey, String strDefault )
70      {
71          String strValue = strDefault;
72          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelect ) )
73          {
74              daoUtil.setString( 1, strUserId );
75              daoUtil.setString( 2, strKey );
76              daoUtil.executeQuery( );
77  
78              if ( daoUtil.next( ) )
79              {
80                  strValue = ( daoUtil.getString( 1 ) );
81              }
82  
83          }
84  
85          return strValue;
86      }
87  
88      /**
89       * {@inheritDoc }
90       */
91      @Override
92      public List<String> getUserId( String strKey, String strValue )
93      {
94          List<String> listUserId = new ArrayList<>( );
95          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelectByValue ) )
96          {
97              daoUtil.setString( 1, strKey );
98              daoUtil.setString( 2, strValue );
99              daoUtil.executeQuery( );
100 
101             while ( daoUtil.next( ) )
102             {
103                 listUserId.add( daoUtil.getString( 1 ) );
104             }
105 
106         }
107 
108         return listUserId;
109     }
110 
111     /**
112      * {@inheritDoc }
113      */
114     @Override
115     public void store( String strUserId, String strKey, String strValue )
116     {
117         String strSQL = _strSqlInsert;
118 
119         if ( existsKey( strUserId, strKey ) )
120         {
121             strSQL = _strSqlUpdate;
122         }
123 
124         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( strSQL ) )
125         {
126 
127             daoUtil.setString( 1, strValue );
128             daoUtil.setString( 2, strUserId );
129             daoUtil.setString( 3, strKey );
130 
131             daoUtil.executeUpdate( );
132         }
133     }
134 
135     /**
136      * {@inheritDoc }
137      */
138     @Override
139     public List<String> keys( String strUserId )
140     {
141         List<String> list = new ArrayList<>( );
142         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelectAll ) )
143         {
144             daoUtil.setString( 1, strUserId );
145             daoUtil.executeQuery( );
146 
147             while ( daoUtil.next( ) )
148             {
149                 list.add( daoUtil.getString( 1 ) );
150             }
151 
152         }
153 
154         return list;
155     }
156 
157     /**
158      * {@inheritDoc }
159      */
160     @Override
161     public void remove( String strUserId )
162     {
163         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlDelete ) )
164         {
165             daoUtil.setString( 1, strUserId );
166             daoUtil.executeUpdate( );
167         }
168     }
169 
170     /**
171      * {@inheritDoc }
172      */
173     @Override
174     public void removeKey( String strUserId, String strKey )
175     {
176         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlDeleteKey ) )
177         {
178             daoUtil.setString( 1, strUserId );
179             daoUtil.setString( 2, strKey );
180             daoUtil.executeUpdate( );
181         }
182     }
183 
184     /**
185      * {@inheritDoc }
186      */
187     @Override
188     public void removeKeyPrefix( String strUserId, String strKeyPrefix )
189     {
190         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlDeleteKeyPrefix ) )
191         {
192             daoUtil.setString( 1, strUserId );
193             daoUtil.setString( 2, "%" + strKeyPrefix );
194             daoUtil.executeUpdate( );
195         }
196     }
197 
198     /**
199      * {@inheritDoc }
200      */
201     @Override
202     public boolean existsKey( String strUserId, String strKey )
203     {
204         int nValue = 0;
205         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelectCount ) )
206         {
207             daoUtil.setString( 1, strUserId );
208             daoUtil.setString( 2, strKey );
209             daoUtil.executeQuery( );
210 
211             if ( daoUtil.next( ) )
212             {
213                 nValue = ( daoUtil.getInt( 1 ) );
214             }
215 
216         }
217 
218         return ( nValue != 0 );
219     }
220 
221     /**
222      * {@inheritDoc }
223      */
224     @Override
225     public boolean existsValueForKey( String strKey, String strValue )
226     {
227         int nValue = 0;
228         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelectCountPrefValue ) )
229         {
230             daoUtil.setString( 1, strKey );
231             daoUtil.setString( 2, strValue );
232             daoUtil.executeQuery( );
233 
234             if ( daoUtil.next( ) )
235             {
236                 nValue = ( daoUtil.getInt( 1 ) );
237             }
238 
239         }
240 
241         return ( nValue != 0 );
242     }
243 }