Java tutorial
/* * Copyright 2013 Marcelo Morales me@marcelomorales.name * * 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. */ package name.marcelomorales.siqisiqi.openjpa.spring; import name.marcelomorales.siqisiqi.openjpa.api.OrmFinderSettings; import org.apache.openjpa.persistence.criteria.ComparisonStyle; import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; import org.springframework.data.repository.core.RepositoryMetadata; import javax.persistence.EntityManager; import javax.persistence.criteria.Path; import javax.persistence.criteria.Root; import javax.persistence.metamodel.Attribute; import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.SingularAttribute; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * @author Marcelo Morales * Since: 9/18/13 */ public class OpenJpaRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory { private EntityManager entityManager; public OpenJpaRepositoryFactory(EntityManager entityManager) { super(entityManager); this.entityManager = entityManager; } protected Object getTargetRepository(final RepositoryMetadata metadata) { return new OpenJpaRepositoryImpl<>((Class<T>) metadata.getDomainType(), entityManager, new OrmFinderSettings<T, I>() { @Override 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; } }); } protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { return OpenJpaRepository.class; } }