View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.updatercatalog.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  
44  /**
45   * This class provides Data Access methods for CatalogPlugin objects
46   */
47  public final class CatalogPluginDAO implements ICatalogPluginDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_SELECT = "SELECT plugin_name, plugin_locale, plugin_description, plugin_author, url_homepage FROM updatercatalog_plugin WHERE plugin_name = ? AND plugin_locale = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO updatercatalog_plugin ( plugin_name, plugin_locale, plugin_description, plugin_author, url_homepage ) VALUES ( ?, ?, ?, ?, ? ) ";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM updatercatalog_plugin WHERE plugin_name = ?  AND plugin_locale = ?";
53      private static final String SQL_QUERY_UPDATE = "UPDATE updatercatalog_plugin SET plugin_name = ?, plugin_locale = ?, plugin_description = ?, plugin_author = ?, url_homepage = ? WHERE plugin_name = ?  AND plugin_locale = ? ";
54      private static final String SQL_QUERY_SELECTALL = "SELECT plugin_name, plugin_locale, plugin_description, plugin_author, url_homepage FROM updatercatalog_plugin";
55      private static final String SQL_QUERY_SELECT_BY_LOCALE = "SELECT plugin_name, plugin_description FROM updatercatalog_plugin WHERE plugin_locale = ?";
56      private static final String SQL_QUERY_SELECT_AVAILABLE_PLUGINS = "SELECT b.id_release , a.plugin_name, a.plugin_description, a.plugin_author, b.plugin_version, b.url_download, a.url_homepage " +
57          "FROM updatercatalog_plugin a, updatercatalog_plugin_release b " +
58          "WHERE a.plugin_name = b.plugin_name AND a.plugin_locale = ? ";
59      private static final String SQL_QUERY_SELECT_PLUGINS = "SELECT DISTINCT plugin_name FROM updatercatalog_plugin";
60  
61      /**
62       * Insert a new record in the table.
63       * @param catalogPlugin instance of the CatalogPlugin object to insert
64       * @param plugin The plugin
65       */
66      public void insert( CatalogPlugin catalogPlugin, Plugin plugin )
67      {
68          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
69  
70          daoUtil.setString( 1, catalogPlugin.getPluginName(  ) );
71          daoUtil.setString( 2, catalogPlugin.getPluginLocale(  ) );
72          daoUtil.setString( 3, catalogPlugin.getPluginDescription(  ) );
73          daoUtil.setString( 4, catalogPlugin.getPluginAuthor(  ) );
74          daoUtil.setString( 5, catalogPlugin.getUrlHomepage(  ) );
75  
76          daoUtil.executeUpdate(  );
77          daoUtil.free(  );
78      }
79  
80      /**
81       * Load the data of the catalogPlugin from the table
82       * @param strPluginName The identifier of the catalogPlugin
83       * @param plugin The plugin
84       * @param strLocale The locale
85       * @return the instance of the CatalogPlugin
86       */
87      public CatalogPlugin load( String strPluginName, String strLocale, Plugin plugin )
88      {
89          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
90          daoUtil.setString( 1, strPluginName );
91          daoUtil.setString( 2, strLocale );
92          daoUtil.executeQuery(  );
93  
94          CatalogPlugin catalogPlugin = null;
95  
96          if ( daoUtil.next(  ) )
97          {
98              catalogPlugin = new CatalogPlugin(  );
99  
100             catalogPlugin.setPluginName( daoUtil.getString( 1 ) );
101             catalogPlugin.setPluginLocale( daoUtil.getString( 2 ) );
102             catalogPlugin.setPluginDescription( daoUtil.getString( 3 ) );
103             catalogPlugin.setPluginAuthor( daoUtil.getString( 4 ) );
104             catalogPlugin.setUrlHomepage( daoUtil.getString( 5 ) );
105         }
106 
107         daoUtil.free(  );
108 
109         return catalogPlugin;
110     }
111 
112     /**
113      * Delete a record from the table
114      * @param strPluginName The identifier of the catalogPlugin
115      * @param strLocale The locale
116      * @param plugin The plugin
117      */
118     public void delete( String strPluginName, String strLocale, Plugin plugin )
119     {
120         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
121         daoUtil.setString( 1, strPluginName );
122         daoUtil.setString( 2, strLocale );
123         daoUtil.executeUpdate(  );
124         daoUtil.free(  );
125     }
126 
127     /**
128      * Update the record in the table
129      * @param catalogPlugin The reference of the catalogPlugin
130      * @param plugin The plugin
131      */
132     public void store( CatalogPlugin catalogPlugin, Plugin plugin )
133     {
134         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
135 
136         daoUtil.setString( 1, catalogPlugin.getPluginName(  ) );
137         daoUtil.setString( 2, catalogPlugin.getPluginLocale(  ) );
138         daoUtil.setString( 3, catalogPlugin.getPluginDescription(  ) );
139         daoUtil.setString( 4, catalogPlugin.getPluginAuthor(  ) );
140         daoUtil.setString( 5, catalogPlugin.getUrlHomepage(  ) );
141         daoUtil.setString( 6, catalogPlugin.getPluginName(  ) );
142         daoUtil.setString( 7, catalogPlugin.getPluginLocale(  ) );
143 
144         daoUtil.executeUpdate(  );
145         daoUtil.free(  );
146     }
147 
148     /**
149      * Load the data of all the catalogPlugins and returns them as a List
150      * @param plugin The plugin
151      * @return The List which contains the data of all the catalogPlugins
152      */
153     public List<CatalogPlugin> selectCatalogPluginsList( Plugin plugin )
154     {
155         List<CatalogPlugin> catalogPluginList = new ArrayList<CatalogPlugin>(  );
156         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
157         daoUtil.executeQuery(  );
158 
159         while ( daoUtil.next(  ) )
160         {
161             CatalogPlugin catalogPlugin = new CatalogPlugin(  );
162 
163             catalogPlugin.setPluginName( daoUtil.getString( 1 ) );
164             catalogPlugin.setPluginLocale( daoUtil.getString( 2 ) );
165             catalogPlugin.setPluginDescription( daoUtil.getString( 3 ) );
166             catalogPlugin.setPluginAuthor( daoUtil.getString( 4 ) );
167             catalogPlugin.setUrlHomepage( daoUtil.getString( 5 ) );
168 
169             catalogPluginList.add( catalogPlugin );
170         }
171 
172         daoUtil.free(  );
173 
174         return catalogPluginList;
175     }
176 
177     /**
178      * Returns a list of plugins for a given locale
179      * @param plugin The plugin
180      * @param strLocale The locale as String
181      * @return The list of plugins
182      */
183     public ReferenceList selectPluginsListByLocale( Plugin plugin, String strLocale )
184     {
185         ReferenceList list = new ReferenceList(  );
186         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_LOCALE, plugin );
187         daoUtil.setString( 1, strLocale );
188         daoUtil.executeQuery(  );
189 
190         while ( daoUtil.next(  ) )
191         {
192             list.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
193         }
194 
195         daoUtil.free(  );
196 
197         return list;
198     }
199 
200     /**
201      * Select available plugins for a given locale
202      * @param plugin The plugin
203      * @param strLocale The locale
204      * @return A list of plugins entries
205      */
206     public List<CatalogPluginEntry> selectAvailablePluginsByLocale( Plugin plugin, String strLocale )
207     {
208         List<CatalogPluginEntry> list = new ArrayList<CatalogPluginEntry>(  );
209         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_AVAILABLE_PLUGINS, plugin );
210         daoUtil.setString( 1, strLocale );
211         daoUtil.executeQuery(  );
212 
213         while ( daoUtil.next(  ) )
214         {
215             CatalogPluginEntry entry = new CatalogPluginEntry(  );
216 
217             entry.setReleaseId( daoUtil.getInt( 1 ) );
218             entry.setPluginName( daoUtil.getString( 2 ) );
219             entry.setPluginDescription( daoUtil.getString( 3 ) );
220             entry.setPluginAuthor( daoUtil.getString( 4 ) );
221             entry.setPluginVersion( daoUtil.getString( 5 ) );
222             entry.setUrlDownload( daoUtil.getString( 6 ) );
223             entry.setUrlHomepage( daoUtil.getString( 7 ) );
224 
225             list.add( entry );
226         }
227 
228         daoUtil.free(  );
229 
230         return list;
231     }
232 
233     /**
234      * Gets the plugins list
235      * @param plugin the plugin
236      * @return A reference list
237      */
238     public ReferenceList getPlugins( Plugin plugin )
239     {
240         ReferenceList list = new ReferenceList(  );
241         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PLUGINS, plugin );
242         daoUtil.executeQuery(  );
243 
244         while ( daoUtil.next(  ) )
245         {
246             String strPluginName = daoUtil.getString( 1 );
247             list.addItem( strPluginName, strPluginName );
248         }
249 
250         daoUtil.free(  );
251 
252         return list;
253     }
254 }