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();
}
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);
}
}
}
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;
}
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);
}
}
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);
}
}
Aggregations