001/**
002 * Copyright 2005-2015 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.kew.useroptions.dao.impl;
017
018import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
019import org.kuali.rice.core.api.util.RiceConstants;
020import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
021import org.kuali.rice.kew.api.KewApiConstants;
022import org.kuali.rice.kew.useroptions.UserOptions;
023import org.kuali.rice.kew.useroptions.dao.UserOptionsDAO;
024
025import com.google.common.collect.Iterables;
026import com.google.common.collect.Lists;
027
028import javax.persistence.EntityManager;
029import javax.persistence.PersistenceContext;
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.List;
033
034
035public class UserOptionsDaoJpaImpl implements UserOptionsDAO {
036
037    @PersistenceContext
038    private EntityManager entityManager;
039
040        public Long getNewOptionIdForActionList() {
041        return getPlatform().getNextValSQL("KREW_ACTN_LIST_OPTN_S", entityManager);
042    }
043
044        protected DatabasePlatform getPlatform() {
045        return (DatabasePlatform) GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM);
046    }
047
048    public List findByUserQualified(String principalId, String likeString) {
049        return new ArrayList(entityManager.createNamedQuery("UserOptions.FindByUserQualified").setParameter("workflowId", principalId).setParameter("optionId", likeString).getResultList());
050    }
051
052    public void deleteByUserQualified(String principalId, String likeString) {
053        List<UserOptions> userOptions = (List<UserOptions>) entityManager.createNamedQuery("UserOptions.FindByUserQualified").setParameter("workflowId", principalId).setParameter("optionId", likeString).getResultList();
054        for (UserOptions uo : userOptions) {
055            entityManager.remove(uo);
056        }
057    }
058
059    public Collection findByWorkflowUser(String principalId) {
060        return entityManager.createNamedQuery("UserOptions.FindByWorkflowId").setParameter("workflowId", principalId).getResultList();
061    }
062
063    public void save(UserOptions userOptions) {
064        if (userOptions.getOptionId() == null) {
065            entityManager.persist(userOptions);
066        } else {
067            entityManager.merge(userOptions);
068        }
069    }
070    
071    public void save(Collection<UserOptions> userOptions) {
072        if (userOptions != null) for (UserOptions option : userOptions) {
073                        save(option);
074                }
075    }
076
077    public void deleteUserOptions(UserOptions userOptions) {
078        UserOptions reattatched = entityManager.merge(userOptions);
079        entityManager.remove(reattatched);
080    }
081
082    public UserOptions findByOptionId(String optionId, String principalId) {
083        return (UserOptions) entityManager.createNamedQuery("UserOptions.FindByOptionId").setParameter("optionId", optionId).setParameter("workflowId", principalId).getSingleResult();
084    }
085
086    public Collection findByOptionValue(String optionId, String optionValue) {
087        return entityManager.createNamedQuery("UserOptions.FindByOptionValue").setParameter("optionId", optionId).setParameter("optionValue", optionValue).getResultList();
088    }
089
090    @Override
091    public List<UserOptions> findEmailUserOptionsByType(String emailSetting) {
092        return Lists.newArrayList(Iterables.filter(entityManager.createNamedQuery("UserOptions.FindByOptionValue")
093                                                                .setParameter("optionId", KewApiConstants.EMAIL_RMNDR_KEY)
094                                                                .setParameter("optionIdLike", "%" + KewApiConstants.DOCUMENT_TYPE_NOTIFICATION_PREFERENCE_SUFFIX)
095                                                                .setParameter("optionValue", emailSetting)
096                                                                .getResultList(),
097                                                   UserOptions.class));
098    }
099
100    public EntityManager getEntityManager() {
101        return this.entityManager;
102    }
103
104    public void setEntityManager(EntityManager entityManager) {
105        this.entityManager = entityManager;
106    }
107}