001    /**
002     * Copyright 2005-2012 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;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Properties;
023    import java.util.Map.Entry;
024    
025    import org.kuali.rice.kew.useroptions.dao.UserOptionsDAO;
026    import org.kuali.rice.kew.api.KewApiConstants;
027    import org.springframework.transaction.annotation.Transactional;
028    
029    @Transactional
030    public class UserOptionsServiceImpl implements UserOptionsService {
031    
032        private UserOptionsDAO userOptionsDAO;
033    
034        private static final Properties defaultProperties = new Properties();
035    
036        static {
037            defaultProperties.setProperty(KewApiConstants.EMAIL_RMNDR_KEY, KewApiConstants.EMAIL_RMNDR_WEEK_VAL);
038        }
039    
040        private Long getNewOptionIdForActionList() {
041                    return getUserOptionsDAO().getNewOptionIdForActionList();
042            }
043    
044        public List<UserOptions> findByUserQualified(String principalId, String likeString) {
045            if ((principalId == null)) {
046                return new ArrayList<UserOptions>(0);
047            }
048            return this.getUserOptionsDAO().findByUserQualified(principalId, likeString);
049        }
050    
051        public UserOptions findByOptionId(String optionId, String principalId) {
052            if (optionId == null || "".equals(optionId) || principalId == null || "".equals(principalId)) {
053                return null;
054            }
055            return this.getUserOptionsDAO().findByOptionId(optionId, principalId);
056        }
057    
058        public Collection<UserOptions> findByOptionValue(String optionId, String optionValue){
059            return this.getUserOptionsDAO().findByOptionValue(optionId, optionValue);
060        }
061    
062        public Collection<UserOptions> findByWorkflowUser(String principalId) {
063            return this.getUserOptionsDAO().findByWorkflowUser(principalId);
064        }
065    
066        public void save(UserOptions userOptions) {
067            this.getUserOptionsDAO().save(userOptions);
068        }
069        
070        /**
071         * This overridden method saves an option for each optionsMap entry, all for the given principalId
072         * 
073         * @see org.kuali.rice.kew.useroptions.UserOptionsService#save(java.lang.String, java.util.Map)
074         */
075        public void save(String principalId, Map<String,String> optionsMap) {
076            // create a collection of UserOptions and save it
077            if (optionsMap != null && optionsMap.size() > 0) {
078                    List<UserOptions> toSave = new ArrayList<UserOptions>();
079                    for (Entry<String, String> entry : optionsMap.entrySet()) {
080                            UserOptions option = findByOptionId(entry.getKey(), principalId);
081                            if (option == null) {
082                                    option = new UserOptions();
083                                    option.setWorkflowId(principalId);
084                            }
085                            option.setOptionId(entry.getKey());
086                            option.setOptionVal(entry.getValue());
087                            toSave.add(option);
088                    }
089                            getUserOptionsDAO().save(toSave);
090            }
091        }
092        
093        public void deleteUserOptions(UserOptions userOptions) {
094            this.getUserOptionsDAO().deleteUserOptions(userOptions);
095        }
096    
097        public void save(String principalId, String optionId, String optionValue) {
098            UserOptions option = findByOptionId(optionId, principalId);
099            if (option == null) {
100                option = new UserOptions();
101                option.setWorkflowId(principalId);
102            }
103            option.setOptionId(optionId);
104            option.setOptionVal(optionValue);
105            getUserOptionsDAO().save(option);
106        }
107    
108        public UserOptionsDAO getUserOptionsDAO() {
109            return userOptionsDAO;
110        }
111    
112        public void setUserOptionsDAO(UserOptionsDAO optionsDAO) {
113            userOptionsDAO = optionsDAO;
114        }
115        
116        @Override
117        public List<UserOptions> retrieveEmailPreferenceUserOptions(String emailSetting) {
118            return this.getUserOptionsDAO().findEmailUserOptionsByType(emailSetting);
119        }
120    }