All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.foyt.fni.persistence.dao.users.UserContactFieldDAO Maven / Gradle / Ivy

There is a newer version: 3.3.13
Show newest version
package fi.foyt.fni.persistence.dao.users;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import fi.foyt.fni.persistence.dao.GenericDAO;
import fi.foyt.fni.persistence.model.users.User;
import fi.foyt.fni.persistence.model.users.UserContactField;
import fi.foyt.fni.persistence.model.users.UserContactFieldType;
import fi.foyt.fni.persistence.model.users.UserContactField_;

public class UserContactFieldDAO extends GenericDAO {

	private static final long serialVersionUID = 1L;

	public UserContactField create(User user, UserContactFieldType type, String value) {
    UserContactField userContactField = new UserContactField();
    userContactField.setUser(user);
    userContactField.setType(type);
    userContactField.setValue(value);
    
    return persist(userContactField);
  }

	public UserContactField findByUserAndType(User user, UserContactFieldType type) {
		EntityManager entityManager = getEntityManager();

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery criteria = criteriaBuilder.createQuery(UserContactField.class);
    Root root = criteria.from(UserContactField.class);
    criteria.select(root);
    criteria.where(
    	criteriaBuilder.and(
    		criteriaBuilder.equal(root.get(UserContactField_.user), user),
    		criteriaBuilder.equal(root.get(UserContactField_.type), type)
    	)
    );

    return getSingleResult(entityManager.createQuery(criteria));
	}

  public List listByUser(User user) {
    EntityManager entityManager = getEntityManager();

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery criteria = criteriaBuilder.createQuery(UserContactField.class);
    Root root = criteria.from(UserContactField.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(UserContactField_.user), user));

    return entityManager.createQuery(criteria).getResultList();
  }

	public UserContactField updateValue(UserContactField contactField, String value) {
		contactField.setValue(value);
		return persist(contactField);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy