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.plugins.pluginwizard.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * This class provides Data Access methods for Model objects
44   */
45  public final class ModelDAO implements IModelDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_plugin ) FROM pluginwizard_plugin_model";
49      private static final String SQL_QUERY_SELECT = "SELECT id_plugin, name, model_json FROM pluginwizard_plugin_model WHERE id_plugin = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO pluginwizard_plugin_model ( id_plugin, name, model_json ) VALUES ( ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM pluginwizard_plugin_model WHERE id_plugin = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE pluginwizard_plugin_model SET name = ?, model_json = ? WHERE id_plugin = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_plugin, name, model_json FROM pluginwizard_plugin_model";
54  
55      /**
56       * Generates a new primary key
57       *
58       * @param plugin
59       *            The Plugin
60       * @return The new primary key
61       */
62      public int newPrimaryKey( Plugin plugin )
63      {
64          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
65          {
66              daoUtil.executeQuery( );
67  
68              int nKey;
69  
70              daoUtil.next( );
71              nKey = daoUtil.getInt( 1 ) + 1;
72              daoUtil.free( );
73  
74              return nKey;
75          }
76      }
77  
78      /**
79       * Insert a new record in the table.
80       *
81       * @param model
82       *            instance of the Model object to insert
83       * @param plugin
84       *            The plugin
85       */
86      @Override
87      public void insert( Model model, Plugin plugin )
88      {
89          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
90          {
91  
92              model.setIdPlugin( newPrimaryKey( plugin ) );
93  
94              daoUtil.setInt( 1, model.getIdPlugin( ) );
95              daoUtil.setString( 2, model.getName( ) );
96              daoUtil.setString( 3, model.getModelJson( ) );
97  
98              daoUtil.executeUpdate( );
99              daoUtil.free( );
100         }
101     }
102 
103     /**
104      * Load the data of the model from the table
105      *
106      * @param nId
107      *            The identifier of the model
108      * @param plugin
109      *            The plugin
110      * @return the instance of the Model
111      */
112     @Override
113     public Model load( int nId, Plugin plugin )
114     {
115         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
116         {
117             daoUtil.setInt( 1, nId );
118             daoUtil.executeQuery( );
119 
120             Model model = null;
121 
122             if ( daoUtil.next( ) )
123             {
124                 model = new Model( );
125 
126                 model.setIdPlugin( daoUtil.getInt( 1 ) );
127                 model.setName( daoUtil.getString( 2 ) );
128                 model.setModelJson( daoUtil.getString( 3 ) );
129             }
130 
131             daoUtil.free( );
132 
133             return model;
134         }
135     }
136 
137     /**
138      * Delete a record from the table
139      *
140      * @param nModelId
141      *            The identifier of the model
142      * @param plugin
143      *            The plugin
144      */
145     @Override
146     public void delete( int nModelId, Plugin plugin )
147     {
148         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
149         {
150             daoUtil.setInt( 1, nModelId );
151             daoUtil.executeUpdate( );
152             daoUtil.free( );
153         }
154     }
155 
156     /**
157      * Update the record in the table
158      *
159      * @param model
160      *            The reference of the model
161      * @param plugin
162      *            The plugin
163      */
164     @Override
165     public void store( Model model, Plugin plugin )
166     {
167         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
168         {
169 
170             daoUtil.setString( 1, model.getName( ) );
171             daoUtil.setString( 2, model.getModelJson( ) );
172             daoUtil.setInt( 3, model.getIdPlugin( ) );
173 
174             daoUtil.executeUpdate( );
175             daoUtil.free( );
176         }
177     }
178 
179     /**
180      * Load the data of all the models and returns them as a List
181      *
182      * @param plugin
183      *            The plugin
184      * @return The List which contains the data of all the models
185      */
186     @Override
187     public List<Model> selectModelsList( Plugin plugin )
188     {
189         List<Model> modelList = new ArrayList<>( );
190         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
191         {
192             daoUtil.executeQuery( );
193 
194             while ( daoUtil.next( ) )
195             {
196                 Modelins/pluginwizard/business/Model.html#Model">Model model = new Model( );
197 
198                 model.setIdPlugin( daoUtil.getInt( 1 ) );
199                 model.setName( daoUtil.getString( 2 ) );
200                 model.setModelJson( daoUtil.getString( 3 ) );
201 
202                 modelList.add( model );
203             }
204 
205             daoUtil.free( );
206 
207             return modelList;
208         }
209     }
210 }