Example usage for org.hibernate SessionFactory getCurrentSession

List of usage examples for org.hibernate SessionFactory getCurrentSession

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getCurrentSession.

Prototype

Session getCurrentSession() throws HibernateException;

Source Link

Document

Obtains the current session.

Usage

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/** {@inheritDoc} */
public void deleteCredentialConfig(CredentialConfig credentialConfig) throws PersistenceException {
    if (credentialConfig != null) {
        try {// ww w  . j  a v a 2 s. c  o m
            boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
            SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
            Session currentSession = sessionFactory.getCurrentSession();

            if (credentialConfig != null) {
                currentSession.delete(credentialConfig);
            }

            if (ownTransaction) {
                TransactionElf.commit();
            }
        } catch (RuntimeException e) {
            TransactionElf.rollback();
            throw new PersistenceException(e);
        }
    }
}

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/** {@inheritDoc} */
public Collection<CredentialConfig> getAllCredentialConfigs() throws PersistenceException {
    Set<CredentialConfig> credentialConfigs = new HashSet<CredentialConfig>();

    try {//w w  w  . j av  a  2 s  .  c o m
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
        SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();

        // Grab all of the non-default credential configurations
        Criteria criteria = currentSession.createCriteria(CredentialConfig.class);
        criteria.add(Restrictions.eq(THE_DEFAULT, false));
        List<?> list = criteria.list();

        for (Iterator<?> iter = list.iterator(); iter.hasNext();) {
            CredentialConfig cc = (CredentialConfig) iter.next();
            credentialConfigs.add(cc);
        }

        if (ownTransaction) {
            TransactionElf.commit();
        }
    } catch (RuntimeException e) {
        throw new PersistenceException(e);
    }

    return credentialConfigs;
}

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/** {@inheritDoc} */
public CredentialConfig getDefaultCredentialConfig() throws PersistenceException {
    CredentialConfig defaultCC = null;//from   ww  w  .j a va 2  s. co  m

    try {
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
        SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();

        // Grab the default credential configuration
        Criteria criteria = currentSession.createCriteria(CredentialConfig.class);
        criteria.add(Restrictions.eq(THE_DEFAULT, true));
        Object uniqueResult = criteria.uniqueResult();

        if (uniqueResult != null) {
            defaultCC = (CredentialConfig) uniqueResult;
        }

        if (ownTransaction) {
            TransactionElf.commit();
        }
    } catch (RuntimeException e) {
        throw new PersistenceException(e);
    }

    return defaultCC;
}

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/** {@inheritDoc} */
public void purgeUnmappedCredentialSets() throws PersistenceException {
    try {//from ww  w.  j a v a2 s .  c om
        // Begin of join a transaction
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();

        // Retrieve the current Hibernate session
        SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();

        // Retrieve all of the credentials to purge.  The keys in the map are credential set IDs and the values
        // in the map are credential config IDs
        Map<Long, Long> credSetsToPurge = getCredentialSetsToPurge();
        for (Entry<Long, Long> mapping : credSetsToPurge.entrySet()) {
            // Attempt to grab the credential config object from the database
            Object credConfigObj = currentSession.get(CredentialConfig.class, mapping.getValue());
            if (credConfigObj != null) {
                CredentialConfig credentialConfig = (CredentialConfig) credConfigObj;
                boolean isTheDefault = credentialConfig.isTheDefault();
                if (!isTheDefault || (isTheDefault && credentialConfig.getCredentialSets().size() > 1)) {
                    Object retrievedObj = currentSession.get(CredentialSet.class, mapping.getKey());
                    if (retrievedObj != null) {
                        CredentialSet credentialSet = (CredentialSet) retrievedObj;
                        credentialConfig.getCredentialSets().remove(credentialSet);
                        deleteCredentialSet(credentialSet);
                    }
                }
            }
        }

        if (ownTransaction) {
            TransactionElf.commit();
        }
    } catch (PersistenceException e) {
        TransactionElf.rollback();
        throw e;
    }
}

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/**
 * Purges any device-to-credential set mappings containing the {@link CredentialSet} object specified,
 * and then the actual {@link CredentialSet} object is removed from the database.
 * //from w  w w.  j  a va2 s . co m
 * @param credentialSet The credentials set to remove.
 * @throws PersistenceException if there was an error while clearing out the device-to-credential set mappings.
 */
private void deleteCredentialSet(CredentialSet credentialSet) throws PersistenceException {
    if (credentialSet != null) {
        // Clear out all the credentials associated with the ID of the credential set
        LOGGER.info("Removing the unused credential set '" + credentialSet.getName() + "'");
        clearDeviceToCredentialSetMappings(credentialSet);

        SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();

        if (credentialSet != null) {
            currentSession.delete(credentialSet);
        }
    }
}

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/**
 * Retrieves the {@link DeviceToCredentialSetMapping} object for the specified device ID.
 * //from  www. j a va 2  s . c om
 * @param deviceID The device ID that should exists on the located device-to-credential set mapping.
 * @param returnStaleCredential Flag to determine whether or not a credential set that has been marked as stale should be returned.
 * @return A valid {@link DeviceToCredentialSetMapping} object if the specified device associated with the device ID has
 * a mapping to a valid credential set; null if there is no such mapping.
 */
private DeviceToCredentialSetMapping getDeviceToCredentialSetMapping(String deviceID,
        boolean returnStaleCredential) {
    DeviceToCredentialSetMapping toReturn = null;

    boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
    SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
    Session currentSession = sessionFactory.getCurrentSession();

    // Get the device to credential set mapping for the specified device ID
    Criteria criteria = currentSession.createCriteria(DeviceToCredentialSetMapping.class);
    criteria.add(Restrictions.eq(DEVICE_ID, Integer.valueOf(deviceID).intValue()));

    // If stale credentials should not be returned, make sure they are culled out
    if (!returnStaleCredential) {
        criteria.add(Restrictions.eq(STALE, false));
    }

    // Grab the actual DeviceToCredentialSetMapping object
    Object uniqueResult = criteria.uniqueResult();
    if (uniqueResult != null) {
        toReturn = (DeviceToCredentialSetMapping) uniqueResult;
    }

    if (ownTransaction) {
        TransactionElf.commit();
    }

    return toReturn;
}

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/**
 * Retrieves a {@link Map} of all the credential sets to be purged. The keys of the {@link Map} are the IDs of the
 * {@link CredentialSet} objects to purge, and the values are the IDs of the {@link CredentialConfig} objects associated with
 * the credential sets.//from  ww  w.  j a  v  a 2 s  .  co m
 * 
 * @return A {@link Map} of credential set IDs to credential config IDs.
 */
private Map<Long, Long> getCredentialSetsToPurge() {
    Map<Long, Long> credSetsToPurge = new HashMap<Long, Long>();
    String sql = "SELECT DISTINCT a.id, a.fkCredentialConfigId FROM cred_set a LEFT OUTER JOIN device_to_cred_set_mappings b ON a.id = b.fkCredentialSetId WHERE (b.id IS NULL)";

    boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
    SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
    Session currentSession = sessionFactory.getCurrentSession();
    SQLQuery sqlQuery = currentSession.createSQLQuery(sql);

    // Execute our SQL statement and grab the results
    List<?> list = sqlQuery.list();

    // Iterate through all of the results, grabbing the CredentialSet ID from the 1st column,
    // and the CredentialConfig ID from the 2nd column.  Unlike JDBC, columns of results are numbered from zero.
    for (Iterator<?> iter = list.iterator(); iter.hasNext();) {
        Object[] row = (Object[]) iter.next();
        credSetsToPurge.put(Long.parseLong(row[0].toString()), Long.parseLong(row[1].toString()));
    }

    if (ownTransaction) {
        TransactionElf.commit();
    }

    return credSetsToPurge;
}

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/**
 * Saves or updates a generic object to the database
 *
 * @param obj The generic object to save or update.
 * @throws PersistenceException If any issue occurred while trying save or update the generic object to the database.
 *///from   w w w. j a v  a  2s  . c  o  m
private void saveOrUpdate(Object obj) throws PersistenceException {
    try {
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
        SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();

        obj = currentSession.merge(obj);

        currentSession.saveOrUpdate(obj);

        if (ownTransaction) {
            TransactionElf.commit();
        }
    } catch (RuntimeException e) {
        TransactionElf.rollback();
        throw new PersistenceException(e);
    }
}

From source file:org.xerela.provider.credentials.DatabaseCredentialsPersister.java

License:Mozilla Public License

/**
 * Executes an update against the database according to a specified HQL string.
 * /*from ww  w .j a  v a 2 s  .  c om*/
 * @param hql The HQL string to describing the update to be executed against the database.
 * @throws PersistenceException If any issue occurred while trying to execute the update against the database.
 */
private void executeUpdate(String hql) throws PersistenceException {
    try {
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
        SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();

        currentSession.createQuery(hql).executeUpdate();

        if (ownTransaction) {
            TransactionElf.commit();
        }
    } catch (RuntimeException e) {
        TransactionElf.rollback();
        throw new PersistenceException(e);
    }
}

From source file:org.xerela.provider.credentials.DatabaseProtocolPersister.java

License:Mozilla Public License

/**
 * {@inheritDoc}//from   w  w  w . j a  va 2s. com
 */
public ProtocolConfig getDefaultProtocolConfig() throws PersistenceException {
    ProtocolConfig defaultProtocolConfig = null;

    try {
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
        SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();

        // Grab the default protocol configuration
        Criteria criteria = currentSession.createCriteria(ProtocolConfig.class);
        criteria.add(Restrictions.eq(THE_DEFAULT, true));
        Object uniqueResult = criteria.uniqueResult();

        if (uniqueResult != null) {
            defaultProtocolConfig = (ProtocolConfig) uniqueResult;
        }

        if (ownTransaction) {
            TransactionElf.commit();
        }
    } catch (RuntimeException e) {
        throw new PersistenceException(e);
    }

    return defaultProtocolConfig;
}