use of org.broadleafcommerce.common.sandbox.domain.SandBox in project BroadleafCommerce by BroadleafCommerce.
the class BroadleafRequestProcessor method isSandboxIdValid.
protected boolean isSandboxIdValid(String sandboxId) {
boolean isValid = false;
if (StringUtils.isNotEmpty(sandboxId)) {
Long id = Long.valueOf(sandboxId);
SandBox sandbox = sandBoxService.retrieveSandBoxById(id);
if (sandbox != null) {
isValid = true;
}
}
return isValid;
}
use of org.broadleafcommerce.common.sandbox.domain.SandBox in project BroadleafCommerce by BroadleafCommerce.
the class BroadleafSandBoxResolverImpl method resolveSandBox.
@Override
public SandBox resolveSandBox(WebRequest request, Site site) {
Long previousSandBoxId = null;
if (BLCRequestUtils.isOKtoUseSession(request)) {
previousSandBoxId = (Long) request.getAttribute(SANDBOX_ID_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
}
SandBox currentSandbox = null;
if (!sandBoxPreviewEnabled) {
if (LOG.isTraceEnabled()) {
LOG.trace("Sandbox preview disabled. Setting sandbox to production");
}
request.setAttribute(SANDBOX_VAR, currentSandbox, WebRequest.SCOPE_REQUEST);
} else if (crossAppAuthService != null && !crossAppAuthService.isAuthedFromAdmin()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Sandbox preview attempted without authentication");
}
request.setAttribute(SANDBOX_VAR, currentSandbox, WebRequest.SCOPE_REQUEST);
} else if (crossAppAuthService != null && crossAppAuthService.hasCsrPermission()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Sandbox preview attempted in CSR mode");
}
request.setAttribute(SANDBOX_VAR, currentSandbox, WebRequest.SCOPE_REQUEST);
} else {
Long sandboxId = null;
// Clear the sandBox - second parameter is to support legacy implementations.
if ((request.getParameter("blClearSandBox") == null) && (request.getParameter("blSandboxDateTimeRibbonProduction") == null)) {
sandboxId = lookupSandboxId(request);
} else {
if (LOG.isTraceEnabled()) {
LOG.trace("Removing sandbox from session.");
}
if (BLCRequestUtils.isOKtoUseSession(request)) {
request.removeAttribute(SANDBOX_DATE_TIME_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
request.removeAttribute(SANDBOX_ID_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
}
SystemTime.resetLocalTimeSource();
}
if (sandboxId != null) {
if (previousSandBoxId != null && !previousSandBoxId.equals(sandboxId)) {
request.setAttribute(BroadleafRequestProcessor.REPROCESS_PARAM_NAME, true, WebRequest.SCOPE_REQUEST);
}
currentSandbox = sandBoxDao.retrieve(sandboxId);
request.setAttribute(SANDBOX_VAR, currentSandbox, WebRequest.SCOPE_REQUEST);
if (currentSandbox != null && !SandBoxType.PRODUCTION.equals(currentSandbox.getSandBoxType())) {
setContentTime(request);
}
}
// if (currentSandbox == null && site != null) {
// currentSandbox = site.getProductionSandbox();
// }
}
if (LOG.isTraceEnabled()) {
if (currentSandbox != null) {
LOG.trace("Serving request using sandbox: " + currentSandbox);
} else {
LOG.trace("Serving request without a sandbox.");
}
}
Date currentSystemDateTime = SystemTime.asDate(true);
Calendar sandboxDateTimeCalendar = Calendar.getInstance();
sandboxDateTimeCalendar.setTime(currentSystemDateTime);
request.setAttribute(SANDBOX_DISPLAY_DATE_TIME_DATE_PARAM, CONTENT_DATE_DISPLAY_FORMATTER.format(currentSystemDateTime), WebRequest.SCOPE_REQUEST);
request.setAttribute(SANDBOX_DISPLAY_DATE_TIME_HOURS_PARAM, CONTENT_DATE_DISPLAY_HOURS_FORMATTER.format(currentSystemDateTime), WebRequest.SCOPE_REQUEST);
request.setAttribute(SANDBOX_DISPLAY_DATE_TIME_MINUTES_PARAM, CONTENT_DATE_DISPLAY_MINUTES_FORMATTER.format(currentSystemDateTime), WebRequest.SCOPE_REQUEST);
request.setAttribute(SANDBOX_DISPLAY_DATE_TIME_AMPM_PARAM, sandboxDateTimeCalendar.get(Calendar.AM_PM), WebRequest.SCOPE_REQUEST);
return currentSandbox;
}
use of org.broadleafcommerce.common.sandbox.domain.SandBox 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.sandbox.domain.SandBox in project BroadleafCommerce by BroadleafCommerce.
the class StructuredContentServiceImpl method convertToDtos.
@Override
public List<StructuredContentDTO> convertToDtos(List<StructuredContent> scs, boolean isSecure) {
List<StructuredContentDTO> contentDTOList = new ArrayList<>();
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
SandBox sandbox = context.getSandBox();
boolean isProductionSandbox = context.isProductionSandBox();
StructuredContentDTO dto;
for (StructuredContent sc : scs) {
String cacheKey = buildNameKey(sandbox, sc, isSecure);
dto = null;
if (isProductionSandbox) {
dto = getSingleStructuredContentFromCache(cacheKey);
}
if (dto == null) {
dto = buildStructuredContentDTO(sc, isSecure);
if (isProductionSandbox) {
addSingleStructuredContentToCache(cacheKey, dto);
}
}
contentDTOList.add(dto);
}
return contentDTOList;
}
Aggregations