1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.logic;
20
21 import java.lang.reflect.Method;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.apache.commons.lang3.ArrayUtils;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.syncope.common.lib.SyncopeClientException;
27 import org.apache.syncope.common.lib.to.MailTemplateTO;
28 import org.apache.syncope.common.lib.types.ClientExceptionType;
29 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
30 import org.apache.syncope.common.lib.types.MailTemplateFormat;
31 import org.apache.syncope.core.persistence.api.dao.DuplicateException;
32 import org.apache.syncope.core.persistence.api.dao.MailTemplateDAO;
33 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
34 import org.apache.syncope.core.persistence.api.dao.NotificationDAO;
35 import org.apache.syncope.core.persistence.api.entity.EntityFactory;
36 import org.apache.syncope.core.persistence.api.entity.MailTemplate;
37 import org.apache.syncope.core.persistence.api.entity.Notification;
38 import org.springframework.security.access.prepost.PreAuthorize;
39 import org.springframework.transaction.annotation.Transactional;
40
41 public class MailTemplateLogic extends AbstractTransactionalLogic<MailTemplateTO> {
42
43 protected final MailTemplateDAO mailTemplateDAO;
44
45 protected final NotificationDAO notificationDAO;
46
47 protected final EntityFactory entityFactory;
48
49 public MailTemplateLogic(
50 final MailTemplateDAO mailTemplateDAO,
51 final NotificationDAO notificationDAO,
52 final EntityFactory entityFactory) {
53
54 this.mailTemplateDAO = mailTemplateDAO;
55 this.notificationDAO = notificationDAO;
56 this.entityFactory = entityFactory;
57 }
58
59 protected MailTemplateTO getMailTemplateTO(final String key) {
60 MailTemplateTO mailTemplateTO = new MailTemplateTO();
61 mailTemplateTO.setKey(key);
62 return mailTemplateTO;
63 }
64
65 @PreAuthorize("hasRole('" + IdRepoEntitlement.MAIL_TEMPLATE_READ + "')")
66 @Transactional(readOnly = true)
67 public MailTemplateTO read(final String key) {
68 MailTemplate mailTemplate = mailTemplateDAO.find(key);
69 if (mailTemplate == null) {
70 LOG.error("Could not find mail template '" + key + '\'');
71
72 throw new NotFoundException(key);
73 }
74
75 return getMailTemplateTO(key);
76 }
77
78 @PreAuthorize("hasRole('" + IdRepoEntitlement.MAIL_TEMPLATE_LIST + "')")
79 @Transactional(readOnly = true)
80 public List<MailTemplateTO> list() {
81 return mailTemplateDAO.findAll().stream().
82 map(template -> getMailTemplateTO(template.getKey())).collect(Collectors.toList());
83 }
84
85 @PreAuthorize("hasRole('" + IdRepoEntitlement.MAIL_TEMPLATE_CREATE + "')")
86 public MailTemplateTO create(final String key) {
87 if (mailTemplateDAO.find(key) != null) {
88 throw new DuplicateException(key);
89 }
90 MailTemplate mailTemplate = entityFactory.newEntity(MailTemplate.class);
91 mailTemplate.setKey(key);
92 mailTemplateDAO.save(mailTemplate);
93
94 return getMailTemplateTO(key);
95 }
96
97 @PreAuthorize("hasRole('" + IdRepoEntitlement.MAIL_TEMPLATE_READ + "')")
98 public String getFormat(final String key, final MailTemplateFormat format) {
99 MailTemplate mailTemplate = mailTemplateDAO.find(key);
100 if (mailTemplate == null) {
101 LOG.error("Could not find mail template '" + key + '\'');
102
103 throw new NotFoundException(key);
104 }
105
106 String template = format == MailTemplateFormat.HTML
107 ? mailTemplate.getHTMLTemplate()
108 : mailTemplate.getTextTemplate();
109 if (StringUtils.isBlank(template)) {
110 LOG.error("Could not find mail template '" + key + "' in " + format + " format");
111
112 throw new NotFoundException(key + " in " + format);
113 }
114
115 return template;
116 }
117
118 @PreAuthorize("hasRole('" + IdRepoEntitlement.MAIL_TEMPLATE_UPDATE + "')")
119 public void setFormat(final String key, final MailTemplateFormat format, final String template) {
120 MailTemplate mailTemplate = mailTemplateDAO.find(key);
121 if (mailTemplate == null) {
122 LOG.error("Could not find mail template '" + key + '\'');
123
124 throw new NotFoundException(key);
125 }
126
127 if (format == MailTemplateFormat.HTML) {
128 mailTemplate.setHTMLTemplate(template);
129 } else {
130 mailTemplate.setTextTemplate(template);
131 }
132
133 mailTemplateDAO.save(mailTemplate);
134 }
135
136 @PreAuthorize("hasRole('" + IdRepoEntitlement.MAIL_TEMPLATE_DELETE + "')")
137 public MailTemplateTO delete(final String key) {
138 MailTemplate mailTemplate = mailTemplateDAO.find(key);
139 if (mailTemplate == null) {
140 LOG.error("Could not find mail template '" + key + '\'');
141
142 throw new NotFoundException(key);
143 }
144
145 List<Notification> notifications = notificationDAO.findByTemplate(mailTemplate);
146 if (!notifications.isEmpty()) {
147 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InUse);
148 sce.getElements().addAll(notifications.stream().map(Notification::getKey).collect(Collectors.toList()));
149 throw sce;
150 }
151
152 MailTemplateTO deleted = getMailTemplateTO(key);
153 mailTemplateDAO.delete(key);
154 return deleted;
155 }
156
157 @Override
158 protected MailTemplateTO resolveReference(final Method method, final Object... args)
159 throws UnresolvedReferenceException {
160
161 String key = null;
162
163 if (ArrayUtils.isNotEmpty(args)) {
164 for (int i = 0; key == null && i < args.length; i++) {
165 if (args[i] instanceof String) {
166 key = ((String) args[i]);
167 } else if (args[i] instanceof MailTemplateTO) {
168 key = ((MailTemplateTO) args[i]).getKey();
169 }
170 }
171 }
172
173 if (key != null) {
174 try {
175 return getMailTemplateTO(key);
176 } catch (Throwable ignore) {
177 LOG.debug("Unresolved reference", ignore);
178 throw new UnresolvedReferenceException(ignore);
179 }
180 }
181
182 throw new UnresolvedReferenceException();
183 }
184 }