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.batch.item.database.AbstractCursorItemReader.java

protected void initializeConnection() {
    Assert.state(getDataSource() != null, "DataSource must not be null.");

    try {//from   w  w  w . ja v  a  2 s.  c o  m
        if (useSharedExtendedConnection) {
            if (!(getDataSource() instanceof ExtendedConnectionDataSourceProxy)) {
                throw new InvalidDataAccessApiUsageException(
                        "You must use a ExtendedConnectionDataSourceProxy for the dataSource when "
                                + "useSharedExtendedConnection is set to true.");
            }
            this.con = DataSourceUtils.getConnection(dataSource);
            ((ExtendedConnectionDataSourceProxy) dataSource).startCloseSuppression(this.con);
        } else {
            this.con = dataSource.getConnection();
        }
    } catch (SQLException se) {
        close();
        throw getExceptionTranslator().translate("Executing query", getSql(), se);
    }
}

From source file:org.springframework.batch.item.database.JdbcBatchItemWriter.java

/**
 * Check mandatory properties - there must be a SimpleJdbcTemplate and an SQL statement plus a
 * parameter source.//from  w  w w  . jav a2  s .  c  om
 */
@Override
public void afterPropertiesSet() {
    Assert.notNull(namedParameterJdbcTemplate, "A DataSource or a NamedParameterJdbcTemplate is required.");
    Assert.notNull(sql, "An SQL statement is required.");
    List<String> namedParameters = new ArrayList<String>();
    parameterCount = JdbcParameterUtils.countParameterPlaceholders(sql, namedParameters);
    if (namedParameters.size() > 0) {
        if (parameterCount != namedParameters.size()) {
            throw new InvalidDataAccessApiUsageException(
                    "You can't use both named parameters and classic \"?\" placeholders: " + sql);
        }
        usingNamedParameters = true;
    }
    if (!usingNamedParameters) {
        Assert.notNull(itemPreparedStatementSetter,
                "Using SQL statement with '?' placeholders requires an ItemPreparedStatementSetter");
    }
}

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

protected <T> void doInsertList(List<? 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  w  ww . j  a v  a2  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()) {
        doInsertList(entry.getKey(), entry.getValue(), this.mongoConverter);
    }
}

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

public <T> void remove(String collectionName, final Query query, Class<T> targetClass) {
    if (query == null) {
        throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null");
    }/*  ww w.ja va 2  s .co m*/
    final DBObject queryObject = query.getQueryObject();
    final MongoPersistentEntity<?> entity = getPersistentEntity(targetClass);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("remove using query: " + queryObject + " in collection: " + collectionName);
    }
    execute(collectionName, new CollectionCallback<Void>() {
        public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            DBObject dboq = mapper.getMappedObject(queryObject, entity);
            WriteResult wr = null;
            if (writeConcern == null) {
                wr = collection.remove(dboq);
            } else {
                wr = collection.remove(dboq, writeConcern);
            }
            handleAnyWriteResultErrors(wr, dboq, "remove");
            return null;
        }
    });
}

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

/**
 * Substitutes the id key if it is found in he query. Any 'id' keys will be replaced with '_id' and the value converted
 * to an ObjectId if possible. This conversion should match the way that the id fields are converted during read
 * operations.//w w  w .  j ava2  s  .com
 *
 * @param query
 * @param targetClass
 * @param reader
 */
protected void substituteMappedIdIfNecessary(DBObject query, Class<?> targetClass, MongoReader<?> reader) {
    MongoConverter converter = null;
    if (reader instanceof SimpleMongoConverter) {
        converter = (MongoConverter) reader;
    } else if (reader instanceof MappingMongoConverter) {
        converter = (MappingMongoConverter) reader;
    } else {
        return;
    }
    String idKey = null;
    if (query.containsField("id")) {
        idKey = "id";
    }
    if (query.containsField("_id")) {
        idKey = "_id";
    }
    if (idKey == null) {
        // no ids in this query
        return;
    }
    MongoPropertyDescriptor descriptor;
    try {
        MongoPropertyDescriptor mpd = new MongoPropertyDescriptor(new PropertyDescriptor(idKey, targetClass),
                targetClass);
        descriptor = mpd;
    } catch (IntrospectionException e) {
        // no property descriptor for this key - try the other
        try {
            String theOtherIdKey = "id".equals(idKey) ? "_id" : "id";
            MongoPropertyDescriptor mpd2 = new MongoPropertyDescriptor(
                    new PropertyDescriptor(theOtherIdKey, targetClass), targetClass);
            descriptor = mpd2;
        } catch (IntrospectionException e2) {
            // no property descriptor for this key either - bail
            return;
        }
    }
    if (descriptor.isIdProperty() && descriptor.isOfIdType()) {
        Object value = query.get(idKey);
        if (value instanceof DBObject) {
            DBObject dbo = (DBObject) value;
            if (dbo.containsField("$in")) {
                List<Object> ids = new ArrayList<Object>();
                int count = 0;
                for (Object o : (Object[]) dbo.get("$in")) {
                    count++;
                    ObjectId newValue = convertIdValue(converter, o);
                    if (newValue != null) {
                        ids.add(newValue);
                    }
                }
                if (ids.size() > 0 && ids.size() != count) {
                    throw new InvalidDataAccessApiUsageException("Inconsistent set of id values provided "
                            + Arrays.asList((Object[]) dbo.get("$in")));
                }
                if (ids.size() > 0) {
                    dbo.removeField("$in");
                    dbo.put("$in", ids.toArray());
                }
            }
            query.removeField(idKey);
            query.put(MongoPropertyDescriptor.ID_KEY, value);
        } else {
            ObjectId newValue = convertIdValue(converter, value);
            query.removeField(idKey);
            if (newValue != null) {
                query.put(MongoPropertyDescriptor.ID_KEY, newValue);
            } else {
                query.put(MongoPropertyDescriptor.ID_KEY, value);
            }
        }
    }
}

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

private String determineCollectionName(Class<?> clazz) {

    if (clazz == null) {
        throw new InvalidDataAccessApiUsageException(
                "No class parameter provided, entity collection can't be determined for " + clazz);
    }//  ww w . j a  v  a2  s . com

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

From source file:org.springframework.data.elasticsearch.core.query.Criteria.java

/**
 * Crates new CriteriaEntry for {@code RANGE [lowerBound TO upperBound]}
 * //www  .j  a  v  a2  s  . co  m
 * @param lowerBound
 * @param upperBound
 * @return
 */
public Criteria between(Object lowerBound, Object upperBound) {
    if (lowerBound == null && upperBound == null) {
        throw new InvalidDataAccessApiUsageException("Range [* TO *] is not allowed");
    }

    criteria.add(new CriteriaEntry(OperationKey.BETWEEN, new Object[] { lowerBound, upperBound }));
    return this;
}

From source file:org.springframework.data.elasticsearch.core.query.Criteria.java

/**
 * Crates new CriteriaEntry for multiple values {@code (arg0 arg1 arg2 ...)}
 * /*from w w w . j  av a 2s .co  m*/
 * @param values
 * @return
 */
public Criteria in(Object... values) {
    if (values.length == 0 || (values.length > 1 && values[1] instanceof Collection)) {
        throw new InvalidDataAccessApiUsageException("At least one element "
                + (values.length > 0 ? ("of argument of type " + values[1].getClass().getName()) : "")
                + " has to be present.");
    }
    return in(Arrays.asList(values));
}

From source file:org.springframework.data.gemfire.listener.adapter.ContinuousQueryListenerAdapter.java

/**
 * Standard {@link ContinuousQueryListener} entry point.
 * <p>Delegates the event to the target listener method, with appropriate
 * conversion of the event argument. In case of an exception, the
 * {@link #handleListenerException(Throwable)} method will be invoked.
 * //from w  ww . j  a v a 2s . c om
 * @param event the incoming GemFire event
 * @see #handleListenerException
 */
@SuppressWarnings("unchecked")
public void onEvent(CqEvent event) {
    try {

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

        // Regular case: find a handler method reflectively.
        String methodName = getListenerMethodName(event);
        if (invoker == null) {
            invoker = new MethodInvoker(delegate, methodName);
        }
        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.");
        }

        invokeListenerMethod(event, methodName);
    } catch (Throwable th) {
        handleListenerException(th);
    }
}

From source file:org.springframework.data.jdbc.support.oracle.SqlStructValue.java

/**
  * The implementation for this specific type. This method is called internally by the
  * Spring Framework during the out parameter processing and it's not accessed by application
  * code directly.//from w w  w . ja  va 2s. c  o m
  * @see org.springframework.jdbc.core.support.AbstractSqlTypeValue
  */
protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException {
    if (typeName == null && defaultTypeName == null) {
        throw new InvalidDataAccessApiUsageException(
                "The typeName is null in this context. Consider setting the defaultTypeName.");
    }
    Struct struct = mapper.toStruct(this.source, conn, typeName != null ? typeName : defaultTypeName);
    return struct;
}