Example usage for org.hibernate Session getNamedQuery

List of usage examples for org.hibernate Session getNamedQuery

Introduction

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

Prototype

org.hibernate.Query getNamedQuery(String queryName);

Source Link

Document

Create a Query instance for the named query.

Usage

From source file:at.gv.egovernment.moa.id.moduls.SSOManager.java

License:EUPL

public String existsOldSSOSession(String ssoId) {

    Logger.trace("Check that the SSOID has already been used");
    Session session = MOASessionDBUtils.getCurrentSession();

    List<OldSSOSessionIDStore> result;

    synchronized (session) {

        session.beginTransaction();//from w w w .  j a  v a  2  s . com
        Query query = session.getNamedQuery("getSSOSessionWithOldSessionID");
        query.setParameter("sessionid", ssoId);
        result = query.list();

        // send transaction

    }

    Logger.trace("Found entries: " + result.size());

    // Assertion requires an unique artifact
    if (result.size() == 0) {
        session.getTransaction().commit();
        return null;
    }

    OldSSOSessionIDStore oldSSOSession = result.get(0);

    AuthenticatedSessionStore correspondingMoaSession = oldSSOSession.getMoasession();

    if (correspondingMoaSession == null) {
        Logger.info("Get request with old SSO SessionID but no corresponding SSO Session is found.");
        return null;
    }

    String moasessionid = correspondingMoaSession.getSessionid();

    session.getTransaction().commit();

    return moasessionid;

}

From source file:at.gv.egovernment.moa.id.monitoring.DatabaseTestModule.java

License:EUPL

private String testMOASessionDatabase() throws Exception {
    Logger.trace("Start Test: MOASessionDatabase");

    Date expioredate = new Date(new Date().getTime() - 120);

    try {/* ww  w  . j a v a 2s. c  o  m*/
        List<AssertionStore> results;
        Session session = MOASessionDBUtils.getCurrentSession();

        synchronized (session) {
            session.beginTransaction();
            Query query = session.getNamedQuery("getAssertionWithTimeOut");
            query.setTimestamp("timeout", expioredate);
            results = query.list();
            session.getTransaction().commit();
        }

        Logger.trace("Finish Test: MOASessionDatabase");
        return null;

    } catch (Throwable e) {
        Logger.warn("Failed Test: MOASessionDatabase", e);
        return "MOASessionDatabase: " + e.getMessage();
    }
}

From source file:at.gv.egovernment.moa.id.monitoring.DatabaseTestModule.java

License:EUPL

private String testMOAAdvancedLoggingDatabase() {

    Date expioredate = new Date(new Date().getTime() - 120);
    try {//from w  w  w.  j av  a 2s .  c o  m
        Session session = StatisticLogDBUtils.getCurrentSession();

        List<StatisticLog> results;

        synchronized (session) {
            session.beginTransaction();
            Query query = session.getNamedQuery("getAllEntriesNotBeforeTimeStamp");
            query.setTimestamp("timeout", expioredate);
            results = query.list();
            session.getTransaction().commit();
        }

        Logger.trace("Finish Test: AdvancedLoggingDataBase");
        return null;

    } catch (Throwable e) {
        Logger.warn("Failed Test: AdvancedLoggingDataBase", e);
        return "AdvancedLoggingDataBase: " + e.getMessage();
    }
}

From source file:at.gv.egovernment.moa.id.storage.AssertionStorage.java

License:EUPL

public void clean(long now, long authDataTimeOut) {
    Date expioredate = new Date(now - authDataTimeOut);

    List<AssertionStore> results;
    Session session = MOASessionDBUtils.getCurrentSession();

    synchronized (session) {
        session.beginTransaction();/* w  w w.  j  ava 2 s  . c  o  m*/
        Query query = session.getNamedQuery("getAssertionWithTimeOut");
        query.setTimestamp("timeout", expioredate);
        results = query.list();
        session.getTransaction().commit();
    }

    if (results.size() != 0) {
        for (AssertionStore result : results) {
            try {
                cleanDelete(result);
                Logger.info("Remove sessioninformation with ID=" + result.getArtifact() + " after timeout.");

            } catch (HibernateException e) {
                Logger.warn("Sessioninformation with ID=" + result.getArtifact()
                        + " not removed after timeout! (Error during Database communication)", e);
            }

        }
    }
}

From source file:at.gv.egovernment.moa.id.storage.AssertionStorage.java

License:EUPL

@SuppressWarnings("rawtypes")
private AssertionStore searchInDatabase(String artifact) throws MOADatabaseException {
    MiscUtil.assertNotNull(artifact, "artifact");
    Logger.trace("Getting sessioninformation with ID " + artifact + " from database.");
    Session session = MOASessionDBUtils.getCurrentSession();
    List result;//  w  ww  . j av a 2 s  .  com

    synchronized (session) {
        session.beginTransaction();
        Query query = session.getNamedQuery("getAssertionWithArtifact");
        query.setParameter("artifact", artifact);
        result = query.list();

        //send transaction
        session.getTransaction().commit();
    }

    Logger.trace("Found entries: " + result.size());

    //Assertion requires an unique artifact
    if (result.size() != 1) {
        Logger.trace("No entries found.");
        throw new MOADatabaseException("No sessioninformation found with this ID");
    }

    return (AssertionStore) result.get(0);
}

From source file:at.gv.egovernment.moa.id.storage.AuthenticationSessionStoreage.java

License:EUPL

public static void destroySession(String moaSessionID) throws MOADatabaseException {

    Session session = MOASessionDBUtils.getCurrentSession();

    List<AuthenticatedSessionStore> result;

    synchronized (session) {

        session.beginTransaction();//from w w  w.  j a  v a  2  s.c  om
        Query query = session.getNamedQuery("getSessionWithID");
        query.setParameter("sessionid", moaSessionID);
        result = query.list();

        Logger.trace("Found entries: " + result.size());

        //Assertion requires an unique artifact
        if (result.size() != 1) {
            Logger.trace("No entries found.");
            throw new MOADatabaseException("No session found with this sessionID");
        }

        AuthenticatedSessionStore dbsession = (AuthenticatedSessionStore) result.get(0);
        session.getTransaction().commit();
        cleanDelete(dbsession);
    }

}

From source file:at.gv.egovernment.moa.id.storage.AuthenticationSessionStoreage.java

License:EUPL

public static String getMOASessionSSOID(String SSOSessionID) {
    MiscUtil.assertNotNull(SSOSessionID, "SSOsessionID");
    Logger.trace("Get authenticated session with SSOID " + SSOSessionID + " from database.");
    Session session = MOASessionDBUtils.getCurrentSession();

    List<AuthenticatedSessionStore> result;

    synchronized (session) {
        session.beginTransaction();//from  www  .j  a v a  2 s . co m
        Query query = session.getNamedQuery("getSessionWithSSOID");
        query.setParameter("sessionid", SSOSessionID);
        result = query.list();

        //send transaction
        session.getTransaction().commit();
    }

    Logger.trace("Found entries: " + result.size());

    //Assertion requires an unique artifact
    if (result.size() != 1) {
        Logger.trace("No entries found.");
        return null;

    } else {
        return result.get(0).getSessionid();

    }
}

From source file:at.gv.egovernment.moa.id.storage.AuthenticationSessionStoreage.java

License:EUPL

public static AuthenticatedSessionStore isValidSessionWithSSOID(String SSOId, String moaSessionId) {
    MiscUtil.assertNotNull(SSOId, "SSOSessionID");
    Logger.trace("Get authenticated session with SSOID " + SSOId + " from database.");
    Session session = MOASessionDBUtils.getCurrentSession();

    List<AuthenticatedSessionStore> result;

    synchronized (session) {
        session.beginTransaction();//from ww w.  j  a v  a  2 s.c om
        Query query = session.getNamedQuery("getSessionWithSSOID");
        query.setParameter("sessionid", SSOId);
        result = query.list();

        //send transaction
        session.getTransaction().commit();
    }

    Logger.trace("Found entries: " + result.size());

    //Assertion requires an unique artifact
    if (result.size() != 1) {
        Logger.trace("No entries found.");
        return null;

    } else {
        return result.get(0);
    }
}

From source file:at.gv.egovernment.moa.id.storage.AuthenticationSessionStoreage.java

License:EUPL

public static void addSSOInformation(String moaSessionID, String SSOSessionID, SLOInformationInterface SLOInfo,
        String OAUrl) throws AuthenticationException {

    AuthenticatedSessionStore dbsession;
    Transaction tx = null;/*ww w  .ja  v a2  s.c  om*/

    try {

        Session session = MOASessionDBUtils.getCurrentSession();
        List<AuthenticatedSessionStore> result;

        Logger.trace("Add SSO information to session " + moaSessionID);

        synchronized (session) {

            tx = session.beginTransaction();
            Query query = session.getNamedQuery("getSessionWithID");
            query.setParameter("sessionid", moaSessionID);
            result = query.list();

            Logger.trace("Found entries: " + result.size());

            //Assertion requires an unique artifact
            if (result.size() != 1) {
                Logger.trace("No entries found.");
                tx.rollback();
                throw new MOADatabaseException("No session found with this sessionID");
            }

            dbsession = (AuthenticatedSessionStore) result.get(0);

            OASessionStore activeOA = null;
            //check if OA already has an active OA session
            if (dbsession.getActiveOAsessions() != null) {
                for (OASessionStore el : dbsession.getActiveOAsessions()) {
                    if (el.getOaurlprefix().equals(OAUrl))
                        activeOA = el;
                }
            }

            if (activeOA == null)
                activeOA = new OASessionStore();

            //set active OA applications
            activeOA.setOaurlprefix(OAUrl);
            activeOA.setMoasession(dbsession);
            activeOA.setCreated(new Date());

            //set additional information for SLO
            if (SLOInfo != null) {
                activeOA.setAssertionSessionID(SLOInfo.getSessionIndex());
                activeOA.setUserNameID(SLOInfo.getUserNameIdentifier());
                activeOA.setUserNameIDFormat(SLOInfo.getUserNameIDFormat());
                activeOA.setProtocolType(SLOInfo.getProtocolType());
                activeOA.setAttributeQueryUsed(false);

            }

            List<OASessionStore> activeOAs = dbsession.getActiveOAsessions();
            activeOAs.add(activeOA);
            dbsession.setActiveOAsessions(activeOAs);

            //Store used SSOId 
            if (dbsession.getSSOsessionid() != null) {
                OldSSOSessionIDStore oldSSOId = new OldSSOSessionIDStore();
                oldSSOId.setOldsessionid(dbsession.getSSOsessionid());
                oldSSOId.setMoasession(dbsession);

                List<OldSSOSessionIDStore> oldSSOIds = dbsession.getOldssosessionids();
                oldSSOIds.add(oldSSOId);
            }

            dbsession.setSSOSession(true);
            dbsession.setSSOsessionid(SSOSessionID);
            dbsession.setAuthenticated(false);
            dbsession.setPendingRequestID("empty");

            //Store MOASession
            session.saveOrUpdate(dbsession);

            //send transaction
            tx.commit();

            Logger.debug("Add SSO-Session login information for OA: " + OAUrl + " and AssertionID: "
                    + SLOInfo.getSessionIndex());
        }

    } catch (MOADatabaseException e) {
        throw new AuthenticationException("No MOASession found with Id=" + moaSessionID, null);

    } catch (HibernateException e) {
        Logger.warn("Error during database saveOrUpdate. Rollback.", e);
        tx.rollback();
        throw new AuthenticationException("SSO Session information can not be stored!  --> SSO is deactivated",
                null);
    }
}

From source file:at.gv.egovernment.moa.id.storage.AuthenticationSessionStoreage.java

License:EUPL

public static AuthenticationSession searchMOASessionWithNameIDandOAID(String oaID, String userNameID) {
    MiscUtil.assertNotNull(oaID, "OnlineApplicationIdentifier");
    MiscUtil.assertNotNull(userNameID, "userNameID");
    Logger.trace("Get moaSession for userNameID " + userNameID + " and OA " + oaID + " from database.");
    Session session = MOASessionDBUtils.getCurrentSession();

    List<AuthenticatedSessionStore> result;

    synchronized (session) {
        session.beginTransaction();//from w  w  w  .  ja va 2s. c  om
        Query query = session.getNamedQuery("getMOASessionWithNameIDandOAID");
        query.setParameter("oaID", oaID);
        query.setParameter("nameID", userNameID);
        result = query.list();

        //send transaction
        session.getTransaction().commit();
    }

    Logger.trace("Found entries: " + result.size());

    //Assertion requires an unique artifact
    if (result.size() != 1) {
        Logger.trace("No unique entry found.");
        return null;

    }
    try {
        return decryptSession(result.get(0));

    } catch (BuildException e) {
        Logger.warn(
                "MOASession deserialization-exception by using MOASessionID=" + result.get(0).getSessionid(),
                e);
        return null;
    }
}