Skip to content

Commit d666df9

Browse files
committed
fix: remove entityManager from CompasPluginsResourceService
Signed-off-by: Stiliyan Valkanov <stiliyan.valkanov@bearingpoint.com>
1 parent 37066e4 commit d666df9

4 files changed

Lines changed: 220 additions & 182 deletions

File tree

app/src/main/java/org/lfenergy/compas/scl/data/service/CompasPluginsResourceService.java

Lines changed: 12 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import jakarta.enterprise.context.ApplicationScoped;
77
import jakarta.inject.Inject;
8-
import jakarta.persistence.EntityManager;
9-
import jakarta.persistence.TypedQuery;
108
import jakarta.transaction.Transactional;
119
import org.apache.logging.log4j.LogManager;
1210
import org.apache.logging.log4j.Logger;
@@ -40,62 +38,33 @@ public class CompasPluginsResourceService {
4038
"application/xml"
4139
);
4240

43-
private final EntityManager entityManager;
4441
private final CustomPluginsResourceRepository repository;
4542

4643
@Inject
47-
public CompasPluginsResourceService(EntityManager entityManager, CustomPluginsResourceRepository repository) {
48-
this.entityManager = entityManager;
44+
public CompasPluginsResourceService(CustomPluginsResourceRepository repository) {
4945
this.repository = repository;
5046
}
5147

5248
@Transactional(SUPPORTS)
5349
public List<PluginsCustomResource> list(String type, Date uploadedAfter, Date uploadedBefore,
5450
String name, int page, int size) {
55-
var queryBuilder = new StringBuilder("SELECT e FROM PluginsCustomResource e WHERE e.type = :type");
56-
var params = new HashMap<String, Object>();
57-
params.put("type", type);
58-
59-
appendFilters(queryBuilder, params, uploadedAfter, uploadedBefore, name);
60-
queryBuilder.append(" ORDER BY e.uploadedAt DESC");
61-
62-
TypedQuery<PluginsCustomResource> query = entityManager.createQuery(queryBuilder.toString(), PluginsCustomResource.class);
63-
params.forEach(query::setParameter);
64-
query.setFirstResult(page * size);
65-
query.setMaxResults(size);
66-
return query.getResultList();
51+
return repository.listFiltered(type, uploadedAfter, uploadedBefore, name, page, size);
6752
}
6853

6954
@Transactional(SUPPORTS)
7055
public long count(String type, Date uploadedAfter, Date uploadedBefore, String name) {
71-
var queryBuilder = new StringBuilder("SELECT COUNT(e) FROM PluginsCustomResource e WHERE e.type = :type");
72-
var params = new HashMap<String, Object>();
73-
params.put("type", type);
74-
75-
appendFilters(queryBuilder, params, uploadedAfter, uploadedBefore, name);
76-
77-
TypedQuery<Long> query = entityManager.createQuery(queryBuilder.toString(), Long.class);
78-
params.forEach(query::setParameter);
79-
return query.getSingleResult();
56+
return repository.countFiltered(type, uploadedAfter, uploadedBefore, name);
8057
}
8158

8259
@Transactional(SUPPORTS)
8360
public PluginsCustomResource findById(UUID id) {
84-
PluginsCustomResource entity = entityManager.find(PluginsCustomResource.class, id);
85-
if (entity == null) {
86-
throw new CompasNoDataFoundException(
87-
String.format("Data entry with id '%s' not found", id));
88-
}
89-
return entity;
61+
return repository.findByIdOptional(id).orElseThrow(() -> new CompasNoDataFoundException(
62+
String.format("Data entry with id '%s' not found", id)));
9063
}
9164

9265
@Transactional(SUPPORTS)
9366
public List<PluginsCustomResource> findLatestByType(String type) {
94-
TypedQuery<PluginsCustomResource> query = entityManager.createQuery(
95-
"SELECT e FROM PluginsCustomResource e WHERE e.type = :type",
96-
PluginsCustomResource.class);
97-
query.setParameter("type", type);
98-
List<PluginsCustomResource> entities = query.getResultList();
67+
List<PluginsCustomResource> entities = repository.list("type", type);
9968

10069
if (entities.isEmpty()) {
10170
throw new CompasNoDataFoundException(
@@ -115,12 +84,7 @@ public List<PluginsCustomResource> findLatestByType(String type) {
11584

11685
@Transactional(SUPPORTS)
11786
public PluginsCustomResource findLatestByTypeAndName(String type, String name) {
118-
List<PluginsCustomResource> entities = entityManager.createQuery(
119-
"SELECT e FROM PluginsCustomResource e WHERE e.type = :type AND e.name = :name",
120-
PluginsCustomResource.class)
121-
.setParameter("type", type)
122-
.setParameter("name", name)
123-
.getResultList();
87+
List<PluginsCustomResource> entities = repository.list("type = ?1 and name = ?2", type, name);
12488

12589
if (entities.isEmpty()) {
12690
throw new CompasNoDataFoundException(
@@ -151,13 +115,10 @@ public PluginsCustomResource getSpecificVersionByTypeAndName(String dataType, St
151115
)
152116
);
153117
}
154-
155118

156119
@Transactional(REQUIRED)
157120
public void deleteByType(String type) {
158-
int deletedCount = entityManager.createQuery("DELETE FROM PluginsCustomResource e WHERE e.type = :type")
159-
.setParameter("type", type)
160-
.executeUpdate();
121+
long deletedCount = repository.delete("type = ?1", type);
161122

162123
if (deletedCount == 0) {
163124
throw new CompasNoDataFoundException(
@@ -167,11 +128,7 @@ public void deleteByType(String type) {
167128

168129
@Transactional(REQUIRED)
169130
public void deleteByTypeAndName(String type, String name) {
170-
int deletedCount = entityManager.createQuery(
171-
"DELETE FROM PluginsCustomResource e WHERE e.type = :type AND e.name = :name")
172-
.setParameter("type", type)
173-
.setParameter("name", name)
174-
.executeUpdate();
131+
long deletedCount = repository.delete("type = ?1 and name = ?2", type, name);
175132

176133
if (deletedCount == 0) {
177134
throw new CompasNoDataFoundException(
@@ -188,15 +145,7 @@ public PluginsCustomResource upload(UploadCustomPluginsResourceData request) {
188145

189146
String resolvedVersion = resolveVersion(request.type(), request.name(), request.version(), request.nextVersionType());
190147

191-
Long duplicateCount = entityManager.createQuery(
192-
"SELECT COUNT(e) FROM PluginsCustomResource e " +
193-
"WHERE e.type = :type AND e.tenant = :tenant AND e.name = :name AND e.version = :version",
194-
Long.class)
195-
.setParameter("type", request.type())
196-
.setParameter("tenant", DEFAULT_TENANT)
197-
.setParameter("name", request.name())
198-
.setParameter("version", resolvedVersion)
199-
.getSingleResult();
148+
long duplicateCount = repository.countDuplicate(request.type(), DEFAULT_TENANT, request.name(), resolvedVersion);
200149

201150
if (duplicateCount > 0) {
202151
throw new CompasDuplicateVersionException(
@@ -213,7 +162,7 @@ public PluginsCustomResource upload(UploadCustomPluginsResourceData request) {
213162
entity.content = request.content();
214163
entity.version = resolvedVersion;
215164
entity.dataCompatibilityVersion = request.dataCompatibilityVersion();
216-
entityManager.persist(entity);
165+
repository.persist(entity);
217166

218167
LOGGER.info("Persisted plugins custom resource with id '{}'", entity.id);
219168
return entity;
@@ -241,14 +190,7 @@ private String resolveVersion(String type, String name,
241190

242191
private String findLatestVersionAndIncrement(String type, String name,
243192
ChangeSetType changeSetType) {
244-
List<PluginsCustomResource> existing = entityManager.createQuery(
245-
"SELECT e FROM PluginsCustomResource e " +
246-
"WHERE e.type = :type AND e.tenant = :tenant AND e.name = :name",
247-
PluginsCustomResource.class)
248-
.setParameter("type", type)
249-
.setParameter("tenant", DEFAULT_TENANT)
250-
.setParameter("name", name)
251-
.getResultList();
193+
List<PluginsCustomResource> existing = repository.list("type = ?1 and tenant = ?2 and name = ?3", type, DEFAULT_TENANT, name);
252194

253195
if (existing.isEmpty()) {
254196
return new Version(1, 0, 0).toString();
@@ -262,22 +204,6 @@ private String findLatestVersionAndIncrement(String type, String name,
262204
return latest.getNextVersion(changeSetType).toString();
263205
}
264206

265-
private void appendFilters(StringBuilder queryBuilder, Map<String, Object> params,
266-
Date uploadedAfter, Date uploadedBefore, String name) {
267-
if (uploadedAfter != null) {
268-
queryBuilder.append(" AND e.uploadedAt >= :uploadedAfter");
269-
params.put("uploadedAfter", toOffsetDateTime(uploadedAfter));
270-
}
271-
if (uploadedBefore != null) {
272-
queryBuilder.append(" AND e.uploadedAt <= :uploadedBefore");
273-
params.put("uploadedBefore", toOffsetDateTime(uploadedBefore));
274-
}
275-
if (name != null && !name.isBlank()) {
276-
queryBuilder.append(" AND LOWER(e.name) LIKE :name");
277-
params.put("name", "%" + name.toLowerCase() + "%");
278-
}
279-
}
280-
281207
private void validateContentType(String contentType) {
282208
var normalizedContentType = contentType != null ? contentType.trim().toLowerCase() : "";
283209
if (!ALLOWED_CONTENT_TYPES.contains(normalizedContentType)) {
@@ -293,9 +219,4 @@ private void validateSemver(String version, String fieldName) {
293219
fieldName, version));
294220
}
295221
}
296-
297-
private OffsetDateTime toOffsetDateTime(Date date) {
298-
if (date == null) return null;
299-
return date.toInstant().atOffset(ZoneOffset.UTC);
300-
}
301222
}

0 commit comments

Comments
 (0)