org_apache_derby_jdbc_EmbeddedDriver.java :  » Web-Framework » rife-1.6.1 » com » uwyn » rife » authentication » credentialsmanagers » databasedrivers » Java Open Source

Java Open Source » Web Framework » rife 1.6.1 
rife 1.6.1 » com » uwyn » rife » authentication » credentialsmanagers » databasedrivers » org_apache_derby_jdbc_EmbeddedDriver.java
/*
 * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
 * Distributed under the terms of either:
 * - the common development and distribution license (CDDL), v1.0; or
 * - the GNU Lesser General Public License, v2.1 or later
 * $Id: org_apache_derby_jdbc_EmbeddedDriver.java 3634 2007-01-08 21:42:24Z gbevin $
 */
package com.uwyn.rife.authentication.credentialsmanagers.databasedrivers;

import com.uwyn.rife.authentication.credentialsmanagers.exceptions.*;

import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsers;
import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
import com.uwyn.rife.config.RifeConfig;
import com.uwyn.rife.database.Datasource;
import com.uwyn.rife.database.DbPreparedStatement;
import com.uwyn.rife.database.DbPreparedStatementHandler;
import com.uwyn.rife.database.exceptions.DatabaseException;
import com.uwyn.rife.database.queries.CreateTable;
import com.uwyn.rife.database.queries.Insert;
import com.uwyn.rife.database.queries.Select;

public class org_apache_derby_jdbc_EmbeddedDriver extends generic
{
  public org_apache_derby_jdbc_EmbeddedDriver(Datasource datasource)
  {
    super(datasource);

    mCreateTableRole
      .customAttribute("roleId", "GENERATED ALWAYS AS IDENTITY");

    mCreateTableRoleLink = new CreateTable(getDatasource())
      .table(RifeConfig.Authentication.getTableRoleLink())
      .column("userId", long.class, CreateTable.NOTNULL)
      .column("roleId", int.class, CreateTable.NOTNULL)
      .primaryKey(RifeConfig.Authentication.getTableRoleLink().toUpperCase()+"_PK", new String[] {"userId", "roleId"})
      .foreignKey(RifeConfig.Authentication.getTableRoleLink().toUpperCase()+"_UI_FK", mCreateTableUser.getTable(), "userId", "userId", null, CreateTable.CASCADE)
      .foreignKey(RifeConfig.Authentication.getTableRoleLink().toUpperCase()+"_RI_FK", mCreateTableRole.getTable(), "roleId", "roleId", null, CreateTable.CASCADE);
    
    mAddRole = new Insert(getDatasource())
      .into(mCreateTableRole.getTable())
      .fieldParameter("name");

    mGetFreeUserId = new Select(getDatasource())
      .field("CASE WHEN MAX(userId) IS NULL THEN 0 ELSE MAX(userId)+1 END AS freeUserId")
      .from(mCreateTableUser.getTable());
  }
  
  public boolean install()
  throws CredentialsManagerException
  {
    try
    {
      executeUpdate(mCreateTableRole);
      executeUpdate(mCreateTableUser);
      executeUpdate(mCreateTableRoleLink);
    }
    catch (DatabaseException e)
    {
      throw new InstallCredentialsErrorException(e);
    }
  
    return true;
  }
  
  public boolean remove()
  throws CredentialsManagerException
  {
    try
    {
      executeUpdate(mDropTableRoleLink);
      executeUpdate(mDropTableUser);
      executeUpdate(mDropTableRole);
    }
    catch (DatabaseException e)
    {
      throw new RemoveCredentialsErrorException(e);
    }
    
    return true;
  }

  public DatabaseUsers addRole(final String role)
  throws CredentialsManagerException
  {
    if (null == role ||
      0 == role.length())
    {
      throw new AddRoleErrorException(role);
    }
    
    try
    {
      if (0 == executeUpdate(mAddRole, new DbPreparedStatementHandler() {
          public void setParameters(DbPreparedStatement statement)
          {
            statement
              .setString("name", role);
          }
        }))
      {
        throw new AddRoleErrorException(role);
      }
    }
    catch (DatabaseException e)
    {
      if (null != e.getCause())
      {
        String message = e.getCause().getMessage().toUpperCase();
        if (-1 != message.indexOf("AUTHROLE_NAME_UQ"))
        {
          throw new DuplicateRoleException(role);
        }
      }
    
      throw new AddRoleErrorException(role, e);
    }

    return this;
  }

  public DatabaseUsers addUser(String login, RoleUserAttributes attributes)
  throws CredentialsManagerException
  {
    try
    {
      _addUser(mAddUserWithId, mGetFreeUserId, mGetRoleId, mAddRoleLink, login, attributes);
    }
    catch (CredentialsManagerException e)
    {
      if (null != e.getCause() &&
        null != e.getCause().getCause())
      {
        String message = e.getCause().getCause().getMessage().toUpperCase();
        if (-1 != message.indexOf("AUTHUSER_LOGIN_UQ"))
        {
          throw new DuplicateLoginException(login);
        }
        if (-1 != message.indexOf("AUTHUSER_PK"))
        {
          throw new DuplicateUserIdException(attributes.getUserId());
        }
      }
    
      throw e;
    }
    
    return this;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.