View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.directory.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * DirectoryActionDAO
44   */
45  public class DirectoryActionDAO implements IDirectoryActionDAO
46  {
47      private static final String SQL_QUERY_SELECT_ACTIONS = "SELECT a.name_key, a.description_key, a.action_url, a.icon_url, a.action_permission ,a.directory_state"
48              + " FROM directory_action a  where a.directory_state=? ";
49      private static final String SQL_QUERY_SELECT_ACTIONS_RECORDS = "SELECT a.name_key, a.description_key, a.action_url, a.icon_url, a.action_permission ,a.directory_state"
50              + " FROM directory_record_action a  where a.directory_state=? ";
51      private static final String SQL_QUERY_SELECT_ACTIONS_XSL = "SELECT a.name_key, a.description_key, a.action_url, a.icon_url, a.action_permission "
52              + " FROM directory_xsl_action a ";
53      private static final String SQL_QUERY_SELECT_MAX_ACTION_RECORD = "SELECT max(id_action) FROM directory_record_action";
54      private static final String SQL_QUERY_ADD_ACTION_RECORD = "INSERT INTO directory_record_action (id_action,name_key,description_key,action_url,icon_url,action_permission,directory_state) VALUES ( ? , ? , ? , ? , ? , ? , ? );";
55      private static final String SQL_QUERY_CHECK_ACTION_RECORD = "SELECT id_action FROM directory_record_action WHERE name_key = ? AND description_key = ? AND action_url = ? AND icon_url = ? AND action_permission = ? AND directory_state = ? ;";
56      private static final String SQL_QUERY_DELETE_ACTION_RECORD = "DELETE FROM directory_record_action WHERE name_key = ? AND description_key = ? AND action_url = ? AND icon_url = ? AND action_permission = ? AND directory_state = ? ;";
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public List<DirectoryAction> selectActionsByDirectoryState( int nState, Plugin plugin )
63      {
64          List<DirectoryAction> listActions = new ArrayList<DirectoryAction>( );
65          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ACTIONS, plugin );
66          daoUtil.setInt( 1, nState );
67          daoUtil.executeQuery( );
68  
69          while ( daoUtil.next( ) )
70          {
71              DirectoryAction action = new DirectoryAction( );
72              action.setNameKey( daoUtil.getString( 1 ) );
73              action.setDescriptionKey( daoUtil.getString( 2 ) );
74              action.setUrl( daoUtil.getString( 3 ) );
75              action.setIconUrl( daoUtil.getString( 4 ) );
76              action.setPermission( daoUtil.getString( 5 ) );
77              action.setFormState( daoUtil.getInt( 6 ) );
78              listActions.add( action );
79          }
80  
81          daoUtil.free( );
82  
83          return listActions;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public List<DirectoryAction> selectActionsByDirectoryRecordState( int nState, Plugin plugin )
91      {
92          List<DirectoryAction> listActions = new ArrayList<DirectoryAction>( );
93          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ACTIONS_RECORDS, plugin );
94          daoUtil.setInt( 1, nState );
95          daoUtil.executeQuery( );
96  
97          while ( daoUtil.next( ) )
98          {
99              DirectoryAction action = new DirectoryAction( );
100             action.setNameKey( daoUtil.getString( 1 ) );
101             action.setDescriptionKey( daoUtil.getString( 2 ) );
102             action.setUrl( daoUtil.getString( 3 ) );
103             action.setIconUrl( daoUtil.getString( 4 ) );
104             action.setPermission( daoUtil.getString( 5 ) );
105             action.setFormState( daoUtil.getInt( 6 ) );
106             listActions.add( action );
107         }
108 
109         daoUtil.free( );
110 
111         return listActions;
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public void addNewActionInDirectoryRecordAction( DirectoryAction directoryAction, Plugin plugin )
119     {
120         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MAX_ACTION_RECORD, plugin );
121         daoUtil.executeQuery( );
122 
123         int nId = 1;
124 
125         while ( daoUtil.next( ) )
126         {
127             nId = daoUtil.getInt( 1 ) + 1;
128         }
129 
130         daoUtil.free( );
131         daoUtil = new DAOUtil( SQL_QUERY_ADD_ACTION_RECORD, plugin );
132         daoUtil.setInt( 1, nId );
133         daoUtil.setString( 2, directoryAction.getNameKey( ) );
134         daoUtil.setString( 3, directoryAction.getDescriptionKey( ) );
135         daoUtil.setString( 4, directoryAction.getUrl( ) );
136         daoUtil.setString( 5, directoryAction.getIconUrl( ) );
137         daoUtil.setString( 6, directoryAction.getPermission( ) );
138         daoUtil.setInt( 7, directoryAction.getFormState( ) );
139         daoUtil.executeUpdate( );
140         daoUtil.free( );
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public boolean checkActionsDirectoryRecord( DirectoryAction directoryAction, Plugin plugin )
148     {
149         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_ACTION_RECORD, plugin );
150         daoUtil.setString( 1, directoryAction.getNameKey( ) );
151         daoUtil.setString( 2, directoryAction.getDescriptionKey( ) );
152         daoUtil.setString( 3, directoryAction.getUrl( ) );
153         daoUtil.setString( 4, directoryAction.getIconUrl( ) );
154         daoUtil.setString( 5, directoryAction.getPermission( ) );
155         daoUtil.setInt( 6, directoryAction.getFormState( ) );
156         daoUtil.executeQuery( );
157 
158         boolean bCheckAction = daoUtil.next( );
159         daoUtil.free( );
160 
161         return bCheckAction;
162     }
163 
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public void deleteActionsDirectoryRecord( DirectoryAction directoryAction, Plugin plugin )
169     {
170         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ACTION_RECORD, plugin );
171         daoUtil.setString( 1, directoryAction.getNameKey( ) );
172         daoUtil.setString( 2, directoryAction.getDescriptionKey( ) );
173         daoUtil.setString( 3, directoryAction.getUrl( ) );
174         daoUtil.setString( 4, directoryAction.getIconUrl( ) );
175         daoUtil.setString( 5, directoryAction.getPermission( ) );
176         daoUtil.setInt( 6, directoryAction.getFormState( ) );
177         daoUtil.executeUpdate( );
178         daoUtil.free( );
179     }
180 
181     /**
182      * {@inheritDoc}
183      */
184     @Override
185     public List<DirectoryAction> selectActionsByDirectoryXsl( Plugin plugin )
186     {
187         List<DirectoryAction> listActions = new ArrayList<DirectoryAction>( );
188         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ACTIONS_XSL, plugin );
189         daoUtil.executeQuery( );
190 
191         while ( daoUtil.next( ) )
192         {
193             DirectoryAction action = new DirectoryAction( );
194             action.setNameKey( daoUtil.getString( 1 ) );
195             action.setDescriptionKey( daoUtil.getString( 2 ) );
196             action.setUrl( daoUtil.getString( 3 ) );
197             action.setIconUrl( daoUtil.getString( 4 ) );
198             action.setPermission( daoUtil.getString( 5 ) );
199             listActions.add( action );
200         }
201 
202         daoUtil.free( );
203 
204         return listActions;
205     }
206 }