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:modelo.dao.UsuarioDAO.java

License:Open Source License

/**
 * Obtiene todos los usuarios que concuerden con los parametros
 *
 * @param apellidoMaterno El patron por el cual se buscaran los usuarios
 * @param tipo el tipo de usuario a buscar
 * @return Lista de UsuarioDTO que concuerden con el nombre ingresado, o
 * null en caso de que ningun usuario concuerde
 *///  w  ww.  j av  a  2  s  .  co  m
public List<UsuarioDTO> obtenerUsuariosPorApellidoM(String apellidoMaterno, Tipo tipo) {
    Session s = getSession();
    Transaction tx = null;
    List<UsuarioDTO> usuarios;

    if (s == null) {
        System.out.println("Session nula, regresando null....");
        return null;
    }

    try {
        tx = s.beginTransaction();
        //Obtiene todos los objetos que concuenrden con el apellido

        usuarios = s.createCriteria(UsuarioDTO.class)
                .add(Restrictions.and(Restrictions.like("apellidoMaterno", "%" + apellidoMaterno + "%"),
                        Restrictions.eq("tipo", tipo)))
                .list();

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        usuarios = null;
    } finally {
        s.close();
        System.out.println("Session cerrada");
    }
    return usuarios;
}

From source file:modelo.dao.UsuarioDAO.java

License:Open Source License

/**
 * Obtiene todos los usuarios que concuerden con los parametros
 *
 * @param nombre El patron por el cual se buscaran los usuarios
 * @param tipo el tipo de usuario a buscar
 * @return Lista de UsuarioDTO que concuerden con el nombre ingresado, o
 * null en caso de que ningun usuario concuerde
 *//*ww  w .  j  a va 2 s  .c  o  m*/
public List<UsuarioDTO> obtenerUsuariosPorNombre(String nombre, Tipo tipo) {
    Session s = getSession();
    Transaction tx = null;
    List<UsuarioDTO> usuarios;

    if (s == null) {
        System.out.println("Session nula, regresando null....");
        return null;
    }

    try {
        tx = s.beginTransaction();
        //Obtiene todos los objetos que concuenrden con el apellido

        usuarios = s
                .createCriteria(UsuarioDTO.class).add(Restrictions
                        .and(Restrictions.like("nombre", "%" + nombre + "%"), Restrictions.eq("tipo", tipo)))
                .list();

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        usuarios = null;
    } finally {
        s.close();
        System.out.println("Session cerrada");
    }
    return usuarios;
}

From source file:models.db.acentera.impl.DeviceImpl.java

License:Open Source License

public static ProjectDevices getProjectDevice(Long projectId, Long serverId) {

    User u = SecurityController.getUser();

    Session session = (Session) HibernateSessionFactory.getSession();
    Criteria criteria = session.createCriteria(ProjectDevices.class);
    UserProjects userProject = ProjectImpl.getUserProject(projectId, u);

    ProjectDevices pdevice = (ProjectDevices) criteria.add(Restrictions
            .and(Restrictions.eq("project", userProject.getProject()), Restrictions.eq("id", serverId)))
            .uniqueResult();/*from  ww w  . jav a2  s  .  c o  m*/

    return pdevice;
}

From source file:models.db.acentera.impl.ProjectImpl.java

License:Open Source License

public static ProjectDevices getProjectServer(Long projectId, Long internalId) throws Exception {
    Session s = (Session) HibernateSessionFactory.getSession();
    Criteria criteria = s.createCriteria(ProjectDevices.class);

    return (ProjectDevices) criteria
            .add(Restrictions.and(Restrictions.eq("project.id", projectId), Restrictions.eq("id", internalId)))
            .uniqueResult();//from  w  w w  .  jav  a2 s  .  co m
}

From source file:models.db.acentera.impl.ProjectImpl.java

License:Open Source License

public static User getUsersForProjectById(Project p, Long userId) {
    Session s = (Session) HibernateSessionFactory.getSession();
    Criteria criteria = s.createCriteria(UserProjects.class);

    UserProjects userProject = (UserProjects) criteria
            .add(Restrictions.and(Restrictions.eq("project", p), Restrictions.eq("user.id", userId)))
            .uniqueResult();//from www  .j av  a  2s.  c  o m

    if (userProject != null)
        return userProject.getUser();

    return null;
}

From source file:models.db.acentera.impl.ProjectImpl.java

License:Open Source License

public static Project getProject(Long projectId, User u) {
    Session s = (Session) HibernateSessionFactory.getSession();
    Criteria criteria = s.createCriteria(UserProjects.class);

    List<UserProjects> lst = (List<UserProjects>) criteria
            .add(Restrictions.and(Restrictions.eq("project.id", projectId), Restrictions.eq("user", u))).list();

    if (lst.size() == 1) {
        return lst.get(0).getProject();
    }/*  w w  w  .ja  v a2  s . co m*/

    return null;
}

From source file:models.db.acentera.impl.ProjectImpl.java

License:Open Source License

public static Project getProject(Long projectId) {
    Session s = (Session) HibernateSessionFactory.getSession();
    Criteria criteria = s.createCriteria(UserProjects.class);

    List<UserProjects> lst = (List<UserProjects>) criteria
            .add(Restrictions.and(Restrictions.eq("project.id", projectId),
                    Restrictions.eq("user", SecurityController.getUser())))
            .list();//from w  w  w .  j  a va 2  s.  c o m

    if (lst.size() == 1) {
        return lst.get(0).getProject();
    }

    return null;
}

From source file:models.db.acentera.impl.ProjectImpl.java

License:Open Source License

public static Project getProjectInfo(Long projectId, User u) {
    Session s = (Session) HibernateSessionFactory.getSession();
    Criteria criteria = s.createCriteria(UserProjects.class);
    UserProjects userProject = (UserProjects) criteria
            .add(Restrictions.and(Restrictions.eq("user", u), Restrictions.eq("project.id", projectId)))
            .uniqueResult();//from ww w.  j  av a2s. c o  m

    Project p = userProject.getProject();

    return p;
}

From source file:models.db.acentera.impl.ProjectImpl.java

License:Open Source License

public static ProjectProviders updateCloudProvider(Long projectId, Long providerId, User u,
        JSONObject updatedCloudProvider) {
    Session s = (Session) HibernateSessionFactory.getSession();

    Criteria criteria = s.createCriteria(UserProjects.class);

    UserProjects userProject = (UserProjects) criteria
            .add(Restrictions.and(Restrictions.eq("user", u), Restrictions.eq("project.id", projectId)))
            .uniqueResult();/* ww w . j av a  2 s  . c  o m*/

    List<ProjectProviders> ll = select(userProject.getProject().getProviders(),
            having(on(ProjectProviders.class).getId(), equalTo(providerId)));
    if (ll.size() == 1) {
        //Ok great we have it.. lets deactivate it..
        ProjectProviders provider = ll.get(0);

        provider.setName(updatedCloudProvider.getString("name"));
        provider.setApikey(updatedCloudProvider.getString("apikey"));

        //we do not update the API Cloud Provider Type they should delete / recreate one...

        if (updatedCloudProvider.has("secretkey")) {
            if (updatedCloudProvider.getString("secretkey").trim().compareTo("") != 0) {
                if (updatedCloudProvider.getString("secretkey").trim().compareTo("null") != 0) {
                    provider.setSecretkey(updatedCloudProvider.getString("secretkey").trim());
                }
            }
        }

        String newProviderTag = updatedCloudProvider.getString("tag").trim();

        if (provider.getTag() == null) {
            //must set the new one...
            //provider.getTag().disable();
            ProjectProvidersTags providerTags = new ProjectProvidersTags();
            providerTags.setProjectTags(ProjectTagsImpl.getOrCreateTags(userProject.getProject(),
                    newProviderTag, TagConstants.PROVIDER));
            provider.setTag(providerTags);
        } else {
            if (provider.getTag().compareTo(newProviderTag) != 0) {
                //must set the new one...
                ProjectProvidersTags providerTags = new ProjectProvidersTags();
                providerTags.setProjectTags(ProjectTagsImpl.getOrCreateTags(userProject.getProject(),
                        newProviderTag, TagConstants.PROVIDER));
                provider.setTag(providerTags);
            }
        }

        if (updatedCloudProvider.containsKey("tags")) {
            JSONArray listOfTags = updatedCloudProvider.getJSONArray("tags");

            final Set<ProjectProvidersQuotaTags> tags = provider.getTags();
            Set<ProjectProvidersQuotaTags> newTags = new HashSet<ProjectProvidersQuotaTags>();

            for (ProjectProvidersQuotaTags tag : tags) {
                tag.disable();
            }

            int len = listOfTags.size();
            for (int i = 0; i < len; i++) {
                JSONObject jsoData = listOfTags.getJSONObject(i);
                String data = jsoData.getString("name");

                List<ProjectProvidersQuotaTags> foundItem = select(tags,
                        having(on(ProjectProvidersQuotaTags.class).getName(), equalTo(data)));

                ProjectProvidersQuotaTags projectProviderQuotaTag = null;
                if (foundItem.size() == 1) {
                    //We found it..
                    projectProviderQuotaTag = foundItem.get(0);
                } else {
                    //This tag is not in this object yet.
                    projectProviderQuotaTag = new ProjectProvidersQuotaTags();
                }
                projectProviderQuotaTag.setProjectTags(
                        ProjectTagsImpl.getOrCreateTags(userProject.getProject(), data, TagConstants.QUOTA));
                projectProviderQuotaTag.enable();

                tags.add(projectProviderQuotaTag);
                s.saveOrUpdate(projectProviderQuotaTag);
                newTags.add(projectProviderQuotaTag);
            }

            //Set the new Tags
            provider.setTags(tags);

            s.saveOrUpdate(provider);

            //Since we disabled all data now.. lets return the object.
            provider.setTags(newTags);

            //Do not return the provider object as someone will potentially call "save on the object"
            return provider;//ProjectsHelpers.getProjectProvidersAsJson(provider);
        }
    }

    return null;
}

From source file:models.db.acentera.impl.ProjectImpl.java

License:Open Source License

public static boolean deleteCloudProvider(Long projectId, Long providerId, User u) {
    Session s = (Session) HibernateSessionFactory.getSession();

    Criteria criteria = s.createCriteria(UserProjects.class);

    UserProjects userProject = (UserProjects) criteria
            .add(Restrictions.and(Restrictions.eq("user", u), Restrictions.eq("project.id", projectId)))
            .uniqueResult();/*from w w  w .j  a va  2s  .co m*/

    //TODO: Check that the end-user have proper role to execute this operation...
    List<ProjectProviders> ll = select(userProject.getProject().getProviders(),
            having(on(ProjectProviders.class).getId(), equalTo(providerId)));
    if (ll.size() == 1) {
        //Ok great we have it.. lets deactivate it.. only if no devices is assinged...
        ProjectProviders prov = ll.get(0);

        List<ProjectDevices> activeDevices = (List<ProjectDevices>) criteria.add(
                Restrictions.and(Restrictions.eq("providers", prov), Restrictions.eq("project.id", projectId)))
                .list();

        //ONly if no devices...
        if (activeDevices == null || activeDevices.size() <= 0) {
            prov.disable();
            s.saveOrUpdate(prov);
            return true;
        }
    }

    return false;
}