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.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  /**
43   * This class provides Data Access methods for Entity objects
44   */
45  public final class DataEntityDAO implements IDataEntityDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_SELECT = "SELECT entity_key, entity_value FROM core_datastore WHERE entity_key = ?";
49      private static final String SQL_QUERY_INSERT = "INSERT INTO core_datastore ( entity_key, entity_value ) VALUES ( ?, ? ) ";
50      private static final String SQL_QUERY_DELETE = "DELETE FROM core_datastore WHERE entity_key = ? ";
51      private static final String SQL_QUERY_UPDATE = "UPDATE core_datastore SET entity_value = ? WHERE entity_key = ?";
52      private static final String SQL_QUERY_SELECTALL = "SELECT entity_key, entity_value FROM core_datastore";
53  
54      /**
55       * Insert a new record in the table.
56       * @param entity instance of the Entity object to insert
57       */
58      @Override
59      public void insert( DataEntity entity )
60      {
61          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
62  
63          daoUtil.setString( 1, entity.getKey(  ) );
64          daoUtil.setString( 2, entity.getValue(  ) );
65  
66          daoUtil.executeUpdate(  );
67          daoUtil.free(  );
68      }
69  
70      /**
71       * Load the data of the entity from the table
72       * @param strKey The identifier of the entity
73       * @return the instance of the Entity
74       */
75      @Override
76      public DataEntity load( String strKey )
77      {
78          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
79          daoUtil.setString( 1, strKey );
80          daoUtil.executeQuery(  );
81  
82          DataEntity entity = null;
83  
84          if ( daoUtil.next(  ) )
85          {
86              entity = new DataEntity(  );
87  
88              entity.setKey( daoUtil.getString( 1 ) );
89              entity.setValue( daoUtil.getString( 2 ) );
90          }
91  
92          daoUtil.free(  );
93  
94          return entity;
95      }
96  
97      /**
98       * Delete a record from the table
99       * @param strKey The identifier of the entity
100      */
101     @Override
102     public void delete( String strKey )
103     {
104         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
105         daoUtil.setString( 1, strKey );
106         daoUtil.executeUpdate(  );
107         daoUtil.free(  );
108     }
109 
110     /**
111      * Update the record in the table
112      * @param entity The reference of the entity
113      */
114     @Override
115     public void store( DataEntity entity )
116     {
117         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
118 
119         daoUtil.setString( 1, entity.getValue(  ) );
120         daoUtil.setString( 2, entity.getKey(  ) );
121 
122         daoUtil.executeUpdate(  );
123         daoUtil.free(  );
124     }
125 
126     /**
127      * Load the data of all the entitys and returns them as a List
128      * @return The List which contains the data of all the entitys
129      */
130     @Override
131     public List<DataEntity> selectEntitiesList(  )
132     {
133         List<DataEntity> entityList = new ArrayList<DataEntity>(  );
134         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
135         daoUtil.executeQuery(  );
136 
137         while ( daoUtil.next(  ) )
138         {
139             DataEntity entity = new DataEntity(  );
140 
141             entity.setKey( daoUtil.getString( 1 ) );
142             entity.setValue( daoUtil.getString( 2 ) );
143 
144             entityList.add( entity );
145         }
146 
147         daoUtil.free(  );
148 
149         return entityList;
150     }
151 }