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.kcb.service.impl; 017 018import java.util.Collection; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.kuali.rice.core.api.exception.RiceRuntimeException; 023import org.kuali.rice.kcb.bo.RecipientDelivererConfig; 024import org.kuali.rice.kcb.bo.RecipientPreference; 025import org.kuali.rice.kcb.deliverer.MessageDeliverer; 026import org.kuali.rice.kcb.exception.ErrorList; 027import org.kuali.rice.kcb.service.RecipientPreferenceService; 028 029/** 030 * RecipientPreferenceService implementation 031 * 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 */ 034public class RecipientPreferenceServiceImpl extends BusinessObjectServiceImpl implements RecipientPreferenceService { 035 /** 036 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#getRecipientPreference(java.lang.String, java.lang.String) 037 */ 038 public RecipientPreference getRecipientPreference(String recipientId, String key) { 039 Map<String, String> fields = new HashMap<String, String>(2); 040 fields.put(RecipientPreference.RECIPIENT_FIELD, recipientId); 041 fields.put(RecipientPreference.PROPERTY_FIELD, key); 042 043 Collection<RecipientPreference> prefs = dao.findMatching(RecipientPreference.class, fields); 044 assert(prefs.size() <= 1); 045 046 if (prefs.size() > 0) { 047 return prefs.iterator().next(); 048 } else { 049 return null; 050 } 051 } 052 053 /** 054 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#deleteRecipientPreference(org.kuali.rice.kcb.bo.RecipientPreference) 055 */ 056 public void deleteRecipientPreference(RecipientPreference pref) { 057 dao.delete(pref); 058 } 059 060 /** 061 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#getRecipientPreferences(java.lang.String) 062 */ 063 public HashMap<String, String> getRecipientPreferences(String recipientId) { 064 Map<String, String> fields = new HashMap<String, String>(1); 065 fields.put(RecipientPreference.RECIPIENT_FIELD, recipientId); 066 HashMap<String, String> prefs = new HashMap<String,String>(); 067 Collection<RecipientPreference> userPrefs = dao.findMatching(RecipientPreference.class, fields); 068 for (RecipientPreference p: userPrefs) { 069 prefs.put(p.getProperty(), p.getValue()); 070 } 071 072 return prefs; 073 } 074 075 /** 076 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#saveRecipientPreference(org.kuali.rice.kcb.bo.RecipientPreference) 077 */ 078 public void saveRecipientPreference(RecipientPreference pref) { 079 dao.save(pref); 080 } 081 082 /** 083 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#saveRecipientPreferences(java.lang.String, java.util.HashMap, org.kuali.rice.kcb.deliverer.MessageDeliverer) 084 */ 085 public void saveRecipientPreferences(String recipientId, HashMap<String, String> prefs, MessageDeliverer deliverer) throws ErrorList { 086 deliverer.validatePreferenceValues(prefs); 087 088 for (Map.Entry<String, String> entry: prefs.entrySet()) { 089 String prop = entry.getKey(); 090 String value = entry.getValue(); 091 092 // We need to check if this property is already set 093 // for the user by checking doing a unique key query...if 094 // it already exists, update, otherwise add it 095 RecipientPreference currentPreference = getRecipientPreference(recipientId, prop); 096 if (currentPreference != null) { 097 currentPreference.setRecipientId(recipientId); 098 currentPreference.setProperty(prop); 099 currentPreference.setValue(value); 100 dao.save(currentPreference); 101 } else { 102 RecipientPreference recipientPreference = new RecipientPreference(); 103 recipientPreference.setRecipientId(recipientId); 104 recipientPreference.setProperty(prop); 105 recipientPreference.setValue(value); 106 dao.save(recipientPreference); 107 } 108 } 109 } 110 111 // deliverer config 112 113 /** 114 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#removeRecipientDelivererConfigs(java.lang.String) 115 */ 116 public void removeRecipientDelivererConfigs(String recipientId) { 117 Map<String, String> fields = new HashMap<String, String>(1); 118 fields.put(RecipientDelivererConfig.RECIPIENT_ID, recipientId); 119 dao.deleteMatching(RecipientDelivererConfig.class, fields); 120 } 121 122 /** 123 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#saveRecipientDelivererConfig(java.lang.String, java.lang.String, java.lang.String[]) 124 */ 125 public void saveRecipientDelivererConfig(String recipientId, String delivererName, String[] channels) { 126 if (channels == null || channels.length == 0) return; 127 128 // if selected[0] is 0 we want to remove this deliverer 129 // for all channels. We already did that above. 130 for (String channel: channels) { 131 RecipientDelivererConfig config = new RecipientDelivererConfig(); 132 133 config.setRecipientId(recipientId); 134 config.setDelivererName(delivererName); 135 config.setChannel(channel); 136 137 // first, verify that we aren't trying to insert a duplicate 138 Collection<RecipientDelivererConfig> deliverers = getDeliverersForRecipientAndChannel(recipientId, channel); 139 if (deliverers != null) { 140 for (RecipientDelivererConfig deliverer : deliverers) { 141 if (deliverer.getDelivererName().equals(delivererName)) { 142 throw new RiceRuntimeException("Attempting to save a duplicate Recipient Deliverer Config."); 143 } 144 } 145 } 146 dao.save(config); 147 } 148 } 149 150 /** 151 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#getDeliverersForRecipient(java.lang.String) 152 */ 153 public Collection<RecipientDelivererConfig> getDeliverersForRecipient(String recipientId) { 154 Map<String, String> fields = new HashMap<String, String>(1); 155 fields.put(RecipientDelivererConfig.RECIPIENT_ID, recipientId); 156 return dao.findMatching(RecipientDelivererConfig.class, fields); 157 } 158 159 /** 160 * @see org.kuali.rice.kcb.service.RecipientPreferenceService#getDeliverersForRecipientAndChannel(java.lang.String, java.lang.String) 161 */ 162 public Collection<RecipientDelivererConfig> getDeliverersForRecipientAndChannel(String recipientId, String channel) { 163 Map<String, String> fields = new HashMap<String, String>(1); 164 fields.put(RecipientDelivererConfig.RECIPIENT_ID, recipientId); 165 fields.put(RecipientDelivererConfig.CHANNEL, channel); 166 167 return dao.findMatching(RecipientDelivererConfig.class, fields); 168 } 169}