1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
38
39 public class RecipientPreferenceServiceImpl implements RecipientPreferenceService {
40
41 private DataObjectService dataObjectService;
42
43
44
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
64
65 public void deleteRecipientPreference(RecipientPreference pref) {
66 dataObjectService.delete(pref);
67 }
68
69
70
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
87
88 public RecipientPreference saveRecipientPreference(RecipientPreference pref) {
89 return dataObjectService.save(pref);
90 }
91
92
93
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
103
104
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
122
123
124
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
134
135 public void saveRecipientDelivererConfig(String recipientId, String delivererName, String[] channels) {
136 if (channels == null || channels.length == 0) return;
137
138
139
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
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
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
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
185
186
187 public void setDataObjectService(DataObjectService dataObjectService) {
188 this.dataObjectService = dataObjectService;
189 }
190 }