diff --git a/commons-data-impl/src/main/java/com/appjars/saturn/dao/jpa/ConstraintTransformerJpaImpl.java b/commons-data-impl/src/main/java/com/appjars/saturn/dao/jpa/ConstraintTransformerJpaImpl.java index b4e9d6e..97fd4e1 100644 --- a/commons-data-impl/src/main/java/com/appjars/saturn/dao/jpa/ConstraintTransformerJpaImpl.java +++ b/commons-data-impl/src/main/java/com/appjars/saturn/dao/jpa/ConstraintTransformerJpaImpl.java @@ -53,25 +53,57 @@ private Expression getExpression(AttributeConstraint c) { @SuppressWarnings("unchecked") private Expression getExpression(AttributeConstraint c, Class type) { Expression expression = root.get(c.getAttribute()); - expression.getJavaType().asSubclass(type); + boxed(expression.getJavaType()).asSubclass(type); return (Expression) expression; } - + + private static Class boxed(Class type) { + if (type.isPrimitive()) { + if (type==boolean.class) return Boolean.class; + if (type==int.class) return Integer.class; + if (type==long.class) return Long.class; + if (type==byte.class) return Byte.class; + if (type==short.class) return Short.class; + if (type==char.class) return Character.class; + if (type==float.class) return Float.class; + if (type==double.class) return Double.class; + } + return type; + } + @Override protected Predicate transformNegatedConstraint(NegatedConstraint c) { return criteriaBuilder.not(transform(c.getConstraint())); } @Override - @SuppressWarnings({ "rawtypes", "unchecked" }) protected Predicate transformRelationalConstraint(AttributeRelationalConstraint c) { - c.getAttribute(); - Expression x = getExpression(c, Comparable.class); - Comparable y = c.getValue(); + switch (c.getOperator()) { + case RelationalConstraint.EQ: + case RelationalConstraint.NE: + return transformEqualityConstraint(c); + default: + return transformComparisonConstraint(c); + } + } + + private Predicate transformEqualityConstraint(AttributeRelationalConstraint c) { + Expression x = getExpression(c); + Object y = (Comparable) c.getValue(); switch (c.getOperator()) { case RelationalConstraint.EQ: return criteriaBuilder.equal(x, y); case RelationalConstraint.NE: return criteriaBuilder.notEqual(x, y); + default: return null; + } + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private Predicate transformComparisonConstraint(AttributeRelationalConstraint c) { + Expression x = getExpression(c, Comparable.class); + Comparable y = (Comparable) c.getValue(); + + switch (c.getOperator()) { case RelationalConstraint.LE: return criteriaBuilder.lessThanOrEqualTo(x, y); case RelationalConstraint.LT: return criteriaBuilder.lessThan(x, y); case RelationalConstraint.GE: return criteriaBuilder.greaterThan(x, y); @@ -79,7 +111,7 @@ protected Predicate transformRelationalConstraint(AttributeRelationalConstraint default: return null; } } - + @Override protected Predicate transformLikeConstraint(AttributeLikeConstraint c) { return criteriaBuilder.like(getExpression(c, String.class), c.getPattern()); diff --git a/commons-data-impl/src/test/java/com/appjars/saturn/dao/PersonCreationDao.java b/commons-data-impl/src/test/java/com/appjars/saturn/dao/PersonCreationDao.java deleted file mode 100644 index badc46e..0000000 --- a/commons-data-impl/src/test/java/com/appjars/saturn/dao/PersonCreationDao.java +++ /dev/null @@ -1,32 +0,0 @@ -/*- - * #%L - * Commons Backend - Data Access Layer Implementations - * %% - * Copyright (C) 2020 - 2021 Flowing Code - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ -package com.appjars.saturn.dao; - -import com.appjars.saturn.model.impl.Person; - -public class PersonCreationDao implements CreationDao { - - @Override - public Integer save(Person entity) { - entity.setId(((int)Math.random() * 10000)); - return entity.getId(); - } - -} diff --git a/commons-data-impl/src/test/resources/META-INF/persistence.xml b/commons-data-impl/src/test/resources/META-INF/persistence.xml index 8a96598..75d8592 100644 --- a/commons-data-impl/src/test/resources/META-INF/persistence.xml +++ b/commons-data-impl/src/test/resources/META-INF/persistence.xml @@ -26,7 +26,7 @@ xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> - com.appjars.saturn.model.Person + com.appjars.saturn.model.impl.Person diff --git a/commons-data/src/test/java/com/appjars/saturn/dao/PersonCreationDao.java b/commons-data/src/test/java/com/appjars/saturn/dao/PersonCreationDao.java deleted file mode 100644 index 9576bf5..0000000 --- a/commons-data/src/test/java/com/appjars/saturn/dao/PersonCreationDao.java +++ /dev/null @@ -1,32 +0,0 @@ -/*- - * #%L - * Commons Backend - Data Access Interfaces - * %% - * Copyright (C) 2020 - 2021 Flowing Code - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ -package com.appjars.saturn.dao; - -import com.appjars.saturn.model.Person; - -public class PersonCreationDao implements CreationDao { - - @Override - public Integer save(Person entity) { - entity.setId(((int)Math.random() * 10000)); - return entity.getId(); - } - -} diff --git a/commons-data/src/test/java/com/appjars/saturn/dao/PersonCrudDao.java b/commons-data/src/test/java/com/appjars/saturn/dao/PersonCrudDao.java deleted file mode 100644 index 48ec196..0000000 --- a/commons-data/src/test/java/com/appjars/saturn/dao/PersonCrudDao.java +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * #%L - * Commons Backend - Data Access Interfaces - * %% - * Copyright (C) 2020 - 2021 Flowing Code - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ -package com.appjars.saturn.dao; - -import java.util.List; -import java.util.Optional; - -import com.appjars.saturn.model.QuerySpec; -import com.appjars.saturn.model.Person; - -public class PersonCrudDao implements CrudDao { - - @Override - public Integer save(Person entity) { - // TODO Auto-generated method stub - return null; - } - - @Override - public void delete(Person entity) { - // TODO Auto-generated method stub - - } - - @Override - public Optional findById(Integer id) { - // TODO Auto-generated method stub - return null; - } - - @Override - public List findAll() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void update(Person entity) { - // TODO Auto-generated method stub - } - - @Override - public List filter(QuerySpec filter) { - // TODO Auto-generated method stub - return null; - } - - @Override - public long count(QuerySpec filter) { - // TODO Auto-generated method stub - return 0; - } - -} diff --git a/commons-data/src/test/java/com/appjars/saturn/model/Person.java b/commons-data/src/test/java/com/appjars/saturn/model/Person.java deleted file mode 100644 index fd3c7a4..0000000 --- a/commons-data/src/test/java/com/appjars/saturn/model/Person.java +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * #%L - * Commons Backend - Data Access Interfaces - * %% - * Copyright (C) 2020 - 2021 Flowing Code - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ -package com.appjars.saturn.model; - -import java.util.Date; - -@SuppressWarnings("serial") -public class Person extends BaseEntity { - - private Integer id; - private String name; - private String lastName; - private Date birthDay; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public Date getBirthDay() { - return birthDay; - } - - public void setBirthDay(Date birthDay) { - this.birthDay = birthDay; - } - - @Override - public Integer getId() { - return id; - } - - @Override - public void setId(Integer id) { - this.id = id; - } - -} diff --git a/commons-model/src/main/java/com/appjars/saturn/model/ConstraintBuilder.java b/commons-model/src/main/java/com/appjars/saturn/model/ConstraintBuilder.java index 10dd645..65c3605 100644 --- a/commons-model/src/main/java/com/appjars/saturn/model/ConstraintBuilder.java +++ b/commons-model/src/main/java/com/appjars/saturn/model/ConstraintBuilder.java @@ -37,11 +37,11 @@ public Constraint not(Constraint c) { return new NegatedConstraint(c); } - public Constraint equal(String attribute, Comparable value) { + public Constraint equal(String attribute, Object value) { return new AttributeRelationalConstraint(attribute, value, RelationalConstraint.EQ); } - public Constraint notEqual(String attribute, Comparable value) { + public Constraint notEqual(String attribute, Object value) { return new AttributeRelationalConstraint(attribute, value, RelationalConstraint.NE); } diff --git a/commons-model/src/main/java/com/appjars/saturn/model/constraints/AttributeRelationalConstraint.java b/commons-model/src/main/java/com/appjars/saturn/model/constraints/AttributeRelationalConstraint.java index 2e42afc..15ec2f0 100644 --- a/commons-model/src/main/java/com/appjars/saturn/model/constraints/AttributeRelationalConstraint.java +++ b/commons-model/src/main/java/com/appjars/saturn/model/constraints/AttributeRelationalConstraint.java @@ -31,7 +31,7 @@ public class AttributeRelationalConstraint implements AttributeConstraint, RelationalConstraint { @NonNull String attribute; - @NonNull Comparable value; + @NonNull Object value; @NonNull String operator; }