Search in sources :

Example 11 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation 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 12 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation 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 13 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation 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 14 with Translation

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

the class TranslationCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        // Get an instance of SystemProperty with the updated values from the form
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Translation adminInstance = (Translation) Class.forName(entity.getType()[0]).newInstance();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Translation.class.getName(), persistencePerspective);
        adminInstance = (Translation) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        Translation res = translationService.getTranslation(adminInstance.getEntityType(), adminInstance.getEntityId(), adminInstance.getFieldName(), adminInstance.getLocaleCode());
        if (res == null) {
            adminInstance = dynamicEntityDao.merge(adminInstance);
            Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
            return adminEntity;
        } else {
            Entity errorEntity = new Entity();
            errorEntity.addValidationError("localeCode", "translation.record.exists.for.locale");
            return errorEntity;
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to perform add for entity: " + Translation.class.getName(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Translation(org.broadleafcommerce.common.i18n.domain.Translation) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 15 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation 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

Translation (org.broadleafcommerce.common.i18n.domain.Translation)26 TranslationImpl (org.broadleafcommerce.common.i18n.domain.TranslationImpl)8 ArrayList (java.util.ArrayList)6 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 StandardCacheItem (org.broadleafcommerce.common.extension.StandardCacheItem)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 Element (net.sf.ehcache.Element)4 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)4 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)4 Predicate (javax.persistence.criteria.Predicate)3 TranslatedEntity (org.broadleafcommerce.common.i18n.domain.TranslatedEntity)3 Transactional (org.springframework.transaction.annotation.Transactional)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)2 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)2 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)2 Entity (org.broadleafcommerce.openadmin.dto.Entity)2 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)2 TranslationForm (org.broadleafcommerce.openadmin.web.form.TranslationForm)2