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.mylutece.modules.oauth.business;
35  
36  import fr.paris.lutece.plugins.mylutece.modules.oauth.authentication.OAuthAuthentication;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  
44  /**
45   *
46   * OAuthAuthenticationDAO.
47   */
48  public class OAuthAuthenticationDAO implements IOAuthAuthenticationDAO
49  {
50      private static final String SQL_QUERY_DELETE = "DELETE FROM mylutece_oauth_authentication WHERE auth_name = ? ";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO mylutece_oauth_authentication " +
52          "(auth_name, auth_service_name, auth_icon_url, request_token_url, access_token_url, authorize_url, consumer_key, consumer_secret, credential_url, credential_format ) " +
53          " VALUES(?,?,?,?,?,?,?,?,?,?)";
54      private static final String SQL_QUERY_UPDATE = "UPDATE mylutece_oauth_authentication SET " +
55          "auth_service_name = ?, auth_icon_url = ?, request_token_url = ?, access_token_url = ?, authorize_url = ?, consumer_key = ?, consumer_secret = ?, credential_url = ?, credential_format = ?" +
56          "WHERE auth_name = ?";
57      private static final String SQL_QUERY_SELECT = "SELECT auth_name, auth_service_name, auth_icon_url, request_token_url, " +
58          "access_token_url, authorize_url, consumer_key, consumer_secret, credential_url, credential_format FROM mylutece_oauth_authentication ";
59      private static final String SQL_QUERY_SELECT_ALL = SQL_QUERY_SELECT;
60      private static final String SQL_QUERY_SELECT_BY_PRIMARY_KEY = SQL_QUERY_SELECT + " WHERE auth_name = ?";
61  
62      /**
63       *
64       *{@inheritDoc}
65       */
66      public void delete( String strIdAuthentication, Plugin plugin )
67      {
68          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
69  
70          daoUtil.setString( 1, strIdAuthentication );
71  
72          daoUtil.executeUpdate(  );
73  
74          daoUtil.free(  );
75      }
76  
77      /**
78       *
79       *{@inheritDoc}
80       */
81      public void insert( OAuthAuthentication authentication, Plugin plugin )
82      {
83          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
84  
85          daoUtil.setString( 1, authentication.getName(  ) );
86          setInsertOrUpdateValues( 2, authentication, daoUtil );
87  
88          daoUtil.executeUpdate(  );
89  
90          daoUtil.free(  );
91      }
92  
93      /**
94       *
95       *{@inheritDoc}
96       */
97      public OAuthAuthentication load( String strIdAuthentication, Plugin plugin )
98      {
99          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PRIMARY_KEY, plugin );
100 
101         daoUtil.setString( 1, strIdAuthentication );
102 
103         daoUtil.executeQuery(  );
104 
105         OAuthAuthentication authAuthentication = null;
106 
107         if ( daoUtil.next(  ) )
108         {
109             authAuthentication = new OAuthAuthentication(  );
110             load( authAuthentication, daoUtil );
111         }
112 
113         daoUtil.free(  );
114 
115         return authAuthentication;
116     }
117 
118     /**
119      *
120      *{@inheritDoc}
121      */
122     public List<OAuthAuthentication> selectListAuthentication( Plugin plugin )
123     {
124         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
125 
126         daoUtil.executeQuery(  );
127 
128         List<OAuthAuthentication> listAuthentication = new ArrayList<OAuthAuthentication>(  );
129 
130         while ( daoUtil.next(  ) )
131         {
132             OAuthAuthentication authentication = new OAuthAuthentication(  );
133             load( authentication, daoUtil );
134             listAuthentication.add( authentication );
135         }
136 
137         daoUtil.free(  );
138 
139         return listAuthentication;
140     }
141 
142     /**
143      *
144      *{@inheritDoc}
145      */
146     public void store( OAuthAuthentication authentication, Plugin plugin )
147     {
148         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
149 
150         int nIndex = setInsertOrUpdateValues( 1, authentication, daoUtil );
151         daoUtil.setString( nIndex, authentication.getName(  ) );
152 
153         daoUtil.executeUpdate(  );
154 
155         daoUtil.free(  );
156     }
157 
158     /**
159      * Sets values
160      * @param nStartIndex start index
161      * @param authAuthentication the authentication
162      * @param daoUtil daoUtil
163      * @return the end index
164      */
165     private int setInsertOrUpdateValues( int nStartIndex, OAuthAuthentication authAuthentication, DAOUtil daoUtil )
166     {
167         int nIndex = nStartIndex;
168 
169         // auth_service_name, auth_icon_url, request_token_url, access_token_url, authorize_url, consumer_key, consumer_secret
170         daoUtil.setString( nIndex++, authAuthentication.getAuthServiceName(  ) );
171         daoUtil.setString( nIndex++, authAuthentication.getIconUrl(  ) );
172         daoUtil.setString( nIndex++, authAuthentication.getRequestTokenEndpointUrl(  ) );
173         daoUtil.setString( nIndex++, authAuthentication.getAccessTokenEndpointUrl(  ) );
174         daoUtil.setString( nIndex++, authAuthentication.getAuthorizeWebsiteUrl(  ) );
175         daoUtil.setString( nIndex++, authAuthentication.getConsumerKey(  ) );
176         daoUtil.setString( nIndex++, authAuthentication.getConsumerSecret(  ) );
177         daoUtil.setString( nIndex++, authAuthentication.getCredentialUrl(  ) );
178         daoUtil.setString( nIndex++, authAuthentication.getCredentialFormat(  ) );
179 
180         return nIndex;
181     }
182 
183     /**
184      * Filds the authentication with daoUtil data.
185      * @param authAuthentication the authentication to fill
186      * @param daoUtil the daoUtil
187      */
188     private void load( OAuthAuthentication authAuthentication, DAOUtil daoUtil )
189     {
190         int nIndex = 1;
191         // auth_name, auth_service_name, auth_icon_url, request_token_url, access_token_url, authorize_url, consumer_key, consumer_secret, credential_url, credential_format
192         authAuthentication.setName( daoUtil.getString( nIndex++ ) );
193         authAuthentication.setAuthServiceName( daoUtil.getString( nIndex++ ) );
194         authAuthentication.setIconUrl( daoUtil.getString( nIndex++ ) );
195         authAuthentication.setRequestTokenEndpointUrl( daoUtil.getString( nIndex++ ) );
196         authAuthentication.setAccessTokenEndpointUrl( daoUtil.getString( nIndex++ ) );
197         authAuthentication.setAuthorizeWebsiteUrl( daoUtil.getString( nIndex++ ) );
198         authAuthentication.setConsumerKey( daoUtil.getString( nIndex++ ) );
199         authAuthentication.setConsumerSecret( daoUtil.getString( nIndex++ ) );
200         authAuthentication.setCredentialUrl( daoUtil.getString( nIndex++ ) );
201         authAuthentication.setCredentialFormat( daoUtil.getString( nIndex++ ) );
202     }
203 }