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.workflow.business.workflow;
35  
36  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
37  import fr.paris.lutece.plugins.workflowcore.business.workflow.IWorkflowDAO;
38  import fr.paris.lutece.plugins.workflowcore.business.workflow.Workflow;
39  import fr.paris.lutece.plugins.workflowcore.business.workflow.WorkflowFilter;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  import java.sql.Statement;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  /**
47   *
48   * WorkflowDAO
49   *
50   */
51  public class WorkflowDAO implements IWorkflowDAO
52  {
53      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_workflow,name,description,creation_date,is_enabled,workgroup_key, uid_workflow"
54              + " FROM workflow_workflow WHERE id_workflow=?";
55      private static final String SQL_QUERY_SELECT_BY_FILTER = "SELECT id_workflow,name,description,creation_date,is_enabled,workgroup_key, uid_workflow"
56              + " FROM workflow_workflow ";
57      private static final String SQL_QUERY_INSERT = "INSERT INTO  workflow_workflow "
58              + "(name,description,creation_date,is_enabled,workgroup_key, uid_workflow)VALUES(?,?,?,?,?,?)";
59      private static final String SQL_QUERY_UPDATE = "UPDATE workflow_workflow  SET id_workflow=?,name=?,description=?,is_enabled=?,workgroup_key=?,uid_workflow=?"
60              + " WHERE id_workflow=?";
61      private static final String SQL_QUERY_DELETE = "DELETE FROM workflow_workflow  WHERE id_workflow=? ";
62      private static final String SQL_FILTER_IS_ENABLED = " is_enabled = ? ";
63      private static final String SQL_FILTER_WORKGROUP = " workgroup_key = ? ";
64      private static final String SQL_FILTRE_NAME = " name = ? ";
65      private static final String SQL_ORDER_BY_DATE_CREATION = " ORDER BY creation_date DESC ";
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public synchronized void insert( Workflow workflow )
72      {
73          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, WorkflowUtils.getPlugin( ) ) )
74          {
75              int nPos = 0;
76              daoUtil.setString( ++nPos, workflow.getName( ) );
77              daoUtil.setString( ++nPos, workflow.getDescription( ) );
78              daoUtil.setTimestamp( ++nPos, workflow.getCreationDate( ) );
79              daoUtil.setBoolean( ++nPos, workflow.isEnabled( ) );
80              daoUtil.setString( ++nPos, workflow.getWorkgroup( ) );
81              daoUtil.setString( ++nPos, workflow.getUid( ) );
82              
83              daoUtil.executeUpdate( );
84              if ( daoUtil.nextGeneratedKey( ) )
85              {
86                  workflow.setId( daoUtil.getGeneratedKeyInt( 1 ) );
87              }
88          }
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void store( Workflow workflow )
96      {
97          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, WorkflowUtils.getPlugin( ) ) )
98          {
99              int nPos = 0;
100 
101             daoUtil.setInt( ++nPos, workflow.getId( ) );
102             daoUtil.setString( ++nPos, workflow.getName( ) );
103             daoUtil.setString( ++nPos, workflow.getDescription( ) );
104             daoUtil.setBoolean( ++nPos, workflow.isEnabled( ) );
105             daoUtil.setString( ++nPos, workflow.getWorkgroup( ) );
106             daoUtil.setString( ++nPos, workflow.getUid( ) );
107 
108             daoUtil.setInt( ++nPos, workflow.getId( ) );
109             daoUtil.executeUpdate( );
110         }
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public Workflow load( int nIdWorkflow )
118     {
119         Workflow workflow = null;
120         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, WorkflowUtils.getPlugin( ) ) )
121         {
122             daoUtil.setInt( 1, nIdWorkflow );
123             daoUtil.executeQuery( );
124 
125             if ( daoUtil.next( ) )
126             {
127                 int nPos = 0;
128                 workflow = new Workflow( );
129                 workflow.setId( daoUtil.getInt( ++nPos ) );
130                 workflow.setName( daoUtil.getString( ++nPos ) );
131                 workflow.setDescription( daoUtil.getString( ++nPos ) );
132                 workflow.setCreationDate( daoUtil.getTimestamp( ++nPos ) );
133                 workflow.setEnabled( daoUtil.getBoolean( ++nPos ) );
134                 workflow.setWorkgroup( daoUtil.getString( ++nPos ) );
135                 workflow.setUid( daoUtil.getString( ++nPos ));
136             }
137         }
138         return workflow;
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public void delete( int nIdWorkflow )
146     {
147         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, WorkflowUtils.getPlugin( ) ) )
148         {
149             daoUtil.setInt( 1, nIdWorkflow );
150             daoUtil.executeUpdate( );
151         }
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     public List<Workflow> selectWorkflowByFilter( WorkflowFilter filter )
159     {
160         List<Workflow> listWorkflow = new ArrayList<>( );
161         Workflow workflow = null;
162         List<String> listStrFilter = new ArrayList<>( );
163 
164         if ( filter.containsIsEnabled( ) )
165         {
166             listStrFilter.add( SQL_FILTER_IS_ENABLED );
167         }
168 
169         if ( filter.containsWorkgroupCriteria( ) )
170         {
171             listStrFilter.add( SQL_FILTER_WORKGROUP );
172         }
173 
174         if ( filter.containsName( ) )
175         {
176             listStrFilter.add( SQL_FILTRE_NAME );
177         }
178         String strSQL = WorkflowUtils.buildRequestWithFilter( SQL_QUERY_SELECT_BY_FILTER, listStrFilter, SQL_ORDER_BY_DATE_CREATION );
179         try ( DAOUtil daoUtil = new DAOUtil( strSQL, WorkflowUtils.getPlugin( ) ) )
180         {
181             int nPos = 0;
182             if ( filter.containsIsEnabled( ) )
183             {
184                 daoUtil.setInt( ++nPos, filter.getIsEnabled( ) );
185             }
186 
187             if ( filter.containsWorkgroupCriteria( ) )
188             {
189                 daoUtil.setString( ++nPos, filter.getWorkgroup( ) );
190             }
191 
192             if ( filter.containsName( ) )
193             {
194                 daoUtil.setString( ++nPos, filter.getName( ) );
195             }
196 
197             daoUtil.executeQuery( );
198 
199             while ( daoUtil.next( ) )
200             {
201                 nPos = 0;
202                 workflow = new Workflow( );
203                 workflow.setId( daoUtil.getInt( ++nPos ) );
204                 workflow.setName( daoUtil.getString( ++nPos ) );
205                 workflow.setDescription( daoUtil.getString( ++nPos ) );
206                 workflow.setCreationDate( daoUtil.getTimestamp( ++nPos ) );
207                 workflow.setEnabled( daoUtil.getBoolean( ++nPos ) );
208                 workflow.setWorkgroup( daoUtil.getString( ++nPos ) );
209                 workflow.setUid( daoUtil.getString( ++nPos ));
210 
211                 listWorkflow.add( workflow );
212             }
213         }
214 
215         return listWorkflow;
216     }
217 }