001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.kew.useroptions.dao.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.List;
021    
022    import org.apache.commons.collections.CollectionUtils;
023    import org.apache.ojb.broker.PersistenceBroker;
024    import org.apache.ojb.broker.query.Criteria;
025    import org.apache.ojb.broker.query.QueryByCriteria;
026    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
027    import org.kuali.rice.core.api.util.RiceConstants;
028    import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
029    import org.kuali.rice.kew.api.KewApiConstants;
030    import org.kuali.rice.kew.useroptions.UserOptions;
031    import org.kuali.rice.kew.useroptions.dao.UserOptionsDAO;
032    import org.springmodules.orm.ojb.PersistenceBrokerCallback;
033    import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
034    
035    import com.google.common.collect.Iterables;
036    import com.google.common.collect.Lists;
037    
038    
039    public class UserOptionsDAOOjbImpl extends PersistenceBrokerDaoSupport implements UserOptionsDAO {
040    
041            public Long getNewOptionIdForActionList() {
042            return (Long)this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
043                public Object doInPersistenceBroker(PersistenceBroker broker) {
044                    return getPlatform().getNextValSQL("KREW_ACTN_LIST_OPTN_S", broker);
045                }
046            });
047        }
048    
049            protected DatabasePlatform getPlatform() {
050            return (DatabasePlatform)GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM);
051        }
052    
053        public List<UserOptions> findByUserQualified(String principalId, String likeString) {
054            Criteria criteria = new Criteria();
055            criteria.addEqualTo("workflowId", principalId);
056            criteria.addLike("optionId", likeString);
057            return new ArrayList<UserOptions>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(UserOptions.class, criteria)));
058        }
059    
060        public void deleteByUserQualified(String principalId, String likeString) {
061            Criteria criteria = new Criteria();
062            criteria.addEqualTo("workflowId", principalId);
063            criteria.addLike("optionId", likeString);
064            this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(UserOptions.class, criteria));
065        }
066    
067        public Collection<UserOptions> findByWorkflowUser(String principalId) {
068            UserOptions userOptions = new UserOptions();
069            userOptions.setWorkflowId(principalId);
070            return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(userOptions));
071        }
072    
073        public void save(UserOptions userOptions) {
074            this.getPersistenceBrokerTemplate().store(userOptions);
075        }
076        
077        public void save(Collection<UserOptions> userOptions) {
078            if (userOptions != null) for (UserOptions option : userOptions) {
079                    this.getPersistenceBrokerTemplate().store(option);
080            }
081        }
082        
083        public void deleteUserOptions(UserOptions userOptions) {
084            this.getPersistenceBrokerTemplate().delete(userOptions);
085        }
086    
087        public UserOptions findByOptionId(String optionId, String principalId) {
088            UserOptions userOptions = new UserOptions();
089            userOptions.setOptionId(optionId);
090            userOptions.setWorkflowId(principalId);
091            return (UserOptions) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(userOptions));
092        }
093    
094        public Collection<UserOptions> findByOptionValue(String optionId, String optionValue) {
095            UserOptions userOptions = new UserOptions();
096            userOptions.setOptionId(optionId);
097            userOptions.setOptionVal(optionValue);
098            return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(userOptions));
099        }
100    
101        @Override
102        public List<UserOptions> findEmailUserOptionsByType(String emailSetting) {
103            Criteria optionIDCriteria = new Criteria();
104            optionIDCriteria.addEqualTo("optionId", KewApiConstants.EMAIL_RMNDR_KEY);
105    
106            Criteria documentTypeNotificationCriteria = new Criteria();
107            documentTypeNotificationCriteria.addLike("optionId", "%" + KewApiConstants.DOCUMENT_TYPE_NOTIFICATION_PREFERENCE_SUFFIX);
108            optionIDCriteria.addOrCriteria(documentTypeNotificationCriteria);
109    
110            Criteria criteria = new Criteria();
111            criteria.addEqualTo("optionVal", emailSetting);
112            criteria.addAndCriteria(optionIDCriteria);
113            return Lists.newArrayList(Iterables.filter(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(UserOptions.class, criteria)), UserOptions.class));
114        }
115    }