View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.phraseanet.business.account;
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  /**
44   * This class provides Data Access methods for Account objects
45   */
46  public final class AccountDAO implements IAccountDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_account ) FROM phraseanet_account";
50      private static final String SQL_QUERY_SELECT = "SELECT id_account, name, description, access_url, customer_id, customer_secret, autthorize_end_point, access_end_point, phraseanet_id, password, token FROM phraseanet_account WHERE id_account = ?";
51      private static final String SQL_QUERY_SELECT_TOKEN = "SELECT token FROM phraseanet_account WHERE id_account = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO phraseanet_account ( id_account, name, description, access_url, customer_id, customer_secret, autthorize_end_point, access_end_point, phraseanet_id, password, token ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM phraseanet_account WHERE id_account = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE phraseanet_account SET name = ?, description = ?, access_url = ?, customer_id = ?, customer_secret = ?, autthorize_end_point = ?, access_end_point = ?, phraseanet_id = ?, password = ?, token = ? WHERE id_account = ?";
55      private static final String SQL_QUERY_UPDATE_TOKEN = "UPDATE phraseanet_account SET token = ? WHERE id_account = ?";
56      private static final String SQL_QUERY_SELECTALL = "SELECT id_account, name, description, access_url, customer_id, customer_secret, autthorize_end_point, access_end_point, phraseanet_id, password, token FROM phraseanet_account";
57  
58      /**
59       * Generates a new primary key
60       * @param plugin The Plugin
61       * @return The new primary key
62       */
63      public int newPrimaryKey( Plugin plugin )
64      {
65          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
66          daoUtil.executeQuery(  );
67  
68          int nKey;
69  
70          if ( !daoUtil.next(  ) )
71          {
72              // if the table is empty
73              nKey = 1;
74          }
75  
76          nKey = daoUtil.getInt( 1 ) + 1;
77          daoUtil.free(  );
78  
79          return nKey;
80      }
81  
82      /**
83       * Insert a new record in the table.
84       * @param account instance of the Account object to insert
85       * @param plugin The plugin
86       */
87      public void insert( Account account, Plugin plugin )
88      {
89          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
90  
91          account.setId( newPrimaryKey( plugin ) );
92  
93          daoUtil.setInt( 1, account.getId(  ) );
94          daoUtil.setString( 2, account.getName(  ) );
95          daoUtil.setString( 3, account.getDescription(  ) );
96          daoUtil.setString( 4, account.getAccessURL(  ) );
97          daoUtil.setString( 5, account.getCustomerId(  ) );
98          daoUtil.setString( 6, account.getCustomerSecret(  ) );
99          daoUtil.setString( 7, account.getAuthorizeEndPoint(  ) );
100         daoUtil.setString( 8, account.getAccessEndPoint(  ) );
101         daoUtil.setString( 9, account.getPhraseanetId(  ) );
102         daoUtil.setString( 10, account.getPassword(  ) );
103         daoUtil.setString( 11, account.getToken(  ) );
104 
105         daoUtil.executeUpdate(  );
106         daoUtil.free(  );
107     }
108 
109     /**
110      * Load the data of the Account from the table
111      * @param nId The identifier of the Account
112      * @param plugin The plugin
113      * @return the instance of the Account
114      */
115     public Account load( int nId, Plugin plugin )
116     {
117         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
118         daoUtil.setInt( 1, nId );
119         daoUtil.executeQuery(  );
120 
121         Account account = null;
122 
123         if ( daoUtil.next(  ) )
124         {
125             account = new Account(  );
126 
127             account.setId( daoUtil.getInt( 1 ) );
128             account.setName( daoUtil.getString( 2 ) );
129             account.setDescription( daoUtil.getString( 3 ) );
130             account.setAccessURL( daoUtil.getString( 4 ) );
131             account.setCustomerId( daoUtil.getString( 5 ) );
132             account.setCustomerSecret( daoUtil.getString( 6 ) );
133             account.setAuthorizeEndPoint( daoUtil.getString( 7 ) );
134             account.setAccessEndPoint( daoUtil.getString( 8 ) );
135             account.setPhraseanetId( daoUtil.getString( 9 ) );
136             account.setPassword( daoUtil.getString( 10 ) );
137             account.setToken( daoUtil.getString( 11 ) );
138         }
139 
140         daoUtil.free(  );
141 
142         return account;
143     }
144 
145     /**
146      * Delete a record from the table
147      * @param nAccountId The identifier of the Account
148      * @param plugin The plugin
149      */
150     public void delete( int nAccountId, Plugin plugin )
151     {
152         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
153         daoUtil.setInt( 1, nAccountId );
154         daoUtil.executeUpdate(  );
155         daoUtil.free(  );
156     }
157 
158     /**
159      * Update the account record in the table
160      * @param account The reference of the Account
161      * @param plugin The plugin
162      */
163     public void store( Account account, Plugin plugin )
164     {
165         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
166 
167         daoUtil.setString( 1, account.getName(  ) );
168         daoUtil.setString( 2, account.getDescription(  ) );
169         daoUtil.setString( 3, account.getAccessURL(  ) );
170         daoUtil.setString( 4, account.getCustomerId(  ) );
171         daoUtil.setString( 5, account.getCustomerSecret(  ) );
172         daoUtil.setString( 6, account.getAuthorizeEndPoint(  ) );
173         daoUtil.setString( 7, account.getAccessEndPoint(  ) );
174         daoUtil.setString( 8, account.getPhraseanetId(  ) );
175         daoUtil.setString( 9, account.getPassword(  ) );
176         daoUtil.setString( 10, account.getToken(  ) );
177         daoUtil.setInt( 11, account.getId(  ) );
178 
179         daoUtil.executeUpdate(  );
180         daoUtil.free(  );
181     }
182 
183      /**
184      * Update the token record in the table
185      * @param account The reference of the Account
186      * @param plugin The plugin
187      */
188     public void updateToken( Account account, Plugin plugin )
189     {
190         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_TOKEN, plugin );
191 
192         daoUtil.setString( 1, account.getToken(  ) );
193         daoUtil.setInt( 2, account.getId(  ) );
194 
195         daoUtil.executeUpdate(  );
196         daoUtil.free(  );
197     }
198     
199     /**
200      * Load the data of all the Accounts and returns them as a List
201      * @param plugin The plugin
202      * @return The List which contains the data of all the Accounts
203      */
204     public List<Account> selectAccountsList( Plugin plugin )
205     {
206         List<Account> accountList = new ArrayList<Account>(  );
207         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
208         daoUtil.executeQuery(  );
209 
210         while ( daoUtil.next(  ) )
211         {
212             Account account = new Account(  );
213 
214             account.setId( daoUtil.getInt( 1 ) );
215             account.setName( daoUtil.getString( 2 ) );
216             account.setDescription( daoUtil.getString( 3 ) );
217             account.setAccessURL( daoUtil.getString( 4 ) );
218             account.setCustomerId( daoUtil.getString( 5 ) );
219             account.setCustomerSecret( daoUtil.getString( 6 ) );
220             account.setAuthorizeEndPoint( daoUtil.getString( 7 ) );
221             account.setAccessEndPoint( daoUtil.getString( 8 ) );
222             account.setPhraseanetId( daoUtil.getString( 9 ) );
223             account.setPassword( daoUtil.getString( 10 ) );
224             account.setToken( daoUtil.getString( 11 ) );
225 
226             accountList.add( account );
227         }
228 
229         daoUtil.free(  );
230 
231         return accountList;
232     }
233 }