Search in sources :

Example 21 with Translation

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

the class TranslationServiceImpl method getTranslatedValue.

@Override
public String getTranslatedValue(Object entity, String property, Locale locale) {
    TranslatedEntity entityType = getEntityType(entity);
    String entityId = dao.getEntityId(entityType, entity);
    String localeCode = locale.getLanguage();
    String localeCountryCode = localeCode;
    if (StringUtils.isNotBlank(locale.getCountry())) {
        localeCountryCode += "_" + locale.getCountry();
    }
    if (TranslationBatchReadCache.hasCache()) {
        Translation translation = TranslationBatchReadCache.getFromCache(entityType, entityId, property, localeCountryCode);
        if (translation != null) {
            return translation.getTranslatedValue();
        } else {
            // There is no translation for this entity if it is not in the cache
            return null;
        }
    }
    boolean isValidForCache = false;
    if (extensionManager != null) {
        ExtensionResultHolder<Boolean> response = new ExtensionResultHolder<Boolean>();
        response.setResult(false);
        extensionManager.getProxy().isValidState(response);
        isValidForCache = response.getResult();
    }
    if (!BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox() || !isValidForCache) {
        Translation translation = dao.readTranslation(entityType, entityId, property, localeCode, localeCountryCode, ResultType.CATALOG_ONLY);
        if (translation != null) {
            return translation.getTranslatedValue();
        } else {
            return null;
        }
    }
    return getOverrideTranslatedValue(property, entityType, entityId, localeCode, localeCountryCode);
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) TranslatedEntity(org.broadleafcommerce.common.i18n.domain.TranslatedEntity) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 22 with Translation

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

the class TranslationServiceImpl method getOverrideTranslatedValue.

protected String getOverrideTranslatedValue(String property, TranslatedEntity entityType, String entityId, String localeCode, String localeCountryCode) {
    boolean specificTranslationDeleted = false;
    boolean generalTranslationDeleted = false;
    StandardCacheItem specificTranslation = null;
    StandardCacheItem generalTranslation = null;
    String specificPropertyKey = property + "_" + localeCountryCode;
    String generalPropertyKey = property + "_" + localeCode;
    String response = null;
    String cacheKey = getCacheKey(ResultType.STANDARD, entityType);
    LocalePair override = null;
    for (TranslationOverrideStrategy strategy : strategies) {
        override = strategy.getLocaleBasedOverride(property, entityType, entityId, localeCode, localeCountryCode, cacheKey);
        if (override != null) {
            specificTranslation = override.getSpecificItem();
            generalTranslation = override.getGeneralItem();
            break;
        }
    }
    if (override == null) {
        throw new IllegalStateException("Expected at least one TranslationOverrideStrategy to return a valid value");
    }
    if (specificTranslation != null) {
        if (ItemStatus.DELETED.equals(specificTranslation.getItemStatus())) {
            specificTranslationDeleted = true;
        } else {
            if (specificTranslation.getCacheItem() instanceof Translation) {
                response = ((Translation) specificTranslation.getCacheItem()).getTranslatedValue();
            } else {
                response = (String) specificTranslation.getCacheItem();
            }
            return replaceEmptyWithNullResponse(response);
        }
    }
    if (generalTranslation != null) {
        if (ItemStatus.DELETED.equals(generalTranslation.getItemStatus())) {
            generalTranslationDeleted = true;
            // If the general translation is override deleted and we've only got a general local code - we're done
            if (specificTranslationDeleted || !localeCountryCode.contains("_")) {
                return null;
            }
        } else {
            if (generalTranslation.getCacheItem() instanceof Translation) {
                response = ((Translation) generalTranslation.getCacheItem()).getTranslatedValue();
            } else {
                response = (String) generalTranslation.getCacheItem();
            }
            // If the general translation is override and we've only got a general local code - we're done
            if (specificTranslationDeleted || !localeCountryCode.contains("_")) {
                return replaceEmptyWithNullResponse(response);
            }
            // We have a valid general override - don't check for general template value
            generalTranslationDeleted = true;
        }
    }
    // Check for a Template Match
    if (specificTranslationDeleted) {
        // only check general properties since we explicitly deleted specific properties at standard (site) level
        specificPropertyKey = generalPropertyKey;
    } else if (generalTranslationDeleted) {
        // only check specific properties since we explicitly deleted general properties at standard (site) level
        generalPropertyKey = specificPropertyKey;
    }
    String templateResponse = getTemplateTranslatedValue(cacheKey, property, entityType, entityId, localeCode, localeCountryCode, specificPropertyKey, generalPropertyKey);
    if (templateResponse != null) {
        response = templateResponse;
    }
    return response;
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) StandardCacheItem(org.broadleafcommerce.common.extension.StandardCacheItem)

Example 23 with Translation

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

the class TranslationServiceImpl method save.

@Override
@Transactional("blTransactionManager")
public Translation save(String entityType, String entityId, String fieldName, String localeCode, String translatedValue) {
    TranslatedEntity te = getEntityType(entityType);
    Translation translation = getTranslation(te, entityId, fieldName, localeCode);
    if (translation == null) {
        translation = dao.create();
        translation.setEntityType(te);
        translation.setEntityId(entityId);
        translation.setFieldName(fieldName);
        translation.setLocaleCode(localeCode);
    }
    translation.setTranslatedValue(translatedValue);
    return save(translation);
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) TranslatedEntity(org.broadleafcommerce.common.i18n.domain.TranslatedEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 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 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)

Example 25 with Translation

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

the class TranslationFormBuilderServiceImpl method buildListGrid.

@Override
public ListGrid buildListGrid(List<Translation> translations, boolean isRte) {
    // Set up the two header fields we're interested in for the translations list grid
    List<Field> headerFields = new ArrayList<Field>();
    headerFields.add(new Field().withName("localeCode").withFriendlyName("Translation_localeCode").withOrder(0));
    headerFields.add(new Field().withName("translatedValue").withFriendlyName("Translation_translatedValue").withOrder(10));
    // Create the list grid and set its basic properties
    ListGrid listGrid = new ListGrid();
    listGrid.getHeaderFields().addAll(headerFields);
    listGrid.setListGridType(ListGrid.Type.TRANSLATION);
    listGrid.setSelectType(ListGrid.SelectType.SINGLE_SELECT);
    listGrid.setCanFilterAndSort(false);
    // Allow add/update/remove actions, but provisioned especially for translation. Because of this, we will clone
    // the default actions so that we may change the class
    ListGridAction addAction = DefaultListGridActions.ADD.clone();
    ListGridAction removeAction = DefaultListGridActions.REMOVE.clone();
    ListGridAction updateAction = DefaultListGridActions.UPDATE.clone();
    addAction.setButtonClass("translation-grid-add");
    removeAction.setButtonClass("translation-grid-remove");
    updateAction.setButtonClass("translation-grid-update");
    listGrid.addToolbarAction(addAction);
    listGrid.addRowAction(updateAction);
    listGrid.addRowAction(removeAction);
    // TODO rework code elsewhere so these don't have to be added
    listGrid.setSectionKey(Translation.class.getCanonicalName());
    listGrid.setSubCollectionFieldName("translation");
    // Create records for each of the entries in the translations list
    for (Translation t : translations) {
        ListGridRecord record = new ListGridRecord();
        record.setListGrid(listGrid);
        record.setId(String.valueOf(t.getId()));
        Locale locale = localeService.findLocaleByCode(t.getLocaleCode());
        record.getFields().add(new Field().withName("localeCode").withFriendlyName("Translation_localeCode").withOrder(0).withValue(locale.getLocaleCode()).withDisplayValue(locale.getFriendlyName()));
        record.getFields().add(new Field().withName("translatedValue").withFriendlyName("Translation_translatedValue").withOrder(10).withValue(t.getTranslatedValue()).withDisplayValue(isRte ? getLocalizedEditToViewMessage() : t.getTranslatedValue()));
        listGrid.getRecords().add(record);
    }
    listGrid.setTotalRecords(listGrid.getRecords().size());
    return listGrid;
}
Also used : ListGridAction(org.broadleafcommerce.openadmin.web.form.component.ListGridAction) Locale(org.broadleafcommerce.common.locale.domain.Locale) ComboField(org.broadleafcommerce.openadmin.web.form.entity.ComboField) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) Translation(org.broadleafcommerce.common.i18n.domain.Translation) ListGridRecord(org.broadleafcommerce.openadmin.web.form.component.ListGridRecord) ArrayList(java.util.ArrayList) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid)

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