
org.broadleafcommerce.common.sandbox.dao.SandBoxDaoImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of broadleaf-common Show documentation
Show all versions of broadleaf-common Show documentation
A collection of classes shared by broadleaf profile, cms, admin, and core.
/*
* #%L
* BroadleafCommerce Common Libraries
* %%
* Copyright (C) 2009 - 2013 Broadleaf Commerce
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.broadleafcommerce.common.sandbox.dao;
import org.broadleafcommerce.common.sandbox.domain.SandBox;
import org.broadleafcommerce.common.sandbox.domain.SandBoxImpl;
import org.broadleafcommerce.common.sandbox.domain.SandBoxManagement;
import org.broadleafcommerce.common.sandbox.domain.SandBoxManagementImpl;
import org.broadleafcommerce.common.sandbox.domain.SandBoxType;
import org.broadleafcommerce.common.util.TransactionUtils;
import org.broadleafcommerce.common.util.dao.TypedQueryBuilder;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
@Repository("blSandBoxDao")
public class SandBoxDaoImpl implements SandBoxDao {
@PersistenceContext(unitName = "blPU")
protected EntityManager sandBoxEntityManager;
@Resource(name = "blTransactionManager")
protected JpaTransactionManager transactionManager;
@Override
public SandBox retrieve(Long id) {
//Need to not create a query through SandBoxManagement here. Otherwise, a Hibernate exception can occur
//(i.e. org.hibernate.HibernateException: Found two representations of same collection: org.broadleafcommerce.core.catalog.domain.ProductImpl.additionalSkus
//when saving a change to product).
return sandBoxEntityManager.find(SandBoxImpl.class, id);
}
@Override
public List retrieveAllSandBoxes() {
CriteriaBuilder builder = sandBoxEntityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(SandBox.class);
Root sandbox = criteria.from(SandBoxManagementImpl.class);
criteria.select(sandbox.get("sandBox").as(SandBox.class));
TypedQuery query = sandBoxEntityManager.createQuery(criteria);
return query.getResultList();
}
@Override
public List retrieveSandBoxesByType(SandBoxType sandboxType) {
CriteriaBuilder builder = sandBoxEntityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(SandBox.class);
Root sandbox = criteria.from(SandBoxManagementImpl.class);
criteria.select(sandbox.get("sandBox").as(SandBox.class));
criteria.where(builder.equal(sandbox.get("sandBox").get("sandboxType"), sandboxType.getType()));
TypedQuery query = sandBoxEntityManager.createQuery(criteria);
return query.getResultList();
}
@Override
public List retrieveAllUserSandBoxes(Long authorId) {
TypedQuery query = new TypedQueryBuilder(SandBox.class, "sb")
.addRestriction("sb.author", "=", authorId)
.addRestriction("sb.sandboxType", "=", SandBoxType.USER.getType())
.toQuery(sandBoxEntityManager);
return query.getResultList();
}
@Override
public SandBox retrieveUserSandBoxForParent(Long authorId, Long parentSandBoxId) {
CriteriaBuilder builder = sandBoxEntityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(SandBox.class);
Root sandbox = criteria.from(SandBoxManagementImpl.class);
criteria.select(sandbox.get("sandBox").as(SandBox.class));
List restrictions = new ArrayList();
restrictions.add(builder.equal(sandbox.get("sandBox").get("sandboxType"), SandBoxType.USER.getType()));
restrictions.add(builder.equal(sandbox.get("sandBox").get("author"), authorId));
restrictions.add(builder.equal(sandbox.get("sandBox").get("parentSandBox").get("id"), parentSandBoxId));
criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
TypedQuery query = sandBoxEntityManager.createQuery(criteria);
try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
@Override
public SandBox retrieveNamedSandBox(SandBoxType sandboxType, String sandboxName) {
CriteriaBuilder builder = sandBoxEntityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(SandBox.class);
Root sandbox = criteria.from(SandBoxManagementImpl.class);
criteria.select(sandbox.get("sandBox").as(SandBox.class));
List restrictions = new ArrayList();
restrictions.add(builder.equal(sandbox.get("sandBox").get("sandboxType"), SandBoxType.USER.getType()));
restrictions.add(builder.equal(sandbox.get("sandBox").get("name"), sandboxName));
criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
TypedQuery query = sandBoxEntityManager.createQuery(criteria);
try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
@Override
public Map retrieveAuthorNamesForSandBoxes(Set sandBoxIds) {
Query query = sandBoxEntityManager.createQuery(
"SELECT sb.sandBox.id, au.name " +
"FROM org.broadleafcommerce.common.sandbox.domain.SandBoxManagementImpl sb, " +
"org.broadleafcommerce.openadmin.server.security.domain.AdminUserImpl au " +
"WHERE sb.sandBox.author = au.id " +
"AND sb.sandBox.id IN :sandBoxIds");
query.setParameter("sandBoxIds", sandBoxIds);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy