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.AttrRepoTO;
26 import org.apache.syncope.common.lib.types.AMEntitlement;
27 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
28 import org.apache.syncope.core.persistence.api.dao.AttrRepoDAO;
29 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
30 import org.apache.syncope.core.persistence.api.entity.am.AttrRepo;
31 import org.apache.syncope.core.provisioning.api.data.AttrRepoDataBinder;
32 import org.springframework.security.access.prepost.PreAuthorize;
33 import org.springframework.transaction.annotation.Transactional;
34
35 public class AttrRepoLogic extends AbstractTransactionalLogic<AttrRepoTO> {
36
37 protected final AttrRepoDataBinder binder;
38
39 protected final AttrRepoDAO attrRepoDAO;
40
41 public AttrRepoLogic(final AttrRepoDataBinder binder, final AttrRepoDAO attrRepoDAO) {
42 this.binder = binder;
43 this.attrRepoDAO = attrRepoDAO;
44 }
45
46 @PreAuthorize("hasRole('" + AMEntitlement.ATTR_REPO_CREATE + "')")
47 public AttrRepoTO create(final AttrRepoTO attrRepoTO) {
48 return binder.getAttrRepoTO(attrRepoDAO.save(binder.create(attrRepoTO)));
49 }
50
51 @PreAuthorize("hasRole('" + AMEntitlement.ATTR_REPO_UPDATE + "')")
52 public AttrRepoTO update(final AttrRepoTO attrRepoTO) {
53 AttrRepo attrRepo = attrRepoDAO.find(attrRepoTO.getKey());
54 if (attrRepo == null) {
55 throw new NotFoundException("AttrRepo " + attrRepoTO.getKey() + " not found");
56 }
57
58 return binder.getAttrRepoTO(attrRepoDAO.save(binder.update(attrRepo, attrRepoTO)));
59 }
60
61 @PreAuthorize("hasRole('" + AMEntitlement.ATTR_REPO_LIST + "') or hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
62 @Transactional(readOnly = true)
63 public List<AttrRepoTO> list() {
64 return attrRepoDAO.findAll().stream().map(binder::getAttrRepoTO).collect(Collectors.toList());
65 }
66
67 @PreAuthorize("hasRole('" + AMEntitlement.ATTR_REPO_READ + "')")
68 @Transactional(readOnly = true)
69 public AttrRepoTO read(final String key) {
70 AttrRepo attrRepo = attrRepoDAO.find(key);
71 if (attrRepo == null) {
72 throw new NotFoundException("AttrRepo " + key + " not found");
73 }
74
75 return binder.getAttrRepoTO(attrRepo);
76 }
77
78 @PreAuthorize("hasRole('" + AMEntitlement.ATTR_REPO_DELETE + "')")
79 public AttrRepoTO delete(final String key) {
80 AttrRepo attrRepo = attrRepoDAO.find(key);
81 if (attrRepo == null) {
82 throw new NotFoundException("AttrRepo " + key + " not found");
83 }
84
85 AttrRepoTO deleted = binder.getAttrRepoTO(attrRepo);
86 attrRepoDAO.delete(attrRepo);
87
88 return deleted;
89 }
90
91 @Override
92 protected AttrRepoTO resolveReference(final Method method, final Object... args)
93 throws UnresolvedReferenceException {
94
95 if (ArrayUtils.isEmpty(args)) {
96 throw new UnresolvedReferenceException();
97 }
98
99 final String key;
100
101 if (args[0] instanceof String) {
102 key = (String) args[0];
103 } else if (args[0] instanceof AttrRepoTO) {
104 key = ((AttrRepoTO) args[0]).getKey();
105 } else {
106 throw new UnresolvedReferenceException();
107 }
108
109 try {
110 return binder.getAttrRepoTO(attrRepoDAO.find(key));
111 } catch (Throwable ignore) {
112 LOG.debug("Unresolved reference", ignore);
113 throw new UnresolvedReferenceException(ignore);
114 }
115 }
116 }