use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class I18nSolrIndexServiceExtensionHandler method addPropertyValues.
@Override
public ExtensionResultStatusType addPropertyValues(Indexable indexable, Field field, FieldType fieldType, Map<String, Object> values, String propertyName, List<Locale> locales) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Set<String> processedLocaleCodes = new HashSet<String>();
ExtensionResultStatusType result = ExtensionResultStatusType.NOT_HANDLED;
if (field.getTranslatable()) {
result = ExtensionResultStatusType.HANDLED;
TranslationConsiderationContext.setTranslationConsiderationContext(getTranslationEnabled());
TranslationConsiderationContext.setTranslationService(translationService);
BroadleafRequestContext tempContext = BroadleafRequestContext.getBroadleafRequestContext();
if (tempContext == null) {
tempContext = new BroadleafRequestContext();
BroadleafRequestContext.setBroadleafRequestContext(tempContext);
}
Locale originalLocale = tempContext.getLocale();
try {
for (Locale locale : locales) {
String localeCode = locale.getLocaleCode();
if (Boolean.FALSE.equals(locale.getUseCountryInSearchIndex())) {
int pos = localeCode.indexOf("_");
if (pos > 0) {
localeCode = localeCode.substring(0, pos);
if (processedLocaleCodes.contains(localeCode)) {
continue;
} else {
locale = localeService.findLocaleByCode(localeCode);
}
}
}
processedLocaleCodes.add(localeCode);
tempContext.setLocale(locale);
Object propertyValue = shs.getPropertyValue(indexable, propertyName);
values.put(localeCode, propertyValue);
}
} finally {
// Reset the original locale.
tempContext.setLocale(originalLocale);
}
}
return result;
}
use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class SolrIndexCachedOperation method setCache.
/**
* Set the cache on the current thread
*
* @param cache the cache object (usually an empty map)
*/
public static void setCache(CatalogStructure cache) {
BroadleafRequestContext ctx = BroadleafRequestContext.getBroadleafRequestContext();
Catalog currentCatalog = ctx == null ? null : ctx.getCurrentCatalog();
Map<Long, CatalogStructure> catalogCaches = CACHE.get();
if (catalogCaches == null) {
catalogCaches = new HashMap<Long, CatalogStructure>();
CACHE.set(catalogCaches);
}
if (currentCatalog != null) {
catalogCaches.put(currentCatalog.getId(), cache);
} else {
catalogCaches.put(DEFAULT_CATALOG_CACHE_KEY, cache);
}
}
use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class Money method defaultCurrency.
/**
* Attempts to load a default currency by using the default locale. {@link Currency#getInstance(Locale)} uses the country component of the locale to resolve the currency. In some instances, the locale may not have a country component, in which case the default currency can be controlled with a
* system property.
* @return The default currency to use when none is specified
*/
public static Currency defaultCurrency() {
if (CurrencyConsiderationContext.getCurrencyConsiderationContext() != null && CurrencyConsiderationContext.getCurrencyConsiderationContext().size() > 0 && CurrencyConsiderationContext.getCurrencyDeterminationService() != null) {
return Currency.getInstance(CurrencyConsiderationContext.getCurrencyDeterminationService().getCurrencyCode(CurrencyConsiderationContext.getCurrencyConsiderationContext()));
}
// Check the BLC Thread
BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
if (brc != null && brc.getBroadleafCurrency() != null) {
assert brc.getBroadleafCurrency().getCurrencyCode() != null;
return Currency.getInstance(brc.getBroadleafCurrency().getCurrencyCode());
}
if (System.getProperty("currency.default") != null) {
return Currency.getInstance(System.getProperty("currency.default"));
}
Locale locale = Locale.getDefault();
if (locale.getCountry() != null && locale.getCountry().length() == 2) {
return Currency.getInstance(locale);
}
return Currency.getInstance("USD");
}
use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class CustomUrlSiteMapGeneratorTest method testSiteMapsWithSiteContext.
@Test
public void testSiteMapsWithSiteContext() throws SiteMapException, IOException {
BroadleafRequestContext brc = new BroadleafRequestContext();
BroadleafRequestContext.setBroadleafRequestContext(brc);
Site site = new SiteImpl();
site.setId(256L);
brc.setSite(site);
CustomUrlSiteMapGeneratorConfiguration smgc = getConfiguration();
testGenerator(smgc, new CustomUrlSiteMapGenerator());
File file1 = fileService.getResource("/sitemap_index.xml");
File file2 = fileService.getResource("/sitemap1.xml");
File file3 = fileService.getResource("/sitemap2.xml");
assertThat(file1.getAbsolutePath(), containsString("site-256"));
assertThat(file2.getAbsolutePath(), containsString("site-256"));
assertThat(file3.getAbsolutePath(), containsString("site-256"));
compareFiles(file1, "src/test/resources/org/broadleafcommerce/sitemap/custom/sitemap_index.xml");
compareFiles(file2, "src/test/resources/org/broadleafcommerce/sitemap/custom/sitemap1.xml");
compareFiles(file3, "src/test/resources/org/broadleafcommerce/sitemap/custom/sitemap2.xml");
// Remove the request context from thread local so it doesn't get in the way of subsequent tests
BroadleafRequestContext.setBroadleafRequestContext(null);
}
use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class FileSystemFileServiceProviderTest method testBuildFileName.
/**
* For example, if the URL is /product/myproductimage.jpg, then the MD5 would be
* 35ec52a8dbd8cf3e2c650495001fe55f resulting in the following file on the filesystem
* {assetFileSystemPath}/64/a7/myproductimage.jpg.
*
* If there is a "siteId" in the BroadleafRequestContext then the site is also distributed
* using a similar algorithm but the system attempts to keep images for sites in their own
* directory resulting in an extra two folders required to reach any given product. So, for
* site with id 125, the system will MD5 "site125" in order to build the URL string. "site125" has an md5
* string of "7d905e85b8cb72a0477632be2c342bd6".
*
* So, in this case with the above product URL in site125, the full URL on the filesystem
* will be:
*
* {assetFileSystemPath}/7d/site125/64/a7/myproductimage.jpg.
* @throws Exception
*/
public void testBuildFileName() throws Exception {
FileSystemFileServiceProvider provider = new FileSystemFileServiceProvider();
String tmpdir = FileUtils.getTempDirectoryPath();
if (!tmpdir.endsWith(File.separator)) {
tmpdir = tmpdir + File.separator;
}
provider.fileSystemBaseDirectory = FilenameUtils.concat(tmpdir, "test");
provider.maxGeneratedDirectoryDepth = 2;
File file = provider.getResource("/product/myproductimage.jpg");
String resultPath = tmpdir + StringUtils.join(new String[] { "test", "35", "ec", "myproductimage.jpg" }, File.separator);
assertEquals(file.getAbsolutePath(), FilenameUtils.normalize(resultPath));
BroadleafRequestContext brc = new BroadleafRequestContext();
BroadleafRequestContext.setBroadleafRequestContext(brc);
Site site = new SiteImpl();
site.setId(125L);
brc.setSite(site);
// try with site specific directory
file = provider.getResource("/product/myproductimage.jpg");
resultPath = tmpdir + StringUtils.join(new String[] { "test", "c8", "site-125", "35", "ec", "myproductimage.jpg" }, File.separator);
assertEquals(file.getAbsolutePath(), resultPath);
// try with 3 max generated directories
provider.maxGeneratedDirectoryDepth = 3;
file = provider.getResource("/product/myproductimage.jpg");
resultPath = tmpdir + StringUtils.join(new String[] { "test", "c8", "site-125", "35", "ec", "52", "myproductimage.jpg" }, File.separator);
assertEquals(file.getAbsolutePath(), resultPath);
// Remove the request context from thread local so it doesn't get in the way of subsequent tests
BroadleafRequestContext.setBroadleafRequestContext(null);
}
Aggregations