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.insertajax.business;
35  
36  import java.sql.Timestamp;
37  import java.util.ArrayList;
38  import java.util.Collection;
39  
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.util.sql.DAOUtil;
42  
43  
44  /**
45   * This class provides Data Access methods for InsertAjax objects
46   */
47  public class InsertAjaxDAO implements IInsertAjaxDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEWPK = "SELECT max( id_insertajax_request ) FROM insertajax_request ";
51      private static final String SQL_QUERY_SELECT = "SELECT id_insertajax_request, name, html, sqlrequest, workgroup_key, role FROM insertajax_request WHERE id_insertajax_request = ? ";
52      private static final String SQL_QUERY_SELECTALL = "SELECT id_insertajax_request, name, sqlrequest, workgroup_key, role FROM insertajax_request";
53      private static final String SQL_QUERY_SELECT_ENABLED_insertAjax_LIST = "SELECT id_insertajax_request, name, html, sqlrequest, workgroup_key FROM insertajax_request";
54      private static final String SQL_QUERY_INSERT = "INSERT INTO insertajax_request ( id_insertajax_request , name, html, sqlrequest, workgroup_key, role, date_update )  VALUES ( ?, ?, ?, ?, ?, ?, ? ) ";
55      private static final String SQL_QUERY_DELETE = "DELETE FROM insertajax_request WHERE id_insertajax_request = ? ";
56      private static final String SQL_QUERY_UPDATE = "UPDATE insertajax_request SET name = ? , html = ?, sqlrequest = ?, workgroup_key = ?, role = ?, date_update = ? WHERE id_insertajax_request = ?  ";
57      private static final String SQL_LIST_IMG = "SELECT id_document, summary FROM document WHERE id_space = ?";
58      
59      /**
60      * Generates a new primary key
61      * @param plugin The plugin
62      * @return The new primary key
63      */
64      private int newPrimaryKey( Plugin plugin )
65      {
66          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEWPK, plugin );
67          daoUtil.executeQuery(  );
68  
69          int nKey;
70  
71          if ( !daoUtil.next(  ) )
72          {
73              // if the table is empty
74              nKey = 1;
75          }
76  
77          nKey = daoUtil.getInt( 1 ) + 1;
78  
79          daoUtil.free(  );
80  
81          return nKey;
82      }
83  
84      ////////////////////////////////////////////////////////////////////////
85      // Methods using a dynamic pool
86  
87      /**
88       * Insert a new record in the table.
89       *
90       * @param insertAjax The insertAjax object
91       * @param plugin The plugin
92       */
93      public void insert( InsertAjax insertAjax, Plugin plugin )
94      {
95          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
96          int id = newPrimaryKey( plugin );
97          insertAjax.replacePrimaryKey(id);
98          insertAjax.setId( id );
99          daoUtil.setInt( 1, insertAjax.getId(  ) );
100         daoUtil.setString( 2, insertAjax.getName(  ) );
101         daoUtil.setString( 3, insertAjax.getHtml(  ) );
102         daoUtil.setString( 4, insertAjax.getSql(  ) );
103         daoUtil.setString( 5, insertAjax.getWorkgroup(  ) );
104         daoUtil.setString( 6, insertAjax.getRole(  ) );
105         daoUtil.setTimestamp( 7, new Timestamp( new java.util.Date(  ).getTime(  ) ) );
106 
107         daoUtil.executeUpdate(  );
108         daoUtil.free(  );
109     }
110 
111     /**
112      * Load the data of insertAjax from the table
113      * @param ninsertAjaxId The identifier of insertAjax
114      * @param plugin The plugin
115      * @return the instance of the insertAjax
116      */
117     public InsertAjax load( int nInsertAjaxId, Plugin plugin )
118     {
119         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
120         daoUtil.setInt( 1, nInsertAjaxId );
121         daoUtil.executeQuery(  );
122 
123         InsertAjax insertAjax = null;
124 
125         if ( daoUtil.next(  ) )
126         {
127             insertAjax = new InsertAjax(  );
128             insertAjax.setId( daoUtil.getInt( 1 ) );
129             insertAjax.setName( daoUtil.getString( 2 ) );
130             insertAjax.setHtml( daoUtil.getString( 3 ) );
131             insertAjax.setSql( daoUtil.getString( 4 ) );
132             insertAjax.setWorkgroup( daoUtil.getString( 5 ) );
133             insertAjax.setRole( daoUtil.getString( 6 ) );
134         }
135 
136         daoUtil.free(  );
137 
138         return insertAjax;
139     }
140 
141     /**
142      * Delete a record from the table
143      * @param insertAjax The insertAjax object
144      * @param plugin The plugin
145      */
146     public void delete( InsertAjax insertAjax, Plugin plugin )
147     {
148         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
149         daoUtil.setInt( 1, insertAjax.getId(  ) );
150         daoUtil.executeUpdate(  );
151         daoUtil.free(  );
152     }
153 
154     /**
155      * Update the record in the table
156      * @param insertAjax The reference of insertAjax
157      * @param plugin The plugin
158      */
159     public void store( InsertAjax insertAjax, Plugin plugin )
160     {
161         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
162         int ninsertAjaxId = insertAjax.getId(  );
163 
164         daoUtil.setString( 1, insertAjax.getName(  ) );
165         daoUtil.setString( 2, insertAjax.getHtml(  ) );
166         daoUtil.setString( 3, insertAjax.getSql(  ) );
167         daoUtil.setString( 4, insertAjax.getWorkgroup(  ) );
168         daoUtil.setString( 5, insertAjax.getRole(  ) );
169         daoUtil.setTimestamp( 6, new Timestamp( new java.util.Date(  ).getTime(  ) ) );
170         daoUtil.setInt( 7, ninsertAjaxId );
171 
172         daoUtil.executeUpdate(  );
173         daoUtil.free(  );
174     }
175 
176     /**
177      * Load the list of insertAjaxs
178      *
179      * @param plugin The plugin
180      * @return The Collection of the insertAjaxs
181      */
182     public Collection<InsertAjax> selectAll( Plugin plugin )
183     {
184         Collection<InsertAjax> insertAjaxList = new ArrayList<InsertAjax>(  );
185         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
186         daoUtil.executeQuery(  );
187 
188         while ( daoUtil.next(  ) )
189         {
190             InsertAjax insertAjax = new InsertAjax(  );
191             insertAjax.setId( daoUtil.getInt( 1 ) );
192             insertAjax.setName( daoUtil.getString( 2 ) );
193             //we don't bring the html code here. Just the sql request.
194             insertAjax.setSql( daoUtil.getString( 3 ) );
195             insertAjax.setWorkgroup( daoUtil.getString( 4 ) );
196             insertAjax.setRole( daoUtil.getString( 5 ) );
197             insertAjaxList.add( insertAjax );
198         }
199 
200         daoUtil.free(  );
201 
202         return insertAjaxList;
203     }
204 
205     /**
206      * Load the list of insertAjaxs
207      *
208      * @param plugin The plugin
209      * @return The Collection of the insertAjaxs
210      */
211     public Collection<InsertAjax> selectEnabledInsertAjaxList( Plugin plugin )
212     {
213         Collection<InsertAjax> insertAjaxList = new ArrayList<InsertAjax>(  );
214         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ENABLED_insertAjax_LIST, plugin );
215         daoUtil.executeQuery(  );
216 
217         while ( daoUtil.next(  ) )
218         {
219             InsertAjax insertAjax = new InsertAjax(  );
220             insertAjax.setId( daoUtil.getInt( 1 ) );
221             insertAjax.setName( daoUtil.getString( 2 ) );
222             insertAjax.setHtml( daoUtil.getString( 3 ) );
223             insertAjax.setSql( daoUtil.getString( 4 ) );
224             insertAjax.setWorkgroup( daoUtil.getString( 5 ) );
225             insertAjaxList.add( insertAjax );
226         }
227 
228         daoUtil.free(  );
229 
230         return insertAjaxList;
231     }
232 
233     /*
234      * @see fr.paris.lutece.plugins.insertajax.business.IInsertAjaxDAO#executeSql(java.lang.String, fr.paris.lutece.portal.service.plugin.Plugin)
235      */
236     public String executeSql( String strSql, Plugin plugin )
237     {
238         DAOUtil daoUtil = new DAOUtil( strSql, plugin );
239         daoUtil.executeQuery(  );
240 
241         String strResult = "";
242 
243         while ( daoUtil.next(  ) )
244         {
245             strResult = daoUtil.getString( 1 );
246         }
247 
248         daoUtil.free(  );
249 
250         return strResult;
251     }
252 
253 	public Collection<Image> loadImagesListOfWorkspace(int nIdWorkspace, Plugin _plugin)
254 	{
255 		
256 		//TODO check that document plugin is installed
257 		//TODO check the role and workgroup to access this workspace
258 		//TODO retreive images from this workspace
259 		
260         Collection<Image> imagesList = new ArrayList<Image>(  );
261 		
262         DAOUtil daoUtil = new DAOUtil( SQL_LIST_IMG, _plugin );
263         daoUtil.setInt( 1, nIdWorkspace );
264         daoUtil.executeQuery(  );
265 
266         while ( daoUtil.next(  ) )
267         {
268         	Image img = new Image();
269         	img.setId( daoUtil.getInt( 1 ) );
270         	img.setComment( daoUtil.getString( 2 ) );
271         	imagesList.add( img );
272         }
273 
274         daoUtil.free(  );
275         return imagesList;
276 	}
277 }