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.plugins.helpdesk.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.Collection;
42  
43  
44  /**
45   * This class provides Data Access methods for Faq objects
46   */
47  public final class FaqDAO implements IFaqDAO
48  {
49      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_faq ) FROM helpdesk_faq";
50      private static final String SQL_QUERY_SELECT = " SELECT id_faq, name, description, role_key, workgroup_key FROM helpdesk_faq WHERE id_faq = ?";
51      private static final String SQL_QUERY_SELECT_BY_SUBJECT = " SELECT hlfs.id_faq, hf.role_key FROM helpdesk_ln_faq_subject hlfs, helpdesk_faq hf WHERE hlfs.id_subject = ? AND hlfs.id_faq=hf.id_faq";
52      private static final String SQL_QUERY_INSERT = " INSERT INTO helpdesk_faq ( id_faq, name, description, role_key, workgroup_key ) VALUES ( ?, ?, ?, ?, ? )";
53      private static final String SQL_QUERY_DELETE = " DELETE FROM helpdesk_faq WHERE id_faq = ?";
54      private static final String SQL_QUERY_UPDATE = " UPDATE helpdesk_faq SET name = ?, description = ?, role_key = ? , workgroup_key = ? WHERE id_faq = ?";
55      private static final String SQL_QUERY_SELECTALL = " SELECT id_faq, name, description, role_key, workgroup_key  FROM helpdesk_faq ORDER BY name ";
56      private static final String SQL_QUERY_SELECT_AUTHORIZED_SELECT = " SELECT id_faq, name, description, role_key, workgroup_key  FROM helpdesk_faq WHERE ";
57      private static final String SQL_QUERY_SELECT_AUTHORIZED_ROLE_KEY = " role_key = ? ";
58      private static final String SQL_QUERY_SELECT_AUTHORIZED_WORKGROUP_KEY = " workgroup_key = ? ";
59      private static final String SQL_QUERY_SELECT_AUTHORIZED_OR = " OR ";
60      private static final String SQL_QUERY_SELECT_AUTHORIZED_ORDER_BY = " ORDER BY name ";
61  
62      ///////////////////////////////////////////////////////////////////////////////////////
63      //Access methods to data
64  
65      /**
66       * Calculate a new primary key to add a new Faq
67       *
68       * @param plugin The Plugin using this data access service
69       * @return The new key.
70       */
71      public int newPrimaryKey( Plugin plugin )
72      {
73          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
74          {
75          	daoUtil.executeQuery(  );
76  
77              int nKey;
78  
79              if ( !daoUtil.next(  ) )
80              {
81                  // if the table is empty
82                  nKey = 1;
83              }
84  
85              nKey = daoUtil.getInt( 1 ) + 1;
86  
87              return nKey;
88          }
89      }
90  
91      /**
92       * Insert a new record in the table.
93       *
94       * @param faq The Instance of the object Faq
95       * @param plugin The Plugin using this data access service
96       */
97      public synchronized void insert( Faq faq, Plugin plugin )
98      {
99          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
100         {
101         	faq.setId( newPrimaryKey( plugin ) );
102 
103             daoUtil.setInt( 1, faq.getId(  ) );
104             daoUtil.setString( 2, faq.getName(  ) );
105             daoUtil.setString( 3, faq.getDescription(  ) );
106             daoUtil.setString( 4, faq.getRoleKey(  ) );
107             daoUtil.setString( 5, faq.getWorkgroup(  ) );
108 
109             daoUtil.executeUpdate(  );
110         }
111     }
112 
113     /**
114      * Delete a record from the table
115      *
116      * @param nIdFaq The indentifier of the object Faq
117      * @param plugin The Plugin using this data access service
118      */
119     public void delete( int nIdFaq, Plugin plugin )
120     {
121         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
122         {
123         	daoUtil.setInt( 1, nIdFaq );
124 
125             daoUtil.executeUpdate(  );
126         }
127     }
128 
129     /**
130      * load the data of Faq from the table
131      *
132      * @param nIdFaq The indentifier of the object Faq
133      * @param plugin The Plugin using this data access service
134      * @return The Instance of the object Faq
135      */
136     public Faq load( int nIdFaq, Plugin plugin )
137     {
138         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
139         {
140         	daoUtil.setInt( 1, nIdFaq );
141             daoUtil.executeQuery(  );
142 
143             Faq faq = null;
144 
145             if ( daoUtil.next(  ) )
146             {
147                 faq = new Faq(  );
148                 faq.setId( daoUtil.getInt( 1 ) );
149                 faq.setName( daoUtil.getString( 2 ) );
150                 faq.setDescription( daoUtil.getString( 3 ) );
151                 faq.setRoleKey( daoUtil.getString( 4 ) );
152                 faq.setWorkgroup( daoUtil.getString( 5 ) );
153             }
154             return faq;
155         }        
156     }
157 
158     /**
159      * Update the record in the table
160      *
161      * @param faq The instance of the Faq to update
162      * @param plugin The Plugin using this data access service
163      */
164     public void store( Faq faq, Plugin plugin )
165     {
166         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
167         {
168         	daoUtil.setString( 1, faq.getName(  ) );
169             daoUtil.setString( 2, faq.getDescription(  ) );
170             daoUtil.setString( 3, faq.getRoleKey(  ) );
171             daoUtil.setString( 4, faq.getWorkgroup(  ) );
172             daoUtil.setInt( 5, faq.getId(  ) );
173 
174             daoUtil.executeUpdate(  );
175         }
176     }
177 
178     /**
179      * Finds all objects of this type
180      * @param plugin The Plugin using this data access service
181      * @return A collection of objects
182      */
183     public Collection<Faq> findAll( Plugin plugin )
184     {
185         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
186         {
187         	Collection<Faq> list = new ArrayList<>(  );
188         	daoUtil.executeQuery(  );
189 
190             while ( daoUtil.next(  ) )
191             {
192                 Faqplugins/helpdesk/business/Faq.html#Faq">Faq faq = new Faq(  );
193                 faq.setId( daoUtil.getInt( 1 ) );
194                 faq.setName( daoUtil.getString( 2 ) );
195                 faq.setDescription( daoUtil.getString( 3 ) );
196                 faq.setRoleKey( daoUtil.getString( 4 ) );
197                 faq.setWorkgroup( daoUtil.getString( 5 ) );
198                 list.add( faq );
199             }
200             return list;
201         }        
202     }
203 
204     /**
205      * Finds all objects of this type
206      * @param plugin The Plugin using this data access service
207      * @return A {@link ReferenceList}
208      */
209     public ReferenceList findReferenceList( Plugin plugin )
210     {
211     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
212     	{
213             ReferenceList list = new ReferenceList(  );
214             daoUtil.executeQuery(  );
215 
216             while ( daoUtil.next(  ) )
217             {
218                 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
219             }
220 
221             return list;
222     	}
223     }
224 
225     /**
226      * Finds all authorized objects of this type specified by roleKey (front office)
227      * @param plugin The Plugin using this data access service
228      * @param arrayRoleKey The role key array
229      * @return A collection of objects
230      */
231     public Collection<Faq> findAuthorizedFaq( String[] arrayRoleKey, Plugin plugin )
232     {
233         return findByKey( arrayRoleKey, false, plugin );
234     }
235 
236     /**
237      * Finds all objects of this type
238      * @param strWorkgroupKey The workgroup key
239      * @param plugin The Plugin using this data access service
240      * @return A collection of objects
241      */
242     public Collection<Faq> findByWorkgroup( String[] strWorkgroupKey, Plugin plugin )
243     {
244         return findByKey( strWorkgroupKey, true, plugin );
245     }
246 
247     /**
248      * Finds all objects for a specified key
249      * @param arrayKeys The key to filter
250      * @param bAdminWorkgroup true = filter by workgroup, false = filter by role key
251      * @param plugin The Plugin using this data access service
252      * @return A collection of objects
253      */
254     private Collection<Faq> findByKey( String[] arrayKeys, boolean bAdminWorkgroup, Plugin plugin )
255     {
256         Collection<Faq> list = new ArrayList<>(  );
257         StringBuilder sB = new StringBuilder( );
258         sB.append( SQL_QUERY_SELECT_AUTHORIZED_SELECT );
259 
260         if ( arrayKeys.length == 0 )
261         {
262             return list;
263         }
264 
265         int i = 1;
266 
267         for ( String strRoleKey : arrayKeys )
268         {
269             if ( i++ > 1 )
270             {
271                 sB.append( SQL_QUERY_SELECT_AUTHORIZED_OR );
272             }
273 
274             if ( bAdminWorkgroup )
275             {
276                 sB.append( SQL_QUERY_SELECT_AUTHORIZED_WORKGROUP_KEY );
277             }
278             else
279             {
280                 sB.append( SQL_QUERY_SELECT_AUTHORIZED_ROLE_KEY );
281             }
282         }
283 
284         sB.append( SQL_QUERY_SELECT_AUTHORIZED_ORDER_BY );
285 
286         try ( DAOUtil daoUtil = new DAOUtil( sB.toString( ), plugin ) )
287         {
288         	i = 1;
289 
290             for ( String strKey : arrayKeys )
291             {
292                 daoUtil.setString( i++, strKey );
293             }
294 
295             daoUtil.executeQuery(  );
296 
297             while ( daoUtil.next(  ) )
298             {
299                 Faqplugins/helpdesk/business/Faq.html#Faq">Faq faq = new Faq(  );
300                 faq.setId( daoUtil.getInt( 1 ) );
301                 faq.setName( daoUtil.getString( 2 ) );
302                 faq.setDescription( daoUtil.getString( 3 ) );
303                 faq.setRoleKey( daoUtil.getString( 4 ) );
304                 faq.setWorkgroup( daoUtil.getString( 5 ) );
305                 list.add( faq );
306             }
307         }
308         return list;
309     }
310 
311     /**
312      * Find a faq containing the subject
313      * @param nSubjectId subject id
314      * @param plugin the plugin
315      * @return the faq
316      */
317     public Faq findBySubjectId( int nSubjectId, Plugin plugin )
318     {
319         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_SUBJECT, plugin ) )
320         {
321             Faqplugins/helpdesk/business/Faq.html#Faq">Faq faq = new Faq(  );
322         	daoUtil.setInt( 1, nSubjectId );
323             daoUtil.executeQuery(  );
324 
325             if ( daoUtil.next(  ) )
326             {
327                 faq.setId( daoUtil.getInt( 1 ) );
328                 faq.setRoleKey( daoUtil.getString( 2 ) );
329             }
330             return faq;
331         }
332     }
333 
334     /**
335      * Finds all objects of this type
336      * @param plugin The Plugin using this data access service
337      * @return A collection of objects
338      */
339     public ReferenceList findListFaq( Plugin plugin )
340     {
341         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
342         {
343             ReferenceList list = new ReferenceList(  );
344         	daoUtil.executeQuery(  );
345 
346             while ( daoUtil.next(  ) )
347             {
348                 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
349             }
350             return list;
351         }
352     }
353 }