Example usage for org.hibernate.criterion Restrictions and

List of usage examples for org.hibernate.criterion Restrictions and

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions and.

Prototype

public static LogicalExpression and(Criterion lhs, Criterion rhs) 

Source Link

Document

Return the conjuction of two expressions

Usage

From source file:eu.jangos.realm.controller.auth.BannedAccountService.java

License:Apache License

/**
 * This method will return the active ban record for the given account.
 * //w  w  w .j  av  a  2s .  c o  m
 * @param account The account for which the active ban must be returned.
 * @return 
 */
public Bannedaccount getActiveBan(Account account) {
    if (account == null) {
        logger.debug("Account parameter is null. Returning null.");
        return null;
    }

    try (Session session = HibernateUtil.getAuthSession().openSession()) {
        return (Bannedaccount) (session.createCriteria(Bannedaccount.class)
                .add(Restrictions.and(Restrictions.eq("accountByFkBannedaccount.id", account.getId()),
                        Restrictions.eq("active", true)))
                .uniqueResult());
    } catch (HibernateException he) {
        logger.error("There was an error connecting to the database.");
        return null;
    }
}

From source file:eu.jangos.realm.controller.auth.BannedIPService.java

License:Apache License

/**
 * This method checks whether an IP is banned or not in the database.
 * @param ip The IP to be verified.// w  w  w  .j  a v  a 2s .c  o  m
 * @return true if the IP is banned, false otherwise. true if the IP is empty.
 */
public boolean isIPBanned(String ip) {
    if (ip == null || ip.isEmpty()) {
        logger.error("IP parameter is empty, exiting. ban = true.");
        return true;
    }

    if (!Utils.isValidIP4Address(ip)) {
        logger.error("A valid IPv4 address must be provided.");
        return true;
    }

    try (Session session = HibernateUtil.getAuthSession().openSession()) {
        return (session.createCriteria(Bannedip.class)
                .add(Restrictions.and(Restrictions.like("ip", ip), Restrictions.eq("active", true)))
                .uniqueResult() != null);
    } catch (HibernateException he) {
        return true;
    }
}

From source file:eu.jangos.realm.controller.auth.RealmAccountService.java

License:Apache License

public RealmAccount getAccountInfo(Account account, Realm realm) {
    if (account == null) {
        logger.debug("The account object is empty, returning null.");
        return null;
    }//from  www . j a  v  a 2  s .  c  o m

    try (Session session = HibernateUtil.getAuthSession().openSession()) {
        return (RealmAccount) session.createCriteria(RealmAccount.class)
                .add(Restrictions.and(Restrictions.eq("id.fkRealm", realm.getId()),
                        Restrictions.eq("id.fkAccount", account.getId())))
                .uniqueResult();
    } catch (HibernateException he) {
        logger.debug("An exception occured while querying the database.");
        return null;
    }
}

From source file:eu.jangos.realm.controller.characters.CharacterService.java

License:Apache License

/**
 * Delete the character corresponding to the ID given in parameter.
 *
 * @param id The id of the character to be deleted.
 * @param account the account to which this character must belong.
 * @param permanent Indicates whether this account must be deleted
 * permanently or not.//from   w  w w.ja  va2 s  .  c o m
 * @return The result of the deletion.
 */
public synchronized CharDeleteEnum deleteChar(long id, Account account, boolean permanent) {
    Realm realm = realmService.getRealmByName(wps.getParameter(WorldParameterConstants.KEY_WORLD_NAME));
    RealmAccount accountInfo = ras.getAccountInfo(account, realm);

    accountInfo.setNumChars((byte) (accountInfo.getNumChars() - 1));

    if (accountInfo.getNumChars() == 0) {
        realm.setCountPlayers(realm.getCountPlayers() - 1);
        realm.setPopulation(realm.getCountPlayers() * 1.0f / realm.getMaxPlayers() * 1.0f);
    }

    try (Session session = HibernateUtil.getCharSession().openSession()) {
        Characters character = (Characters) session.createCriteria(Characters.class).add(
                Restrictions.and(Restrictions.eq("guid", id), Restrictions.eq("fkAccount", account.getId())))
                .uniqueResult();
        session.beginTransaction();
        if (!permanent) {
            character.setDeleted(true);
            character.setDeleteDate(new Date());
            session.merge(character);
        } else {
            session.delete(character);
        }

        // We update realm infos.
        try (Session authSession = HibernateUtil.getAuthSession().openSession()) {
            authSession.beginTransaction();
            authSession.merge(realm);
            authSession.merge(accountInfo);
            authSession.flush();
            authSession.getTransaction().commit();
            ;
        } catch (HibernateException he) {
            session.getTransaction().rollback();
            return CharDeleteEnum.CHAR_DELETE_FAILED;
        }

        session.flush();
        session.getTransaction().commit();
    } catch (HibernateException he) {
        logger.error("Character with ID " + id + " does not exist for the account " + account.getName()
                + ", can't delete it.");
        return CharDeleteEnum.CHAR_DELETE_FAILED;
    }

    return CharDeleteEnum.CHAR_DELETE_SUCCESS;
}

From source file:eu.jangos.realm.controller.characters.CharacterService.java

License:Apache License

/**
 * Returns the list of characters for a given account.
 *
 * @param account The account for which the list of characters must be
 * retrieved./*  w  w  w  .ja  va 2s  .  c o m*/
 * @return The list of characters for this account or an empty list.
 */
public List<Characters> getCharactersForAccount(Account account) {
    if (account == null) {
        logger.debug("Account parameter is empty, exiting.");
        return null;
    }

    try (Session session = HibernateUtil.getCharSession().openSession()) {
        return session.createCriteria(Characters.class).add(
                Restrictions.and(Restrictions.isNull("deleted"), Restrictions.eq("fkAccount", account.getId())))
                .list();
    } catch (HibernateException he) {
        logger.error("No result associated to the account: " + account.getId());
        return null;
    }
}

From source file:eu.jangos.realm.controller.characters.CharacterService.java

License:Apache License

/**
 * Return the number of characters for this account.
 *
 * @param account The account for which the number of characters needs to be
 * retrieved.//from   www.  j a  va 2  s  . c o  m
 * @return The number of characters for this account.
 */
public long getNumberOfCharacters(Account account) {
    if (account == null) {
        logger.debug("Account parameter is empty, exiting.");
        return 0;
    }

    try (Session session = HibernateUtil.getCharSession().openSession()) {
        return (long) session.createCriteria(Characters.class)
                .add(Restrictions.and(Restrictions.isNull("deleted"),
                        Restrictions.eq("fkAccount", account.getId())))
                .setProjection(Projections.rowCount()).uniqueResult();
    } catch (HibernateException he) {
        logger.error("No result associated to the account: " + account.getId());
        return 0;
    }
}

From source file:eu.jangos.realm.controller.characters.CharacterService.java

License:Apache License

/**
 * Return whether the name is already in use in database.
 *
 * @param name The name to be checked./*  w w w  . j  a  v a 2s  . c o m*/
 * @return True if the name is already in use.
 */
private boolean nameInUse(String name) {
    if (name == null || name.isEmpty()) {
        logger.debug("Name parameter is empty, exiting. exist = false.");
        return false;
    }

    try (Session session = HibernateUtil.getCharSession().openSession()) {
        Characters character = (Characters) session.createCriteria(Characters.class)
                .add(Restrictions.and(Restrictions.isNull("deleted"), Restrictions.like("name", name)))
                .uniqueResult();
        logger.debug("The character " + name + " is found.");
        return (character != null);
    } catch (HibernateException he) {
        logger.error("The character " + name + " doesn't exist, exiting.");
        return true;
    }
}

From source file:eu.jangos.realm.controller.characters.ItemInstanceService.java

License:Apache License

/**
 * Return the equipment of the character in parameter.
 *
 * @param character/*  ww  w .ja  v a  2  s .c o m*/
 * @return The corresponding entity or null if not found.
 */
public List<ItemInstance> getEquipment(Characters character) {
    logger.debug("getEquipment " + character.getName());

    try (Session session = HibernateUtil.getCharSession().openSession()) {
        return (List<ItemInstance>) session.createCriteria(ItemInstance.class)
                .add(Restrictions.and(Restrictions.eq("characters.guid", character.getGuid()),
                        Restrictions.eq("itemStorageType.id", ItemStorageEnum.EQUIPPED.getValue())))
                .addOrder(Order.asc("slot")).list();
    } catch (HibernateException he) {
        logger.debug("There was an error querying the database.");
        return null;
    }
}

From source file:eu.jangos.realm.controller.characters.ItemInstanceService.java

License:Apache License

/**
 * Return only the necessary fields of the equipment of the character in parameter for the CharEnum packet.
 *
 * @param character//ww w . j  a va  2 s  .  com
 * @return A List of arrays of Object with the following structure: [slot,entry] where slot is the slot 
 * into which this item is located and entry is the ID of the item_template.
 */
public List getEquipmentCharEnum(Characters character) {
    logger.debug("getEquipment " + character.getName());

    try (Session session = HibernateUtil.getCharSession().openSession()) {
        ProjectionList proList = Projections.projectionList();
        proList.add(Projections.property("slot"), "slot");
        proList.add(Projections.property("fkObjectEntry"), "entry");

        return session.createCriteria(ItemInstance.class).setProjection(proList)
                .add(Restrictions.and(Restrictions.eq("characters.guid", character.getGuid()),
                        Restrictions.eq("itemStorageType.id", ItemStorageEnum.EQUIPPED.getValue())))
                .addOrder(Order.asc("slot")).list();
    } catch (HibernateException he) {
        logger.debug("There was an error querying the database.");
        return null;
    }
}

From source file:eu.jangos.realm.controller.world.StartingSpellService.java

License:Apache License

/**
 * Return the list of spell for a given race and a given class.
 * @param race The race of the character.
 * @param profession The profession of the character.    
 * @return The list of starting spells for this combination.
 *//*from w w  w  .jav a2s .com*/
public List<Startingspell> getStartingSpells(Race race, Professions profession) {
    List<Startingspell> listStartingSpells = null;
    try (Session session = HibernateUtil.getWorldSession().openSession()) {
        listStartingSpells = (List<Startingspell>) session.createCriteria(Startingspell.class).add(
                Restrictions.and(Restrictions.eq("race", race), Restrictions.eq("professions", profession)))
                .list();
    } catch (Exception e) {
        e.printStackTrace();
        logger.debug("Exception raised while querying the database.");
    }

    return listStartingSpells;
}