001/**
002 * Copyright 2005-2018 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.ken.api.notification;
017
018import java.io.Serializable;
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.List;
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAnyElement;
025import javax.xml.bind.annotation.XmlElement;
026import javax.xml.bind.annotation.XmlElementWrapper;
027import javax.xml.bind.annotation.XmlRootElement;
028import javax.xml.bind.annotation.XmlType;
029
030import org.apache.commons.collections.CollectionUtils;
031import org.kuali.rice.core.api.CoreConstants;
032import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
033import org.kuali.rice.core.api.mo.ModelBuilder;
034import org.w3c.dom.Element;
035
036@XmlRootElement(name = NotificationChannel.Constants.ROOT_ELEMENT_NAME)
037@XmlAccessorType(XmlAccessType.NONE)
038@XmlType(name = NotificationChannel.Constants.TYPE_NAME, propOrder = {
039        NotificationChannel.Elements.NAME,
040        NotificationChannel.Elements.DESCRIPTION,
041        NotificationChannel.Elements.SUBSCRIBABLE,
042        NotificationChannel.Elements.RECIPIENT_LISTS,
043        NotificationChannel.Elements.PRODUCERS,
044        NotificationChannel.Elements.REVIEWERS,
045        NotificationChannel.Elements.SUBSCRIPTIONS,
046        NotificationChannel.Elements.ID,
047        CoreConstants.CommonElements.VERSION_NUMBER,
048        CoreConstants.CommonElements.OBJECT_ID,
049        CoreConstants.CommonElements.FUTURE_ELEMENTS
050})
051public final class NotificationChannel
052        extends AbstractDataTransferObject
053        implements NotificationChannelContract
054{
055
056    @XmlElement(name = Elements.NAME, required = false)
057    private final String name;
058    @XmlElement(name = Elements.DESCRIPTION, required = false)
059    private final String description;
060    @XmlElement(name = Elements.SUBSCRIBABLE, required = false)
061    private final boolean subscribable;
062    @XmlElementWrapper(name = Elements.RECIPIENT_LISTS, required = false)
063    @XmlElement(name = Elements.RECIPIENT_LIST, required = false)
064    private final List<NotificationListRecipient> recipientLists;
065    @XmlElementWrapper(name = Elements.PRODUCERS, required = false)
066    @XmlElement(name = Elements.PRODUCER, required = false)
067    private final List<NotificationProducer> producers;
068    @XmlElementWrapper(name = Elements.REVIEWERS, required = false)
069    @XmlElement(name = Elements.REVIEWER, required = false)
070    private final List<NotificationChannelReviewer> reviewers;
071    @XmlElementWrapper(name = Elements.SUBSCRIPTIONS, required = false)
072    @XmlElement(name = Elements.SUBSCRIPTION, required = false)
073    private final List<UserChannelSubscription> subscriptions;
074    @XmlElement(name = Elements.ID, required = false)
075    private final Long id;
076    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
077    private final Long versionNumber;
078    @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
079    private final String objectId;
080    @SuppressWarnings("unused")
081    @XmlAnyElement
082    private final Collection<Element> _futureElements = null;
083
084    /**
085     * Private constructor used only by JAXB.
086     *
087     */
088    private NotificationChannel() {
089        this.name = null;
090        this.description = null;
091        this.subscribable = false;
092        this.recipientLists = null;
093        this.producers = null;
094        this.reviewers = null;
095        this.subscriptions = null;
096        this.id = null;
097        this.versionNumber = null;
098        this.objectId = null;
099    }
100
101    private NotificationChannel(Builder builder) {
102        this.name = builder.getName();
103        this.description = builder.getDescription();
104        this.subscribable = builder.isSubscribable();
105
106        this.recipientLists = new ArrayList<NotificationListRecipient>();
107        if (CollectionUtils.isNotEmpty(builder.getRecipientLists())) {
108            for (NotificationListRecipient.Builder listRecipient : builder.getRecipientLists()) {
109                this.recipientLists.add(listRecipient.build());
110            }
111        }
112
113        this.producers = new ArrayList<NotificationProducer>();
114        if (CollectionUtils.isNotEmpty(builder.getProducers())) {
115            for (NotificationProducer.Builder producer : builder.getProducers()) {
116                this.producers.add(producer.build());
117            }
118        }
119
120        this.reviewers = new ArrayList<NotificationChannelReviewer>();
121        if (CollectionUtils.isNotEmpty(builder.getReviewers())) {
122            for (NotificationChannelReviewer.Builder reviewer : builder.getReviewers()) {
123                this.reviewers.add(reviewer.build());
124            }
125        }
126
127        this.subscriptions = new ArrayList<UserChannelSubscription>();
128        if (CollectionUtils.isNotEmpty(builder.getSubscriptions())) {
129            for (UserChannelSubscription.Builder subscription : builder.getSubscriptions()) {
130                this.subscriptions.add(subscription.build());
131            }
132        }
133
134        this.id = builder.getId();
135        this.versionNumber = builder.getVersionNumber();
136        this.objectId = builder.getObjectId();
137    }
138
139    @Override
140    public String getName() {
141        return this.name;
142    }
143
144    @Override
145    public String getDescription() {
146        return this.description;
147    }
148
149    @Override
150    public boolean isSubscribable() {
151        return this.subscribable;
152    }
153
154    @Override
155    public List<NotificationListRecipient> getRecipientLists() {
156        return this.recipientLists;
157    }
158
159    @Override
160    public List<NotificationProducer> getProducers() {
161        return this.producers;
162    }
163
164    @Override
165    public List<NotificationChannelReviewer> getReviewers() {
166        return this.reviewers;
167    }
168
169    @Override
170    public List<UserChannelSubscription> getSubscriptions() {
171        return this.subscriptions;
172    }
173
174    @Override
175    public Long getId() {
176        return this.id;
177    }
178
179    @Override
180    public Long getVersionNumber() {
181        return this.versionNumber;
182    }
183
184    @Override
185    public String getObjectId() {
186        return this.objectId;
187    }
188
189
190    /**
191     * A builder which can be used to construct {@link NotificationChannel} instances.  Enforces the constraints of the {@link NotificationChannelContract}.
192     *
193     */
194    public final static class Builder
195            implements Serializable, ModelBuilder, NotificationChannelContract
196    {
197
198        private String name;
199        private String description;
200        private boolean subscribable;
201        private List<NotificationListRecipient.Builder> recipientLists;
202        private List<NotificationProducer.Builder> producers;
203        private List<NotificationChannelReviewer.Builder> reviewers;
204        private List<UserChannelSubscription.Builder> subscriptions;
205        private Long id;
206        private Long versionNumber;
207        private String objectId;
208
209        private Builder() {
210        }
211
212        public static Builder create() {
213            return new Builder();
214        }
215
216        public static Builder create(NotificationChannelContract contract) {
217            if (contract == null) {
218                throw new IllegalArgumentException("contract was null");
219            }
220            Builder builder = create();
221            builder.setName(contract.getName());
222            builder.setDescription(contract.getDescription());
223            builder.setSubscribable(contract.isSubscribable());
224            if (contract.getRecipientLists() != null) {
225                List<NotificationListRecipient.Builder> tempListRecipient = new ArrayList<NotificationListRecipient.Builder>();
226                for (NotificationListRecipientContract listRecipient : contract.getRecipientLists()) {
227                    tempListRecipient.add(NotificationListRecipient.Builder.create(listRecipient));
228                }
229                builder.setRecipientLists(tempListRecipient);
230            }
231            if (contract.getProducers() != null) {
232                List<NotificationProducer.Builder> tempProducers = new ArrayList<NotificationProducer.Builder>();
233                for (NotificationProducerContract producer : contract.getProducers()) {
234                    tempProducers.add(NotificationProducer.Builder.create(producer));
235                }
236                builder.setProducers(tempProducers);
237            }
238            if (contract.getReviewers() != null) {
239                List<NotificationChannelReviewer.Builder> tempReviewers = new ArrayList<NotificationChannelReviewer.Builder>();
240                for (NotificationChannelReviewerContract reviewer : contract.getReviewers()) {
241                    tempReviewers.add(NotificationChannelReviewer.Builder.create(reviewer));
242                }
243                builder.setReviewers(tempReviewers);
244            }
245            if (contract.getSubscriptions() != null) {
246                List<UserChannelSubscription.Builder> tempSubscriptions = new ArrayList<UserChannelSubscription.Builder>();
247                for (UserChannelSubscriptionContract subscription : contract.getSubscriptions()) {
248                    tempSubscriptions.add(UserChannelSubscription.Builder.create(subscription));
249                }
250                builder.setSubscriptions(tempSubscriptions);
251            }
252            builder.setId(contract.getId());
253            builder.setVersionNumber(contract.getVersionNumber());
254            builder.setObjectId(contract.getObjectId());
255            return builder;
256        }
257
258        public NotificationChannel build() {
259            return new NotificationChannel(this);
260        }
261
262        @Override
263        public String getName() {
264            return this.name;
265        }
266
267        @Override
268        public String getDescription() {
269            return this.description;
270        }
271
272        @Override
273        public boolean isSubscribable() {
274            return this.subscribable;
275        }
276
277        @Override
278        public List<NotificationListRecipient.Builder> getRecipientLists() {
279            return this.recipientLists;
280        }
281
282        @Override
283        public List<NotificationProducer.Builder> getProducers() {
284            return this.producers;
285        }
286
287        @Override
288        public List<NotificationChannelReviewer.Builder> getReviewers() {
289            return this.reviewers;
290        }
291
292        @Override
293        public List<UserChannelSubscription.Builder> getSubscriptions() {
294            return this.subscriptions;
295        }
296
297        @Override
298        public Long getId() {
299            return this.id;
300        }
301
302        @Override
303        public Long getVersionNumber() {
304            return this.versionNumber;
305        }
306
307        @Override
308        public String getObjectId() {
309            return this.objectId;
310        }
311
312        public void setName(String name) {
313            this.name = name;
314        }
315
316        public void setDescription(String description) {
317            this.description = description;
318        }
319
320        public void setSubscribable(boolean subscribable) {
321            this.subscribable = subscribable;
322        }
323
324        public void setRecipientLists(List<NotificationListRecipient.Builder> recipientLists) {
325            this.recipientLists = recipientLists;
326        }
327
328        public void setProducers(List<NotificationProducer.Builder> producers) {
329            this.producers = producers;
330        }
331
332        public void setReviewers(List<NotificationChannelReviewer.Builder> reviewers) {
333            this.reviewers = reviewers;
334        }
335
336        public void setSubscriptions(List<UserChannelSubscription.Builder> subscriptions) {
337            this.subscriptions = subscriptions;
338        }
339
340        public void setId(Long id) {
341            this.id = id;
342        }
343
344        public void setVersionNumber(Long versionNumber) {
345            this.versionNumber = versionNumber;
346        }
347
348        public void setObjectId(String objectId) {
349            this.objectId = objectId;
350        }
351
352    }
353
354
355    /**
356     * Defines some internal constants used on this class.
357     *
358     */
359    static class Constants {
360
361        final static String ROOT_ELEMENT_NAME = "notificationChannel";
362        final static String TYPE_NAME = "NotificationChannelType";
363
364    }
365
366
367    /**
368     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
369     *
370     */
371    static class Elements {
372
373        final static String NAME = "name";
374        final static String DESCRIPTION = "description";
375        final static String SUBSCRIBABLE = "subscribable";
376        final static String RECIPIENT_LISTS = "recipientLists";
377        final static String RECIPIENT_LIST = "recipientList";
378        final static String PRODUCERS = "producers";
379        final static String PRODUCER = "producer";
380        final static String REVIEWERS = "reviewers";
381        final static String REVIEWER = "reviewer";
382        final static String SUBSCRIPTIONS = "subscriptions";
383        final static String SUBSCRIPTION = "subscription";
384        final static String ID = "id";
385
386    }
387
388}