Search in sources :

Example 1 with TranslatedEntity

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

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

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

the class AdminTranslationController method addTranslation.

/**
 * Saves a new translation to the database.
 *
 * Note that if the ceiling entity, entity id, property name, and locale code match a previously existing translation,
 * this method will update that translation.
 *
 * @param request
 * @param response
 * @param model
 * @param entityForm
 * @param result
 * @return the result of a call to {@link #viewTranslation}, which renders the list grid
 * @throws Exception
 */
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addTranslation(HttpServletRequest request, HttpServletResponse response, Model model, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result) throws Exception {
    final TranslationForm form = getTranslationForm(entityForm);
    adminRemoteSecurityService.securityCheck(form.getCeilingEntity(), EntityOperationType.UPDATE);
    SectionCrumb sectionCrumb = new SectionCrumb();
    sectionCrumb.setSectionIdentifier(TranslationImpl.class.getName());
    List<SectionCrumb> sectionCrumbs = Arrays.asList(sectionCrumb);
    entityForm.setCeilingEntityClassname(Translation.class.getName());
    entityForm.setEntityType(TranslationImpl.class.getName());
    Field entityType = new Field();
    entityType.setName("entityType");
    String ceilingEntity = form.getCeilingEntity();
    TranslatedEntity translatedEntity = TranslatedEntity.getInstance(ceilingEntity);
    if (translatedEntity == null && ceilingEntity.endsWith("Impl")) {
        int pos = ceilingEntity.lastIndexOf("Impl");
        ceilingEntity = ceilingEntity.substring(0, pos);
        translatedEntity = TranslatedEntity.getInstance(ceilingEntity);
    }
    entityType.setValue(translatedEntity.getFriendlyType());
    Field fieldName = new Field();
    fieldName.setName("fieldName");
    fieldName.setValue(form.getPropertyName());
    entityForm.getFields().put("entityType", entityType);
    entityForm.getFields().put("fieldName", fieldName);
    String[] sectionCriteria = customCriteriaService.mergeSectionCustomCriteria(ceilingEntity, getSectionCustomCriteria());
    Entity entity = service.addEntity(entityForm, sectionCriteria, sectionCrumbs).getEntity();
    entityFormValidator.validate(entityForm, entity, result);
    if (result.hasErrors()) {
        entityForm.setPreventSubmit();
        String jsErrorMap = resultToJS(result);
        entityForm.setJsErrorMap(jsErrorMap);
        model.addAttribute("entity", entity);
        model.addAttribute("entityForm", entityForm);
        model.addAttribute("viewType", "modal/translationAdd");
        model.addAttribute("currentUrl", request.getRequestURL().toString());
        model.addAttribute("modalHeaderType", ModalHeaderType.ADD_TRANSLATION.getType());
        return "modules/modalContainer";
    } else {
        return viewTranslation(request, response, model, form, result);
    }
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) Entity(org.broadleafcommerce.openadmin.dto.Entity) TranslatedEntity(org.broadleafcommerce.common.i18n.domain.TranslatedEntity) Translation(org.broadleafcommerce.common.i18n.domain.Translation) TranslationImpl(org.broadleafcommerce.common.i18n.domain.TranslationImpl) TranslationForm(org.broadleafcommerce.openadmin.web.form.TranslationForm) TranslatedEntity(org.broadleafcommerce.common.i18n.domain.TranslatedEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

TranslatedEntity (org.broadleafcommerce.common.i18n.domain.TranslatedEntity)3 Translation (org.broadleafcommerce.common.i18n.domain.Translation)3 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 TranslationImpl (org.broadleafcommerce.common.i18n.domain.TranslationImpl)1 Entity (org.broadleafcommerce.openadmin.dto.Entity)1 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)1 TranslationForm (org.broadleafcommerce.openadmin.web.form.TranslationForm)1 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1