View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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.modules.ticketing.business.anonymisation;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.util.sql.DAOUtil;
45  
46  public class AnonymisationDAO implements IAnonymisationDAO
47  {
48      private static final String IDS_TO_REPLACE                                           = "%IDS%";
49      private static final String SQL_QUERY_SELECT_UPLOAD_FILES_HISTORY = "SELECT id_file FROM workflow_task_upload_files WHERE id_history = ?";
50  
51      private static final String SQL_QUERY_SELECT_MESSAGE_NOTIFY_GRU_HISTORY_TOTAL = "SELECT message_email, message_guichet, message_agent,message_broadcast,message_sms FROM workflow_task_notify_gru_history WHERE id_history = ?";
52  
53      // DELETE
54      private static final String SQL_QUERY_DELETE_MESSAGE_NOTIFY_GRU_HISTORY_LIST         = "DELETE FROM workflow_task_notify_gru_history WHERE id_history IN (" + IDS_TO_REPLACE + ")";
55      private static final String SQL_QUERY_DELETE_ANONYMISATION_COMMENT_VALUE_HISTORY_LIST = "DELETE FROM workflow_task_comment_value WHERE id_history IN (" + IDS_TO_REPLACE + ")";
56      private static final String SQL_QUERY_DELETE_UPLOAD_FILES_HISTORY_LIST                = "DELETE FROM workflow_task_upload_files WHERE id_history IN (" + IDS_TO_REPLACE + ")";
57      private static final String SQL_QUERY_DELETE_UPLOAD_HISTORY_LIST                      = "DELETE FROM workflow_task_upload_history WHERE id_history IN (" + IDS_TO_REPLACE + ")";
58      // workflow history ticketing
59      private static final String SQL_QUERY_DELETE_WORKFLOW_TICKETING_HISTORY_LIST          = "DELETE FROM workflow_resource_history_ticketing WHERE id_history IN (" + IDS_TO_REPLACE + ")";
60      // workflow history
61      private static final String SQL_QUERY_DELETE_HIST_WORKFLOW_HISTORY_LIST               = "DELETE FROM workflow_resource_history WHERE id_history IN (" + IDS_TO_REPLACE + ")";
62      // workflow user history
63      private static final String SQL_QUERY_DELETE_USER_WORKFLOW_HISTORY_LIST               = "DELETE FROM workflow_resource_user_history WHERE id_history IN (" + IDS_TO_REPLACE + ")";
64      // workflow resource
65      private static final String SQL_QUERY_DELETE_RESOURCE_WORKFLOW                        = "DELETE FROM workflow_resource_workflow WHERE id_resource = ? ";
66  
67      /**
68       * {@inheritDoc }
69       */
70      @Override
71      public Map<String, String> loadMessageNotifyHIstoryTotal( int idHistory, Plugin plugin )
72      {
73          Map<String, String> messageListHistory = new HashMap<>( );
74          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MESSAGE_NOTIFY_GRU_HISTORY_TOTAL, plugin ) )
75          {
76              daoUtil.setInt( 1, idHistory );
77              daoUtil.executeQuery( );
78  
79              if ( daoUtil.next( ) )
80              {
81                  messageListHistory.put( "message_email", daoUtil.getString( 1 ) );
82                  messageListHistory.put( "message_guichet", daoUtil.getString( 2 ) );
83                  messageListHistory.put( "message_agent", daoUtil.getString( 3 ) );
84                  messageListHistory.put( "message_broadcast", daoUtil.getString( 4 ) );
85                  messageListHistory.put( "message_sms", daoUtil.getString( 5 ) );
86  
87              }
88          }
89          return messageListHistory;
90      }
91  
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public List<Integer> getIdUploadFilesByIdHistory( int idHistory, Plugin plugin )
98      {
99          List<Integer> uploadIdFilesHistoryList = new ArrayList<>( );
100 
101         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_UPLOAD_FILES_HISTORY, plugin ) )
102         {
103             daoUtil.setInt( 1, idHistory );
104 
105             daoUtil.executeQuery( );
106 
107             while ( daoUtil.next( ) )
108             {
109                 uploadIdFilesHistoryList.add( daoUtil.getInt( 1 ) );
110             }
111         }
112         return uploadIdFilesHistoryList;
113     }
114 
115     //// PURGE ANONYMISATION ////
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public void deleteMessageNotifyGruByIdHistoryList( List<Integer> idHistoryList, Plugin plugin )
122     {
123         final String sql = StringUtils.replace( SQL_QUERY_DELETE_MESSAGE_NOTIFY_GRU_HISTORY_LIST, IDS_TO_REPLACE, StringUtils.join( idHistoryList, "," ) );
124         try ( DAOUtil daoUtil = new DAOUtil( sql, plugin ) )
125         {
126             daoUtil.executeUpdate( );
127         }
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public void deleteHistoryWorkflowHistoryList( List<Integer> idHistoryList, Plugin plugin )
135     {
136         final String sql = StringUtils.replace( SQL_QUERY_DELETE_HIST_WORKFLOW_HISTORY_LIST, IDS_TO_REPLACE, StringUtils.join( idHistoryList, "," ) );
137         try ( DAOUtil daoUtil = new DAOUtil( sql, plugin ) )
138         {
139             daoUtil.executeUpdate( );
140         }
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public void deleteWorkflowResource( int idTicket, Plugin plugin )
148     {
149         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_RESOURCE_WORKFLOW, plugin ) )
150         {
151             daoUtil.setInt( 1, idTicket );
152             daoUtil.executeUpdate( );
153         }
154     }
155 
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public void deleteWorkflowTicketingHistoryList( List<Integer> idHistoryList, Plugin plugin )
161     {
162         final String sql = StringUtils.replace( SQL_QUERY_DELETE_WORKFLOW_TICKETING_HISTORY_LIST, IDS_TO_REPLACE, StringUtils.join( idHistoryList, "," ) );
163         try ( DAOUtil daoUtil = new DAOUtil( sql, plugin ) )
164         {
165             daoUtil.executeUpdate( );
166         }
167     }
168 
169     /**
170      * {@inheritDoc}
171      */
172     @Override
173     public void deleteCommentValueIdHistoryList( List<Integer> idHistoryList, Plugin plugin )
174     {
175         final String sql = StringUtils.replace( SQL_QUERY_DELETE_ANONYMISATION_COMMENT_VALUE_HISTORY_LIST, IDS_TO_REPLACE, StringUtils.join( idHistoryList, "," ) );
176         try ( DAOUtil daoUtil = new DAOUtil( sql, plugin ) )
177         {
178             daoUtil.executeUpdate( );
179         }
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public void deleteWorkflowUserHistoryList( List<Integer> idHistoryList, Plugin plugin )
187     {
188         final String sql = StringUtils.replace( SQL_QUERY_DELETE_USER_WORKFLOW_HISTORY_LIST, IDS_TO_REPLACE, StringUtils.join( idHistoryList, "," ) );
189         try ( DAOUtil daoUtil = new DAOUtil( sql, plugin ) )
190         {
191             daoUtil.executeUpdate( );
192         }
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public void deleteUploadFilesIdHistoryList( List<Integer> idHistoryList, Plugin plugin )
200     {
201         final String sql = StringUtils.replace( SQL_QUERY_DELETE_UPLOAD_FILES_HISTORY_LIST, IDS_TO_REPLACE, StringUtils.join( idHistoryList, "," ) );
202         try ( DAOUtil daoUtil = new DAOUtil( sql, plugin ) )
203         {
204             daoUtil.executeUpdate( );
205         }
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     @Override
212     public void deleteUploadHistoryList( List<Integer> idHistoryList, Plugin plugin )
213     {
214         final String sql = StringUtils.replace( SQL_QUERY_DELETE_UPLOAD_HISTORY_LIST, IDS_TO_REPLACE, StringUtils.join( idHistoryList, "," ) );
215         try ( DAOUtil daoUtil = new DAOUtil( sql, plugin ) )
216         {
217             daoUtil.executeUpdate( );
218         }
219     }
220 
221 }