Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service

File BaseRepository.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
14% of files have more coverage

Code metrics

4
35
19
1
170
137
22
0.63
1.84
19
1.16

Classes

Class Line # Actions
BaseRepository 25 35 22
0.775862177.6%
 

Contributing tests

This file is covered by 77 tests. .

Source view

1    package io.sunshower.service;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.model.core.auth.ProtectedDistributableEntity;
5    import io.sunshower.persistence.core.DistributableEntity;
6    import io.sunshower.persistence.core.DistributableHierarchicalEntity;
7    import io.sunshower.persistence.core.Persistable;
8    import io.sunshower.service.repository.EntityRepository;
9    import io.sunshower.service.security.PermissionsService;
10    import io.sunshower.service.security.Session;
11    import java.io.Serializable;
12    import java.util.List;
13    import javax.inject.Inject;
14    import javax.persistence.EntityManager;
15    import javax.persistence.EntityNotFoundException;
16    import javax.persistence.PersistenceContext;
17    import javax.persistence.TypedQuery;
18    import org.springframework.security.access.prepost.PreAuthorize;
19    import org.springframework.security.acls.domain.BasePermission;
20    import org.springframework.security.acls.model.Permission;
21    import org.springframework.transaction.annotation.Transactional;
22   
23    /** Created by haswell on 5/16/17. */
24    @Transactional
 
25    public abstract class BaseRepository<ID extends Serializable, E extends Persistable<ID>>
26    implements EntityRepository<ID, E> {
27   
28    private final String entityName;
29    private final Class<E> entityType;
30   
31    @Inject private Session session;
32   
33    @PersistenceContext private EntityManager entityManager;
34   
35    @Inject private PermissionsService<Permission> permissionService;
36   
 
37  0 toggle protected BaseRepository(Class<E> entityType) {
38  0 this.entityType = entityType;
39  0 this.entityName = entityType.getSimpleName();
40    }
41   
 
42  6 toggle protected BaseRepository(Class<E> entityType, final String entityName) {
43  6 this.entityName = entityName;
44  6 this.entityType = entityType;
45    }
46   
 
47  131 toggle @Override
48    @PreAuthorize("hasAuthority('tenant:user')")
49    public E create(E entity) {
50  131 entityManager.persist(entity);
51  131 permissionService.grantWithCurrentSession(
52    entityType,
53    entity,
54    BasePermission.ADMINISTRATION,
55    BasePermission.WRITE,
56    BasePermission.READ,
57    BasePermission.DELETE);
58  131 return entity;
59    }
60   
 
61  3 toggle @Override
62    @PreAuthorize("hasPermission(#entity, 'WRITE')")
63    public E update(E entity) {
64  3 return entityManager.merge(entity);
65    }
66   
 
67  27 toggle @Override
68    @PreAuthorize("hasAuthority('tenant:user')")
69    public List<E> list() {
70  27 return listQuery().getResultList();
71    }
72   
 
73  71 toggle @Override
74    public E get(ID id) {
75  71 return entityManager.find(entityType, id);
76    }
77   
 
78  12 toggle @Override
79    public E delete(ID id) {
80  12 E entity = entityManager.find(entityType, id);
81  12 if (entity == null) {
82  0 throw new EntityNotFoundException(
83    String.format(
84    "No %s " + "identified by '%s' was found for current user!", entityType, id));
85    }
86  12 entityManager.remove(entity);
87  12 permissionService.delete(entityType, entity);
88  12 return entity;
89    }
90   
 
91  77 toggle @Override
92    @PreAuthorize("hasAuthority('tenant:user')")
93    public E save(E entity) {
94  77 long w = count(entity);
95    // if (w == 0) {
96  77 return create(entity);
97    // } else {
98    // return update(entity);
99    // }
100    // return entity;
101    }
102   
 
103  77 toggle protected long count(E entity) {
104  77 final String template =
105    String.format(
106    "select count(e) from %s as e "
107    + "join e.identity oid "
108    + "where oid.owner.username = :id "
109    + "and e.id = :wid",
110    entityName);
111  77 final Long w =
112    entityManager
113    .createQuery(template, Long.class)
114    .setParameter("id", session.getUsername())
115    .setParameter("wid", idFor(entity))
116    .getSingleResult();
117  77 return w;
118    }
119   
 
120  27 toggle protected TypedQuery<E> listQuery() {
121   
122  27 final String template =
123    "select e from %s e " + "join e.identity oid where " + "oid.owner.username = :id";
124   
125  27 final String query = String.format(template, entityName);
126  27 return entityManager.createQuery(query, entityType).setParameter("id", session.getUsername());
127    }
128   
 
129  102 toggle protected Session getSession() {
130  102 return session;
131    }
132   
 
133  14 toggle protected void flush() {
134  14 entityManager.flush();
135    }
136   
 
137  128 toggle protected EntityManager getEntityManager() {
138  128 return entityManager;
139    }
140   
 
141  14 toggle protected <T extends Persistable> void grant(
142    Class<T> entityType, T instance, Permission... permissions) {
143  14 permissionService.grantWithCurrentSession(entityType, instance, permissions);
144    }
145   
 
146  0 toggle @SuppressWarnings("unchecked")
147    protected <T extends Persistable> void grant(T instance, Permission... permissions) {
148  0 permissionService.grantWithCurrentSession((Class<T>) entityType, instance, permissions);
149    }
150   
 
151  0 toggle protected <T extends ProtectedDistributableEntity> void checkPermission(
152    T instance, Permission... permissions) {
153  0 permissionService.checkPermission(instance, permissions);
154    }
155   
 
156  4 toggle protected <T extends Persistable> void revokeAll(Class<T> type, T instance) {
157  4 permissionService.delete(type, instance);
158    }
159   
 
160  77 toggle protected <T extends Serializable> T idFor(E e) {
161  77 if (e instanceof DistributableEntity || e instanceof DistributableHierarchicalEntity) {
162  77 return (T) ((Identifier) e.getId()).value();
163    }
164  0 return (T) e.getId();
165    }
166   
 
167  0 toggle protected void revokeAll(E instance) {
168  0 permissionService.delete(entityType, instance);
169    }
170    }