package org.fao.geonet.repository;
import com.google.common.base.Optional;
import org.fao.geonet.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.SingularAttribute;
import java.util.List;
/**
* Implementation for all {@link OperationAllowed} queries that cannot be automatically generated by Spring-data.
*
* @author Jesse
*/
public class OperationAllowedRepositoryImpl implements OperationAllowedRepositoryCustom {
@PersistenceContext
EntityManager _entityManager;
@Override
public List<OperationAllowed> findByMetadataId(String metadataId) {
CriteriaBuilder builder = _entityManager.getCriteriaBuilder();
CriteriaQuery<OperationAllowed> query = builder.createQuery(OperationAllowed.class);
int iMdId = Integer.parseInt(metadataId);
Root<OperationAllowed> root = query.from(OperationAllowed.class);
query.where(builder.equal(builder.literal(iMdId), root.get(OperationAllowed_.id).get(OperationAllowedId_.metadataId)));
return _entityManager.createQuery(query).getResultList();
}
@Override
public List<OperationAllowed> findAllWithOwner(int userId, Optional<Specification<OperationAllowed>> specification) {
CriteriaBuilder cb = _entityManager.getCriteriaBuilder();
CriteriaQuery<OperationAllowed> query = cb.createQuery(OperationAllowed.class);
Root<OperationAllowed> operationAllowedRoot = query.from(OperationAllowed.class);
Root<Metadata> metadataRoot = query.from(Metadata.class);
query.select(operationAllowedRoot);
Predicate userEqualsPredicate = cb.equal(metadataRoot.get(Metadata_.sourceInfo).get(MetadataSourceInfo_.owner), userId);
Predicate mdIdEquals = cb.equal(metadataRoot.get(Metadata_.id), operationAllowedRoot.get(OperationAllowed_.id).get
(OperationAllowedId_
.metadataId));
if (specification.isPresent()) {
Predicate otherPredicate = specification.get().toPredicate(operationAllowedRoot, query, cb);
query.where(mdIdEquals, userEqualsPredicate, otherPredicate);
} else {
query.where(mdIdEquals, userEqualsPredicate);
}
return _entityManager.createQuery(query).getResultList();
}
@Override
public List<Integer> findAllIds(Specification<OperationAllowed> spec, SingularAttribute<OperationAllowedId, Integer> idAttribute) {
final CriteriaBuilder cb = _entityManager.getCriteriaBuilder();
final CriteriaQuery<Integer> query = cb.createQuery(Integer.class);
final Root<OperationAllowed> root = query.from(OperationAllowed.class);
query.where(spec.toPredicate(root, query, cb));
query.select(root.get(OperationAllowed_.id).get(idAttribute));
query.distinct(true);
return _entityManager.createQuery(query).getResultList();
}
@Transactional
@Override
public int deleteAllByMetadataIdExceptGroupId(int metadataId, int groupId) {
final String opAllowedEntityName = OperationAllowed.class.getSimpleName();
final String metadataIdPath = SortUtils.createPath(OperationAllowed_.id, OperationAllowedId_.metadataId);
final String groupIdPath = SortUtils.createPath(OperationAllowed_.id, OperationAllowedId_.groupId);
final Query query = _entityManager.createQuery("DELETE FROM " + opAllowedEntityName + " where " + metadataIdPath + " = "
+ metadataId + "and " + groupIdPath + " != " + groupId);
final int affected = query.executeUpdate();
_entityManager.flush();
_entityManager.clear();
return affected;
}
@Transactional
@Override
public int deleteAllByIdAttribute(SingularAttribute<OperationAllowedId, Integer> idAttribute, int id) {
final String opAllowedEntityName = OperationAllowed.class.getSimpleName();
final String idPath = SortUtils.createPath(OperationAllowed_.id, idAttribute);
final Query query = _entityManager.createQuery("DELETE FROM " + opAllowedEntityName + " where " + idPath + " = " + id);
final int affected = query.executeUpdate();
_entityManager.flush();
_entityManager.clear();
return affected;
}
}