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.syncope.common.lib.to.ApplicationTO;
26 import org.apache.syncope.common.lib.to.PrivilegeTO;
27 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
28 import org.apache.syncope.core.persistence.api.dao.ApplicationDAO;
29 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
30 import org.apache.syncope.core.persistence.api.entity.Application;
31 import org.apache.syncope.core.persistence.api.entity.Privilege;
32 import org.apache.syncope.core.provisioning.api.data.ApplicationDataBinder;
33 import org.springframework.security.access.prepost.PreAuthorize;
34 import org.springframework.transaction.annotation.Transactional;
35
36 public class ApplicationLogic extends AbstractTransactionalLogic<ApplicationTO> {
37
38 protected final ApplicationDataBinder binder;
39
40 protected final ApplicationDAO applicationDAO;
41
42 public ApplicationLogic(final ApplicationDataBinder binder, final ApplicationDAO applicationDAO) {
43 this.binder = binder;
44 this.applicationDAO = applicationDAO;
45 }
46
47 @PreAuthorize("hasRole('" + IdRepoEntitlement.APPLICATION_READ + "')")
48 @Transactional(readOnly = true)
49 public ApplicationTO read(final String key) {
50 Application application = applicationDAO.find(key);
51 if (application == null) {
52 LOG.error("Could not find application '" + key + '\'');
53
54 throw new NotFoundException(key);
55 }
56
57 return binder.getApplicationTO(application);
58 }
59
60 @PreAuthorize("hasRole('" + IdRepoEntitlement.APPLICATION_READ + "')")
61 @Transactional(readOnly = true)
62 public PrivilegeTO readPrivilege(final String key) {
63 Privilege privilege = applicationDAO.findPrivilege(key);
64 if (privilege == null) {
65 LOG.error("Could not find privilege '" + key + '\'');
66
67 throw new NotFoundException(key);
68 }
69
70 return binder.getPrivilegeTO(privilege);
71 }
72
73 @PreAuthorize("hasRole('" + IdRepoEntitlement.APPLICATION_LIST + "')")
74 @Transactional(readOnly = true)
75 public List<ApplicationTO> list() {
76 return applicationDAO.findAll().stream().map(binder::getApplicationTO).collect(Collectors.toList());
77 }
78
79 @PreAuthorize("hasRole('" + IdRepoEntitlement.APPLICATION_CREATE + "')")
80 public ApplicationTO create(final ApplicationTO applicationTO) {
81 return binder.getApplicationTO(applicationDAO.save(binder.create(applicationTO)));
82 }
83
84 @PreAuthorize("hasRole('" + IdRepoEntitlement.APPLICATION_UPDATE + "')")
85 public ApplicationTO update(final ApplicationTO applicationTO) {
86 Application application = applicationDAO.find(applicationTO.getKey());
87 if (application == null) {
88 LOG.error("Could not find application '" + applicationTO.getKey() + '\'');
89 throw new NotFoundException(applicationTO.getKey());
90 }
91
92 return binder.getApplicationTO(applicationDAO.save(binder.update(application, applicationTO)));
93 }
94
95 @PreAuthorize("hasRole('" + IdRepoEntitlement.APPLICATION_DELETE + "')")
96 public ApplicationTO delete(final String key) {
97 Application application = applicationDAO.find(key);
98 if (application == null) {
99 LOG.error("Could not find application '" + key + '\'');
100
101 throw new NotFoundException(key);
102 }
103
104 ApplicationTO deleted = binder.getApplicationTO(application);
105 applicationDAO.delete(key);
106 return deleted;
107 }
108
109 @Override
110 protected ApplicationTO resolveReference(final Method method, final Object... args)
111 throws UnresolvedReferenceException {
112
113 String key = null;
114
115 if (ArrayUtils.isNotEmpty(args)) {
116 for (int i = 0; key == null && i < args.length; i++) {
117 if (args[i] instanceof String) {
118 key = (String) args[i];
119 } else if (args[i] instanceof ApplicationTO) {
120 key = ((ApplicationTO) args[i]).getKey();
121 }
122 }
123 }
124
125 if (key != null) {
126 try {
127 return binder.getApplicationTO(applicationDAO.find(key));
128 } catch (Throwable ignore) {
129 LOG.debug("Unresolved reference", ignore);
130 throw new UnresolvedReferenceException(ignore);
131 }
132 }
133
134 throw new UnresolvedReferenceException();
135 }
136 }