View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.elasticdata.modules.identity.business;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  import org.apache.commons.collections.CollectionUtils;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.util.sql.DAOUtil;
44  
45  /**
46   * This class provides Data Access methods for IdentityAttribute objects
47   */
48  public final class IdentityDataObjectDAO implements IIdentityDataObjectDAO
49  {
50      // Constants
51      private static final String SQL_QUERY_SELECT_ID_IDENTITY_TO_EXPORT = "SELECT id_identity FROM identitystore_identity";
52      private static final String SQL_QUERY_SELECT_IDENTITY_TO_EXPORT = "SELECT id_identity, customer_id, connection_id, date_create, date_delete FROM identitystore_identity";
53      private static final String SQL_QUERY_SELECT_ATTRIBUTE = "SELECT id_attribute, key_name FROM identitystore_ref_attribute";
54      private static final String SQL_QUERY_SELECT_IDENTITY_ATTRIBUTE    = "SELECT a.id_identity, a.id_attribute, attribute_value, lastupdate_date,certifier_code,certificate_date,level, expiration_date " 
55      		+ " FROM identitystore_identity_attribute a , identitystore_identity_attribute_certificate c, identitystore_ref_certification_level l, identitystore_ref_certification_processus p,  identitystore_ref_certification_attribute_level lp "
56      		+ " WHERE  a.id_certification = c.id_attribute_certificate  and c.certifier_code = p.code and p.id_ref_attribute_certification_processus = lp.id_ref_attribute_certification_processus and lp.id_attribute = a.id_attribute and lp.id_ref_certification_level = l.id_ref_certification_level ";
57      private static final String SQL_QUERY_SELECT_FILTER = " and  id_identity  in ( ";
58      private static final String SQL_QUERY_SELECT_IDENTITY_FILTER = " WHERE id_identity in (?";
59      private static final String SQL_CLOSE_PARENTHESIS = " ) ";
60      private static final String SQL_ADITIONAL_PARAMETER = ",?";
61  
62      /**
63       * {@inheritDoc }
64       */
65      @Override
66      public List<IdentityAttributeDataObject> selectAttributes( Collection<IdentityDataObject> lIdIdentity, Plugin plugin )
67      {
68          List<IdentityAttributeDataObject> ListIdentityAttributes = new ArrayList<>( );
69          StringBuffer strQuery = new StringBuffer( SQL_QUERY_SELECT_IDENTITY_ATTRIBUTE );
70          strQuery.append( SQL_QUERY_SELECT_FILTER );
71          if ( !CollectionUtils.isEmpty( lIdIdentity ) )
72              for ( IdentityDataObject id : lIdIdentity )
73              {
74                  strQuery.append( "?," );
75              }
76          strQuery.deleteCharAt( strQuery.length( ) - 1 );
77          strQuery.append( ")" );
78          DAOUtil daoUtil = new DAOUtil( strQuery.toString( ), plugin );
79          int ncpt = 1;
80          for ( IdentityDataObject id : lIdIdentity )
81          {
82              daoUtil.setInt( ncpt++, Integer.valueOf( id.getId( ) ) );
83          }
84          daoUtil.executeQuery( );
85          int nIndex;
86          while ( daoUtil.next( ) )
87          {
88              IdentityAttributeDataObjecttity/business/IdentityAttributeDataObject.html#IdentityAttributeDataObject">IdentityAttributeDataObject identityAttribute = new IdentityAttributeDataObject( );
89              nIndex = 1;
90              identityAttribute.setIdIdentity( daoUtil.getInt( nIndex++ ) );
91              identityAttribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
92              identityAttribute.setValue( daoUtil.getString( nIndex++ ) );
93              identityAttribute.setLastUpdateDate( daoUtil.getTimestamp( nIndex++ ) );
94              identityAttribute.setCertifierCode( daoUtil.getString( nIndex++ ) );
95              identityAttribute.setCertificateDate( daoUtil.getTimestamp( nIndex++ ) );
96              identityAttribute.setCertificateLevel( daoUtil.getString( nIndex++ ) );
97              identityAttribute.setCertificateExpirationDate( daoUtil.getTimestamp( nIndex++ ) );
98              ListIdentityAttributes.add( identityAttribute );
99          }
100         daoUtil.free( );
101         return ListIdentityAttributes;
102     }
103 
104     /**
105      * {@inheritDoc }
106      */
107     @Override
108     public List<IdentityDataObject> selectAllIdentity( List<Integer> listIdIdentity, Plugin plugin )
109     {
110         List<IdentityDataObject> listIdentity = new ArrayList<>( );
111         int nlistIdIdentitySize = listIdIdentity.size( );
112         if ( nlistIdIdentitySize > 0 )
113         {
114             StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_IDENTITY_TO_EXPORT + " " + SQL_QUERY_SELECT_IDENTITY_FILTER );
115             for ( int i = 1; i < nlistIdIdentitySize; i++ )
116             {
117                 sbSQL.append( SQL_ADITIONAL_PARAMETER );
118             }
119             sbSQL.append( SQL_CLOSE_PARENTHESIS );
120             try ( DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ) )
121             {
122                 for ( int i = 0; i < nlistIdIdentitySize; i++ )
123                 {
124                     daoUtil.setInt( i + 1, listIdIdentity.get( i ) );
125                 }
126                 daoUtil.executeQuery( );
127                 while ( daoUtil.next( ) )
128                 {
129                     listIdentity
130                             .add( new IdentityDataObject( daoUtil.getInt( 1 ), daoUtil.getString( 2 ), daoUtil.getString( 3 ), daoUtil.getTimestamp( 4 ), daoUtil.getTimestamp( 5 ) ) );
131                 }
132             }
133         }
134         return listIdentity;
135     }
136 
137     /**
138      * {@inheritDoc }
139      */
140     @Override
141     public List<Integer> selectAllIdIdentity( Plugin plugin )
142     {
143         List<Integer> listIdentity = new ArrayList<>( );
144         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ID_IDENTITY_TO_EXPORT, plugin ) )
145         {
146             daoUtil.executeQuery( );
147             while ( daoUtil.next( ) )
148             {
149                 listIdentity.add( daoUtil.getInt( 1 ) );
150             }
151         }
152         return listIdentity;
153     }
154 
155     /**
156      * {@inheritDoc }
157      */
158     @Override
159     public Map<Integer, String> selectAllAttributes( Plugin plugin )
160     {
161         Map<Integer, String> mapAttribute = new HashMap<>( );
162         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ATTRIBUTE, plugin ) )
163         {
164             daoUtil.executeQuery( );
165             while ( daoUtil.next( ) )
166             {
167                 mapAttribute.put( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
168             }
169         }
170         return mapAttribute;
171     }
172 }