Search in sources :

Example 91 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class CoherenceUpdateExecution method updateChildObject.

private void updateChildObject(Table t) throws TranslatorException {
    List<ForeignKey> fks = t.getForeignKeys();
    ForeignKey fk = fks.get(0);
    Table parentTable = fk.getParent();
    // the name of the method to obtain the collection is the nameInSource of the foreginKey
    String parentToChildMethod = fk.getNameInSource();
    if (parentToChildMethod == null) {
        // $NON-NLS-1$
        final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.noNameInSourceForForeingKey", new Object[] { fk.getName() });
        throw new TranslatorException(msg);
    }
    // there must only be 1 column in the primary key
    String parentColName = visitor.getNameFromElement(fk.getPrimaryKey().getColumns().get(0));
    List<SetClause> updateList = ((Update) command).getChanges();
    Condition criteria = ((Update) command).getWhere();
    ColumnReference leftElement;
    Expression rightExpr;
    String nameLeftElement;
    Object valueRightExpr;
    // API).
    for (int i = 0; i < updateList.size(); i++) {
        SetClause setClause = updateList.get(i);
        // trust that connector API is right and left side
        // will always be an IElement
        leftElement = setClause.getSymbol();
        // call utility method to get NameInSource/Name for element
        nameLeftElement = visitor.getNameFromElement(leftElement.getMetadataObject());
        // get right expression - if it is not a literal we
        // can't handle that so throw an exception
        rightExpr = setClause.getValue();
        // if (!(rightExpr instanceof Literal)) {
        // final String msg = CoherencePlugin.Util.getString("LDAPUpdateExecution.valueNotLiteralError",nameLeftElement); //$NON-NLS-1$
        // throw new TranslatorException(msg);
        // }
        valueRightExpr = ((Literal) rightExpr).getValue();
    // add in the modification as a replacement - meaning
    // any existing value(s) for this attribute will
    // be replaced by the new value.  If the attribute
    // didn't exist, it will automatically be created
    // TODO - since null is a valid attribute
    // value, we don't do any special handling of it right
    // now.  But maybe null should mean to delete an
    // attribute?
    }
}
Also used : Condition(org.teiid.language.Condition) Table(org.teiid.metadata.Table) ForeignKey(org.teiid.metadata.ForeignKey) Update(org.teiid.language.Update) Expression(org.teiid.language.Expression) TranslatorException(org.teiid.translator.TranslatorException) SetClause(org.teiid.language.SetClause) ColumnReference(org.teiid.language.ColumnReference)

Example 92 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class CoherenceUpdateExecution method executeDelete.

private void executeDelete() throws TranslatorException {
    DeleteVisitor visitor = new DeleteVisitor(metadata);
    visitor.visitNode((Delete) command);
    if (visitor.getException() != null) {
        throw visitor.getException();
    }
    if (visitor.getKeys() == null || visitor.getKeys().isEmpty()) {
        // $NON-NLS-1$
        final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.objectNotDeleted", new Object[] { visitor.getTableName() });
        throw new TranslatorException(msg);
    }
    for (java.util.Iterator it = visitor.getKeys().iterator(); it.hasNext(); ) {
        Object key = it.next();
        try {
            this.connection.remove(key);
        } catch (ResourceException e) {
            throw new TranslatorException(e);
        }
    }
}
Also used : TranslatorException(org.teiid.translator.TranslatorException) ResourceException(javax.resource.ResourceException) DeleteVisitor(org.teiid.translator.coherence.visitor.DeleteVisitor)

Example 93 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class ParmHolder method setValue.

private Object setValue(ParmHolder holder, Object cachedObject, Object value, int level) throws TranslatorException {
    // if there are muliple nameNodes,
    // then do "get" for all but the last node, then set
    // if a "container" type object is encountered, then create the object type for the table and add it to the container
    // if a map, do a put
    // if a collection, do an add
    final String columnName = holder.nameNodes.get(level);
    boolean atTheBottom = false;
    if (holder.nodeSize == (level + 1))
        atTheBottom = true;
    if (!atTheBottom) {
        final Object containerObject = ObjectSourceMethodManager.getValue("get" + columnName, cachedObject);
        if (containerObject.getClass().isArray() || containerObject instanceof Collection || containerObject instanceof Map) {
            return cachedObject;
        }
    }
    ArrayList argTypes = new ArrayList(1);
    argTypes.add(holder.attributeType);
    Method m = ObjectSourceMethodManager.getMethod(cachedObject.getClass(), "set" + columnName, argTypes);
    Class[] setTypes = m.getParameterTypes();
    Object newValue = null;
    if (value instanceof Collection || value instanceof Map || value.getClass().isArray()) {
        newValue = value;
    } else {
        try {
            newValue = DataTypeManager.transformValue(value, setTypes[0]);
        } catch (TransformationException e) {
            // TODO Auto-generated catch block
            throw new TranslatorException(e);
        }
    }
    ObjectSourceMethodManager.executeMethod(m, cachedObject, new Object[] { newValue });
    // $NON-NLS-1$
    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Set value " + newValue);
    return newValue;
}
Also used : TransformationException(org.teiid.core.types.TransformationException) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TranslatorException(org.teiid.translator.TranslatorException) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Example 94 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class ParmHolder method createObject.

public Object createObject(List<ColumnReference> columnList, List<Expression> valueList, CoherenceVisitor visitor, Table t) throws TranslatorException {
    if (columnList.size() != valueList.size()) {
        throw new TranslatorException("Error:  columns.size and values.size are not the same.");
    }
    // create the new object that will either be added as a top level object or added to the parent container
    String tableName = visitor.getNameFromTable(t);
    Object newObject = createObjectFromMetadata(tableName);
    for (int i = 0; i < columnList.size(); i++) {
        final ColumnReference insertElement = columnList.get(i);
        if (!insertElement.getMetadataObject().isUpdatable())
            continue;
        final String elementName = visitor.getNameFromElement(insertElement.getMetadataObject());
        final Object value = valueList.get(i);
        Object val;
        if (value instanceof Literal) {
            Literal literalValue = (Literal) value;
            val = literalValue.getValue();
            // .toString();
            if (null != val && val instanceof String) {
                // !val.isEmpty()) {
                val = stripQutes((String) val);
            }
        } else {
            val = value;
        // .toString();
        }
        setValue(tableName, elementName, newObject, val, insertElement.getType());
    }
    return newObject;
}
Also used : Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

Example 95 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class ObjectSourceMethodManager method getMethod.

public static Method getMethod(Class api, String methodName, List argumentsClasses) throws TranslatorException {
    try {
        Method[] namedMethods = findMethods(methodName, getMethodsFromAPI(api));
        if (namedMethods != null && namedMethods.length == 1) {
            return namedMethods[0];
        }
        Method m = findBestMethod(api, methodName, argumentsClasses);
        if (m == null) {
            throw new NoSuchMethodException(CoherencePlugin.Util.getString("ObjectSourceMethodManager.No_method_implemented_for", // $NON-NLS-1$
            new Object[] { methodName, api.getName() }));
        }
        return m;
    } catch (NoSuchMethodException nsme) {
        String msg = CoherencePlugin.Util.getString("ObjectSourceMethodManager.No_method_implemented_for", new Object[] { methodName, api.getName() });
        // $NON-NLS-1$
        throw new TranslatorException(msg);
    } catch (Exception e) {
        // $NON-NLS-1$
        final String msg = CoherencePlugin.Util.getString("ObjectSourceMethodManager.No_method_implemented_for", new Object[] { methodName, api.getName() });
        throw new TranslatorException(msg);
    }
}
Also used : TranslatorException(org.teiid.translator.TranslatorException) Method(java.lang.reflect.Method) TranslatorException(org.teiid.translator.TranslatorException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

TranslatorException (org.teiid.translator.TranslatorException)227 ArrayList (java.util.ArrayList)51 Column (org.teiid.metadata.Column)47 List (java.util.List)32 Table (org.teiid.metadata.Table)30 IOException (java.io.IOException)26 SQLException (java.sql.SQLException)26 ResourceException (javax.resource.ResourceException)26 Test (org.junit.Test)16 Expression (org.teiid.language.Expression)16 Literal (org.teiid.language.Literal)16 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)16 Blob (java.sql.Blob)15 Argument (org.teiid.language.Argument)13 DBObject (com.mongodb.DBObject)11 HashMap (java.util.HashMap)11 ColumnReference (org.teiid.language.ColumnReference)11 ExecutionContext (org.teiid.translator.ExecutionContext)11 BasicDBObject (com.mongodb.BasicDBObject)10 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10