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  
53      /**
54       * Insert a new record in the table.
55       * 
56       * @param entity
57       *            instance of the Entity object to insert
58       */
59      @Override
60      public void insert( DataEntity entity )
61      {
62          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
63          {
64  
65              daoUtil.setString( 1, entity.getKey( ) );
66              daoUtil.setString( 2, entity.getValue( ) );
67  
68              daoUtil.executeUpdate( );
69          }
70      }
71  
72      /**
73       * Load the data of the entity from the table
74       * 
75       * @param strKey
76       *            The identifier of the entity
77       * @return the instance of the Entity
78       */
79      @Override
80      public DataEntity load( String strKey )
81      {
82          DataEntity entity = null;
83          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
84          {
85              daoUtil.setString( 1, strKey );
86              daoUtil.executeQuery( );
87  
88              if ( daoUtil.next( ) )
89              {
90                  entity = new DataEntity( );
91  
92                  entity.setKey( daoUtil.getString( 1 ) );
93                  entity.setValue( daoUtil.getString( 2 ) );
94              }
95  
96          }
97  
98          return entity;
99      }
100 
101     /**
102      * Delete a record from the table
103      * 
104      * @param strKey
105      *            The identifier of the entity
106      */
107     @Override
108     public void delete( String strKey )
109     {
110         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
111         {
112             daoUtil.setString( 1, strKey );
113             daoUtil.executeUpdate( );
114         }
115     }
116 
117     /**
118      * Update the record in the table
119      * 
120      * @param entity
121      *            The reference of the entity
122      */
123     @Override
124     public void store( DataEntity entity )
125     {
126         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
127         {
128 
129             daoUtil.setString( 1, entity.getValue( ) );
130             daoUtil.setString( 2, entity.getKey( ) );
131 
132             daoUtil.executeUpdate( );
133         }
134     }
135 
136     /**
137      * Load the data of all the entitys and returns them as a List
138      * 
139      * @return The List which contains the data of all the entitys
140      */
141     @Override
142     public List<DataEntity> selectEntitiesList( )
143     {
144         List<DataEntity> entityList = new ArrayList<>( );
145         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
146         {
147             daoUtil.executeQuery( );
148 
149             while ( daoUtil.next( ) )
150             {
151                 DataEntityiness/datastore/DataEntity.html#DataEntity">DataEntity entity = new DataEntity( );
152 
153                 entity.setKey( daoUtil.getString( 1 ) );
154                 entity.setValue( daoUtil.getString( 2 ) );
155 
156                 entityList.add( entity );
157             }
158 
159         }
160 
161         return entityList;
162     }
163 }