Example usage for org.hibernate Session refresh

List of usage examples for org.hibernate Session refresh

Introduction

In this page you can find the example usage for org.hibernate Session refresh.

Prototype

void refresh(Object object);

Source Link

Document

Re-read the state of the given instance from the underlying database.

Usage

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RequestCapabilitiesDaoTester.java

License:Open Source License

public void testDeleteResourcePermission() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//  w ww .j  a  va 2s .  c o  m
    RequestCapabilities req = new RequestCapabilities("a,b");
    ses.persist(req);
    UserClass uc = new UserClass();
    uc.setName("user_class");
    ses.persist(uc);
    RigCapabilities rig1 = new RigCapabilities("a,b,c,d");
    ses.persist(rig1);
    RigCapabilities rig2 = new RigCapabilities("a,b,c,d,e");
    ses.persist(rig2);
    ResourcePermission rc = new ResourcePermission();
    rc.setType("CAPABILITY");
    rc.setRequestCapabilities(req);
    rc.setExpiryTime(new Date());
    rc.setStartTime(new Date());
    rc.setUserClass(uc);
    ses.persist(rc);
    MatchingCapabilities match1 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig1.getId(), req.getId()), req, rig1);
    ses.persist(match1);
    MatchingCapabilities match2 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig2.getId(), req.getId()), req, rig2);
    ses.persist(match2);
    ses.getTransaction().commit();

    this.dao.delete(req.getId());
    assertNull(this.dao.get(req.getId()));

    ses.refresh(rig1);
    assertEquals(0, rig1.getMatchingCapabilitieses().size());
    ses.refresh(rig2);
    assertEquals(0, rig2.getMatchingCapabilitieses().size());

    ses.beginTransaction();
    ses.delete(rig1);
    ses.delete(rig2);
    ses.delete(rc);
    ses.delete(uc);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigCapabilitiesDaoTester.java

License:Open Source License

@Test
public void testDelete() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//from   ww  w  .jav a  2s.com
    RequestCapabilities req1 = new RequestCapabilities("a,b");
    ses.persist(req1);
    RequestCapabilities req2 = new RequestCapabilities("c,d");
    ses.persist(req2);
    RigCapabilities rig = new RigCapabilities("a,b,c,d");
    ses.persist(rig);
    MatchingCapabilities match1 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig.getId(), req1.getId()), req1, rig);
    ses.persist(match1);
    MatchingCapabilities match2 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig.getId(), req2.getId()), req2, rig);
    ses.persist(match2);
    ses.getTransaction().commit();

    this.dao.delete(rig.getId());
    assertNull(this.dao.get(rig.getId()));

    ses.refresh(req1);
    assertEquals(0, req1.getMatchingCapabilitieses().size());
    ses.refresh(req2);
    assertEquals(0, req2.getMatchingCapabilitieses().size());

    ses.beginTransaction();
    ses.delete(req1);
    ses.delete(req2);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#findLogs(au.edu.uts.eng.remotelabs.schedserver.dataaccess.entities.RigType, java.util.Date, java.util.Date)}.
 *//*from www .j  ava  2 s . c o  m*/
@Test
public void testFindLogsRigTypeDateDate() {
    Date before = new Date(System.currentTimeMillis() - 1000000);
    Date now = new Date();
    Date after = new Date(System.currentTimeMillis() + 1000000);

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();

    RigType type = new RigType();
    type.setName("log_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("log_name_test1");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(after);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOfflineReason("Tomorrows problem");
    ses.save(rig);

    Rig rig2 = new Rig();
    rig2.setName("log_name_test2");
    rig2.setRigType(type);
    rig2.setContactUrl("http://url");
    rig2.setRigCapabilities(caps);
    rig2.setLastUpdateTimestamp(after);
    rig2.setManaged(false);
    rig2.setMeta("iLabs");
    rig2.setOfflineReason("Tomorrows problem");
    ses.save(rig2);

    RigLog online = new RigLog();
    online.setOldState(RigLog.NOT_REGISTERED);
    online.setNewState(RigLog.ONLINE);
    online.setReason("First registration");
    online.setTimeStamp(before);
    online.setRig(rig);
    ses.save(online);

    RigLog offline = new RigLog();
    offline.setOldState(RigLog.ONLINE);
    offline.setNewState(RigLog.OFFLINE);
    offline.setReason("Offline");
    offline.setTimeStamp(now);
    offline.setRig(rig2);
    ses.save(offline);

    RigLog off2 = new RigLog();
    off2.setOldState(RigLog.ONLINE);
    off2.setNewState(RigLog.OFFLINE);
    off2.setReason("One Offline");
    off2.setTimeStamp(now);
    off2.setRig(rig);
    ses.save(off2);

    RigLog notreg = new RigLog();
    notreg.setOldState(RigLog.OFFLINE);
    notreg.setNewState(RigLog.NOT_REGISTERED);
    notreg.setReason("Not reg");
    notreg.setTimeStamp(after);
    notreg.setRig(rig2);
    ses.save(notreg);
    ses.getTransaction().commit();

    ses.refresh(type);
    System.out.println(type.getRigs().size());
    Map<Rig, List<RigLog>> logs = this.dao.findLogs(type, new Date(before.getTime() - 1000),
            new Date(after.getTime() + 1000));

    ses.beginTransaction();
    ses.delete(notreg);
    ses.delete(offline);
    ses.delete(off2);
    ses.delete(online);
    ses.delete(rig);
    ses.delete(rig2);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();

    assertEquals(2, logs.size());
    for (Entry<Rig, List<RigLog>> rlogs : logs.entrySet()) {
        assertEquals(2, rlogs.getValue().size());
        if (rig.getId().longValue() == rlogs.getKey().getId().longValue()) {
            assertEquals(online.getId().longValue(), rlogs.getValue().get(0).getId().longValue());
            assertEquals(off2.getId().longValue(), rlogs.getValue().get(1).getId().longValue());
            assertEquals("First registration", rlogs.getValue().get(0).getReason());
            assertEquals("One Offline", rlogs.getValue().get(1).getReason());
        } else if (rig2.getId().longValue() == rlogs.getKey().getId().longValue()) {
            assertEquals(offline.getId().longValue(), rlogs.getValue().get(0).getId().longValue());
            assertEquals(notreg.getId().longValue(), rlogs.getValue().get(1).getId().longValue());
            assertEquals("Offline", rlogs.getValue().get(0).getReason());
            assertEquals("Not reg", rlogs.getValue().get(1).getReason());
        } else {
            fail("Impossible rig.");
        }
    }
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#findLogsOfState(au.edu.uts.eng.remotelabs.schedserver.dataaccess.entities.RigType, java.lang.String, java.util.Date, java.util.Date)}.
 *//*  w  w  w  .j av  a 2  s  .  c o m*/
@Test
public void testFindLogsOfStateRigTypeStringDateDate() {
    Date before = new Date(System.currentTimeMillis() - 1000000);
    Date now = new Date();
    Date after = new Date(System.currentTimeMillis() + 1000000);

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();

    RigType type = new RigType();
    type.setName("log_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("log_name_test1");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(after);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOfflineReason("Tomorrows problem");
    ses.save(rig);

    Rig rig2 = new Rig();
    rig2.setName("log_name_test2");
    rig2.setRigType(type);
    rig2.setContactUrl("http://url");
    rig2.setRigCapabilities(caps);
    rig2.setLastUpdateTimestamp(after);
    rig2.setManaged(false);
    rig2.setMeta("iLabs");
    rig2.setOfflineReason("Tomorrows problem");
    ses.save(rig2);

    RigLog online = new RigLog();
    online.setOldState(RigLog.NOT_REGISTERED);
    online.setNewState(RigLog.ONLINE);
    online.setReason("First registration");
    online.setTimeStamp(before);
    online.setRig(rig);
    ses.save(online);

    RigLog offline = new RigLog();
    offline.setOldState(RigLog.ONLINE);
    offline.setNewState(RigLog.OFFLINE);
    offline.setReason("Offline");
    offline.setTimeStamp(now);
    offline.setRig(rig2);
    ses.save(offline);

    RigLog off2 = new RigLog();
    off2.setOldState(RigLog.ONLINE);
    off2.setNewState(RigLog.OFFLINE);
    off2.setReason("One Offline");
    off2.setTimeStamp(now);
    off2.setRig(rig);
    ses.save(off2);

    RigLog notreg = new RigLog();
    notreg.setOldState(RigLog.OFFLINE);
    notreg.setNewState(RigLog.NOT_REGISTERED);
    notreg.setReason("Not reg");
    notreg.setTimeStamp(after);
    notreg.setRig(rig2);
    ses.save(notreg);
    ses.getTransaction().commit();

    ses.refresh(type);
    System.out.println(type.getRigs().size());
    Map<Rig, List<RigLog>> logs = this.dao.findLogsOfState(type, RigLog.OFFLINE,
            new Date(before.getTime() - 1000), new Date(after.getTime() + 1000));

    ses.beginTransaction();
    ses.delete(notreg);
    ses.delete(offline);
    ses.delete(off2);
    ses.delete(online);
    ses.delete(rig);
    ses.delete(rig2);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();

    assertEquals(2, logs.size());
    for (Entry<Rig, List<RigLog>> rlogs : logs.entrySet()) {
        assertEquals(1, rlogs.getValue().size());
        if (rig.getId().longValue() == rlogs.getKey().getId().longValue()) {
            assertEquals(off2.getId().longValue(), rlogs.getValue().get(0).getId().longValue());
            assertEquals("One Offline", rlogs.getValue().get(0).getReason());
        } else if (rig2.getId().longValue() == rlogs.getKey().getId().longValue()) {
            assertEquals(offline.getId().longValue(), rlogs.getValue().get(0).getId().longValue());
            assertEquals("Offline", rlogs.getValue().get(0).getReason());
        } else {
            fail("Impossible rig.");
        }
    }
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.UserClassDaoTester.java

License:Open Source License

@Test
public void testDeleteId() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/*from  ww  w  . ja  v  a2s  .c o  m*/
    RigType ty = new RigType("type123", 10, false);
    ses.save(ty);
    RigCapabilities caps = new RigCapabilities("a,b");
    ses.save(caps);
    Rig rig = new Rig(ty, caps, "rig123", "http://rig/", new Date(), true, null, false, true, true);
    ses.save(rig);

    User us = new User("user", "userns", "ADMIN");
    ses.save(us);
    UserClass uc = new UserClass();
    uc.setName("clazz");
    uc.setPriority((short) 10);
    uc.setActive(true);
    uc.setKickable(true);
    uc.setQueuable(true);
    uc.setBookable(true);
    uc.setUsersLockable(true);
    ses.save(uc);
    ResourcePermission perm = new ResourcePermission();
    perm.setUserClass(uc);
    perm.setType("RIG");
    perm.setActivityDetected(false);
    perm.setAllowedExtensions((short) 5);
    perm.setSessionActivityTimeout(300);
    perm.setSessionDuration(3600);
    perm.setExtensionDuration(300);
    perm.setQueueActivityTimeout(300);
    perm.setExpiryTime(new Date());
    perm.setStartTime(new Date());
    perm.setRig(rig);
    ses.save(perm);
    UserAssociationId id = new UserAssociationId(us.getId(), uc.getId());
    UserAssociation assoc = new UserAssociation(id, uc, us);
    ses.save(assoc);
    ses.getTransaction().commit();

    this.dao.delete(uc.getId());

    GenericDao<ResourcePermission> rpDao = new GenericDao<ResourcePermission>(ResourcePermission.class);
    assertNull(rpDao.get(perm.getId()));
    ses.refresh(us);
    assertEquals(0, us.getUserAssociations().size());

    ses.beginTransaction();
    ses.delete(us);
    ses.delete(rig);
    ses.delete(caps);
    ses.delete(ty);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.UserClassDaoTester.java

License:Open Source License

@Test
public void testDeleteName() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//  w w w  .  j ava2  s. com
    RigType ty = new RigType("type23", 10, false);
    ses.save(ty);
    RigCapabilities caps = new RigCapabilities("a,b");
    ses.save(caps);
    Rig rig = new Rig(ty, caps, "rig23", "http://rig/", new Date(), true, null, false, true, true);
    ses.save(rig);

    User us = new User("user", "userns", "ADMIN");
    ses.save(us);
    UserClass uc = new UserClass();
    uc.setName("clazz");
    uc.setPriority((short) 10);
    uc.setActive(true);
    uc.setKickable(true);
    uc.setQueuable(true);
    uc.setBookable(true);
    uc.setUsersLockable(true);
    ses.save(uc);
    ResourcePermission perm = new ResourcePermission();
    perm.setUserClass(uc);
    perm.setType("RIG");
    perm.setActivityDetected(false);
    perm.setAllowedExtensions((short) 5);
    perm.setSessionActivityTimeout(300);
    perm.setSessionDuration(3600);
    perm.setExtensionDuration(300);
    perm.setQueueActivityTimeout(300);
    perm.setExpiryTime(new Date());
    perm.setStartTime(new Date());
    perm.setRig(rig);
    ses.save(perm);
    UserAssociationId id = new UserAssociationId(us.getId(), uc.getId());
    UserAssociation assoc = new UserAssociation(id, uc, us);
    ses.save(assoc);
    ses.getTransaction().commit();

    this.dao.delete(this.dao.findByName("clazz"));

    GenericDao<ResourcePermission> rpDao = new GenericDao<ResourcePermission>(ResourcePermission.class);
    assertNull(rpDao.get(perm.getId()));
    ses.refresh(us);
    assertEquals(0, us.getUserAssociations().size());

    ses.beginTransaction();
    ses.delete(us);
    ses.delete(rig);
    ses.delete(caps);
    ses.delete(ty);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.impl.tests.UserAdminTester.java

License:Open Source License

@Test
public void testEditUserNewNameNSPersona() {
    String name = "tuser";
    String ns = "tuserns";

    User user = new User(name, ns, User.ADMIN);
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/* w w w  .  jav  a 2  s. com*/
    ses.save(user);
    ses.getTransaction().commit();

    assertTrue(this.admin.editUser(user.getId(), "newName", "newNS", User.USER));

    User edittedUser = this.dao.findByName("newName", "newNS");
    ses.refresh(user);
    assertEquals(user.getName(), edittedUser.getName());
    assertEquals(user.getNamespace(), edittedUser.getNamespace());
    assertEquals(user.getId(), edittedUser.getId());
    assertEquals(user.getPersona(), edittedUser.getPersona());

    this.dao.delete(edittedUser);
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.impl.tests.UserAdminTester.java

License:Open Source License

@Test
public void testEditUserNewPersona() {
    String name = "tuser";
    String ns = "tuserns";

    User user = new User(name, ns, User.ADMIN);
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/*from  ww w  . j av a 2s.  c o  m*/
    ses.save(user);
    ses.getTransaction().commit();

    assertTrue(this.admin.editUser(user.getId(), ns, name, User.USER));

    ses.refresh(user);
    assertEquals(name, user.getName());
    assertEquals(ns, user.getNamespace());
    assertEquals(User.USER, user.getPersona());

    ses.beginTransaction();
    ses.delete(user);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.impl.tests.UserAdminTester.java

License:Open Source License

@Test
public void testEditUserNoNameNoNSPersona() {
    String name = "tuser";
    String ns = "tuserns";

    User user = new User(name, ns, User.ADMIN);
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/*from w w w. j av  a2  s . c o m*/
    ses.save(user);
    ses.getTransaction().commit();

    assertTrue(this.admin.editUser(user.getId(), null, null, User.USER));

    ses.refresh(user);
    assertEquals(name, user.getName());
    assertEquals(ns, user.getNamespace());
    assertEquals(User.USER, user.getPersona());

    ses.beginTransaction();
    ses.delete(user);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

/**
 * Test method for {@link PermissionsSOAPImpl#unlockUserLock(UnlockUserLock)}.
 *//*  www .j a  v a2  s  .c om*/
@Test
public void testUnlockUserLockID() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    User user = new User("locktest", "ns", "USER");
    ses.persist(user);
    UserClass userClass = new UserClass();
    userClass.setName("uc");
    ses.persist(userClass);
    ResourcePermission perm = new ResourcePermission();
    perm.setStartTime(new Date());
    perm.setExpiryTime(new Date());
    perm.setUserClass(userClass);
    perm.setType("RIG");
    ses.persist(perm);
    UserLock lock = new UserLock(user, perm, true, "abc123");
    ses.persist(lock);
    ses.getTransaction().commit();

    /* Request. */
    UnlockUserLock request = new UnlockUserLock();
    UserLockType lockReq = new UserLockType();
    request.setUnlockUserLock(lockReq);
    lockReq.setRequestorQName("TESTNS:mdiponio");
    lockReq.setLockKey("abc123");
    lockReq.setUserLockID(lock.getId().intValue());

    UnlockUserLockResponse resp = this.permissions.unlockUserLock(request);
    assertNotNull(resp);
    OperationResponseType op = resp.getUnlockUserLockResponse();
    assertNotNull(op);
    assertTrue(op.getSuccessful());
    assertEquals(0, op.getFailureCode());
    assertNull(op.getFailureReason());

    ses.refresh(lock);
    assertFalse(lock.isIsLocked());

    ses.beginTransaction();
    ses.delete(lock);
    ses.delete(perm);
    ses.delete(userClass);
    ses.delete(user);
    ses.getTransaction().commit();
    ses.close();
}