use of org.broadleafcommerce.common.sandbox.domain.SandBox in project BroadleafCommerce by BroadleafCommerce.
the class AdminTestHelper method startView.
/**
* Useful to start an EntityManager-In-View. This allows test operations that want to read directly from the database
* to work without lazy init exceptions, etc... This is not needed for MockMVC#perform operations, since those
* requests will include the OpenEntityManagerInView filter as part of their flow. At the completion of the test
* operation, the {@link #endView()} method should be called to end the scope of the view.
* </p>
* This view scope is also aware of nested views against the same persistence unit, so you don't need to worry
* about coding carefully to avoid nesting calls to startView.
*
* @param siteId
* @param sandBoxName
*/
public void startView(Long siteId, String sandBoxName) {
EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory();
boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf);
if (!isEntityManagerInView) {
EntityManager em = emf.createEntityManager();
em.clear();
EntityManagerHolder emHolder = new EntityManagerHolder(em);
TransactionSynchronizationManager.bindResource(emf, emHolder);
Stack<String> stack = new Stack<>();
TransactionSynchronizationManager.bindResource("emStack", stack);
if (siteId != null) {
Site site = siteService.retrievePersistentSiteById(siteId);
ThreadLocalManager.remove();
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
context.setSite(site);
context.setDeployBehavior(DeployBehavior.CLONE_PARENT);
context.setAdmin(true);
if (!StringUtils.isEmpty(sandBoxName)) {
SandBox sb = findSandBox(siteId, sandBoxName, false);
context.setSandBox(sb);
}
}
}
Stack<String> stack = (Stack<String>) TransactionSynchronizationManager.getResource("emStack");
RuntimeException trace = new RuntimeException();
StringWriter writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
trace.printStackTrace(pw);
stack.push(writer.toString());
}
use of org.broadleafcommerce.common.sandbox.domain.SandBox in project BroadleafCommerce by BroadleafCommerce.
the class BroadleafProcessURLFilter method determineSandbox.
private SandBox determineSandbox(HttpServletRequest request, Site site) {
SandBox currentSandbox = null;
if (!sandBoxPreviewEnabled) {
if (LOG.isTraceEnabled()) {
LOG.trace("Sandbox preview disabled. Setting sandbox to production");
}
request.setAttribute(SANDBOX_VAR, currentSandbox);
} else {
Long sandboxId = null;
if (request.getParameter("blSandboxDateTimeRibbonProduction") == null) {
sandboxId = lookupSandboxId(request);
} else {
request.getSession().removeAttribute(SANDBOX_DATE_TIME_VAR);
request.getSession().removeAttribute(SANDBOX_ID_VAR);
}
if (sandboxId != null) {
currentSandbox = sandBoxService.retrieveSandBoxById(sandboxId);
request.setAttribute(SANDBOX_VAR, currentSandbox);
if (currentSandbox != null && !SandBoxType.PRODUCTION.equals(currentSandbox.getSandBoxType())) {
setContentTime(request);
}
}
// if (currentSandbox == null && site != null) {
// currentSandbox = site.getProductionSandbox();
// }
}
if (LOG.isTraceEnabled()) {
LOG.trace("Serving request using sandbox: " + currentSandbox);
}
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));
request.setAttribute(SANDBOX_DISPLAY_DATE_TIME_HOURS_PARAM, CONTENT_DATE_DISPLAY_HOURS_FORMATTER.format(currentSystemDateTime));
request.setAttribute(SANDBOX_DISPLAY_DATE_TIME_MINUTES_PARAM, CONTENT_DATE_DISPLAY_MINUTES_FORMATTER.format(currentSystemDateTime));
request.setAttribute(SANDBOX_DISPLAY_DATE_TIME_AMPM_PARAM, sandboxDateTimeCalendar.get(Calendar.AM_PM));
return currentSandbox;
}
use of org.broadleafcommerce.common.sandbox.domain.SandBox in project BroadleafCommerce by BroadleafCommerce.
the class BroadleafProcessURLFilter method doFilterInternal.
/**
* (non-Javadoc)
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
if (!shouldProcessURL(request, request.getRequestURI())) {
if (LOG.isTraceEnabled()) {
LOG.trace("Process URL not processing URL " + request.getRequestURI());
}
filterChain.doFilter(request, response);
return;
}
final String requestURIWithoutContext;
if (request.getContextPath() != null) {
requestURIWithoutContext = request.getRequestURI().substring(request.getContextPath().length());
} else {
requestURIWithoutContext = request.getRequestURI();
}
if (LOG.isTraceEnabled()) {
LOG.trace("Process URL Filter Begin " + requestURIWithoutContext);
}
if (request.getAttribute(REQUEST_DTO) == null) {
request.setAttribute(REQUEST_DTO, new RequestDTOImpl(request));
}
Site site = determineSite(request);
SandBox currentSandbox = determineSandbox(request, site);
BroadleafRequestContext brc = new BroadleafRequestContext();
brc.setLocale(determineLocale(request, site));
brc.setSandBox(currentSandbox);
brc.setRequest(request);
brc.setResponse(response);
BroadleafRequestContext.setBroadleafRequestContext(brc);
try {
URLProcessor urlProcessor = null;
if (isProduction(currentSandbox)) {
try {
urlProcessor = lookupProcessorFromCache(requestURIWithoutContext);
} catch (ExecutionException e) {
LOG.error(e);
}
}
if (urlProcessor == null) {
urlProcessor = determineURLProcessor(requestURIWithoutContext);
}
if (urlProcessor instanceof NullURLProcessor) {
// Pass request down the filter chain
if (LOG.isTraceEnabled()) {
LOG.trace("URL not being processed by a Broadleaf URLProcessor " + requestURIWithoutContext);
}
StatusExposingServletResponse sesResponse = new StatusExposingServletResponse(response);
filterChain.doFilter(request, sesResponse);
if (sesResponse.getStatus() == sesResponse.SC_NOT_FOUND) {
if (LOG.isWarnEnabled()) {
LOG.warn("Page not found. Unable to render " + requestURIWithoutContext);
}
urlCache.invalidate(requestURIWithoutContext);
}
} else {
if (LOG.isTraceEnabled()) {
LOG.trace("URL about to be processed by a Broadleaf URLProcessor " + requestURIWithoutContext);
}
urlProcessor.processURL(requestURIWithoutContext);
}
} finally {
// If the system-time was overridden, set it back to normal
SystemTime.resetLocalTimeSource();
}
}
use of org.broadleafcommerce.common.sandbox.domain.SandBox in project BroadleafCommerce by BroadleafCommerce.
the class SandBoxDaoImpl method retrieveSandboxNamesForSandBoxes.
@Override
public Map<Long, String> retrieveSandboxNamesForSandBoxes(Set<Long> sandBoxIds) {
CriteriaBuilder builder = sandBoxEntityManager.getCriteriaBuilder();
CriteriaQuery<SandBox> criteria = builder.createQuery(SandBox.class);
Root<SandBoxManagementImpl> sandbox = criteria.from(SandBoxManagementImpl.class);
criteria.select(sandbox.get("sandBox").as(SandBox.class));
criteria.where(builder.and(builder.in(sandbox.get("sandBox").get("id")).value(sandBoxIds), builder.or(builder.isNull(sandbox.get("sandBox").get("archiveStatus").get("archived").as(String.class)), builder.notEqual(sandbox.get("sandBox").get("archiveStatus").get("archived").as(Character.class), 'Y'))));
TypedQuery<SandBox> query = sandBoxEntityManager.createQuery(criteria);
List<SandBox> results = query.getResultList();
Map<Long, String> map = new HashMap<Long, String>();
for (SandBox result : results) {
map.put(result.getId(), result.getName());
}
return map;
}
use of org.broadleafcommerce.common.sandbox.domain.SandBox in project BroadleafCommerce by BroadleafCommerce.
the class SandBoxDaoImpl method retrieveNamedSandBox.
@Override
public SandBox retrieveNamedSandBox(SandBoxType sandBoxType, String sandboxName, Long authorId) {
CriteriaBuilder builder = sandBoxEntityManager.getCriteriaBuilder();
CriteriaQuery<SandBox> criteria = builder.createQuery(SandBox.class);
Root<SandBoxManagementImpl> sandbox = criteria.from(SandBoxManagementImpl.class);
criteria.select(sandbox.get("sandBox").as(SandBox.class));
List<Predicate> restrictions = new ArrayList<Predicate>();
restrictions.add(builder.equal(sandbox.get("sandBox").get("sandboxType"), sandBoxType.getType()));
restrictions.add(builder.equal(sandbox.get("sandBox").get("name"), sandboxName));
if (authorId != null) {
restrictions.add(builder.equal(sandbox.get("sandBox").get("author"), authorId));
}
restrictions.add(builder.or(builder.isNull(sandbox.get("sandBox").get("archiveStatus").get("archived").as(String.class)), builder.notEqual(sandbox.get("sandBox").get("archiveStatus").get("archived").as(Character.class), 'Y')));
criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
TypedQuery<SandBox> query = sandBoxEntityManager.createQuery(criteria);
if (query.getResultList() != null && query.getResultList().size() == 1) {
return query.getSingleResult();
} else {
return null;
}
}
Aggregations