View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.plugins.appstore.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.Collection;
41  import java.util.List;
42  
43  /**
44   * This class provides Data Access methods for Component objects
45   */
46  public final class ComponentDAO implements IComponentDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_component ) FROM appstore_component";
50      private static final String SQL_QUERY_SELECT = "SELECT id_component, group_id, title, description, artifact_id, version, component_type FROM appstore_component WHERE id_component = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO appstore_component ( id_component, group_id, title, description, artifact_id, version, component_type ) VALUES ( ?, ?, ?, ?, ?, ?, ? ) ";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM appstore_component WHERE id_component = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE appstore_component SET id_component = ?, group_id = ?, title = ?, description = ?, artifact_id = ?, version = ?, component_type = ? WHERE id_component = ?";
54      private static final String SQL_QUERY_SELECTALL = "SELECT id_component, group_id, title, description, artifact_id, version, component_type FROM appstore_component";
55      private static final String SQL_QUERY_SELECT_BY_APPLICATION = "SELECT a.id_component, a.group_id, a.title, a.description, a.artifact_id, a.version, a.component_type "
56              + " FROM appstore_component a , appstore_application_component b WHERE a.id_component = b.id_component AND b.id_application = ?";
57      private static final String SQL_QUERY_DELETE_APPLICATION = "DELETE FROM appstore_application_component WHERE id_component = ? ";
58  
59      /**
60       * Generates a new primary key
61       * 
62       * @param plugin
63       *            The Plugin
64       * @return The new primary key
65       */
66      public int newPrimaryKey( Plugin plugin )
67      {
68          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
69          daoUtil.executeQuery( );
70  
71          int nKey;
72  
73          daoUtil.next( );
74  
75          nKey = daoUtil.getInt( 1 ) + 1;
76          daoUtil.free( );
77  
78          return nKey;
79      }
80  
81      /**
82       * Insert a new record in the table.
83       * 
84       * @param component
85       *            instance of the Component object to insert
86       * @param plugin
87       *            The plugin
88       */
89      @Override
90      public void insert( Component component, Plugin plugin )
91      {
92          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
93  
94          component.setId( newPrimaryKey( plugin ) );
95  
96          daoUtil.setInt( 1, component.getId( ) );
97          daoUtil.setString( 2, component.getGroupId( ) );
98          daoUtil.setString( 3, component.getTitle( ) );
99          daoUtil.setString( 4, component.getDescription( ) );
100         daoUtil.setString( 5, component.getArtifactId( ) );
101         daoUtil.setString( 6, component.getVersion( ) );
102         daoUtil.setString( 7, component.getComponentType( ) );
103 
104         daoUtil.executeUpdate( );
105         daoUtil.free( );
106     }
107 
108     /**
109      * Load the data of the component from the table
110      * 
111      * @param nId
112      *            The identifier of the component
113      * @param plugin
114      *            The plugin
115      * @return the instance of the Component
116      */
117     @Override
118     public Component load( int nId, Plugin plugin )
119     {
120         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
121         daoUtil.setInt( 1, nId );
122         daoUtil.executeQuery( );
123 
124         Component component = null;
125 
126         if ( daoUtil.next( ) )
127         {
128             component = new Component( );
129 
130             component.setId( daoUtil.getInt( 1 ) );
131             component.setGroupId( daoUtil.getString( 2 ) );
132             component.setTitle( daoUtil.getString( 3 ) );
133             component.setDescription( daoUtil.getString( 4 ) );
134             component.setArtifactId( daoUtil.getString( 5 ) );
135             component.setVersion( daoUtil.getString( 6 ) );
136             component.setComponentType( daoUtil.getString( 7 ) );
137         }
138 
139         daoUtil.free( );
140 
141         return component;
142     }
143 
144     /**
145      * Delete a record from the table
146      * 
147      * @param nComponentId
148      *            The identifier of the component
149      * @param plugin
150      *            The plugin
151      */
152     @Override
153     public void delete( int nComponentId, Plugin plugin )
154     {
155         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_APPLICATION, plugin );
156         daoUtil.setInt( 1, nComponentId );
157         daoUtil.executeUpdate( );
158         daoUtil.free( );
159 
160         daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
161         daoUtil.setInt( 1, nComponentId );
162         daoUtil.executeUpdate( );
163         daoUtil.free( );
164     }
165 
166     /**
167      * Update the record in the table
168      * 
169      * @param component
170      *            The reference of the component
171      * @param plugin
172      *            The plugin
173      */
174     @Override
175     public void store( Component component, Plugin plugin )
176     {
177         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
178 
179         daoUtil.setInt( 1, component.getId( ) );
180         daoUtil.setString( 2, component.getGroupId( ) );
181         daoUtil.setString( 3, component.getTitle( ) );
182         daoUtil.setString( 4, component.getDescription( ) );
183         daoUtil.setString( 5, component.getArtifactId( ) );
184         daoUtil.setString( 6, component.getVersion( ) );
185         daoUtil.setString( 7, component.getComponentType( ) );
186         daoUtil.setInt( 8, component.getId( ) );
187 
188         daoUtil.executeUpdate( );
189         daoUtil.free( );
190     }
191 
192     /**
193      * Load the data of all the components and returns them as a collection
194      * 
195      * @param plugin
196      *            The plugin
197      * @return The Collection which contains the data of all the components
198      */
199     @Override
200     public Collection<Component> selectComponentsList( Plugin plugin )
201     {
202         Collection<Component> componentList = new ArrayList<Component>( );
203         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
204         daoUtil.executeQuery( );
205 
206         while ( daoUtil.next( ) )
207         {
208             Component component = new Component( );
209 
210             component.setId( daoUtil.getInt( 1 ) );
211             component.setGroupId( daoUtil.getString( 2 ) );
212             component.setTitle( daoUtil.getString( 3 ) );
213             component.setDescription( daoUtil.getString( 4 ) );
214             component.setArtifactId( daoUtil.getString( 5 ) );
215             component.setVersion( daoUtil.getString( 6 ) );
216             component.setComponentType( daoUtil.getString( 7 ) );
217 
218             componentList.add( component );
219         }
220 
221         daoUtil.free( );
222 
223         return componentList;
224     }
225 
226     @Override
227     public List<Component> selectByApplication( int nApplicationId, Plugin plugin )
228     {
229         List<Component> componentList = new ArrayList<Component>( );
230         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_APPLICATION, plugin );
231         daoUtil.setInt( 1, nApplicationId );
232         daoUtil.executeQuery( );
233 
234         while ( daoUtil.next( ) )
235         {
236             Component component = new Component( );
237 
238             component.setId( daoUtil.getInt( 1 ) );
239             component.setGroupId( daoUtil.getString( 2 ) );
240             component.setTitle( daoUtil.getString( 3 ) );
241             component.setDescription( daoUtil.getString( 4 ) );
242             component.setArtifactId( daoUtil.getString( 5 ) );
243             component.setVersion( daoUtil.getString( 6 ) );
244             component.setComponentType( daoUtil.getString( 7 ) );
245 
246             componentList.add( component );
247         }
248 
249         daoUtil.free( );
250 
251         return componentList;
252     }
253 }