BrightSide Workbench Full Report + Source Code
PublicationElephantNotification.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.publication.util;
20 
21 import java.util.Collection;
22 import java.util.EnumSet;
23 import java.util.HashSet;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import org.turro.action.queue.IElephantNotification;
27 import org.turro.action.queue.NotificationCategory;
28 import org.turro.action.queue.NotificationConstraint;
29 import org.turro.action.queue.NotificationRole;
30 import org.turro.action.queue.QueuePeriod;
31 import org.turro.annotation.ElephantNotification;
32 import org.turro.elephant.entities.db.QueueOptions;
33 import org.turro.mail.queue.NotificationCategoryMap;
34 import org.turro.mail.queue.QueueManager;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.publication.entity.PublicationCategory;
37 
42 @ElephantNotification
44 
45  @Override
46  public Collection<NotificationCategory> getCategories() {
47  NotificationCategoryMap map = initCategories(this);
48  return map.values();
49  }
50 
51  @Override
52  public Collection<NotificationCategory> getAllCategories() {
53  return getCategories();
54  }
55 
56  @Override
57  public Collection<String> getCategoryIds() {
58  NotificationCategoryMap map = initCategories(this);
59  return map.keySet();
60  }
61 
62  @Override
63  public Collection<String> getAllCategoryIds() {
64  return getCategoryIds();
65  }
66 
67  @Override
68  public NotificationCategory getCategory(String id) {
69  NotificationCategoryMap map = initCategories(this);
70  return map.get(id);
71  }
72 
73  @Override
74  public QueuePeriod period(String idCategory, IContact contact) {
75  QueueManager qm = new QueueManager();
76  QueueOptions qo = qm.getConfiguration(contact, idCategory);
77  return qo != null ? qo.getPeriod() : null;
78  }
79 
80  @Override
81  public void subscribe(String idCategory, IContact contact, QueuePeriod period) {
82  QueueManager qm = new QueueManager();
83  qm.setPeriod(contact, idCategory, period, true);
84  }
85 
86  @Override
87  public void subscribeIfNot(String idCategory, IContact contact, QueuePeriod period) {
88  QueueManager qm = new QueueManager();
89  qm.setPeriod(contact, idCategory, period, false);
90  }
91 
92  @Override
93  public void removeAll(String idCategory) {
94  QueueManager qm = new QueueManager();
95  qm.removeOptions(idCategory);
96  }
97 
98  @Override
99  public void periodToAll(String idCategory, QueuePeriod period) {
100  QueueManager qm = new QueueManager();
101  qm.setPeriodToAll(idCategory, period);
102  }
103 
104  @Override
105  public void checkConstraints(String idCategory) {
106  QueueManager qm = new QueueManager();
107  qm.checkConstraints(idCategory);
108  }
109 
110  @Override
111  public void reset() {
112  _categories = null;
113  }
114 
115  public static String convertId(PublicationCategory category) {
116  return PUBLICATION_NOTIFICATION + "/" + category.getId();
117  }
118 
119  /* Categories */
120 
121  private static NotificationCategoryMap _categories;
122 
123  public static final String
124  PUBLICATION_NOTIFICATION = "/publication";
125 
126  private static NotificationCategoryMap initCategories(IElephantNotification en) {
127  if(_categories == null) {
128  _categories = new NotificationCategoryMap();
130  Set<NotificationConstraint> constraints = pc.getConstraints().stream()
131  .map(c -> new NotificationConstraint(c.getKeyId(), c.isOptional()))
132  .collect(Collectors.toCollection(HashSet::new));
133  _categories.addCategory(convertId(pc),
134  pc.getName(), pc.getDescription(),
137  constraints, pc.isPublishable());
138  }
139  }
140  return _categories;
141  }
142 
143 }
144 
NotificationCategory addCategory(String id, String name, String description, EnumSet< QueuePeriod > allowedPeriods, boolean unique, QueuePeriod defaultPeriod, IElephantNotification notification, NotificationRole role, Set< NotificationConstraint > constraints, boolean publishable)
void setPeriod(IContact contact, String idCategory, QueuePeriod queuePeriod, boolean override)
QueueOptions getConfiguration(IContact contact, String idCategory)
void setPeriodToAll(String idCategory, QueuePeriod period)
void removeOptions(String idCategory)
void checkConstraints(String idCategory)
static List< PublicationCategory > getPublicationCategories()
void subscribe(String idCategory, IContact contact, QueuePeriod period)
void subscribeIfNot(String idCategory, IContact contact, QueuePeriod period)