/
AbstractRepository.java
215 lines (184 loc) · 6.97 KB
/
AbstractRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.mapping.core.query;
import jakarta.data.page.Page;
import jakarta.data.page.Pageable;
import jakarta.data.repository.CrudRepository;
import jakarta.data.repository.BasicRepository;
import jakarta.nosql.Template;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.metadata.FieldMetadata;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static java.util.Objects.nonNull;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
import static org.eclipse.jnosql.mapping.IdNotFoundException.KEY_NOT_FOUND_EXCEPTION_SUPPLIER;
/**
* An abstract template class providing a base implementation for repositories managing entities
* through Jakarta Data's {@link BasicRepository} and {@link CrudRepository} interfaces.
* This class encapsulates common CRUD (Create, Read, Update, Delete) operations and supports pagination
* for a specific entity type. Subclasses are required to implement certain abstract methods to customize
* behavior for a particular database or data model.
*
* @param <T> The entity type managed by this repository.
* @param <K> The type of the entity's primary key.
*/
public abstract class AbstractRepository<T, K> implements BasicRepository<T, K>, CrudRepository<T, K> {
/**
* Retrieves the template associated with this repository.
*
* @return The template used for database operations.
*/
protected abstract Template template();
/**
* Retrieves the metadata information about the entity managed by this repository.
*
* @return The entity metadata information.
*/
protected abstract EntityMetadata entityMetadata();
/**
* Retrieves the Class object representing the entity type managed by this repository.
*
* @return The Class object of the entity type.
*/
@SuppressWarnings("unchecked")
protected Class<T> type() {
return (Class<T>) entityMetadata().type();
}
/**
* Converts an entity ID to a Stream of entities, wrapping it in an Optional.
*
* @return A function that converts an entity ID to a Stream of entities.
*/
protected Function<K, Stream<T>> optionalToStream() {
return id -> {
Optional<T> entity = this.findById(id);
return entity.stream();
};
}
/**
* Retrieves the metadata information about the ID field of the entity.
*
* @return The metadata information about the ID field.
* @throws org.eclipse.jnosql.mapping.IdNotFoundException if the ID field metadata is not found.
*/
protected FieldMetadata getIdField() {
return entityMetadata().id().orElseThrow(KEY_NOT_FOUND_EXCEPTION_SUPPLIER);
}
/**
* Retrieves the error message template for unsupported repository methods.
*
* @return The error message template.
*/
protected String getErrorMessage(){
return "The AbstractRepository does not support %s method";
}
@Override
public <S extends T> S save(S entity) {
Objects.requireNonNull(entity, "Entity is required");
Object id = getIdField().read(entity);
if (nonNull(id) && existsById((K) id)) {
return template().update(entity);
} else {
return template().insert(entity);
}
}
@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
requireNonNull(entities, "entities is required");
return StreamSupport.stream(entities.spliterator(), false)
.map(this::save).collect(toList());
}
@Override
public void deleteById(K id) {
requireNonNull(id, "is is required");
template().delete(type(), id);
}
@Override
public void deleteByIdIn(Iterable<K> ids) {
requireNonNull(ids, "ids is required");
ids.forEach(this::deleteById);
}
@Override
public Optional<T> findById(K id) {
requireNonNull(id, "id is required");
return template().find(type(), id);
}
@Override
public Stream<T> findByIdIn(Iterable<K> ids) {
requireNonNull(ids, "ids is required");
return stream(ids.spliterator(), false)
.flatMap(optionalToStream());
}
@Override
public boolean existsById(K id) {
return findById(id).isPresent();
}
@Override
public void delete(T entity) {
Objects.requireNonNull(entity, "entity is required");
EntityMetadata metadata = entityMetadata();
FieldMetadata id = metadata.id().orElseThrow(KEY_NOT_FOUND_EXCEPTION_SUPPLIER);
template().delete(metadata.type(), id.read(entity));
}
@Override
public void deleteAll(Iterable<? extends T> entities) {
Objects.requireNonNull(entities, "entities is required");
StreamSupport.stream(entities.spliterator(), false)
.forEach(this::delete);
}
@Override
public <S extends T> S insert(S entity) {
Objects.requireNonNull(entity, "entity is required");
return template().insert(entity);
}
@Override
public <S extends T> Iterable<S> insertAll(Iterable<S> entities) {
Objects.requireNonNull(entities, "entities is required");
return template().insert(entities);
}
@Override
public boolean update(T entity) {
Objects.requireNonNull(entity, "entity is required");
return template().update(entity) != null;
}
@Override
public int updateAll(Iterable<T> entities) {
Objects.requireNonNull(entities, "entities is required");
template().update(entities);
return (int) StreamSupport.stream(entities.spliterator(), false).count();
}
@Override
public void deleteAll() {
throw new UnsupportedOperationException(String.format(getErrorMessage(), "deleteAll"));
}
@Override
public long count() {
throw new UnsupportedOperationException(String.format(getErrorMessage(), "count"));
}
@Override
public Page<T> findAll(Pageable pageable) {
throw new UnsupportedOperationException(String.format(getErrorMessage(), "findAll"));
}
@Override
public Stream<T> findAll() {
throw new UnsupportedOperationException(String.format(getErrorMessage(), "findAll"));
}
}