Example usage for org.springframework.data.repository.core RepositoryMetadata getIdType

List of usage examples for org.springframework.data.repository.core RepositoryMetadata getIdType

Introduction

In this page you can find the example usage for org.springframework.data.repository.core RepositoryMetadata getIdType.

Prototype

Class<?> getIdType();

Source Link

Document

Returns the id class the given class is declared for.

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/* w  w w  . j a  va  2s  .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;
                }
            });
}