package org.hisp.dhis.user.hibernate;

/*
 * Copyright (c) 2004-2020, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.hisp.dhis.hibernate.HibernateGenericStore;
import org.hisp.dhis.user.UserCredentials;
import org.hisp.dhis.user.UserCredentialsStore;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author Lars Helge Overland
 */
@Repository( "org.hisp.dhis.user.UserCredentialsStore" )
public class HibernateUserCredentialsStore
    extends HibernateGenericStore<UserCredentials>
    implements UserCredentialsStore
{
    public HibernateUserCredentialsStore( SessionFactory sessionFactory, JdbcTemplate jdbcTemplate, ApplicationEventPublisher publisher )
    {
        super( sessionFactory, jdbcTemplate, publisher, UserCredentials.class, true );
    }

    @Override
    public UserCredentials getUserCredentialsByUsername( String username )
    {
        Query<UserCredentials> query = getQuery( "from UserCredentials uc where uc.username = :username" );
        query.setParameter( "username", username );
        return query.uniqueResult();
    }

    @Override
    public UserCredentials getUserCredentialsByOpenId( String openId )
    {
        Query<UserCredentials> query = getQuery( "from UserCredentials uc where uc.openId = :openId" );
        query.setParameter( "openId", openId );
        return query.uniqueResult();
    }

    @Override
    public UserCredentials getUserCredentialsByLdapId( String ldapId )
    {
        Query<UserCredentials> query = getQuery( "from UserCredentials uc where uc.ldapId = :ldapId" );
        query.setParameter( "ldapId", ldapId );
        return query.uniqueResult();
    }
}

@@ -1682,16 +1682,23 @@
 sp.dhis.
+common.
 hibernat
@@ -1708,27 +1708,130 @@
 ibernate
-GenericStor
+IdentifiableObjectStore;%0Aimport org.hisp.dhis.security.acl.AclService;%0Aimport org.hisp.dhis.user.CurrentUserServic
 e;%0Aimpor
@@ -2244,15 +2244,26 @@
 nate
-Generic
+IdentifiableObject
 Stor
@@ -2418,16 +2418,24 @@
 emplate,
+%0A       
  Applica
@@ -2462,16 +2462,78 @@
 ublisher
+, CurrentUserService currentUserService, AclService aclService
  )%0A    %7B
@@ -2611,16 +2611,48 @@
 s.class,
+ currentUserService, aclService,
  true );

make HibernateUserCredentialsStore extend HibernateIdentifiableObjectStore so it gets addded to identifiableObjectStores (#5783)