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.datastore;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  /**
42   * This class provides Data Access methods for Entity objects
43   */
44  public final class DataEntityDAO implements IDataEntityDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_SELECT = "SELECT entity_key, entity_value FROM core_datastore WHERE entity_key = ?";
48      private static final String SQL_QUERY_INSERT = "INSERT INTO core_datastore ( entity_key, entity_value ) VALUES ( ?, ? ) ";
49      private static final String SQL_QUERY_DELETE = "DELETE FROM core_datastore WHERE entity_key = ? ";
50      private static final String SQL_QUERY_UPDATE = "UPDATE core_datastore SET entity_value = ? WHERE entity_key = ?";
51      private static final String SQL_QUERY_SELECTALL = "SELECT entity_key, entity_value FROM core_datastore";
52      private static final String SQL_QUERY_SELECTBYPREFIX = "SELECT entity_key, entity_value FROM core_datastore WHERE entity_key LIKE ?";
53  
54      /**
55       * Insert a new record in the table.
56       * 
57       * @param entity
58       *            instance of the Entity object to insert
59       */
60      @Override
61      public void insert( DataEntity entity )
62      {
63          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
64          {
65  
66              daoUtil.setString( 1, entity.getKey( ) );
67              daoUtil.setString( 2, entity.getValue( ) );
68  
69              daoUtil.executeUpdate( );
70          }
71      }
72  
73      /**
74       * Load the data of the entity from the table
75       * 
76       * @param strKey
77       *            The identifier of the entity
78       * @return the instance of the Entity
79       */
80      @Override
81      public DataEntity load( String strKey )
82      {
83          DataEntity entity = null;
84          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
85          {
86              daoUtil.setString( 1, strKey );
87              daoUtil.executeQuery( );
88  
89              if ( daoUtil.next( ) )
90              {
91                  entity = new DataEntity( );
92  
93                  entity.setKey( daoUtil.getString( 1 ) );
94                  entity.setValue( daoUtil.getString( 2 ) );
95              }
96  
97          }
98  
99          return entity;
100     }
101 
102     /**
103      * Delete a record from the table
104      * 
105      * @param strKey
106      *            The identifier of the entity
107      */
108     @Override
109     public void delete( String strKey )
110     {
111         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
112         {
113             daoUtil.setString( 1, strKey );
114             daoUtil.executeUpdate( );
115         }
116     }
117 
118     /**
119      * Update the record in the table
120      * 
121      * @param entity
122      *            The reference of the entity
123      */
124     @Override
125     public void store( DataEntity entity )
126     {
127         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
128         {
129 
130             daoUtil.setString( 1, entity.getValue( ) );
131             daoUtil.setString( 2, entity.getKey( ) );
132 
133             daoUtil.executeUpdate( );
134         }
135     }
136 
137     /**
138      * Load the data of all the entitys and returns them as a List
139      * 
140      * @return The List which contains the data of all the entitys
141      */
142     @Override
143     public List<DataEntity> selectEntitiesList( )
144     {
145         List<DataEntity> entityList = new ArrayList<>( );
146         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
147         {
148             daoUtil.executeQuery( );
149 
150             while ( daoUtil.next( ) )
151             {
152                 DataEntityiness/datastore/DataEntity.html#DataEntity">DataEntity entity = new DataEntity( );
153 
154                 entity.setKey( daoUtil.getString( 1 ) );
155                 entity.setValue( daoUtil.getString( 2 ) );
156 
157                 entityList.add( entity );
158             }
159 
160         }
161 
162         return entityList;
163     }
164 
165     @Override
166     public List<DataEntity> selectEntitiesByPrefix( String strPrefix )
167     {
168         List<DataEntity> entityList = new ArrayList<>( );
169         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTBYPREFIX ) )
170         {
171             daoUtil.setString( 1, strPrefix + "%" );
172             daoUtil.executeQuery( );
173 
174             while ( daoUtil.next( ) )
175             {
176                 DataEntityiness/datastore/DataEntity.html#DataEntity">DataEntity entity = new DataEntity( );
177 
178                 entity.setKey( daoUtil.getString( 1 ) );
179                 entity.setValue( daoUtil.getString( 2 ) );
180 
181                 entityList.add( entity );
182             }
183 
184         }
185         return entityList;
186     }
187 }