Search in sources :

Example 6 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 entityType, String entityId, String fieldName, String localeCode, String localeCountryCode, ResultType stage) {
    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"), fieldName));
    restrictions.add(builder.like(root.get("localeCode").as(String.class), localeCode + "%"));
    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);
        List<Translation> translations = query.getResultList();
        if (!translations.isEmpty()) {
            if (!localeCode.equals(localeCountryCode)) {
                return findBestTranslation(localeCountryCode, translations);
            } else {
                return findSpecificTranslation(localeCountryCode, translations);
            }
        } else {
            return null;
        }
    } 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)

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