View Javadoc
1   package fr.paris.lutece.plugins.workflow.business.action;
2   
3   import java.sql.Statement;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
8   import fr.paris.lutece.plugins.workflowcore.business.action.IActionStateDAO;
9   import fr.paris.lutece.util.sql.DAOUtil;
10  
11  public class ActionStateDAO implements IActionStateDAO 
12  {
13  	private static final String SQL_QUERY_SELECT_ALL = "SELECT id_action,id_state_before ";
14      private static final String SQL_QUERY_FIND_BY_ID_ACTION = "SELECT id_state_before FROM workflow_action_state_before WHERE id_action= ? ";
15      private static final String SQL_QUERY_FIND_BY_UID_ACTION = "SELECT uid_state FROM workflow_action_state_before asb, workflow_state s, workflow_action a WHERE asb.id_state_before = s.id_state AND a.id_action = asb.id_action and a.uid_action = ?";
16  	private static final String SQL_QUERY_INSERT = "INSERT INTO workflow_action_state_before "
17              + "(id_action,id_state_before)"
18              + " VALUES(?,?)";
19  	private static final String SQL_QUERY_DELETE = "DELETE FROM workflow_action_state_before  WHERE id_action=? ";
20  	
21  	/**
22       * {@inheritDoc}
23       */
24      @Override
25      public synchronized void insert( int nIdAction, List<Integer> listIdStateBefore )
26      {
27          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, WorkflowUtils.getPlugin( ) ) )
28          {
29          	for ( Integer nIdStateBefore : listIdStateBefore )
30          	{
31  	            int nPos = 0;
32  	            daoUtil.setInt( ++nPos, nIdAction );
33  	            daoUtil.setInt( ++nPos, nIdStateBefore );
34  	            daoUtil.addBatch( );
35          	}
36              //daoUtil.executeUpdate( );
37          	daoUtil.executeBatch( );
38              
39          }
40      }
41  
42      /**
43       * {@inheritDoc}
44       */
45      @Override
46      public List<Integer> load( int nIdAction )
47      {
48          List<Integer> listState = new ArrayList<>( );
49          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_ID_ACTION, WorkflowUtils.getPlugin( ) ) )
50          {
51              daoUtil.setInt( 1, nIdAction );
52              daoUtil.executeQuery( );
53  
54              while ( daoUtil.next( ) )
55              {
56              	int nPos = 0;
57                  listState.add( Integer.valueOf(daoUtil.getInt( ++nPos ) ) );
58              }
59  
60          }
61          return listState;
62      }
63      
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public List<String> load( String strUidAction )
69      {
70          List<String> listState = new ArrayList<>( );
71          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_UID_ACTION, WorkflowUtils.getPlugin( ) ) )
72          {
73              daoUtil.setString( 1, strUidAction );
74              daoUtil.executeQuery( );
75  
76              while ( daoUtil.next( ) )
77              {
78              	int nPos = 0;
79                  listState.add( String.valueOf(daoUtil.getString( ++nPos ) ) );
80              }
81  
82          }
83          return listState;
84      }
85  
86  	@Override
87  	public void delete(int nIdAction) {
88  		try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, WorkflowUtils.getPlugin( ) ) )
89          {
90              daoUtil.setInt( 1, nIdAction );
91              daoUtil.executeUpdate( );
92          }
93  		
94  	}
95  
96  }