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.DatabaseProtocolPersister.java

License:Mozilla Public License

/**
 * {@inheritDoc}/*w w  w.ja va 2s .c o m*/
 */
public Set<ProtocolConfig> getAllProtocolConfigs() throws PersistenceException {
    Set<ProtocolConfig> protocolConfigs = new HashSet<ProtocolConfig>();

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

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

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

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

    return protocolConfigs;
}

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

License:Mozilla Public License

/**
 * {@inheritDoc}/* w ww. ja  va 2  s . c o m*/
 */
public void deleteProtocolConfig(ProtocolConfig protocolConfig) throws PersistenceException {
    if (protocolConfig != null) {
        try {
            boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
            SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
            Session currentSession = sessionFactory.getCurrentSession();

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

            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} */
public void clearDeviceToProtocolMapping(String deviceID) throws PersistenceException {
    List<DeviceToProtocolMapping> deviceToProtocolMappings = getDeviceToProtocolMappings(deviceID, true);
    if (deviceToProtocolMappings.size() > 0) {
        try {/*from   w  ww  .ja v  a  2  s  .co m*/
            boolean ownTransaction = TransactionElf.beginOrJoinTransaction();
            SessionFactory sessionFactory = CredentialsProviderActivator.getSessionFactory();
            Session currentSession = sessionFactory.getCurrentSession();

            for (DeviceToProtocolMapping dpm : deviceToProtocolMappings) {
                currentSession.delete(dpm);
            }

            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

/**
 * Retrieves a {@link List} of {@link DeviceToProtocolMapping} objects that are associated with the specified device ID.
 * //from w w  w  .  j a  va 2s  . com
 * @param deviceID The device ID that should exists on the located device-to-protocol mapping.
 * @param returnStaleProtocols Flag to determine whether or not a protocol that has been marked as stale should be returned.
 * @return A valid {@link DeviceToProtocolMapping} object if the specified device associated with the device ID has
 * a mapping to a valid set of protocols; null if there is no such mapping.
 */
private List<DeviceToProtocolMapping> getDeviceToProtocolMappings(String deviceID,
        boolean returnStaleProtocol) {
    List<DeviceToProtocolMapping> toReturn = new ArrayList<DeviceToProtocolMapping>();

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

    // Get the device protocol mapping for the specified IP address
    Criteria criteria = currentSession.createCriteria(DeviceToProtocolMapping.class);
    criteria.add(Restrictions.eq(DEVICE_ID, Integer.valueOf(deviceID).intValue()));

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

    // Grab all of the DeviceProtocolMapping objects
    List<?> list = criteria.list();
    for (Iterator<?> iter = list.iterator(); iter.hasNext();) {
        DeviceToProtocolMapping dpm = (DeviceToProtocolMapping) iter.next();
        toReturn.add(dpm);
    }

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

    return toReturn;
}

From source file:org.xerela.provider.devices.DeviceProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public ZDeviceCore getDevice(String ipAddress, String managedNetwork) {
    String network = resolveManagedNetwork(managedNetwork);

    // IPAddress address = (IPAddress) NetworkAddressElf.parseAddress(ipAddress);
    String ipv6 = NetworkAddressElf.toDatabaseString(ipAddress);

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

    return (ZDeviceCore) currentSession.createCriteria(ZDeviceCore.class).add(Restrictions.eq(IP_ADDRESS, ipv6))
            .add(Restrictions.eq(NETWORK, network)).uniqueResult();
}

From source file:org.xerela.provider.devices.DeviceProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void updateDevice(String ipAddress, String managedNetwork, ZDeviceCore device) {
    ZDeviceCore dbDevice = getDevice(ipAddress, managedNetwork);
    if (dbDevice == null) {
        return;//from  ww w  .  ja va  2 s.  co m
    }

    if (DeviceProviderActivator.getAdapterService().getAdapterMetadata(device.getAdapterId()) != null) {
        if (!dbDevice.getAdapterId().equals(device.getAdapterId())) {
            dbDevice.setAdapterId(device.getAdapterId());
            deviceNotifier.notifyDeviceObservers(dbDevice, DeviceNotification.CHANGE_DEVICE_TYPE);
        }
    }

    // When setting the host name, we should accept any host name that is given.  There are situations
    // where the hostname retrieved by DNS lookup gives us a string that does not validate.
    dbDevice.setHostname(device.getHostname());
    //if (NetworkAddressElf.isValidHostname(device.getHostname()))
    //{
    //    dbDevice.setHostname(device.getHostname());
    //}

    if (NetworkAddressElf.isValidAddress(device.getIpAddress())) {
        dbDevice.setIpAddress(NetworkAddressElf.toDatabaseString(device.getIpAddress()));
    }

    if (DeviceProviderActivator.getNetworksProvider().getManagedNetwork(device.getManagedNetwork()) != null) {
        dbDevice.setManagedNetwork(device.getManagedNetwork());
    }

    SessionFactory sessionFactory = DeviceProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    session.merge(dbDevice);
}

From source file:org.xerela.provider.devices.DeviceProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public List<ZDeviceCore> createDeviceBatched(List<ZDeviceCore> devices) {
    ArrayList<ZDeviceCore> failedDevices = new ArrayList<ZDeviceCore>();

    SessionFactory sessionFactory = DeviceProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    int batch = 0;
    for (ZDeviceCore device : devices) {
        try {// w  ww .ja  v a2  s  . c o  m
            createDevice(device);
        } catch (RuntimeException e) {
            failedDevices.add(device);
            continue;
        }

        if (++batch % BATCH_SIZE == 0) {
            session.flush();
            session.clear();
            LOGGER.info(Messages.bind(Messages.DeviceProvider_addedBatchDevices, BATCH_SIZE));
        }
    }

    return failedDevices;
}

From source file:org.xerela.provider.devices.DeviceProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public List<String> getAllHardwareVendors() {
    SessionFactory sessionFactory = DeviceProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    Query query = session.createQuery("SELECT DISTINCT d.hardwareVendor FROM ZDeviceLite d"); //$NON-NLS-1$
    List<?> list = query.list();

    return (List<String>) list;
}

From source file:org.xerela.provider.netman.NetworksProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void defineManagedNetwork(String name) {
    if (name == null || name.length() == 0) {
        return;//  w ww  . j  a v  a 2  s.co  m
    }

    Session session = null;
    try {
        rwLock.writeLock().lock();

        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();

        ManagedNetwork defaultManagedNetwork = getDefaultManagedNetwork();

        ManagedNetwork managedNetwork = new ManagedNetwork();
        managedNetwork.setName(name);
        managedNetwork.setDefault((defaultManagedNetwork == null));

        SessionFactory sessionFactory = NetworksActivator.getSessionFactory();
        session = sessionFactory.getCurrentSession();

        session.save(managedNetwork);

        if (ownTransaction) {
            TransactionElf.commit();
        }
    } catch (RuntimeException e) {
        TransactionElf.rollback();
        throw e;
    } finally {
        rwLock.writeLock().unlock();
    }
}

From source file:org.xerela.provider.netman.NetworksProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void deleteManagedNetwork(String name) {
    if (name == null || name.length() == 0) {
        return;// www.ja  v  a2  s  . co  m
    }

    try {
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();

        SessionFactory sessionFactory = NetworksActivator.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();

        Criteria criteria = session.createCriteria(ManagedNetwork.class).add(Restrictions.eq(NAME, name));
        ManagedNetwork network = (ManagedNetwork) criteria.uniqueResult();
        session.delete(network);

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

        throw new RuntimeException("Not implemented."); //$NON-NLS-1$
    } catch (RuntimeException e) {
        TransactionElf.rollback();
        throw e;
    }
}