Skip to content

Commit 90e7f7e

Browse files
author
Gerald Unterrainer
committed
some convenience methods for tenant-IDs
1 parent 3751239 commit 90e7f7e

File tree

8 files changed

+113
-20
lines changed

8 files changed

+113
-20
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<modelVersion>4.0.0</modelVersion>
1919
<artifactId>http-server</artifactId>
20-
<version>0.2.15</version>
20+
<version>0.2.16</version>
2121
<name>HttpServer</name>
2222
<packaging>jar</packaging>
2323

src/main/java/info/unterrainer/commons/httpserver/daos/BasicJpqlDao.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ private <T> T getFirst(final EntityManager em, final TypedQuery<T> query) {
4646
}
4747

4848
UpsertResult<P> upsert(final EntityManager em, final TypedQuery<P> query, final P entity,
49-
final Set<Long> tenantIds) {
49+
final Set<Long> readTenantIds, final Set<Long> writeTenantIds) {
5050
boolean wasInserted = false;
5151
boolean wasUpdated = false;
5252
P e = getFirst(em, query);
5353
if (e == null) {
54-
e = coreDao.create(em, entity, tenantIds);
54+
e = coreDao.create(em, entity, writeTenantIds);
5555
wasInserted = true;
5656
} else {
5757
entity.setId(e.getId());
5858
entity.setCreatedOn(e.getCreatedOn());
59-
e = coreDao.update(em, entity, tenantIds);
59+
e = coreDao.update(em, entity, readTenantIds);
6060
wasUpdated = true;
6161
}
6262
return UpsertResult.<P>builder().wasInserted(wasInserted).wasUpdated(wasUpdated).jpa(e).build();

src/main/java/info/unterrainer/commons/httpserver/daos/BasicListQueryBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ void setSelect(final String selectClause) {
3232

3333
public TypedQuery<X> getTypedQuery(final EntityManager em) {
3434
return dao.coreDao.getQuery(em, selectClause, joinClause, whereClause, parameters, resultType, orderByClause,
35-
lockPessimistic, null, tenantIds);
35+
lockPessimistic, null, readTenantIds);
3636
}
3737

3838
public javax.persistence.Query getCountQuery(final EntityManager em) {
39-
return dao.coreDao.getCountQuery(em, selectClause, joinClause, whereClause, parameters, null, tenantIds);
39+
return dao.coreDao.getCountQuery(em, selectClause, joinClause, whereClause, parameters, null, readTenantIds);
4040
}
4141

4242
/**

src/main/java/info/unterrainer/commons/httpserver/daos/BasicQueryEntityManagerBuilder.java

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package info.unterrainer.commons.httpserver.daos;
22

33
import java.util.Arrays;
4+
import java.util.Collection;
45
import java.util.HashSet;
56
import java.util.Set;
7+
import java.util.stream.Collectors;
68

79
import javax.persistence.EntityManager;
810

@@ -19,7 +21,8 @@ public class BasicQueryEntityManagerBuilder<P extends BasicJpa, T, R extends Bas
1921
@Getter
2022
protected EntityManager entityManager;
2123
@Getter
22-
protected Set<Long> tenantIds;
24+
protected Set<Long> readTenantIds;
25+
@Getter
2326
protected Set<Long> writeTenantIds;
2427

2528
/**
@@ -38,32 +41,119 @@ public R entityManager(final EntityManager em) {
3841
}
3942

4043
/**
41-
* Set custom tenant-IDs to be used when querying / writing to the database.
44+
* Set custom tenant-IDs to be used when querying the database.
45+
* <p>
46+
* Overwrites the existing set.
47+
*
48+
* @param ids the tenant-ID to use
49+
* @return an instance of this builder to provide a fluent interface
50+
*/
51+
@SuppressWarnings("unchecked")
52+
public R readTenant(final Long... ids) {
53+
readTenantIds = new HashSet<>(Arrays.asList(ids));
54+
return (R) this;
55+
}
56+
57+
/**
58+
* Set custom tenant-IDs to be used when querying the database.
59+
* <p>
60+
* Overwrites the existing set.
61+
*
62+
* @param ids the tenant-ID to use
63+
* @return an instance of this builder to provide a fluent interface
64+
*/
65+
@SuppressWarnings("unchecked")
66+
public R readTenant(final Collection<Long> ids) {
67+
readTenantIds = new HashSet<>(ids);
68+
return (R) this;
69+
}
70+
71+
/**
72+
* Set custom tenant-IDs to be used when querying the database.
73+
* <p>
74+
* Overwrites the existing set.
75+
*
76+
* @param ids the tenant-ID to use
77+
* @return an instance of this builder to provide a fluent interface
78+
*/
79+
public R readTenant(final String commaSeparatedList) {
80+
return readTenant(createSetFrom(commaSeparatedList));
81+
}
82+
83+
/**
84+
* Set custom tenant-IDs to be used when writing to the database.
85+
* <p>
86+
* Overwrites the existing set.
87+
*
88+
* @param ids the tenant-ID to use
89+
* @return an instance of this builder to provide a fluent interface
90+
*/
91+
@SuppressWarnings("unchecked")
92+
public R writeTenant(final Long... ids) {
93+
writeTenantIds = new HashSet<>(Arrays.asList(ids));
94+
return (R) this;
95+
}
96+
97+
/**
98+
* Set custom tenant-IDs to be used when writing to the database.
4299
* <p>
43100
* Overwrites the existing set.
44101
*
45102
* @param ids the tenant-ID to use
46103
* @return an instance of this builder to provide a fluent interface
47104
*/
48105
@SuppressWarnings("unchecked")
49-
public R tenant(final Long... ids) {
50-
tenantIds = new HashSet<>(Arrays.asList(ids));
106+
public R writeTenant(final Collection<Long> ids) {
107+
writeTenantIds = new HashSet<>(ids);
51108
return (R) this;
52109
}
53110

111+
/**
112+
* Set custom tenant-IDs to be used when writing to the database.
113+
* <p>
114+
* Overwrites the existing set.
115+
*
116+
* @param ids the tenant-ID to use
117+
* @return an instance of this builder to provide a fluent interface
118+
*/
119+
public R writeTenant(final String commaSeparatedList) {
120+
return writeTenant(createSetFrom(commaSeparatedList));
121+
}
122+
54123
/**
55124
* Set a custom tenant-ID that is retrieved from the given context to be used
56125
* when querying the database.
57126
* <p>
58-
* Overwrites the existing set.
127+
* Overwrites the existing sets of {@link #readTenant(Long...)} and
128+
* {@link #writeTenant(Long...)}.
59129
*
60130
* @param ctx the context that contains the tenant-ID to use
61131
* @return an instance of this builder to provide a fluent interface
62132
*/
63133
@SuppressWarnings("unchecked")
64134
public R tenant(final Context ctx) {
65-
tenantIds = (Set<Long>) ctx.attribute(Attribute.USER_TENANTS_READ_SET);
135+
readTenantIds = (Set<Long>) ctx.attribute(Attribute.USER_TENANTS_READ_SET);
66136
writeTenantIds = (Set<Long>) ctx.attribute(Attribute.USER_TENANTS_WRITE_SET);
67137
return (R) this;
68138
}
139+
140+
private Set<Long> createSetFrom(final String commaSeparatedList) {
141+
if (commaSeparatedList == null || commaSeparatedList.isBlank())
142+
return new HashSet<>();
143+
144+
return Arrays.stream(commaSeparatedList.split(","))
145+
.map(this::parseLong)
146+
.filter(java.util.Objects::nonNull)
147+
.collect(Collectors.toSet());
148+
}
149+
150+
private Long parseLong(final String s) {
151+
if (s == null || s.isBlank())
152+
return null;
153+
try {
154+
return Long.parseLong(s.trim());
155+
} catch (NumberFormatException e) {
156+
return null;
157+
}
158+
}
69159
}

src/main/java/info/unterrainer/commons/httpserver/daos/CountQueryBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public class CountQueryBuilder<P extends BasicJpa> extends BasicListQueryBuilder
1414
public Long build() {
1515
if (entityManager != null)
1616
return (Long) dao.coreDao
17-
.getCountQuery(entityManager, selectClause, joinClause, whereClause, parameters, null, tenantIds)
17+
.getCountQuery(entityManager, selectClause, joinClause, whereClause, parameters, null, readTenantIds)
1818
.getSingleResult();
1919
return (Long) Transactions.withNewTransactionReturning(emf,
20-
em -> dao.coreDao.getCountQuery(em, selectClause, joinClause, whereClause, parameters, null, tenantIds)
20+
em -> dao.coreDao.getCountQuery(em, selectClause, joinClause, whereClause, parameters, null, readTenantIds)
2121
.getSingleResult());
2222
}
2323
}

src/main/java/info/unterrainer/commons/httpserver/daos/JpaListQuery.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public class JpaListQuery<P extends BasicJpa> extends ListQuery<P, P> {
2424
* update, and if it was an update, what the old values were
2525
*/
2626
public UpsertResult<P> upsert(final P entity) {
27-
return withEntityManager(
28-
em -> builder.getDao().upsert(em, builder.getTypedQuery(em), entity, builder.getTenantIds()));
27+
return withEntityManager(em -> builder.getDao()
28+
.upsert(em, builder.getTypedQuery(em), entity, builder.getReadTenantIds(),
29+
builder.getWriteTenantIds()));
2930
}
3031

3132
/**
@@ -39,7 +40,7 @@ public void delete() {
3940
withEntityManager(em -> {
4041
List<P> list = builder.getDao().getList(em, builder.getTypedQuery(em), 0, Long.MAX_VALUE);
4142
for (P jpa : list)
42-
builder.getDao().coreDao.delete(em, jpa.getId(), builder.getTenantIds());
43+
builder.getDao().coreDao.delete(em, jpa.getId(), builder.getReadTenantIds());
4344
return null;
4445
});
4546
}

src/main/java/info/unterrainer/commons/httpserver/daos/QueryInterface.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ interface QueryInterface<P extends BasicJpa, T> {
1717

1818
javax.persistence.Query getCountQuery(final EntityManager em);
1919

20-
Set<Long> getTenantIds();
20+
Set<Long> getReadTenantIds();
21+
22+
Set<Long> getWriteTenantIds();
2123
}

src/main/java/info/unterrainer/commons/httpserver/daos/SingleQueryBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected <V> V withEntityManager(final Function<EntityManager, V> func) {
3131
*/
3232
public P get() {
3333
return withEntityManager(em -> dao.coreDao
34-
.getQuery(em, "o", null, "o.id = :id", Map.of("id", id), dao.type, null, false, null, tenantIds)
34+
.getQuery(em, "o", null, "o.id = :id", Map.of("id", id), dao.type, null, false, null, readTenantIds)
3535
.getSingleResult());
3636
}
3737

@@ -40,7 +40,7 @@ public P get() {
4040
*/
4141
public void delete() {
4242
withEntityManager(em -> {
43-
dao.coreDao.delete(em, id, tenantIds);
43+
dao.coreDao.delete(em, id, readTenantIds);
4444
return null;
4545
});
4646
}

0 commit comments

Comments
 (0)