001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.edl.impl.dao.impl;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import javax.persistence.EntityManager;
022import javax.persistence.PersistenceContext;
023
024import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
025import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
026import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
027import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
028import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
029import org.kuali.rice.edl.impl.dao.EDocLiteDAO;
030
031/**
032 * JPA implementation of the EDOcLiteDAO
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public class EDocLiteDAOJpaImpl implements EDocLiteDAO {
037    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EDocLiteDAOJpaImpl.class);
038
039    private static final String ACTIVE_IND_CRITERIA = "activeInd";
040    private static final String NAME_CRITERIA = "name";
041
042    @PersistenceContext(unitName = "kew-unit")
043    private EntityManager entityManager;
044
045    /**
046     * Save a EDocLiteDefinition
047     *
048     * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#saveEDocLiteDefinition(org.kuali.rice.edl.impl.bo.EDocLiteDefinition)
049     */
050    public void saveEDocLiteDefinition(final EDocLiteDefinition edocLiteData) {
051        if (edocLiteData.getEDocLiteDefId() == null) {
052            entityManager.persist(edocLiteData);
053        } else {
054            OrmUtils.merge(entityManager, edocLiteData);
055        }
056    }
057
058    /**
059     * Save a EDocLiteAssocitaion
060     *
061     * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#saveEDocLiteAssociation(org.kuali.rice.edl.impl.bo.EDocLiteAssociation)
062     */
063    public void saveEDocLiteAssociation(final EDocLiteAssociation assoc) {
064        if (assoc.getEdocLiteAssocId() == null) {
065            entityManager.persist(assoc);
066        } else {
067            OrmUtils.merge(entityManager, assoc);
068        }
069    }
070
071    /**
072     * Get a EDocLiteDefinition
073     *
074     * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteDefinition(java.lang.String)
075     */
076    public EDocLiteDefinition getEDocLiteDefinition(final String defName) {
077        final Criteria crit = new Criteria(EDocLiteDefinition.class.getName());
078        crit.eq(NAME_CRITERIA, defName);
079        crit.eq(ACTIVE_IND_CRITERIA, Boolean.TRUE);
080        return (EDocLiteDefinition) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
081    }
082
083    /**
084     * Get a EDocLiteAssociation
085     *
086     * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteAssociation(java.lang.String)
087     */
088    public EDocLiteAssociation getEDocLiteAssociation(final String docTypeName) {
089        final Criteria crit = new Criteria(EDocLiteAssociation.class.getName());
090        crit.eq("edlName", docTypeName);
091        crit.eq(ACTIVE_IND_CRITERIA, Boolean.TRUE);
092        return (EDocLiteAssociation) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
093    }
094
095    /**
096     * Returns the names of all active Definitions
097     *
098     * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteDefinitions()
099     */
100    @SuppressWarnings("unchecked")
101    public List<String> getEDocLiteDefinitions() {
102        final Criteria crit = new Criteria(EDocLiteDefinition.class.getName());
103        crit.eq(ACTIVE_IND_CRITERIA, Boolean.TRUE);
104
105        final List<EDocLiteDefinition> defs = (List<EDocLiteDefinition>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
106        final ArrayList<String> names = new ArrayList<String>(defs.size());
107        for (EDocLiteDefinition def : defs) {
108            names.add(def.getName());
109        }
110        return names;
111    }
112
113    /**
114     * Returns all active Definitions
115     *
116     * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteAssociations()
117     */
118    @SuppressWarnings("unchecked")
119    public List<EDocLiteAssociation> getEDocLiteAssociations() {
120        final Criteria crit = new Criteria(EDocLiteAssociation.class.getName());
121        crit.eq(ACTIVE_IND_CRITERIA, Boolean.TRUE);
122        return (List<EDocLiteAssociation>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
123    }
124
125    /**
126     * Finds matching Associations
127     *
128     * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#search(org.kuali.rice.edl.impl.bo.EDocLiteAssociation)
129     */
130    @SuppressWarnings("unchecked")
131    public List<EDocLiteAssociation> search(final EDocLiteAssociation edocLite) {
132        final Criteria crit = new Criteria(EDocLiteAssociation.class.getName());
133        if (edocLite.getActiveInd() != null) {
134            crit.eq(ACTIVE_IND_CRITERIA, edocLite.getActiveInd());
135        }
136        if (edocLite.getDefinition() != null) {
137            crit.like("UPPER(definition)", "%" + edocLite.getDefinition().toUpperCase() + "%");
138        }
139        if (edocLite.getEdlName() != null) {
140            crit.like("UPPER(edlName)", "%" + edocLite.getEdlName().toUpperCase() + "%");
141        }
142        if (edocLite.getStyle() != null) {
143            crit.like("UPPER(style)", "%" + edocLite.getStyle().toUpperCase() + "%");
144        }
145        return (List<EDocLiteAssociation>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
146    }
147
148    /**
149     * Returns a specific Association
150     *
151     * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteAssociation(java.lang.Long)
152     */
153    public EDocLiteAssociation getEDocLiteAssociation(final Long associationId) {
154        final Criteria crit = new Criteria(EDocLiteAssociation.class.getName());
155        crit.eq("edocLiteAssocId", associationId);
156        return (EDocLiteAssociation) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
157    }
158
159    public EntityManager getEntityManager() {
160        return this.entityManager;
161    }
162
163    public void setEntityManager(EntityManager entityManager) {
164        this.entityManager = entityManager;
165    }
166}