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.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void createUser(String username, String fullName, String email, String password, String role) {
    SessionFactory sessionFactory = SecurityProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    ZRole zrole = getRole(role);//ww  w  .  j  a  v a  2  s  . c  o m
    ZPrincipal newUser = new ZPrincipal(username, fullName, email, SecurityElf.calcMD5(username, password),
            zrole);
    session.save(newUser);
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void updateUser(String username, String fullName, String email, String role) {
    if (username.equals(ADMIN_USER) && !role.equals(ADMINISTRATOR_ROLE)) {
        throw new SecurityException("You cannot change the role of the 'admin' account."); //$NON-NLS-1$
    }//from  w w  w  .j av  a2 s  .c o m

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

    ZPrincipal principal = (ZPrincipal) session.get(ZPrincipal.class, username);
    if (principal != null) {
        principal.setFullName(fullName);
        principal.setEmail(email);
        ZRole newRole = (ZRole) session.get(ZRole.class, role);
        if (newRole == null) {
            throw new SecurityException("Specified role " + role + " does not exist."); //$NON-NLS-1$ //$NON-NLS-2$
        }
        principal.setRole(newRole);
        session.update(principal);
    }
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void deleteUser(String username) {
    if (username.equals(ADMIN_USER)) {
        throw new SecurityException("You cannot delete Administrator role."); //$NON-NLS-1$
    }/*from  w  ww  .  j ava2s.c o  m*/

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

    ZPrincipal principal = (ZPrincipal) session.get(ZPrincipal.class, username);
    session.delete(principal);
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

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

    Criteria criteria = session.createCriteria(ZPrincipal.class);
    List<ZPrincipal> list = criteria.list();

    Collections.sort(list, new Comparator<ZPrincipal>() {
        public int compare(ZPrincipal p1, ZPrincipal p2) {
            return p1.getName().compareToIgnoreCase(p2.getName());
        }/*from   w w  w .  ja va2s .  c o  m*/
    });

    return list;
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

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

    Criteria criteria = session.createCriteria(ZRole.class);

    List<ZRole> list = new ArrayList<ZRole>();
    for (ZRole role : (List<ZRole>) criteria.list()) {
        session.evict(role);/*from  www  .jav a 2  s  .co  m*/
        if (role.getName().equals(ADMINISTRATOR_ROLE)) {
            list.add(getRole(ADMINISTRATOR_ROLE));
        } else {
            list.add(role);
        }
    }

    return list;
}

From source file:org.xerela.provider.telemetry.DiscoveryEventHandler.java

License:Mozilla Public License

/**
 * Update the timestamp on the device//from   w w  w .j  a v a 2  s.  c o  m
 * @param deviceId
 */
private void updateDeviceStatus(int deviceId) {
    SessionFactory factory = TelemetryActivator.getSessionFactory();
    Session session = factory.getCurrentSession();

    ZDeviceStatus deviceStatus = (ZDeviceStatus) session.get(ZDeviceStatus.class, deviceId);
    if (deviceStatus != null) {
        deviceStatus.setLastTelemetry(new Date());
        session.saveOrUpdate(deviceStatus);
    }

}

From source file:org.xerela.provider.telemetry.DiscoveryEventHandler.java

License:Mozilla Public License

private void deleteOldRecords(DiscoveryEvent discoveryEvent) {
    SessionFactory sessionFactory = TelemetryActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    session.createSQLQuery(//from   w  w  w  .  j a v a2s . com
            "DELETE FROM " + ARP_TABLE + " WHERE " + DEVICE_ID + " = " + discoveryEvent.getDeviceId())
            .executeUpdate();
    session.createSQLQuery(
            "DELETE FROM " + XDP_TABLE + " WHERE " + DEVICE_ID + " = " + discoveryEvent.getDeviceId())
            .executeUpdate();
    session.createSQLQuery(
            "DELETE FROM " + MAC_TABLE + " WHERE " + DEVICE_ID + " = " + discoveryEvent.getDeviceId())
            .executeUpdate();
    session.createSQLQuery(
            "DELETE FROM " + ROUTING_TABLE + " WHERE " + DEVICE_ID + " = " + discoveryEvent.getDeviceId())
            .executeUpdate();
}

From source file:org.xerela.server.job.backup.BackupTask.java

License:Mozilla Public License

/** {@inheritDoc} */
@Override/*  ww  w.j  av a 2s. co  m*/
protected Outcome performTask(CredentialSet credentialSet, ProtocolSet protocolSet,
        ConnectionPath connectionPath) throws Exception {
    ZDeviceCore device = getDevice();
    String ipAddress = device.getIpAddress();
    String adapterId = device.getAdapterId();
    String deviceId = Integer.toString(device.getDeviceId());
    SessionFactory sessionFactory = CoreJobsActivator.getSessionFactory();

    String backupOutput = null;
    File modelXmlFile = null;

    boolean success = false;
    TransactionElf.beginOrJoinTransaction(); // This thread OWNS this transaction

    try {
        // Execute the backup
        backupOutput = AdapterEndpointElf.getEndpoint(Backup.class, adapterId).backup(connectionPath);
        String filename = (ipAddress + "_backup.xml").replaceAll(":+", "."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        modelXmlFile = StringElf.stringToTempFile(filename, backupOutput);
        backupOutput = null;

        Session currentSession = sessionFactory.getCurrentSession();
        StaxProcessor processor = new StaxProcessor();
        processor.process(device, modelXmlFile);

        currentSession.flush();

        // Save the protocol set and credential set that were both successfully used to backup the device
        // and map this information to the device itself.
        CredentialsProvider credProvider = CoreJobsActivator.getCredentialsProvider();
        credProvider.mapDeviceToProtocolSet(deviceId, protocolSet);
        credProvider.mapDeviceToCredentialSet(deviceId, credentialSet);

        success = true;

        return Outcome.SUCCESS;

    }
    // TODO dwhite: This is for checking to see the contents of a problematic XML related to
    // bug #635 (http://bugs.xerela.org/show_bug.cgi?id=635)
    catch (XMLStreamException xse) {
        throw new XMLStreamException(
                xse.getMessage() + "\nProblematic XML:\n" + (backupOutput != null ? backupOutput : "null"),
                xse);
    } finally {
        if (modelXmlFile != null && modelXmlFile.exists()) {
            modelXmlFile.delete();
        }

        if (success) {
            TransactionElf.commit();
        } else {
            TransactionElf.rollback();
        }
    }
}

From source file:org.xerela.server.job.backup.DeviceInterfacePersister.java

License:Mozilla Public License

/** {@inheritDoc} */
public void endDocument(XMLEvent xmlEvent) {
    boolean sameIpSpace = !nattedIp();

    SessionFactory factory = CoreJobsActivator.getSessionFactory();
    Session session = factory.getCurrentSession();

    String hql = "DELETE " + InterfaceIpAddress.class.getName() + " WHERE device_id = " + device.getDeviceId();
    session.createQuery(hql).executeUpdate();

    // Prepare for batching ... see http://www.hibernate.org/hib_docs/reference/en/html/batch.html
    session.flush();/* w  w  w.j  a va2 s .c  o  m*/
    session.clear();

    int batchCount = 0;
    for (InterfaceIpAddress intIp : interfaceIps) {
        intIp.setSameIpSpace(sameIpSpace);
        session.save(intIp);
        if (batchCount++ % BATCH_SIZE == 0) {
            session.flush();
            session.clear();
        }
    }
}

From source file:org.xerela.server.job.backup.DeviceStatusBackupPersister.java

License:Mozilla Public License

/** {@inheritDoc} */
public void setDevice(ZDeviceCore device) {
    // We're ALWAYS called within the context of a transaction.
    SessionFactory factory = CoreJobsActivator.getSessionFactory();
    Session session = factory.getCurrentSession();

    deviceStatus = (ZDeviceStatus) session.get(ZDeviceStatus.class, device.getDeviceId());
    if (deviceStatus == null) {
        deviceStatus = new ZDeviceStatus();
        deviceStatus.setDeviceId(device.getDeviceId());
    }/*from   w  ww .  ja  v  a 2s  . c  o m*/

    adapterMetadata = CoreJobsActivator.getAdapterService().getAdapterMetadata(device.getAdapterId());
}