Example usage for org.springframework.dao InvalidDataAccessApiUsageException InvalidDataAccessApiUsageException

List of usage examples for org.springframework.dao InvalidDataAccessApiUsageException InvalidDataAccessApiUsageException

Introduction

In this page you can find the example usage for org.springframework.dao InvalidDataAccessApiUsageException InvalidDataAccessApiUsageException.

Prototype

public InvalidDataAccessApiUsageException(String msg) 

Source Link

Document

Constructor for InvalidDataAccessApiUsageException.

Usage

From source file:org.springframework.data.keyvalue.redis.listener.adapter.MessageListenerAdapter.java

/**
 * Standard Redis {@link MessageListener} entry point.
 * <p>Delegates the message to the target listener method, with appropriate
 * conversion of the message argument. In case of an exception, the
 * {@link #handleListenerException(Throwable)} method will be invoked.
 * //from  www .  java 2s.c o m
 * @param message the incoming Redis message
 * @see #handleListenerException
 */
@Override
@SuppressWarnings("unchecked")
public void onMessage(Message message, byte[] pattern) {
    try {

        // Check whether the delegate is a MessageListener impl itself.
        // In that case, the adapter will simply act as a pass-through.
        if (delegate != this) {
            if (delegate instanceof MessageListener) {
                ((MessageListener) delegate).onMessage(message, pattern);
            }
        }

        // Regular case: find a handler method reflectively.
        Object convertedMessage = extractMessage(message);
        String methodName = getListenerMethodName(message, convertedMessage);
        if (methodName == null) {
            throw new InvalidDataAccessApiUsageException("No default listener method specified: "
                    + "Either specify a non-null value for the 'defaultListenerMethod' property or "
                    + "override the 'getListenerMethodName' method.");
        }

        // Invoke the handler method with appropriate arguments.
        Object[] listenerArguments = buildListenerArguments(convertedMessage);
        invokeListenerMethod(methodName, listenerArguments);
    } catch (Throwable th) {
        handleListenerException(th);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

@SuppressWarnings("unchecked")
public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String collectionName) {

    if (near == null) {
        throw new InvalidDataAccessApiUsageException("NearQuery must not be null!");
    }/*www  . j a  v  a2 s  . c om*/

    if (entityClass == null) {
        throw new InvalidDataAccessApiUsageException("Entity class must not be null!");
    }

    String collection = StringUtils.hasText(collectionName) ? collectionName
            : determineCollectionName(entityClass);
    BasicDBObject command = new BasicDBObject("geoNear", collection);
    command.putAll(near.toDBObject());

    CommandResult commandResult = executeCommand(command);
    List<Object> results = (List<Object>) commandResult.get("results");
    results = results == null ? Collections.emptyList() : results;

    DbObjectCallback<GeoResult<T>> callback = new GeoNearResultDbObjectCallback<T>(
            new ReadDbObjectCallback<T>(mongoConverter, entityClass), near.getMetric());
    List<GeoResult<T>> result = new ArrayList<GeoResult<T>>(results.size());

    for (Object element : results) {
        result.add(callback.doWith((DBObject) element));
    }

    DBObject stats = (DBObject) commandResult.get("stats");
    double averageDistance = stats == null ? 0 : (Double) stats.get("avgDistance");
    return new GeoResults<T>(result, new Distance(averageDistance, near.getMetric()));
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected <T> void doInsertAll(Collection<? extends T> listToSave, MongoWriter<T> writer) {
    Map<String, List<T>> objs = new HashMap<String, List<T>>();

    for (T o : listToSave) {

        MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(o.getClass());
        if (entity == null) {
            throw new InvalidDataAccessApiUsageException(
                    "No Persitent Entity information found for the class " + o.getClass().getName());
        }//from ww w . j  a va2 s.  c  o  m
        String collection = entity.getCollection();

        List<T> objList = objs.get(collection);
        if (null == objList) {
            objList = new ArrayList<T>();
            objs.put(collection, objList);
        }
        objList.add(o);

    }

    for (Map.Entry<String, List<T>> entry : objs.entrySet()) {
        doInsertBatch(entry.getKey(), entry.getValue(), this.mongoConverter);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

private void assertUpdateableIdIfNotSet(Object entity) {

    MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entity.getClass());
    MongoPersistentProperty idProperty = persistentEntity.getIdProperty();

    if (idProperty == null) {
        return;/*from  www  .  j  a va  2 s .co m*/
    }

    ConversionService service = mongoConverter.getConversionService();
    Object idValue = BeanWrapper.create(entity, service).getProperty(idProperty, Object.class, true);

    if (idValue == null && !MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(idProperty.getType())) {
        throw new InvalidDataAccessApiUsageException(
                String.format("Cannot autogenerate id of type %s for entity of type %s!",
                        idProperty.getType().getName(), entity.getClass().getName()));
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected <T> void doRemove(final String collectionName, final Query query, final Class<T> entityClass) {
    if (query == null) {
        throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null");
    }//from w  ww .  j a  v a 2 s. com
    final DBObject queryObject = query.getQueryObject();
    final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
    execute(collectionName, new CollectionCallback<Void>() {
        public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            DBObject dboq = mapper.getMappedObject(queryObject, entity);
            WriteResult wr = null;
            MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName,
                    entityClass, null, queryObject);
            WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("remove using query: " + dboq + " in collection: " + collection.getName());
            }
            if (writeConcernToUse == null) {
                wr = collection.remove(dboq);
            } else {
                wr = collection.remove(dboq, writeConcernToUse);
            }
            handleAnyWriteResultErrors(wr, dboq, "remove");
            return null;
        }
    });
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected String replaceWithResourceIfNecessary(String function) {

    String func = function;/*w  ww  .ja va2  s. c o m*/

    if (this.resourceLoader != null && ResourceUtils.isUrl(function)) {

        Resource functionResource = resourceLoader.getResource(func);

        if (!functionResource.exists()) {
            throw new InvalidDataAccessApiUsageException(String.format("Resource %s not found!", function));
        }

        try {
            return new Scanner(functionResource.getInputStream()).useDelimiter("\\A").next();
        } catch (IOException e) {
            throw new InvalidDataAccessApiUsageException(
                    String.format("Cannot read map-reduce file %s!", function), e);
        }
    }

    return func;
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

private DBObject copyQuery(Query query, DBObject copyMapReduceOptions) {
    if (query != null) {
        if (query.getSkip() != 0 || query.getFieldsObject() != null) {
            throw new InvalidDataAccessApiUsageException(
                    "Can not use skip or field specification with map reduce operations");
        }// www.  j a  v a 2 s  .c o  m
        if (query.getQueryObject() != null) {
            copyMapReduceOptions.put("query", query.getQueryObject());
        }
        if (query.getLimit() > 0) {
            copyMapReduceOptions.put("limit", query.getLimit());
        }
        if (query.getSortObject() != null) {
            copyMapReduceOptions.put("sort", query.getSortObject());
        }
    }
    return copyMapReduceOptions;
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

String determineCollectionName(Class<?> entityClass) {

    if (entityClass == null) {
        throw new InvalidDataAccessApiUsageException(
                "No class parameter provided, entity collection can't be determined!");
    }/* w ww  .  ja va  2s .c o m*/

    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
    if (entity == null) {
        throw new InvalidDataAccessApiUsageException(
                "No Persitent Entity information found for the class " + entityClass.getName());
    }
    return entity.getCollection();
}

From source file:org.springframework.data.neo4j.support.Neo4jTemplate.java

@Override
public Node getNode(long id) {
    if (id < 0)
        throw new InvalidDataAccessApiUsageException("id is negative");
    try {/*from w  ww  . j ava 2 s . c o  m*/
        return infrastructure.getGraphDatabase().getNodeById(id);
    } catch (RuntimeException e) {
        throw translateExceptionIfPossible(e);
    }
}

From source file:org.springframework.data.neo4j.support.Neo4jTemplate.java

@Override
public Relationship getRelationship(long id) {
    if (id < 0)
        throw new InvalidDataAccessApiUsageException("id is negative");
    try {//from   ww w.  j  av  a2s.com
        return infrastructure.getGraphDatabase().getRelationshipById(id);
    } catch (RuntimeException e) {
        throw translateExceptionIfPossible(e);
    }
}