DocumentGroupRepositoryImpl.java

package com.tradecloud.repository.dms.impl;

import com.tradecloud.domain.base.utils.ObjectUtil;
import com.tradecloud.domain.dms.*;
import com.tradecloud.repository.base.impl.RepositoryBaseImpl;
import com.tradecloud.repository.dms.DocumentGroupRepository;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Repository(value = "documentGroupRepository")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class DocumentGroupRepositoryImpl extends RepositoryBaseImpl<DocumentGroup, Object> implements DocumentGroupRepository, Serializable {

    private static final long serialVersionUID = 1L;

    Logger log = Logger.getLogger(DocumentGroupRepositoryImpl.class);

    public static final String BY_NAME_QUERY = "from DocumentGroup as d where d.name = :name";

    // public static final String BY_TYPE_ID_QUERY =
    // "from DocumentGroup as d where d.documentGroupType.id = :id";

    public DocumentGroup findByName(String name) throws ObjectNotFoundException {
        Query query = getCurrentSession().createQuery(BY_NAME_QUERY);
        query.setString("name", name);
        query.setMaxResults(1);
        List<DocumentGroup> list = query.list();
        if (list == null || list.isEmpty()) {
            throw new ObjectNotFoundException(SpringSecurityMessageSource.getAccessor().getMessage("JdbcDaoImpl.notFound", new Object[]{name},
                    "DocumentGroup with name {0} not found"), name);
        }
        return list.get(0);
    }

    public List<DocumentGroup> findByTypeId(DocumentGroupType documentGroupType) throws ObjectNotFoundException {
        List<DocumentGroup> documentGroups = (List<DocumentGroup>) findByNamedQueryAndNamedParam("findByType", new String[]{"documentGroupType"},
                new Object[]{documentGroupType,});
        return documentGroups;
    }

    public List<DocumentGroup> findByReference(String reference) {
        Criteria criteria = getCurrentSession().createCriteria(DocumentGroup.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        if (reference != null) {
            criteria.add(Restrictions.eq("key", reference));
        }

        List<DocumentGroup> list = criteria.list();
        Collections.sort(list);
        return list;
    }

    public DocumentGroup findByReferenceSingular(String reference) {
        Criteria criteria = getCurrentSession().createCriteria(DocumentGroup.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        if (reference != null) {
            criteria.add(Restrictions.ilike("key", reference));
        }
       criteria.setMaxResults(1);
        criteria.addOrder(Order.asc("key"));
        List<DocumentGroup> list = criteria.list();
//        Collections.sort(list);
        return list.get(0);
    }

    public List<DocumentGroup> findByProperty(String property) {
        Criteria criteria = getCurrentSession().createCriteria(DocumentGroup.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        if (property != null) {
            criteria.createCriteria("properties").add(Restrictions.ilike("value", "%" + property + "%"));
        }

        List<DocumentGroup> list = criteria.list();
        Collections.sort(list);
        return list;
    }

    public Collection<DocumentGroup> findByReference(String searchCriteria, DocumentGroupType documentGroupType) {

        Criteria criteria = getCurrentSession().createCriteria(DocumentGroup.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        criteria.add(Restrictions.eq("documentGroupType", documentGroupType));

        if (searchCriteria != null) {
            //TFG-2537
            /*criteria.add(Restrictions.ilike("key", "%" + searchCriteria + "%"));*/
            criteria.add(Restrictions.ilike("key", searchCriteria));
        }

        List<DocumentGroup> list = criteria.list();
        Collections.sort(list);
        return list;
    }

    public Collection<DocumentGroup> findByReference(String searchCriteria, DocumentGroupType documentGroupType, DocumentType documentType) {

        Criteria criteria = getCurrentSession().createCriteria(DocumentGroup.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        criteria.add(Restrictions.eq("documentGroupType", documentGroupType));

        if (searchCriteria != null) {
            criteria.add(Restrictions.ilike("key", "%" + searchCriteria + "%"));
        }

        if (documentType != null) {
            criteria.createCriteria("documents").add(Restrictions.eq("documentType", documentType));
        }

        return criteria.list();
    }

    public Collection<DocumentGroup> findByProperty(String searchCriteria, DocumentGroupType documentGroupType) {
        Criteria criteria = getCurrentSession().createCriteria(DocumentGroup.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        criteria.add(Restrictions.eq("documentGroupType", documentGroupType));
        if (searchCriteria != null) {
            criteria.createCriteria("properties").add(Restrictions.ilike("value", "%" + searchCriteria + "%"));
        }

        List<DocumentGroup> list = criteria.list();
        Collections.sort(list);
        return list;
    }

    public Collection<DocumentGroup> findByProperty(String searchCriteria, DocumentGroupType documentGroupType, DocumentType documentType) {
        Criteria criteria = getCurrentSession().createCriteria(DocumentGroup.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        criteria.add(Restrictions.eq("documentGroupType", documentGroupType));
        if (searchCriteria != null) {
            criteria.createCriteria("properties").add(Restrictions.ilike("value", "%" + searchCriteria + "%"));
        }

        if (documentType != null) {
            criteria.createCriteria("documents").add(Restrictions.eq("documentType", documentType));
        }

        List<DocumentGroup> list = criteria.list();
        Collections.sort(list);
        return list;
    }

    @Override
    public void save(DocumentGroup documentGroup) {
        Collection<DocumentGroup> collection = findByReference(documentGroup.getKey(), documentGroup.getDocumentGroupType());
        if (collection != null && collection.size() > 0) {
            throw new DuplicateDocumentGroupException("A DocumentGroup already exists with the same unique reference:"
                    + documentGroup.getKey()
                    + " and type: " + documentGroup.getDocumentGroupType().getName());
        }
        super.save(documentGroup);
    }

    @Override
    public List<Document> findDocuments(Set<String> documentGroupTypes, Set<String> documentTypes, Set<String> entityKeys) {
        ObjectUtil.validateNotEmpty(documentGroupTypes, "documentGroupTypes");
        ObjectUtil.validateNotEmpty(documentGroupTypes, "documentTypes");
        ObjectUtil.validateNotEmpty(documentGroupTypes, "entityKeys");
        final org.hibernate.query.Query query = getSessionCustom().createQuery("select d from " + DocumentGroup.class.getSimpleName() +
                " dg join dg.documents d  where dg.key in (:entityKeys) and dg.documentGroupType.name in (:documentGroupTypes)" +
                " and d.documentType.name in (:documentTypes) and d.documentName is not null");
        return query
                .setParameterList("documentGroupTypes", documentGroupTypes)
                .setParameterList("documentTypes", documentTypes)
                .setParameterList("entityKeys", entityKeys)
                .list();

    }
}