Search in sources :

Example 61 with TranslatorException

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

the class FileExecutionFactory method createProcedureExecution.

// @Override
public ProcedureExecution createProcedureExecution(final Call command, final ExecutionContext executionContext, final RuntimeMetadata metadata, final Connection conn) throws TranslatorException {
    if (conn instanceof VirtualFileConnection) {
        return new VirtualFileProcedureExecution(command, (VirtualFileConnection) conn);
    }
    final FileConnection fc = (FileConnection) conn;
    if (command.getProcedureName().equalsIgnoreCase(SAVEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                Object file = command.getArguments().get(1).getArgumentValue().getValue();
                if (file == null || filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Saving", filePath);
                InputStream is = null;
                try {
                    if (file instanceof SQLXML) {
                        is = ((SQLXML) file).getBinaryStream();
                    } else if (file instanceof Clob) {
                        is = new ReaderInputStream(((Clob) file).getCharacterStream(), encoding);
                    } else if (file instanceof Blob) {
                        is = ((Blob) file).getBinaryStream();
                    } else {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("unknown_type"));
                    }
                    ObjectConverterUtil.write(is, fc.getFile(filePath));
                } catch (IOException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (SQLException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                }
            }

            @Override
            public void close() {
            }

            @Override
            public void cancel() throws TranslatorException {
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                return null;
            }

            @Override
            public List<?> getOutputParameterValues() throws TranslatorException {
                return Collections.emptyList();
            }
        };
    } else if (command.getProcedureName().equalsIgnoreCase(DELETEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                if (filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Deleting", filePath);
                try {
                    File f = fc.getFile(filePath);
                    if (!f.exists()) {
                        if (exceptionIfFileNotFound) {
                            // $NON-NLS-1$
                            throw new TranslatorException(DataPlugin.Util.gs("file_not_found", filePath));
                        }
                    } else if (!f.delete()) {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("error_deleting"));
                    }
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_deleting"));
                }
            }

            @Override
            public void close() {
            }

            @Override
            public void cancel() throws TranslatorException {
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                return null;
            }

            @Override
            public List<?> getOutputParameterValues() throws TranslatorException {
                return Collections.emptyList();
            }
        };
    }
    return new FileProcedureExecution(command, fc);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SQLXML(java.sql.SQLXML) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ProcedureExecution(org.teiid.translator.ProcedureExecution) TranslatorException(org.teiid.translator.TranslatorException) ResourceException(javax.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Clob(java.sql.Clob) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) VirtualFileConnection(org.teiid.file.VirtualFileConnection) VirtualFileConnection(org.teiid.file.VirtualFileConnection) FileConnection(org.teiid.translator.FileConnection)

Example 62 with TranslatorException

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

the class LanguageBridgeFactory method translate.

/* Execute */
Call translate(StoredProcedure sp) {
    Procedure proc = null;
    if (sp.getProcedureID() != null) {
        try {
            proc = this.metadataFactory.getProcedure(sp.getGroup().getName());
        } catch (TranslatorException e) {
            throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30486, e);
        }
    }
    Class<?> returnType = null;
    List<Argument> translatedParameters = new ArrayList<Argument>();
    for (SPParameter param : sp.getParameters()) {
        Direction direction = Direction.IN;
        switch(param.getParameterType()) {
            case ParameterInfo.IN:
                direction = Direction.IN;
                break;
            case ParameterInfo.INOUT:
                direction = Direction.INOUT;
                break;
            case ParameterInfo.OUT:
                direction = Direction.OUT;
                break;
            case ParameterInfo.RESULT_SET:
                // already part of the metadata
                continue;
            case ParameterInfo.RETURN_VALUE:
                returnType = param.getClassType();
                continue;
        }
        if (param.isUsingDefault() && BaseColumn.OMIT_DEFAULT.equalsIgnoreCase(metadataFactory.getMetadata().getExtensionProperty(param.getMetadataID(), BaseColumn.DEFAULT_HANDLING, false))) {
            continue;
        }
        ProcedureParameter metadataParam = metadataFactory.getParameter(param);
        // we can assume for now that all arguments will be literals, which may be multivalued
        org.teiid.language.Expression value = null;
        if (direction != Direction.OUT) {
            if (param.isVarArg()) {
                ArrayImpl av = (ArrayImpl) ((Constant) param.getExpression()).getValue();
                if (av != null) {
                    for (Object obj : av.getValues()) {
                        Argument arg = new Argument(direction, new Literal(obj, param.getClassType().getComponentType()), param.getClassType().getComponentType(), metadataParam);
                        translatedParameters.add(arg);
                    }
                }
                break;
            }
            value = translate(param.getExpression());
        }
        Argument arg = new Argument(direction, value, param.getClassType(), metadataParam);
        translatedParameters.add(arg);
    }
    Call call = new Call(removeSchemaName(sp.getProcedureName()), translatedParameters, proc);
    call.setReturnType(returnType);
    return call;
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) ArrayImpl(org.teiid.core.types.ArrayImpl) org.teiid.language(org.teiid.language) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) Direction(org.teiid.language.Argument.Direction) Procedure(org.teiid.metadata.Procedure) TranslatorException(org.teiid.translator.TranslatorException)

Example 63 with TranslatorException

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

the class ConnectorManager method buildCapabilities.

private BasicSourceCapabilities buildCapabilities(ExecutionFactory<Object, Object> translator) throws TranslatorException {
    if (translator.isSourceRequiredForCapabilities()) {
        Object connection = null;
        Object connectionFactory = null;
        try {
            connectionFactory = getConnectionFactory();
            if (connectionFactory != null) {
                connection = translator.getConnection(connectionFactory, null);
            }
            if (connection == null) {
                // $NON-NLS-1$);
                throw new TranslatorException(QueryPlugin.Event.TEIID31108, QueryPlugin.Util.getString("datasource_not_found", getConnectionName()));
            }
            if (connection instanceof WrappedConnection) {
                try {
                    connection = ((WrappedConnection) connection).unwrap();
                } catch (ResourceException e) {
                    throw new TranslatorException(QueryPlugin.Event.TEIID30477, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30477, getConnectionName()));
                }
            }
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Initializing the capabilities for", translatorName);
            synchronized (executionFactory) {
                executionFactory.initCapabilities(connection);
            }
        } finally {
            if (connection != null) {
                translator.closeConnection(connection, connectionFactory);
            }
        }
    }
    BasicSourceCapabilities resultCaps = CapabilitiesConverter.convertCapabilities(translator, id);
    return resultCaps;
}
Also used : BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) TranslatorException(org.teiid.translator.TranslatorException) ResourceException(javax.resource.ResourceException) WrappedConnection(org.teiid.resource.spi.WrappedConnection)

Example 64 with TranslatorException

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

the class LDAPQueryExecution method convertSingleValue.

private Object convertSingleValue(Column modelElement, String modelAttrName, Class<?> modelAttrClass, Object objResult) throws TranslatorException, InvalidNameException {
    if (objResult == null) {
        return null;
    }
    // return null.
    if (!(objResult instanceof String)) {
        return objResult;
    }
    String strResult = (String) objResult;
    // There is no way to differentiate between being unset and being the empty string.
    if (strResult.equals("")) {
        // $NON-NLS-1$
        return null;
    }
    // MPW: 3-11-07: Added support for java.lang.Integer conversion.
    if (TypeFacility.RUNTIME_TYPES.TIMESTAMP.equals(modelAttrClass)) {
        String timestampFormat = modelElement.getFormat();
        if (timestampFormat == null) {
            timestampFormat = LDAPConnectorConstants.ldapTimestampFormat;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(timestampFormat);
        try {
            Date dateResult = dateFormat.parse(strResult);
            Timestamp tsResult = new Timestamp(dateResult.getTime());
            return tsResult;
        } catch (ParseException pe) {
            // $NON-NLS-1$
            throw new TranslatorException(pe, LDAPPlugin.Util.getString("LDAPSyncQueryExecution.timestampParseFailed", modelAttrName));
        }
    // TODO: Extend support for more types in the future.
    // Specifically, add support for byte arrays, since that's actually supported
    // in the underlying data source.
    }
    // extract rdn
    String type = modelElement.getProperty(LDAPExecutionFactory.RDN_TYPE, false);
    if (type != null) {
        String prefix = modelElement.getProperty(LDAPExecutionFactory.DN_PREFIX, false);
        LdapName name = new LdapName(strResult);
        if (prefix != null) {
            if (!name.getPrefix(name.size() - 1).toString().equals(prefix)) {
                throw new InvalidNameException();
            }
        } else if (name.size() > 1) {
            throw new InvalidNameException();
        }
        Rdn rdn = name.getRdn(name.size() - 1);
        if (!rdn.getType().equals(type)) {
            throw new InvalidNameException();
        }
        return rdn.getValue();
    }
    // the Teiid type conversion logic will handle refine from here if necessary
    return strResult;
}
Also used : InvalidNameException(javax.naming.InvalidNameException) TranslatorException(org.teiid.translator.TranslatorException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) Rdn(javax.naming.ldap.Rdn) Date(java.util.Date) LdapName(javax.naming.ldap.LdapName)

Example 65 with TranslatorException

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

the class LDAPQueryExecution method getValue.

/**
 * Add Result to Row
 * @param modelElement the model element
 * @param attrs the attributes
 * @param row the row
 * @throws InvalidNameException
 */
// GHH 20080326 - added resultDistinguishedName to method signature.  If
// there is an element in the model named "DN" and there is no attribute
// with this name in the search result, we return this new parameter
// value for that column in the result
// GHH 20080326 - added handling of ClassCastException when non-string
// attribute is returned
private Object getValue(Column modelElement, SearchResult result, Attributes attrs, boolean unwrap) throws TranslatorException, InvalidNameException {
    String modelAttrName = modelElement.getSourceName();
    Class<?> modelAttrClass = modelElement.getJavaType();
    String multivalAttr = modelElement.getDefaultValue();
    if (modelAttrName == null) {
        // $NON-NLS-1$
        final String msg = LDAPPlugin.Util.getString("LDAPSyncQueryExecution.nullAttrError");
        throw new TranslatorException(msg);
    }
    Attribute resultAttr = attrs.get(modelAttrName);
    // If the attribute is not present, we return NULL.
    if (resultAttr == null) {
        // if DN attribute is not present in search result
        if (modelAttrName.equalsIgnoreCase("DN")) {
            // $NON-NLS-1$
            return result.getNameInNamespace();
        }
        return null;
    }
    Object objResult = null;
    try {
        if (TypeFacility.RUNTIME_TYPES.STRING.equals(modelAttrClass) && MULTIVALUED_CONCAT.equalsIgnoreCase(multivalAttr)) {
            // mpw 5/09
            // Order the multi-valued attrs alphabetically before creating a single string,
            // using the delimiter to separate each token
            ArrayList<String> multivalList = new ArrayList<String>();
            NamingEnumeration<?> attrNE = resultAttr.getAll();
            int length = 0;
            while (attrNE.hasMore()) {
                String val = (String) attrNE.next();
                multivalList.add(val);
                length += ((val == null ? 0 : val.length()) + 1);
            }
            Collections.sort(multivalList);
            StringBuilder multivalSB = new StringBuilder(length);
            Iterator<String> itr = multivalList.iterator();
            while (itr.hasNext()) {
                multivalSB.append(itr.next());
                if (itr.hasNext()) {
                    multivalSB.append(delimiter);
                }
            }
            return multivalSB.toString();
        }
        if (modelAttrClass.isArray()) {
            return getArray(modelAttrClass.getComponentType(), resultAttr, modelElement, modelAttrName);
        }
        if (unwrap && resultAttr.size() > 1) {
            return getArray(modelAttrClass, resultAttr, modelElement, modelAttrName);
        }
        // just a single value
        objResult = resultAttr.get();
    } catch (NamingException ne) {
        // $NON-NLS-1$m
        final String msg = LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12004, modelAttrName) + " : " + ne.getExplanation();
        LogManager.logWarning(LogConstants.CTX_CONNECTOR, msg);
        throw new TranslatorException(ne, msg);
    }
    return convertSingleValue(modelElement, modelAttrName, modelAttrClass, objResult);
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException) NamingException(javax.naming.NamingException)

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