Skip to content

Commit

Permalink
fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-pestano committed Oct 3, 2019
1 parent fe065ee commit 4d1e48e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.logging.Logger;
import org.apache.deltaspike.data.impl.criteria.QueryCriteria;

Expand Down Expand Up @@ -234,7 +232,7 @@ public Criteria example(Criteria criteria, T example, Attribute<T, ?>... usingAt
}

if (usingAttributes == null || usingAttributes.length == 0) {
throw new RuntimeException("Please provide attributes to example criteria.");
usingAttributes = resolveEntityAttributes(example);
}

for (Attribute<T, ?> usingAttribute : usingAttributes) {
Expand All @@ -249,6 +247,27 @@ public Criteria example(Criteria criteria, T example, Attribute<T, ?>... usingAt
return criteria;
}

private Attribute<T, ?>[] resolveEntityAttributes(T example) {
Set<Attribute<?, ?>> attributes = (Set<Attribute<?, ?>>) getEntityManager().getMetamodel().entity(example.getClass()).getAttributes();
if(attributes != null && !attributes.isEmpty()) {
return attributes.toArray(new Attribute[0]);
}
return Collections.emptyList().toArray(new Attribute[0]);
}

private SingularAttribute<T, String>[] resolveEntitySingularStringAttributes(T example) {
Set<SingularAttribute<?, ?>> singularAttributes = (Set<SingularAttribute<?, ?>>) getEntityManager().getMetamodel().entity(example.getClass()).getSingularAttributes();
List<SingularAttribute<T, String>> stringAttributes = new ArrayList<>();
if(singularAttributes != null && !singularAttributes.isEmpty()) {
for (SingularAttribute<?, ?> singularAttribute : singularAttributes) {
if(singularAttribute.getType().getJavaType().isAssignableFrom(String.class)) {
stringAttributes.add((SingularAttribute<T, String>) singularAttribute);
}
}
}
return stringAttributes.toArray(new SingularAttribute[0]);
}


private void addEqExampleRestriction(Criteria criteria, T example, Attribute<T, ?> attribute) {
if (attribute.getJavaMember() instanceof Field) {
Expand Down Expand Up @@ -325,7 +344,7 @@ public Criteria exampleLike(T example, SingularAttribute<T, String>... usingAttr
public Criteria exampleLike(Criteria criteria, T example, SingularAttribute<T, String>... usingAttributes) {

if (usingAttributes == null || usingAttributes.length == 0) {
throw new RuntimeException("Please provide attributes to example criteria.");
usingAttributes = resolveEntitySingularStringAttributes(example);
}

if (criteria == null) {
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/com/github/adminfaces/persistence/CrudServiceIt.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,23 @@ public void shouldFindCarsByExample() {
.contains("porche avenger");
}

@Test(expected = RuntimeException.class)
@DataSet("cars.yml")
public void shouldNotFindCarsByExampleWithoutAttributes() {
public void shouldFindCarsByExampleWithoutPassingAttributes() {
Car carExample = new Car().model("Ferrari");
List<Car> cars = crudService.example(carExample).getResultList();
assertThat(cars).isNotNull().hasSize(1)
.extracting("model")
.contains("Ferrari");
}

@Test
@DataSet("cars.yml")
public void shouldFindCarsByExampleLikeWithoutPassingAttributes() {
Car carExample = new Car().model("porche").name("%avenger");
List<Car> cars = crudService.exampleLike(carExample).getResultList();
assertThat(cars).isNotNull().hasSize(1)
.extracting("name")
.contains("porche avenger");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@ApplicationScoped
public class EntityManagerProducer {


@Produces
public EntityManager produce() {
return EntityManagerProvider.instance("persistenceDB").em();
Expand Down

0 comments on commit 4d1e48e

Please sign in to comment.