use of org.broadleafcommerce.common.locale.domain.Locale 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;
}
use of org.broadleafcommerce.common.locale.domain.Locale in project BroadleafCommerce by BroadleafCommerce.
the class BroadleafLocaleResolverImpl method resolveLocale.
@Override
public Locale resolveLocale(WebRequest request) {
Locale locale = null;
// First check for request attribute
locale = (Locale) request.getAttribute(LOCALE_VAR, WebRequest.SCOPE_REQUEST);
// Second, check for a request parameter
if (locale == null && BLCRequestUtils.getURLorHeaderParameter(request, LOCALE_CODE_PARAM) != null) {
String localeCode = BLCRequestUtils.getURLorHeaderParameter(request, LOCALE_CODE_PARAM);
locale = localeService.findLocaleByCode(localeCode);
if (BLCRequestUtils.isOKtoUseSession(request)) {
request.removeAttribute(BroadleafCurrencyResolverImpl.CURRENCY_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
}
if (LOG.isTraceEnabled()) {
LOG.trace("Attempt to find locale by param " + localeCode + " resulted in " + locale);
}
}
// Third, check the session
if (locale == null && BLCRequestUtils.isOKtoUseSession(request)) {
locale = (Locale) request.getAttribute(LOCALE_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
if (LOG.isTraceEnabled()) {
LOG.trace("Attempt to find locale from session resulted in " + locale);
}
if (locale != null) {
request.setAttribute(LOCALE_PULLED_FROM_SESSION, Boolean.TRUE, WebRequest.SCOPE_REQUEST);
}
}
// Finally, use the default
if (locale == null) {
locale = localeService.findDefaultLocale();
if (BLCRequestUtils.isOKtoUseSession(request)) {
request.removeAttribute(BroadleafCurrencyResolverImpl.CURRENCY_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
}
if (LOG.isTraceEnabled()) {
LOG.trace("Locale set to default locale " + locale);
}
}
// Set the default locale to override Spring's cookie resolver
request.setAttribute(LOCALE_VAR, locale, WebRequest.SCOPE_REQUEST);
java.util.Locale javaLocale = BroadleafRequestContext.convertLocaleToJavaLocale(locale);
request.setAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME, javaLocale, WebRequest.SCOPE_REQUEST);
if (BLCRequestUtils.isOKtoUseSession(request)) {
request.setAttribute(LOCALE_VAR, locale, WebRequest.SCOPE_GLOBAL_SESSION);
}
return locale;
}
use of org.broadleafcommerce.common.locale.domain.Locale in project BroadleafCommerce by BroadleafCommerce.
the class SolrIndexServiceImpl method buildIncrementalIndex.
@Override
public Collection<SolrInputDocument> buildIncrementalIndex(List<? extends Indexable> indexables, SolrClient solrServer) throws ServiceException {
TransactionStatus status = TransactionUtils.createTransaction("executeIncrementalIndex", TransactionDefinition.PROPAGATION_REQUIRED, transactionManager, true);
if (SolrIndexCachedOperation.getCache() == null) {
LOG.warn("Consider using SolrIndexService.performCachedOperation() in combination with " + "SolrIndexService.buildIncrementalIndex() for better caching performance during solr indexing");
}
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Building incremental product index - pageSize: [%s]...", indexables.size()));
}
StopWatch s = new StopWatch();
try {
sandBoxHelper.ignoreCloneCache(true);
extensionManager.getProxy().startBatchEvent(indexables);
Collection<SolrInputDocument> documents = new ArrayList<>();
List<Locale> locales = getAllLocales();
List<Long> productIds = BLCCollectionUtils.collectList(indexables, new TypedTransformer<Long>() {
@Override
public Long transform(Object input) {
return shs.getCurrentProductId((Indexable) input);
}
});
solrIndexDao.populateProductCatalogStructure(productIds, SolrIndexCachedOperation.getCache());
List<IndexField> fields = null;
FieldEntity currentFieldType = null;
for (Indexable indexable : indexables) {
if (fields == null || ObjectUtils.notEqual(currentFieldType, indexable.getFieldEntityType())) {
fields = indexFieldDao.readFieldsByEntityType(indexable.getFieldEntityType());
}
SolrInputDocument doc = buildDocument(indexable, fields, locales);
// to the index.
if (doc != null) {
documents.add(doc);
}
}
extensionManager.getProxy().modifyBuiltDocuments(documents, indexables, fields, locales);
logDocuments(documents);
if (!CollectionUtils.isEmpty(documents) && solrServer != null) {
solrServer.add(documents);
commit(solrServer);
}
TransactionUtils.finalizeTransaction(status, transactionManager, false);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Built incremental product index - pageSize: [%s] in [%s]", indexables.size(), s.toLapString()));
}
return documents;
} catch (SolrServerException e) {
TransactionUtils.finalizeTransaction(status, transactionManager, true);
throw new ServiceException("Could not rebuild index", e);
} catch (IOException e) {
TransactionUtils.finalizeTransaction(status, transactionManager, true);
throw new ServiceException("Could not rebuild index", e);
} catch (RuntimeException e) {
TransactionUtils.finalizeTransaction(status, transactionManager, true);
throw e;
} finally {
extensionManager.getProxy().endBatchEvent(indexables);
sandBoxHelper.ignoreCloneCache(false);
}
}
use of org.broadleafcommerce.common.locale.domain.Locale in project BroadleafCommerce by BroadleafCommerce.
the class ContentProcessor method populateModelVariables.
@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
String contentType = tagAttributes.get("contentType");
String contentName = tagAttributes.get("contentName");
String maxResultsStr = tagAttributes.get("maxResults");
if (StringUtils.isEmpty(contentType) && StringUtils.isEmpty(contentName)) {
throw new IllegalArgumentException("The content processor must have a non-empty attribute value for 'contentType' or 'contentName'");
}
Integer maxResults = null;
if (maxResultsStr != null) {
maxResults = Ints.tryParse(maxResultsStr);
}
if (maxResults == null) {
maxResults = Integer.MAX_VALUE;
}
String contentListVar = getAttributeValue(tagAttributes, "contentListVar", "contentList");
String contentItemVar = getAttributeValue(tagAttributes, "contentItemVar", "contentItem");
String numResultsVar = getAttributeValue(tagAttributes, "numResultsVar", "numResults");
String fieldFilters = tagAttributes.get("fieldFilters");
final String sorts = tagAttributes.get("sorts");
BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
HttpServletRequest request = blcContext.getRequest();
Map<String, Object> mvelParameters = buildMvelParameters(blcContext.getRequest(), tagAttributes, context);
SandBox currentSandbox = blcContext.getSandBox();
List<StructuredContentDTO> contentItems;
StructuredContentType structuredContentType = null;
if (contentType != null) {
structuredContentType = structuredContentService.findStructuredContentTypeByName(contentType);
}
Locale locale = blcContext.getLocale();
Map<String, Object> newModelVars = new HashMap<>();
contentItems = getContentItems(contentName, maxResults, request, mvelParameters, currentSandbox, structuredContentType, locale, tagName, tagAttributes, newModelVars, context);
if (contentItems.size() > 0) {
// sort the resulting list by the configured property sorts on the tag
if (StringUtils.isNotEmpty(sorts)) {
final BroadleafTemplateContext finalContext = context;
// In order to use the context in a comparator it needs to be final
Collections.sort(contentItems, new Comparator<StructuredContentDTO>() {
@Override
public int compare(StructuredContentDTO o1, StructuredContentDTO o2) {
List<BroadleafAssignation> sortAssignments = finalContext.getAssignationSequence(sorts, false);
CompareToBuilder compareBuilder = new CompareToBuilder();
for (BroadleafAssignation sortAssignment : sortAssignments) {
String property = sortAssignment.getLeftStringRepresentation(finalContext);
Object val1 = o1.getPropertyValue(property);
Object val2 = o2.getPropertyValue(property);
if (sortAssignment.parseRight(finalContext).equals("ASCENDING")) {
compareBuilder.append(val1, val2);
} else {
compareBuilder.append(val2, val1);
}
}
return compareBuilder.toComparison();
}
});
}
List<Map<String, Object>> contentItemFields = new ArrayList<>();
for (StructuredContentDTO item : contentItems) {
if (StringUtils.isNotEmpty(fieldFilters)) {
List<BroadleafAssignation> assignments = context.getAssignationSequence(fieldFilters, false);
boolean valid = true;
for (BroadleafAssignation assignment : assignments) {
if (ObjectUtils.notEqual(assignment.parseRight(context), item.getValues().get(assignment.getLeftStringRepresentation(context)))) {
LOG.info("Excluding content " + item.getId() + " based on the property value of " + assignment.getLeftStringRepresentation(context));
valid = false;
break;
}
}
if (valid) {
contentItemFields.add(item.getValues());
}
} else {
contentItemFields.add(item.getValues());
}
}
Map<String, Object> contentItem = null;
if (contentItemFields.size() > 0) {
contentItem = contentItemFields.get(0);
}
newModelVars.put(contentItemVar, contentItem);
newModelVars.put(contentListVar, contentItemFields);
newModelVars.put(numResultsVar, contentItems.size());
} else {
if (LOG.isInfoEnabled()) {
LOG.info("**************************The contentItems is null*************************");
}
newModelVars.put(contentItemVar, null);
newModelVars.put(contentListVar, null);
newModelVars.put(numResultsVar, 0);
}
String deepLinksVar = tagAttributes.get("deepLinks");
if (StringUtils.isNotBlank(deepLinksVar) && contentItems.size() > 0) {
List<DeepLink> links = contentDeepLinkService.getLinks(contentItems.get(0));
extensionManager.getProxy().addExtensionFieldDeepLink(links, tagName, tagAttributes, context);
extensionManager.getProxy().postProcessDeepLinks(links);
newModelVars.put(deepLinksVar, links);
}
return newModelVars;
}
use of org.broadleafcommerce.common.locale.domain.Locale in project BroadleafCommerce by BroadleafCommerce.
the class BroadleafProcessURLFilter method determineLocale.
/**
* If another filter has already set the language as a request attribute, that will be honored.
* Otherwise, the request parameter is checked followed by the session attribute.
*
* @param request
* @param site
* @return
*/
private Locale determineLocale(HttpServletRequest request, Site site) {
Locale locale = null;
// First check for request attribute
locale = (Locale) request.getAttribute(LOCALE_VAR);
// Second, check for a request parameter
if (locale == null && request.getParameter(LOCALE_CODE_PARAM) != null) {
String localeCode = request.getParameter(LOCALE_CODE_PARAM);
locale = localeService.findLocaleByCode(localeCode);
if (LOG.isTraceEnabled()) {
LOG.trace("Attempt to find locale by param " + localeCode + " resulted in " + locale);
}
}
// Third, check the session
if (locale == null) {
HttpSession session = request.getSession(true);
if (session != null) {
locale = (Locale) session.getAttribute(LOCALE_VAR);
}
if (LOG.isTraceEnabled()) {
LOG.trace("Attempt to find locale from session resulted in " + locale);
}
}
// Finally, use the default
if (locale == null) {
locale = localeService.findDefaultLocale();
if (LOG.isTraceEnabled()) {
LOG.trace("Locale set to default locale " + locale);
}
}
request.setAttribute(LOCALE_VAR, locale);
request.getSession().setAttribute(LOCALE_VAR, locale);
Map<String, Object> ruleMap = (Map<String, Object>) request.getAttribute("blRuleMap");
if (ruleMap == null) {
ruleMap = new HashMap<String, Object>();
request.setAttribute("blRuleMap", ruleMap);
}
ruleMap.put("locale", locale);
return locale;
}
Aggregations