Java tutorial
/* * #%L * BroadleafCommerce CMS Module * %% * 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.cms.file.dao; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Order; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import org.broadleafcommerce.cms.file.domain.StaticAsset; import org.broadleafcommerce.cms.file.domain.StaticAssetImpl; import org.broadleafcommerce.common.persistence.EntityConfiguration; import org.broadleafcommerce.common.sandbox.domain.SandBox; import org.broadleafcommerce.common.sandbox.domain.SandBoxImpl; import org.hibernate.ejb.QueryHints; import org.springframework.stereotype.Repository; import org.springframework.util.CollectionUtils; /** * Created by bpolster. */ @Repository("blStaticAssetDao") public class StaticAssetDaoImpl implements StaticAssetDao { private static SandBox DUMMY_SANDBOX = new SandBoxImpl(); { DUMMY_SANDBOX.setId(-1l); } @PersistenceContext(unitName = "blPU") protected EntityManager em; @Resource(name = "blEntityConfiguration") protected EntityConfiguration entityConfiguration; @Resource(name = "blStaticAssetDaoQueryExtensionManager") protected StaticAssetDaoQueryExtensionManager queryExtensionManager; @Override public StaticAsset readStaticAssetById(Long id) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class); Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class); criteria.select(handler); List<Predicate> restrictions = new ArrayList<Predicate>(); restrictions.add(builder.equal(handler.get("id"), id)); if (queryExtensionManager != null) { queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null); queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria, handler, restrictions); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<StaticAsset> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); List<StaticAsset> response = query.getResultList(); if (response.size() > 0) { return response.get(0); } return null; } public List<StaticAsset> readAllStaticAssets() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class); Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class); criteria.select(handler); List<Predicate> restrictions = new ArrayList<Predicate>(); List<Order> sorts = new ArrayList<Order>(); try { if (queryExtensionManager != null) { queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null); queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria, handler, restrictions); queryExtensionManager.getProxy().refineOrder(StaticAssetImpl.class, null, builder, criteria, handler, sorts); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); return em.createQuery(criteria).getResultList(); } catch (NoResultException e) { return new ArrayList<StaticAsset>(); } } @Override public StaticAsset readStaticAssetByFullUrl(String fullUrl) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class); Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class); criteria.select(handler); List<Predicate> restrictions = new ArrayList<Predicate>(); List<Order> sorts = new ArrayList<Order>(); restrictions.add(builder.equal(handler.get("fullUrl"), fullUrl)); try { if (queryExtensionManager != null) { queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null); queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria, handler, restrictions); queryExtensionManager.getProxy().refineOrder(StaticAssetImpl.class, null, builder, criteria, handler, sorts); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); if (!org.apache.commons.collections.CollectionUtils.isEmpty(sorts)) { criteria.orderBy(sorts); } TypedQuery<StaticAsset> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); List<StaticAsset> response = query.getResultList(); if (response.size() > 0) { return response.get(0); } return null; } finally { if (queryExtensionManager != null) { queryExtensionManager.getProxy().breakdown(StaticAssetImpl.class, null); } } } @Override public StaticAsset addOrUpdateStaticAsset(StaticAsset asset, boolean clearLevel1Cache) { if (clearLevel1Cache) { em.detach(asset); } return em.merge(asset); } @Override public void delete(StaticAsset asset) { if (!em.contains(asset)) { asset = readStaticAssetById(asset.getId()); } em.remove(asset); } }