Search in sources :

Example 1 with Property

use of org.apache.deltaspike.data.impl.property.Property in project deltaspike by apache.

the class EntityRepositoryHandler method executeExampleQuery.

private List<E> executeExampleQuery(E example, int start, int max, boolean useLikeOperator, SingularAttribute<E, ?>... attributes) {
    // return a empty list instead of all results
    if (isEmpty(attributes)) {
        return findAll(start, max);
    }
    List<Property<Object>> properties = extractProperties(attributes);
    String jpqlQuery = exampleQuery(allQuery(), properties, useLikeOperator);
    log.log(Level.FINER, "findBy|findByLike: Created query {0}", jpqlQuery);
    TypedQuery<E> query = entityManager().createQuery(jpqlQuery, entityClass());
    // set starting position
    if (start > 0) {
        query.setFirstResult(start);
    }
    // set maximum results
    if (max > 0) {
        query.setMaxResults(max);
    }
    context.applyRestrictions(query);
    addParameters(query, example, properties, useLikeOperator);
    return query.getResultList();
}
Also used : QueryUtils.isString(org.apache.deltaspike.data.impl.util.QueryUtils.isString) Property(org.apache.deltaspike.data.impl.property.Property)

Example 2 with Property

use of org.apache.deltaspike.data.impl.property.Property in project deltaspike by apache.

the class TimestampsProvider method updateTimestamps.

private void updateTimestamps(Object entity, boolean create) {
    long systime = System.currentTimeMillis();
    List<Property<Object>> properties = new LinkedList<Property<Object>>();
    PropertyQuery<Object> query = PropertyQueries.<Object>createQuery(entity.getClass()).addCriteria(new AnnotatedPropertyCriteria(ModifiedOn.class));
    properties.addAll(query.getWritableResultList());
    if (create) {
        query = PropertyQueries.<Object>createQuery(entity.getClass()).addCriteria(new AnnotatedPropertyCriteria(CreatedOn.class));
        properties.addAll(query.getWritableResultList());
    }
    for (Property<Object> property : properties) {
        setProperty(entity, property, systime, create);
    }
}
Also used : ModifiedOn(org.apache.deltaspike.data.api.audit.ModifiedOn) Property(org.apache.deltaspike.data.impl.property.Property) LinkedList(java.util.LinkedList) AnnotatedPropertyCriteria(org.apache.deltaspike.data.impl.property.query.AnnotatedPropertyCriteria)

Example 3 with Property

use of org.apache.deltaspike.data.impl.property.Property in project deltaspike by apache.

the class PropertyQuery method getResultList.

/**
     * Get the result from the query, causing the query to be run.
     *
     * @param writable
     *            if this query should only return properties that are not read only
     * @return the results, or an empty list if there are no results
     */
private List<Property<V>> getResultList(boolean writable) {
    List<Property<V>> results = new ArrayList<Property<V>>();
    // First check public accessor methods (we ignore private methods)
    for (Method method : targetClass.getMethods()) {
        if (!(method.getName().startsWith("is") || method.getName().startsWith("get"))) {
            continue;
        }
        boolean match = true;
        for (PropertyCriteria c : criteria) {
            if (!c.methodMatches(method)) {
                match = false;
                break;
            }
        }
        if (match) {
            MethodProperty<V> property = Properties.<V>createProperty(method);
            if (!writable || !property.isReadOnly()) {
                results.add(property);
            }
        }
    }
    Class<?> cls = targetClass;
    while (cls != null && !cls.equals(Object.class)) {
        // Now check declared fields
        for (Field field : cls.getDeclaredFields()) {
            boolean match = true;
            for (PropertyCriteria c : criteria) {
                if (!c.fieldMatches(field)) {
                    match = false;
                    break;
                }
            }
            Property<V> prop = Properties.<V>createProperty(field);
            if (match && !resultsContainsProperty(results, prop.getName())) {
                if (!writable || !prop.isReadOnly()) {
                    results.add(prop);
                }
            }
        }
        cls = cls.getSuperclass();
    }
    return results;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MethodProperty(org.apache.deltaspike.data.impl.property.MethodProperty) Property(org.apache.deltaspike.data.impl.property.Property)

Example 4 with Property

use of org.apache.deltaspike.data.impl.property.Property in project deltaspike by apache.

the class EntityRepositoryHandler method executeCountQuery.

private Long executeCountQuery(E example, boolean useLikeOperator, SingularAttribute<E, ?>... attributes) {
    if (isEmpty(attributes)) {
        return count();
    }
    List<Property<Object>> properties = extractProperties(attributes);
    String jpqlQuery = exampleQuery(countQuery(), properties, useLikeOperator);
    log.log(Level.FINER, "count: Created query {0}", jpqlQuery);
    TypedQuery<Long> query = entityManager().createQuery(jpqlQuery, Long.class);
    addParameters(query, example, properties, useLikeOperator);
    context.applyRestrictions(query);
    return query.getSingleResult();
}
Also used : QueryUtils.isString(org.apache.deltaspike.data.impl.util.QueryUtils.isString) Property(org.apache.deltaspike.data.impl.property.Property)

Example 5 with Property

use of org.apache.deltaspike.data.impl.property.Property in project deltaspike by apache.

the class EntityRepositoryHandler method prepareWhere.

private String prepareWhere(List<Property<Object>> properties, boolean useLikeOperator) {
    Iterator<Property<Object>> iterator = properties.iterator();
    StringBuilder result = new StringBuilder();
    while (iterator.hasNext()) {
        Property<Object> property = iterator.next();
        String name = property.getName();
        if (useLikeOperator && property.getJavaClass().getName().equals(String.class.getName())) {
            result.append("UPPER(e.").append(name).append(") like :").append(name).append(iterator.hasNext() ? " and " : "");
        } else {
            result.append("e.").append(name).append(" = :").append(name).append(iterator.hasNext() ? " and " : "");
        }
    }
    return result.toString();
}
Also used : QueryUtils.isString(org.apache.deltaspike.data.impl.util.QueryUtils.isString) Property(org.apache.deltaspike.data.impl.property.Property)

Aggregations

Property (org.apache.deltaspike.data.impl.property.Property)5 QueryUtils.isString (org.apache.deltaspike.data.impl.util.QueryUtils.isString)3 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 ModifiedOn (org.apache.deltaspike.data.api.audit.ModifiedOn)1 MethodProperty (org.apache.deltaspike.data.impl.property.MethodProperty)1 AnnotatedPropertyCriteria (org.apache.deltaspike.data.impl.property.query.AnnotatedPropertyCriteria)1