Skip to content

Commit 934f9d7

Browse files
authored
Merge pull request #3 from tdilber/spring-boot-3.x
Spring boot 3.x
2 parents fd80c7c + a145b87 commit 934f9d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2263
-71
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ You can find the sample code from: https://github.com/tdilber/spring-jpa-dynamic
9999
<dependency>
100100
<groupId>io.github.tdilber</groupId>
101101
<artifactId>spring-boot-starter-jpa-dynamic-query</artifactId>
102-
<version>0.1.0</version>
102+
<version>0.2.0</version>
103103
</dependency>
104104
```
105105

@@ -108,7 +108,7 @@ You can find the sample code from: https://github.com/tdilber/spring-jpa-dynamic
108108
<dependency>
109109
<groupId>io.github.tdilber</groupId>
110110
<artifactId>spring-jpa-dynamic-query</artifactId>
111-
<version>0.4.0</version>
111+
<version>0.5.0</version>
112112
</dependency>
113113
```
114114

@@ -169,7 +169,7 @@ more multi value operators with java code touches.
169169

170170
#### Comparable Operators
171171

172-
This operator is used to compare numbers and dates. Available Java Types are **Date, Double, Long, LocalDate,
172+
This operator is used to compare numbers and dates. Available Java Types are **Timestamp, Date, Double, Long, LocalDate,
173173
ZonedDateTime, Instant, Integer**.
174174

175175
The following operators are available:

pom.xml

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

1414
<groupId>io.github.tdilber</groupId>
1515
<artifactId>spring-boot-starter-jpa-dynamic-query</artifactId>
16-
<version>0.1.0</version>
16+
<version>0.2.0</version>
1717
<packaging>jar</packaging>
1818
<name>Spring Jpa Dynamic Query</name>
1919
<description>Spring Jpa Dynamic Query (JDQ) Project</description>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.beyt.jdq.exception;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
/**
6+
* Created by tdilber at 14-Seo-2024
7+
*/
8+
@Slf4j
9+
public class DynamicQueryIllegalArgumentException extends IllegalArgumentException {
10+
11+
public DynamicQueryIllegalArgumentException(String errorMessage) {
12+
super(errorMessage);
13+
log.error(errorMessage, this);
14+
}
15+
16+
public DynamicQueryIllegalArgumentException(String errorMessage, Throwable err) {
17+
super(errorMessage, err);
18+
log.error(errorMessage, err);
19+
}
20+
}

src/main/java/com/beyt/jdq/query/DynamicQueryManager.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.beyt.jdq.dto.Criteria;
77
import com.beyt.jdq.dto.DynamicQuery;
88
import com.beyt.jdq.dto.enums.CriteriaOperator;
9+
import com.beyt.jdq.exception.DynamicQueryIllegalArgumentException;
910
import com.beyt.jdq.query.rule.specification.*;
1011
import com.beyt.jdq.repository.DynamicSpecificationRepositoryImpl;
1112
import com.beyt.jdq.util.ApplicationContextUtil;
@@ -33,6 +34,7 @@
3334
import jakarta.persistence.criteria.*;
3435
import java.lang.reflect.Field;
3536
import java.lang.reflect.Method;
37+
import java.lang.reflect.RecordComponent;
3638
import java.util.*;
3739
import java.util.stream.Collectors;
3840
import java.util.stream.Stream;
@@ -171,6 +173,9 @@ private static <ResultType> void extractIfJdqModel(DynamicQuery dynamicQuery, Cl
171173
List<Pair<String, String>> select = new ArrayList<>();
172174
for (Field declaredField : resultTypeClass.getDeclaredFields()) {
173175
if (declaredField.isAnnotationPresent(JdqIgnoreField.class)) {
176+
if (resultTypeClass.isRecord()) {
177+
throw new DynamicQueryIllegalArgumentException("Record class can not have @JdqIgnoreField annotation");
178+
}
174179
continue;
175180
}
176181

@@ -307,27 +312,38 @@ protected static long executeCountQuery(TypedQuery<Long> query) {
307312

308313
protected static <ResultType> Iterable<ResultType> convertResultToResultTypeList(List<Pair<String, String>> querySelects, Class<ResultType> resultTypeClass, Iterable<Tuple> entityListBySelectableFilter, boolean isPage) {
309314
Map<Integer, Method> setterMethods = new HashMap<>();
310-
for (int i = 0; i < querySelects.size(); i++) {
311-
String select = querySelects.get(i).getSecond();
315+
if (!resultTypeClass.isRecord()) {
316+
for (int i = 0; i < querySelects.size(); i++) {
317+
String select = querySelects.get(i).getSecond();
312318

313-
Optional<Method> methodOptional = Arrays.stream(resultTypeClass.getMethods())
314-
.filter(c -> c.getName().equalsIgnoreCase("set" + select)
315-
&& c.getParameterCount() == 1).findFirst();
319+
Optional<Method> methodOptional = Arrays.stream(resultTypeClass.getMethods())
320+
.filter(c -> c.getName().equalsIgnoreCase("set" + select)
321+
&& c.getParameterCount() == 1).findFirst();
316322

317-
if (methodOptional.isPresent()) {
318-
setterMethods.put(i, methodOptional.get());
323+
if (methodOptional.isPresent()) {
324+
setterMethods.put(i, methodOptional.get());
325+
}
319326
}
320327
}
321328
Stream<Tuple> stream = isPage ? ((Page<Tuple>) entityListBySelectableFilter).stream() : ((List<Tuple>) entityListBySelectableFilter).stream();
322329

323330
List<ResultType> resultTypeList = stream.map(t -> {
324331
try {
325-
ResultType resultObj = resultTypeClass.getConstructor().newInstance();
326-
327-
for (Map.Entry<Integer, Method> entry : setterMethods.entrySet()) {
328-
entry.getValue().invoke(resultObj, t.get(entry.getKey()));
332+
if (resultTypeClass.isRecord()) {
333+
Object[] args = new Object[querySelects.size()];
334+
for (int i = 0; i < querySelects.size(); i++) {
335+
args[i] = t.get(i);
336+
}
337+
return resultTypeClass.getDeclaredConstructor(Arrays.stream(resultTypeClass.getRecordComponents())
338+
.map(RecordComponent::getType)
339+
.toArray(Class[]::new)).newInstance(args);
340+
} else {
341+
ResultType resultObj = resultTypeClass.getConstructor().newInstance();
342+
for (Map.Entry<Integer, Method> entry : setterMethods.entrySet()) {
343+
entry.getValue().invoke(resultObj, t.get(entry.getKey()));
344+
}
345+
return resultObj;
329346
}
330-
return resultObj;
331347
} catch (Exception e) {
332348
return null;
333349
}

src/main/java/com/beyt/jdq/util/ApplicationContextUtil.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,20 @@
1010

1111
public class ApplicationContextUtil implements ApplicationContextAware {
1212
private static ApplicationContext applicationContext;
13-
private static IEntityManagerProvider entityManagerProvider;
14-
private static IDeserializer deserializer;
15-
1613
@Override
1714
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
1815
ApplicationContextUtil.applicationContext = applicationContext;
19-
ApplicationContextUtil.entityManagerProvider = applicationContext.getBean(IEntityManagerProvider.class);
20-
ApplicationContextUtil.deserializer = applicationContext.getBean(IDeserializer.class);
2116
}
2217

2318
public static ApplicationContext getApplicationContext() {
2419
return applicationContext;
2520
}
2621

2722
public static EntityManager getEntityManager(){
28-
return entityManagerProvider.provide();
23+
return applicationContext.getBean(IEntityManagerProvider.class).provide();
2924
}
3025

3126
public static IDeserializer getDeserializer() {
32-
return deserializer;
27+
return applicationContext.getBean(IDeserializer.class);
3328
}
3429
}

src/main/java/com/beyt/jdq/util/field/FieldUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Date;
1010
import java.util.HashMap;
1111
import java.util.Map;
12+
import java.sql.Timestamp;
1213

1314
/**
1415
* Created by tdilber at 11/17/2020
@@ -25,6 +26,7 @@ private FieldUtil() {
2526
fieldHelperMap.put(String.class, new StringFieldHelper());
2627
fieldHelperMap.put(Boolean.class, new BooleanFieldHelper());
2728
fieldHelperMap.put(Date.class, new DateFieldHelper());
29+
fieldHelperMap.put(Timestamp.class, new TimestampFieldHelper());
2830
fieldHelperMap.put(Double.class, new DoubleFieldHelper());
2931
fieldHelperMap.put(Long.class, new LongFieldHelper());
3032
fieldHelperMap.put(LocalDate.class, new LocalDateFieldHelper());
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.beyt.jdq.util.field.helper;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.text.DateFormat;
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import java.sql.Timestamp;
9+
10+
/**
11+
* Created by tdilber at 11/17/2020
12+
*/
13+
@Slf4j
14+
public class TimestampFieldHelper implements IFieldHelper<Timestamp> {
15+
@Override
16+
public Timestamp fillRandom() {
17+
return new Timestamp(System.currentTimeMillis() + random.nextInt(1000000000));
18+
}
19+
20+
@Override
21+
public Timestamp fillValue(String value) {
22+
DateFormat dateFormat = new SimpleDateFormat();
23+
try {
24+
return new Timestamp(dateFormat.parse(value).getTime());
25+
} catch (ParseException e) {
26+
throw new IllegalStateException(e.getMessage(), e);
27+
}
28+
}
29+
30+
@Override
31+
public String createGeneratorCode(String value) {
32+
return "new Timestamp(" + fillValue(value).getTime() + "L)";
33+
}
34+
}

0 commit comments

Comments
 (0)