Example usage for javax.persistence.metamodel EntityType getSingularAttribute

List of usage examples for javax.persistence.metamodel EntityType getSingularAttribute

Introduction

In this page you can find the example usage for javax.persistence.metamodel EntityType getSingularAttribute.

Prototype

SingularAttribute<? super X, ?> getSingularAttribute(String name);

Source Link

Document

Return the single-valued attribute of the managed type that corresponds to the specified name.

Usage

From source file:name.marcelomorales.siqisiqi.openjpa.spring.OpenJpaRepositoryFactory.java

protected Object getTargetRepository(final RepositoryMetadata metadata) {
    return new OpenJpaRepositoryImpl<>((Class<T>) metadata.getDomainType(), entityManager,
            new OrmFinderSettings<T, I>() {

                @Override/*from www .j  a v a 2  s  .co  m*/
                public boolean returnsNullIfTermsAreNull() {
                    final Class<?> repositoryInterface = metadata.getRepositoryInterface();
                    if (!repositoryInterface.isAnnotationPresent(OpenJpaSettings.class)) {
                        return super.returnsNullIfTermsAreNull();
                    }

                    final OpenJpaSettings annotation = repositoryInterface.getAnnotation(OpenJpaSettings.class);
                    return annotation.returnsNullIfTermsAreNull();
                }

                @Override
                public Iterable<Path<String>> getFullTexts(Root<T> from, Class<T> aClass) {
                    final Class<?> repositoryInterface = metadata.getRepositoryInterface();
                    if (!repositoryInterface.isAnnotationPresent(OpenJpaSettings.class)) {
                        return super.getFullTexts(from, aClass);
                    }

                    final OpenJpaSettings annotation = repositoryInterface.getAnnotation(OpenJpaSettings.class);
                    String[] fulltexts = annotation.fullTexts();
                    List<Path<String>> paths = new ArrayList<>(fulltexts.length);
                    for (String fulltext : fulltexts) {
                        paths.add(from.<String>get(fulltext));
                    }
                    return paths;
                }

                @Override
                public ComparisonStyle getComparisonStyle() {
                    final Class<?> repositoryInterface = metadata.getRepositoryInterface();
                    if (!repositoryInterface.isAnnotationPresent(OpenJpaSettings.class)) {
                        return new ComparisonStyle.Default();
                    }

                    final OpenJpaSettings annotation = repositoryInterface.getAnnotation(OpenJpaSettings.class);

                    final ComparisonStyle.Default aDefault = new ComparisonStyle.Default();
                    aDefault.setDisjunction(annotation.disjunction());
                    aDefault.setExcludeDefault(annotation.excludeDefault());
                    aDefault.setExcludeIdentity(annotation.excludeIdentity());
                    aDefault.setExcludeNull(annotation.excludeNull());
                    aDefault.setExcludeVersion(annotation.excludeVersion());
                    aDefault.setStringComparisonMode(annotation.stringComparisonMode());
                    return aDefault;
                }

                @Override
                public Path<I> countPath(Root<T> from, Class<T> aClass) {
                    final Class<I> idType = (Class<I>) metadata.getIdType();
                    final SingularAttribute<T, I> declaredId = from.getModel().getDeclaredId(idType);
                    return from.get(declaredId);
                }

                @Override
                public Attribute<?, ?>[] betweens(Root<T> from, Class<T> aClass) {
                    final Class<?> repositoryInterface = metadata.getRepositoryInterface();
                    if (!repositoryInterface.isAnnotationPresent(OpenJpaSettings.class)) {
                        return super.betweens(from, aClass);
                    }

                    final EntityType<T> model = from.getModel();
                    final OpenJpaSettings annotation = repositoryInterface.getAnnotation(OpenJpaSettings.class);
                    String[] betweens = annotation.betweens();
                    Attribute[] attributes = new Attribute[betweens.length];
                    for (int i = 0; i < betweens.length; i++) {
                        attributes[i] = model.getSingularAttribute(betweens[i]);
                    }
                    return attributes;
                }
            });
}

From source file:org.apache.openejb.util.proxy.QueryProxy.java

private <T> Query createFinderQuery(final EntityManager entityManager, final String methodName,
        final Class<T> entityType, final Object[] args) {
    final List<String> conditions = parseMethodName(methodName);

    final EntityType<T> et = entityManager.getMetamodel().entity(entityType);
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    CriteriaQuery<Object> query = cb.createQuery();
    final Root<T> from = query.from(entityType);
    query = query.select(from);//from  w ww .j a  v a  2 s.co m

    int i = 0;
    Predicate where = null;
    for (final String condition : conditions) {
        final SingularAttribute<? super T, ?> attribute = et.getSingularAttribute(condition);
        final Path<?> path = from.get(attribute);
        final Class<?> javaType = attribute.getType().getJavaType();

        final Predicate currentClause;
        if (javaType.equals(String.class)) {
            currentClause = cb.like((Expression<String>) path, (String) args[i++]);
        } else if (Number.class.isAssignableFrom(javaType) || javaType.isPrimitive()) {
            currentClause = cb.equal(path, args[i++]);
        } else {
            LOGGER.warning("field " + condition + " not found, ignoring");
            continue;
        }

        if (where == null) {
            where = currentClause;
        } else {
            where = cb.and(where, currentClause);
        }
    }

    if (where != null) {
        query = query.where(where);
    }

    // pagination
    final TypedQuery<?> emQuery = entityManager.createQuery(query);
    if (args != null && args.length == conditions.size() + 2 && isInt(args[args.length - 2].getClass())
            && isInt(args[args.length - 1].getClass())) {
        final int first = (Integer) args[args.length - 2];
        final int max = (Integer) args[args.length - 1];

        emQuery.setFirstResult(first);
        emQuery.setMaxResults(max);
    }

    return emQuery;
}

From source file:org.apache.ranger.service.XTrxLogService.java

private Predicate generatePredicate(SearchCriteria searchCriteria, EntityManager em,
        CriteriaBuilder criteriaBuilder, Root<VXXTrxLog> rootEntityType) {
    Predicate predicate = criteriaBuilder.conjunction();
    Map<String, Object> paramList = searchCriteria.getParamList();
    if (CollectionUtils.isEmpty(paramList)) {
        return predicate;
    }//  w w  w  .j a  v  a2  s  .  c o m

    Metamodel entityMetaModel = em.getMetamodel();
    EntityType<VXXTrxLog> entityType = entityMetaModel.entity(VXXTrxLog.class);

    for (Map.Entry<String, Object> entry : paramList.entrySet()) {
        String key = entry.getKey();
        for (SearchField searchField : searchFields) {
            if (!key.equalsIgnoreCase(searchField.getClientFieldName())) {
                continue;
            }

            String fieldName = searchField.getFieldName();
            if (!StringUtils.isEmpty(fieldName)) {
                fieldName = fieldName.contains(".") ? fieldName.substring(fieldName.indexOf(".") + 1)
                        : fieldName;
            }

            Object paramValue = entry.getValue();
            boolean isListValue = false;
            if (paramValue != null && paramValue instanceof Collection) {
                isListValue = true;
            }

            // build where clause depending upon given parameters
            if (SearchField.DATA_TYPE.STRING.equals(searchField.getDataType())) {
                // build where clause for String datatypes
                SingularAttribute attr = entityType.getSingularAttribute(fieldName);
                if (attr != null) {
                    Predicate stringPredicate = null;
                    if (SearchField.SEARCH_TYPE.PARTIAL.equals(searchField.getSearchType())) {
                        String val = "%" + paramValue + "%";
                        stringPredicate = criteriaBuilder.like(rootEntityType.get(attr), val);
                    } else {
                        stringPredicate = criteriaBuilder.equal(rootEntityType.get(attr), paramValue);
                    }
                    predicate = criteriaBuilder.and(predicate, stringPredicate);
                }

            } else if (SearchField.DATA_TYPE.INT_LIST.equals(searchField.getDataType())
                    || isListValue && SearchField.DATA_TYPE.INTEGER.equals(searchField.getDataType())) {
                // build where clause for integer lists or integers datatypes
                Collection<Number> intValueList = null;
                if (paramValue != null && (paramValue instanceof Integer || paramValue instanceof Long)) {
                    intValueList = new ArrayList<Number>();
                    intValueList.add((Number) paramValue);
                } else {
                    intValueList = (Collection<Number>) paramValue;
                }
                for (Number value : intValueList) {
                    SingularAttribute attr = entityType.getSingularAttribute(fieldName);
                    if (attr != null) {
                        Predicate intPredicate = criteriaBuilder.equal(rootEntityType.get(attr), value);
                        predicate = criteriaBuilder.and(predicate, intPredicate);
                    }
                }

            } else if (SearchField.DATA_TYPE.DATE.equals(searchField.getDataType())) {
                // build where clause for date datatypes
                Date fieldValue = (Date) paramList.get(searchField.getClientFieldName());
                if (fieldValue != null && searchField.getCustomCondition() == null) {
                    SingularAttribute attr = entityType.getSingularAttribute(fieldName);
                    Predicate datePredicate = null;
                    if (SearchField.SEARCH_TYPE.LESS_THAN.equals(searchField.getSearchType())) {
                        datePredicate = criteriaBuilder.lessThan(rootEntityType.get(attr), fieldValue);
                    } else if (SearchField.SEARCH_TYPE.LESS_EQUAL_THAN.equals(searchField.getSearchType())) {
                        datePredicate = criteriaBuilder.lessThanOrEqualTo(rootEntityType.get(attr), fieldValue);
                    } else if (SearchField.SEARCH_TYPE.GREATER_THAN.equals(searchField.getSearchType())) {
                        datePredicate = criteriaBuilder.greaterThan(rootEntityType.get(attr), fieldValue);
                    } else if (SearchField.SEARCH_TYPE.GREATER_EQUAL_THAN.equals(searchField.getSearchType())) {
                        datePredicate = criteriaBuilder.greaterThanOrEqualTo(rootEntityType.get(attr),
                                fieldValue);
                    } else {
                        datePredicate = criteriaBuilder.equal(rootEntityType.get(attr), fieldValue);
                    }
                    predicate = criteriaBuilder.and(predicate, datePredicate);
                }
            }
        }
    }
    return predicate;
}