|
17 | 17 | import java.lang.reflect.ParameterizedType; |
18 | 18 | import java.lang.reflect.Type; |
19 | 19 | import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
20 | 21 | import java.util.Collection; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.logging.Logger; |
23 | 24 |
|
24 | 25 | /** |
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 |
28 | 28 | */ |
29 | 29 | @Service |
30 | 30 | 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) { |
385 | 385 |
|
386 | 386 | public void afterRemove(T entity) { |
387 | 387 | } |
| 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 | + } |
388 | 407 |
|
389 | 408 | } |
0 commit comments