Skip to content

Commit 5e7cf94

Browse files
author
rafael-pestano
committed
Fixes #30
1 parent bb640d6 commit 5e7cf94

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ test-output/
4545

4646
#linux te files
4747
*~
48+
/.apt_generated/

src/main/java/com/github/adminfaces/persistence/service/CrudService.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
import java.lang.reflect.ParameterizedType;
1818
import java.lang.reflect.Type;
1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Collection;
2122
import java.util.List;
2223
import java.util.logging.Logger;
2324

2425
/**
25-
* @author <a href="http://github.com/rmpestano">Rafael Pestano</a>
26-
*
27-
* Template service for CRUD operations on top of a JPA entity
26+
* @author rmpestano
27+
* Utility service for crud operations
2828
*/
2929
@Service
3030
public class CrudService<T extends PersistenceEntity, PK extends Serializable> extends CriteriaSupportHandler<T> implements CriteriaSupport<T>, Serializable {
@@ -385,5 +385,24 @@ public void beforeRemove(T entity) {
385385

386386
public void afterRemove(T entity) {
387387
}
388+
389+
/**
390+
* Creates an array of Ids (pks) from a list of entities.
391+
* It is useful when working with `in clauses` on DeltaSpike criteria
392+
* because the API only support primitive arrays.
393+
*
394+
* @param entities list of entities to create
395+
* @param idsType the type of the pk list, e.g new Long[0]
396+
*
397+
* @return primitive array containing entities pks.
398+
*/
399+
@SuppressWarnings("unchecked")
400+
protected <ID extends Serializable> ID[] toListOfIds(Collection<? extends PersistenceEntity> entities, ID[] idsType) {
401+
List<ID> ids = new ArrayList<>();
402+
for (PersistenceEntity entity : entities) {
403+
ids.add((ID) entity.getId());
404+
}
405+
return (ID[]) ids.toArray(idsType);
406+
}
388407

389408
}

src/test/java/com/github/adminfaces/persistence/CrudServiceIt.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import javax.inject.Inject;
1515
import java.util.ArrayList;
16+
import java.util.Arrays;
1617
import java.util.List;
1718

1819
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
@@ -60,6 +61,19 @@ public void shouldFindCarByExample() {
6061
.extracting("id")
6162
.contains(new Integer(1));
6263
}
64+
65+
@Test
66+
@DataSet("cars.yml")
67+
public void shouldFindCarsByListOfIds() {
68+
Car ferrari = new Car(1);
69+
Car mustang = new Car(2);
70+
List<Car> carsToFind = Arrays.asList(ferrari,mustang);
71+
List<Car> cars = carService.findCarsInList(carsToFind);
72+
assertThat(cars).isNotNull()
73+
.hasSize(2)
74+
.extracting("id")
75+
.contains(new Integer(1), new Integer(2));
76+
}
6377

6478
@Test
6579
public void shouldNotInsertCarWithoutName() {

src/test/java/com/github/adminfaces/persistence/service/CarService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public Double getTotalPriceByModel(Car car) {
107107
}
108108
return carRepository.getTotalPriceByModel(car.getModel().toUpperCase());
109109
}
110+
111+
public List<Car> findCarsInList(List<Car> carsToFind) {
112+
return criteria().in(Car_.id, toListOfIds(carsToFind, new Integer[0])).getResultList();
113+
}
110114

111115
@Override
112116
@Transactional

0 commit comments

Comments
 (0)