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