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?
}
}
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);
}
}
}
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;
}
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;
}
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);
}
}
Aggregations