Package org.springframework.data.cassandra.repository.support

Source Code of org.springframework.data.cassandra.repository.support.CassandraRepositoryFactory$CassandraQueryLookupStrategy

/*
* Copyright 2013-2014 the original author or authors
*
* 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 org.springframework.data.cassandra.repository.support;

import java.io.Serializable;
import java.lang.reflect.Method;

import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository;
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
import org.springframework.data.cassandra.repository.query.CassandraQueryMethod;
import org.springframework.data.cassandra.repository.query.StringBasedCassandraQuery;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.util.Assert;

/**
* Factory to create {@link TypedIdCassandraRepository} instances.
*
* @author Alex Shvid
* @author Matthew T. Adams
*/

public class CassandraRepositoryFactory extends RepositoryFactorySupport {

  private final CassandraOperations cassandraTemplate;
  private final CassandraMappingContext mappingContext;

  /**
   * Creates a new {@link CassandraRepositoryFactory} with the given {@link CassandraOperations}.
   *
   * @param cassandraOperations must not be {@literal null}
   */
  public CassandraRepositoryFactory(CassandraOperations cassandraOperations) {

    Assert.notNull(cassandraOperations);

    this.cassandraTemplate = cassandraOperations;
    this.mappingContext = cassandraOperations.getConverter().getMappingContext();

    // TODO: remove when supporting declarative query methods
    setQueryLookupStrategyKey(QueryLookupStrategy.Key.USE_DECLARED_QUERY);
  }

  @Override
  protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
    return SimpleCassandraRepository.class;
  }

  @Override
  @SuppressWarnings({ "rawtypes", "unchecked" })
  protected Object getTargetRepository(RepositoryMetadata metadata) {

    CassandraEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());

    return new SimpleCassandraRepository(entityInformation, cassandraTemplate);

  }

  @Override
  @SuppressWarnings("unchecked")
  public <T, ID extends Serializable> CassandraEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {

    CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(domainClass);

    if (entity == null) {
      throw new MappingException(String.format("Could not lookup mapping metadata for domain class %s!",
          domainClass.getName()));
    }

    return new MappingCassandraEntityInformation<T, ID>((CassandraPersistentEntity<T>) entity,
        cassandraTemplate.getConverter());
  }

  @Override
  protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
    return new CassandraQueryLookupStrategy();
  }

  private class CassandraQueryLookupStrategy implements QueryLookupStrategy {

    @Override
    public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {

      CassandraQueryMethod queryMethod = new CassandraQueryMethod(method, metadata, mappingContext);
      String namedQueryName = queryMethod.getNamedQueryName();

      if (namedQueries.hasQuery(namedQueryName)) {
        String namedQuery = namedQueries.getQuery(namedQueryName);
        return new StringBasedCassandraQuery(namedQuery, queryMethod, cassandraTemplate);
      } else if (queryMethod.hasAnnotatedQuery()) {
        return new StringBasedCassandraQuery(queryMethod, cassandraTemplate);
      } else {
        throw new InvalidDataAccessApiUsageException("declarative query methods are a todo");
      }
    }
  }
}
TOP

Related Classes of org.springframework.data.cassandra.repository.support.CassandraRepositoryFactory$CassandraQueryLookupStrategy

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.