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.forms.modules.template.business;
35  
36  import java.sql.Statement;
37  import java.util.ArrayList;
38  import java.util.List;
39  import java.util.stream.Collectors;
40  
41  import fr.paris.lutece.plugins.forms.business.Group;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.util.sql.DAOUtil;
44  
45  /**
46   * This class provides Data Access methods for Group objects
47   */
48  public final class TemplateGroupDAO implements ITemplateGroupDAO
49  {
50      // Constants
51      private static final String SQL_QUERY_SELECTALL = "SELECT id_group, title, description, id_template, iteration_min, iteration_max, iteration_add_label, iteration_remove_label FROM template_group";
52      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECTALL + " WHERE id_group = ?";
53      private static final String SQL_QUERY_INSERT = "INSERT INTO template_group ( title, description, id_template, iteration_min, iteration_max, iteration_add_label, iteration_remove_label ) VALUES ( ?, ?, ?, ?, ?, ?, ? ) ";
54      private static final String SQL_QUERY_DELETE = "DELETE FROM template_group WHERE id_group = ? ";
55      private static final String SQL_QUERY_UPDATE = "UPDATE template_group SET id_group = ?, title = ?, description = ?, id_template = ?, iteration_min = ?, iteration_max = ?, iteration_add_label = ?, iteration_remove_label = ? WHERE id_group = ?";
56  
57      /**
58       * {@inheritDoc }
59       */
60      @Override
61      public void insert( Group group, Plugin plugin )
62      {
63  
64          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
65          {
66              int nIndex = 1;
67              daoUtil.setString( nIndex++, group.getTitle( ) );
68              daoUtil.setString( nIndex++, group.getDescription( ) );
69              daoUtil.setInt( nIndex++, group.getIdStep( ) );
70              daoUtil.setInt( nIndex++, group.getIterationMin( ) );
71              daoUtil.setInt( nIndex++, group.getIterationMax( ) );
72              daoUtil.setString( nIndex++, group.getIterationAddLabel( ) );
73              daoUtil.setString( nIndex++, group.getIterationRemoveLabel( ) );
74  
75              daoUtil.executeUpdate( );
76              if ( daoUtil.nextGeneratedKey( ) )
77              {
78                  group.setId( daoUtil.getGeneratedKeyInt( 1 ) );
79              }
80          }
81  
82      }
83  
84      /**
85       * {@inheritDoc }
86       */
87      @Override
88      public Group load( int nKey, Plugin plugin )
89      {
90  
91          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
92          {
93              daoUtil.setInt( 1, nKey );
94              daoUtil.executeQuery( );
95              Group group = null;
96  
97              if ( daoUtil.next( ) )
98              {
99                  group = dataToObject( daoUtil );
100             }
101 
102             return group;
103         }
104     }
105 
106     /**
107      * {@inheritDoc }
108      */
109     @Override
110     public void delete( int nKey, Plugin plugin )
111     {
112         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
113         {
114 
115             daoUtil.setInt( 1, nKey );
116             daoUtil.executeUpdate( );
117         }
118 
119     }
120 
121     /**
122      * {@inheritDoc }
123      */
124     @Override
125     public void store( Group group, Plugin plugin )
126     {
127         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
128         {
129 
130             int nIndex = 1;
131 
132             daoUtil.setInt( nIndex++, group.getId( ) );
133             daoUtil.setString( nIndex++, group.getTitle( ) );
134             daoUtil.setString( nIndex++, group.getDescription( ) );
135             daoUtil.setInt( nIndex++, group.getIdStep( ) );
136             daoUtil.setInt( nIndex++, group.getIterationMin( ) );
137             daoUtil.setInt( nIndex++, group.getIterationMax( ) );
138             daoUtil.setString( nIndex++, group.getIterationAddLabel( ) );
139             daoUtil.setString( nIndex++, group.getIterationRemoveLabel( ) );
140             daoUtil.setInt( nIndex, group.getId( ) );
141 
142             daoUtil.executeUpdate( );
143         }
144     }
145 
146     @Override
147     public List<Group> selectGroupsListByListIdStep( List<Integer> idSteplist, Plugin plugin )
148     {
149         List<Group> groupList = new ArrayList<>( );
150 
151         String query = SQL_QUERY_SELECTALL + " WHERE id_template IN ( ";
152         query += idSteplist.stream( ).map( i -> "?" ).collect( Collectors.joining( "," ) );
153         query += " )";
154 
155         try ( DAOUtil daoUtil = new DAOUtil( query, plugin ) )
156         {
157             int index = 0;
158             for ( Integer id : idSteplist )
159             {
160                 daoUtil.setInt( ++index, id );
161             }
162             daoUtil.executeQuery( );
163 
164             while ( daoUtil.next( ) )
165             {
166                 groupList.add( dataToObject( daoUtil ) );
167             }
168         }
169         return groupList;
170     }
171 
172     /**
173      * 
174      * @param daoUtil
175      *            The daoutil
176      * @return The populated Group object
177      */
178     private Group dataToObject( DAOUtil daoUtil )
179     {
180         Group group = new Group( );
181 
182         group.setId( daoUtil.getInt( "id_group" ) );
183         group.setTitle( daoUtil.getString( "title" ) );
184         group.setDescription( daoUtil.getString( "description" ) );
185         group.setIdStep( daoUtil.getInt( "id_template" ) );
186         group.setIterationMin( daoUtil.getInt( "iteration_min" ) );
187         group.setIterationMax( daoUtil.getInt( "iteration_max" ) );
188         group.setIterationAddLabel( daoUtil.getString( "iteration_add_label" ) );
189         group.setIterationRemoveLabel( daoUtil.getString( "iteration_remove_label" ) );
190 
191         return group;
192     }
193 }