Search in sources :

Example 1 with TranslationImpl

use of org.broadleafcommerce.common.i18n.domain.TranslationImpl in project BroadleafCommerce by BroadleafCommerce.

the class TranslationDaoImpl method readTranslations.

@Override
public List<Translation> readTranslations(TranslatedEntity entity, String entityId, String fieldName) {
    entityId = getUpdatedEntityId(entity, entityId);
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> translation = criteria.from(TranslationImpl.class);
    criteria.select(translation);
    criteria.where(builder.equal(translation.get("entityType"), entity.getFriendlyType()), builder.equal(translation.get("entityId"), entityId), builder.equal(translation.get("fieldName"), fieldName));
    TypedQuery<Translation> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Translation(org.broadleafcommerce.common.i18n.domain.Translation) TranslationImpl(org.broadleafcommerce.common.i18n.domain.TranslationImpl)

Example 2 with TranslationImpl

use of org.broadleafcommerce.common.i18n.domain.TranslationImpl in project BroadleafCommerce by BroadleafCommerce.

the class TranslationDaoImpl method readAllTranslationEntries.

public List<Translation> readAllTranslationEntries(TranslatedEntity entityType, ResultType stage, List<String> entityIds) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> root = criteria.from(TranslationImpl.class);
    criteria.select(root);
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType()));
    if (CollectionUtils.isNotEmpty(entityIds)) {
        restrictions.add(root.get("entityId").in(entityIds));
    }
    try {
        if (extensionManager != null) {
            extensionManager.getProxy().setup(TranslationImpl.class, stage);
            extensionManager.getProxy().refineParameterRetrieve(TranslationImpl.class, stage, builder, criteria, root, restrictions);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
        TypedQuery<Translation> query = em.createQuery(criteria);
        if (extensionManager != null) {
            extensionManager.getProxy().refineQuery(TranslationImpl.class, stage, query);
        }
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        return query.getResultList();
    } finally {
        if (extensionManager != null) {
            extensionManager.getProxy().breakdown(TranslationImpl.class, stage);
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Translation(org.broadleafcommerce.common.i18n.domain.Translation) TranslationImpl(org.broadleafcommerce.common.i18n.domain.TranslationImpl) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate)

Example 3 with TranslationImpl

use of org.broadleafcommerce.common.i18n.domain.TranslationImpl in project BroadleafCommerce by BroadleafCommerce.

the class TranslationDaoImpl method countTranslationEntries.

@Override
public Long countTranslationEntries(TranslatedEntity entityType, ResultType stage) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
    Root<TranslationImpl> root = criteria.from(TranslationImpl.class);
    criteria.select(builder.count(root));
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType()));
    try {
        if (extensionManager != null) {
            extensionManager.getProxy().setup(TranslationImpl.class, stage);
            extensionManager.getProxy().refineParameterRetrieve(TranslationImpl.class, stage, builder, criteria, root, restrictions);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
        TypedQuery<Long> query = em.createQuery(criteria);
        if (extensionManager != null) {
            extensionManager.getProxy().refineQuery(TranslationImpl.class, stage, query);
        }
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        return query.getSingleResult();
    } finally {
        if (extensionManager != null) {
            extensionManager.getProxy().breakdown(TranslationImpl.class, stage);
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TranslationImpl(org.broadleafcommerce.common.i18n.domain.TranslationImpl) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate)

Example 4 with TranslationImpl

use of org.broadleafcommerce.common.i18n.domain.TranslationImpl in project BroadleafCommerce by BroadleafCommerce.

the class TranslationDaoImpl method readTranslation.

@Override
public Translation readTranslation(TranslatedEntity entity, String entityId, String fieldName, String localeCode) {
    entityId = getUpdatedEntityId(entity, entityId);
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> translation = criteria.from(TranslationImpl.class);
    criteria.select(translation);
    criteria.where(builder.equal(translation.get("entityType"), entity.getFriendlyType()), builder.equal(translation.get("entityId"), entityId), builder.equal(translation.get("fieldName"), fieldName), builder.equal(translation.get("localeCode"), localeCode));
    TypedQuery<Translation> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<Translation> translations = query.getResultList();
    if (translations.size() > 1) {
        throw new IllegalStateException("Found multiple translations for: " + entity.getFriendlyType() + "|" + entityId + "|" + fieldName + "|" + localeCode);
    }
    if (!translations.isEmpty()) {
        return translations.get(0);
    }
    return null;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Translation(org.broadleafcommerce.common.i18n.domain.Translation) TranslationImpl(org.broadleafcommerce.common.i18n.domain.TranslationImpl)

Example 5 with TranslationImpl

use of org.broadleafcommerce.common.i18n.domain.TranslationImpl in project BroadleafCommerce by BroadleafCommerce.

the class SparseTranslationOverrideStrategy method getTemplateTranslations.

protected List<Translation> getTemplateTranslations(TranslatedEntity entityType, String entityId, String property, String localeCode) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> root = criteria.from(TranslationImpl.class);
    criteria.select(root);
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType()));
    restrictions.add(builder.equal(root.get("entityId"), entityId));
    restrictions.add(builder.equal(root.get("fieldName"), property));
    restrictions.add(builder.like(root.get("localeCode").as(String.class), localeCode + "%"));
    try {
        Object testObject = null;
        if (restrictAssociation) {
            try {
                Class<?> type = Class.forName(entityType.getType());
                SessionFactory sessionFactory = ((CriteriaBuilderImpl) em.getCriteriaBuilder()).getEntityManagerFactory().getSessionFactory();
                Class<?>[] entities = helper.getAllPolymorphicEntitiesFromCeiling(type, sessionFactory, true, true);
                // This should already be in level 1 cache and this should not cause a hit to the database.
                Map<String, Object> idMetadata = helper.getIdMetadata(entities[entities.length - 1], (HibernateEntityManager) em);
                Type idType = (Type) idMetadata.get("type");
                if (idType instanceof StringType) {
                    testObject = em.find(entities[entities.length - 1], entityId);
                } else if (idType instanceof LongType) {
                    testObject = em.find(entities[entities.length - 1], Long.parseLong(entityId));
                }
            } catch (ClassNotFoundException e) {
                throw ExceptionHelper.refineException(e);
            }
        }
        if (extensionManager != null) {
            extensionManager.setup(TranslationImpl.class);
            extensionManager.refineParameterRetrieve(TranslationImpl.class, testObject, builder, criteria, root, restrictions);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
        TypedQuery<Translation> query = em.createQuery(criteria);
        if (extensionManager != null) {
            extensionManager.refineQuery(TranslationImpl.class, testObject, query);
        }
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        List response = query.getResultList();
        if (extensionManager != null) {
            extensionManager.filterResults(TranslationImpl.class, testObject, response);
        }
        return response;
    } finally {
        extensionManager.breakdown(TranslationImpl.class);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SessionFactory(org.hibernate.SessionFactory) Translation(org.broadleafcommerce.common.i18n.domain.Translation) LongType(org.hibernate.type.LongType) TranslationImpl(org.broadleafcommerce.common.i18n.domain.TranslationImpl) StringType(org.hibernate.type.StringType) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) StringType(org.hibernate.type.StringType) LongType(org.hibernate.type.LongType) ResultType(org.broadleafcommerce.common.extension.ResultType) Type(org.hibernate.type.Type) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 TranslationImpl (org.broadleafcommerce.common.i18n.domain.TranslationImpl)6 Translation (org.broadleafcommerce.common.i18n.domain.Translation)5 ArrayList (java.util.ArrayList)4 Predicate (javax.persistence.criteria.Predicate)4 List (java.util.List)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 ResultType (org.broadleafcommerce.common.extension.ResultType)1 SessionFactory (org.hibernate.SessionFactory)1 LongType (org.hibernate.type.LongType)1 StringType (org.hibernate.type.StringType)1 Type (org.hibernate.type.Type)1