View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kcb.service.impl;
17  
18  import java.util.Collection;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.rice.core.api.criteria.QueryByCriteria;
24  import org.kuali.rice.core.api.exception.RiceRuntimeException;
25  import org.kuali.rice.kcb.bo.RecipientDelivererConfig;
26  import org.kuali.rice.kcb.bo.RecipientPreference;
27  import org.kuali.rice.kcb.deliverer.MessageDeliverer;
28  import org.kuali.rice.kcb.exception.ErrorList;
29  import org.kuali.rice.kcb.service.RecipientPreferenceService;
30  import org.kuali.rice.krad.data.DataObjectService;
31  
32  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
33  
34  /**
35   * RecipientPreferenceService implementation 
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class RecipientPreferenceServiceImpl implements RecipientPreferenceService {
40  
41      private DataObjectService dataObjectService;
42  
43      /**
44       * @see org.kuali.rice.kcb.service.RecipientPreferenceService#getRecipientPreference(java.lang.String, java.lang.String)
45       */
46      public RecipientPreference getRecipientPreference(String recipientId, String key) {
47          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
48          criteria.setPredicates(
49                  equal(RecipientPreference.RECIPIENT_FIELD, recipientId),
50                  equal(RecipientPreference.PROPERTY_FIELD, key)
51          );
52          List<RecipientPreference> prefs = dataObjectService.findMatching(RecipientPreference.class, criteria.build()).getResults();
53          assert(prefs.size() <= 1);
54  
55          if (prefs.isEmpty()) {
56              return null;
57          } else {
58              return prefs.get(0);
59          }
60      }
61  
62      /**
63       * @see org.kuali.rice.kcb.service.RecipientPreferenceService#deleteRecipientPreference(org.kuali.rice.kcb.bo.RecipientPreference)
64       */
65      public void deleteRecipientPreference(RecipientPreference pref) {
66          dataObjectService.delete(pref);
67      }
68  
69      /**
70       * @see org.kuali.rice.kcb.service.RecipientPreferenceService#getRecipientPreferences(java.lang.String)
71       */
72      public HashMap<String, String> getRecipientPreferences(String recipientId) {
73          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
74          criteria.setPredicates(equal(RecipientPreference.RECIPIENT_FIELD, recipientId));
75          List<RecipientPreference> userPrefs = dataObjectService.findMatching(RecipientPreference.class, criteria.build()).getResults();
76  
77          HashMap<String, String> prefs = new HashMap<String,String>();
78          for (RecipientPreference p: userPrefs) {
79              prefs.put(p.getProperty(), p.getValue());
80          }
81  
82          return prefs;
83      }
84  
85      /**
86       * @see org.kuali.rice.kcb.service.RecipientPreferenceService#saveRecipientPreference(org.kuali.rice.kcb.bo.RecipientPreference)
87       */
88      public RecipientPreference saveRecipientPreference(RecipientPreference pref) {
89          return dataObjectService.save(pref);
90      }
91  
92      /**
93       * @see org.kuali.rice.kcb.service.RecipientPreferenceService#saveRecipientPreferences(java.lang.String, java.util.HashMap, org.kuali.rice.kcb.deliverer.MessageDeliverer)
94       */
95      public void saveRecipientPreferences(String recipientId, HashMap<String, String> prefs, MessageDeliverer deliverer) throws ErrorList {
96          deliverer.validatePreferenceValues(prefs);         
97          
98          for (Map.Entry<String, String> entry: prefs.entrySet()) {
99             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 }