Search in sources :

Example 6 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class XLSInstanceIOTest method test.

/**
 * Exports the instances created by
 * {@link XLSInstanceWriterTestExamples#createInstanceCollection} into a
 * temporary XLS file by executing {@link XLSInstanceWriter#execute}.
 * Afterwards, the schema is read by {@link XLSSchemaReader} and the
 * instances are loaded by {@link XLSInstanceReader}. Each of the imported
 * instances are compared with the original instances. In addtion, a
 * different set of instances is compared with the imported instances.
 */
@Test
public void test() {
    // set instances to xls instance writer
    XLSInstanceWriter writer = new XLSInstanceWriter();
    InstanceCollection instances = XLSInstanceWriterTestExamples.createInstanceCollection();
    IContentType contentType = HalePlatform.getContentTypeManager().getContentType("eu.esdihumboldt.hale.io.xls.xls");
    writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    File tempDir = Files.createTempDir();
    File tempFile = new File(tempDir, "data.xls");
    writer.setInstances(instances);
    try {
        // write instances to a temporary XLS file
        writer.setTarget(new FileIOSupplier(tempFile));
        writer.setContentType(contentType);
        IOReport report = writer.execute(null);
        assertTrue(report.isSuccess());
    } catch (IOProviderConfigurationException | IOException e) {
        fail("Execution of xls instance writer failed.");
    }
    // read the schema from the temporary XLS file
    XLSSchemaReader schemaReader = new XLSSchemaReader();
    schemaReader.setContentType(contentType);
    schemaReader.setSource(new FileIOSupplier(tempFile));
    schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("ItemType"));
    schemaReader.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(0));
    try {
        IOReport report = schemaReader.execute(null);
        assertTrue(report.isSuccess());
    } catch (IOProviderConfigurationException | IOException e1) {
        fail("Execution of schema reader failed.");
    }
    Schema schema = schemaReader.getSchema();
    // read the instances form the temporary XLS file
    XLSInstanceReader reader = new XLSInstanceReader();
    reader.setSourceSchema(schema);
    reader.setParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE, Value.of(true));
    reader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("ItemType"));
    reader.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    // read sheet with index 0 since there is only one sheet
    reader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(0));
    reader.setContentType(contentType);
    reader.setSource(new FileIOSupplier(tempFile));
    try {
        IOReport report = reader.execute(null);
        assertTrue(report.isSuccess());
    } catch (IOProviderConfigurationException | IOException e) {
        fail("Execution of xls instance reader failed.");
    }
    // compare size of instance collection
    InstanceCollection inst = reader.getInstances();
    assertEquals(4, inst.size());
    // check if instance collection contains current instance
    Iterator<Instance> instanceIt = inst.iterator();
    while (instanceIt.hasNext()) {
        Instance instance = instanceIt.next();
        assertTrue(contains(instances.iterator(), instance));
    }
    // other instance should be contained in the imported instances
    InstanceCollection falseInstances = XLSInstanceWriterTestExamples.createFalseTestInstanceCollection();
    instanceIt = inst.iterator();
    while (instanceIt.hasNext()) {
        Instance instance = instanceIt.next();
        assertFalse(contains(falseInstances.iterator(), instance));
    }
    // delete file and temporary directory
    tempFile.delete();
    tempDir.delete();
}
Also used : XLSInstanceWriter(eu.esdihumboldt.hale.io.xls.writer.XLSInstanceWriter) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) XLSSchemaReader(eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) XLSInstanceReader(eu.esdihumboldt.hale.io.xls.reader.XLSInstanceReader) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) Test(org.junit.Test)

Example 7 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class XLSInstanceReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    boolean skipFirst = getParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE).as(Boolean.class);
    // first sheet as default
    sheetNum = getParameter(InstanceTableIOConstants.SHEET_INDEX).as(int.class, 0);
    instances = new DefaultInstanceCollection(new ArrayList<Instance>());
    try {
        // analyze the excel sheet to get all information
        analyser = new AnalyseXLSSchemaTable(getSource().getLocation(), sheetNum);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Reading the excel sheet has failed", e));
        return reporter;
    }
    // get type definition of the schema
    type = getSourceSchema().getType(QName.valueOf(getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class)));
    // get property definition
    propAr = type.getChildren().toArray(new PropertyDefinition[type.getChildren().size()]);
    Collection<List<String>> rows = analyser.getRows();
    // skip if first row is a header
    if (!skipFirst) {
        // otherwise first line is also an instance
        createInstanceCollection(analyser.getHeader(), reporter);
        line++;
    }
    // iterate over all rows to create the instances
    Iterator<List<String>> allRows = rows.iterator();
    while (allRows.hasNext()) {
        List<String> row = allRows.next();
        createInstanceCollection(row, reporter);
        line++;
    }
    reporter.setSuccess(true);
    return reporter;
}
Also used : AnalyseXLSSchemaTable(eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable) ArrayList(java.util.ArrayList) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ArrayList(java.util.ArrayList) List(java.util.List) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 8 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class XLSSchemaReader method loadFromSource.

@Override
protected Schema loadFromSource(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    sheetNum = getParameter(InstanceTableIOConstants.SHEET_INDEX).as(int.class, 0);
    progress.begin("Load XLS/XLSX schema", ProgressIndicator.UNKNOWN);
    String namespace = "http://www.esdi-humboldt.eu/hale/xls";
    DefaultSchema schema = new DefaultSchema(namespace, getSource().getLocation());
    AnalyseXLSSchemaTable analyser;
    try {
        analyser = new AnalyseXLSSchemaTable(getSource().getLocation(), sheetNum);
        header = analyser.getHeader();
        // create type definition
        String typename = getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class);
        if (typename == null || typename.isEmpty()) {
            reporter.setSuccess(false);
            reporter.error(new IOMessageImpl("No Typename was set", null));
            return null;
        }
        DefaultTypeDefinition type = new DefaultTypeDefinition(new QName(typename));
        // constraints on main type
        type.setConstraint(MappingRelevantFlag.ENABLED);
        type.setConstraint(MappableFlag.ENABLED);
        type.setConstraint(HasValueFlag.DISABLED);
        type.setConstraint(AbstractFlag.DISABLED);
        // set metadata for main type
        type.setLocation(getSource().getLocation());
        StringBuffer defaultPropertyTypeBuffer = new StringBuffer();
        String[] comboSelections;
        if (getParameter(PARAM_PROPERTYTYPE).isEmpty()) {
            for (int i = 0; i < header.size(); i++) {
                defaultPropertyTypeBuffer.append("java.lang.String");
                defaultPropertyTypeBuffer.append(",");
            }
            defaultPropertyTypeBuffer.deleteCharAt(defaultPropertyTypeBuffer.lastIndexOf(","));
            String combs = defaultPropertyTypeBuffer.toString();
            comboSelections = combs.split(",");
        } else {
            comboSelections = getParameter(PARAM_PROPERTYTYPE).as(String.class).split(",");
        }
        String[] properties;
        if (getParameter(PARAM_PROPERTY).isEmpty()) {
            properties = header.toArray(new String[0]);
        } else {
            properties = getParameter(PARAM_PROPERTY).as(String.class).split(",");
        }
        // than the entries in the first line
        if ((header.size() != properties.length && properties.length != 0) || (header.size() != comboSelections.length && comboSelections.length != 0)) {
            fail("Not the same number of entries for property names, property types and words in the first line of the file");
        }
        for (int i = 0; i < comboSelections.length; i++) {
            PropertyType propertyType = PropertyTypeExtension.getInstance().getFactory(comboSelections[i]).createExtensionObject();
            DefaultPropertyDefinition property = new DefaultPropertyDefinition(new QName(properties[i]), type, propertyType.getTypeDefinition());
            configureProperty(property);
        }
        boolean skip = Arrays.equals(properties, header.toArray(new String[0]));
        type.setConstraint(new CSVConfiguration(CSVUtil.getSep(this), CSVUtil.getQuote(this), CSVUtil.getEscape(this), skip));
        schema.addType(type);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Cannot load xls/xlsx schema", e));
        reporter.setSuccess(false);
        return null;
    }
    reporter.setSuccess(true);
    return schema;
}
Also used : DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) AnalyseXLSSchemaTable(eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PropertyType(eu.esdihumboldt.hale.io.csv.PropertyType) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) CSVConfiguration(eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)

Example 9 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class XmlSchemaReader method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    // $NON-NLS-1$
    progress.begin(Messages.getString("ApacheSchemaProvider.21"), ProgressIndicator.UNKNOWN);
    this.reporter = reporter;
    XmlSchema xmlSchema = null;
    XmlSchemaCollection schemaCol = new XmlSchemaCollection();
    // Check if the file is located on web
    URI location = getSource().getLocation();
    if (location.getHost() == null) {
        schemaCol.setSchemaResolver(new ProgressURIResolver(new HumboldtURIResolver(), progress));
        schemaCol.setBaseUri(findBaseUri(location));
    } else if (location.getScheme().equals("bundleresource")) {
        // $NON-NLS-1$
        schemaCol.setSchemaResolver(new ProgressURIResolver(new HumboldtURIResolver(), progress));
        // $NON-NLS-1$
        schemaCol.setBaseUri(findBaseUri(location) + "/");
    } else {
        schemaCol.setSchemaResolver(new ProgressURIResolver(new HumboldtURIResolver(), progress));
        // $NON-NLS-1$
        schemaCol.setBaseUri(findBaseUri(location) + "/");
    }
    InputStream is = null;
    // try resolving using (local) Resources
    InputSupplier<? extends InputStream> input = Resources.tryResolve(location, Resources.RESOURCE_TYPE_XML_SCHEMA);
    if (input != null) {
        try {
            is = input.getInput();
        } catch (Exception e) {
        // ignore
        }
    }
    if (is == null) {
        is = getSource().getInput();
    }
    StreamSource ss = new StreamSource(is);
    ss.setSystemId(location.toString());
    xmlSchema = schemaCol.read(ss, null);
    is.close();
    String namespace = xmlSchema.getTargetNamespace();
    if (namespace == null) {
        namespace = XMLConstants.NULL_NS_URI;
    }
    xmlSchema.setSourceURI(location.toString());
    // create index
    index = new XmlIndex(namespace, location);
    // create group counter
    groupCounter = new TObjectIntHashMap<String>();
    Set<String> imports = new HashSet<String>();
    imports.add(location.toString());
    // load XML Schema schema (for base type definitions)
    try {
        is = XmlSchemaReader.class.getResourceAsStream("/schemas/XMLSchema.xsd");
        ss = new StreamSource(is);
        schemaCol.setSchemaResolver(new ProgressURIResolver(new HumboldtURIResolver(), progress));
        schemaCol.setBaseUri(findBaseUri(XmlSchemaReader.class.getResource("/schemas/XMLSchema.xsd").toURI()) + "/");
        XmlSchema xsSchema = schemaCol.read(ss, null);
        is.close();
        xsSchema.setSourceURI("http://www.w3.org/2001/XMLSchema.xsd");
        XmlSchemaImport xmlSchemaImport = new XmlSchemaImport();
        xmlSchemaImport.setSchema(xsSchema);
        // add it to includes as XmlSchemaImport (not XmlSchemaInclude!)
        xmlSchema.getIncludes().add(xmlSchemaImport);
    } catch (Exception e) {
        _log.error("Exception while loading XML Schema schema", e);
    }
    loadSchema(location.toString(), xmlSchema, imports, progress, true);
    groupCounter.clear();
    // post processing
    applyRelevantElements(index);
    applyCustomTypeContent(index);
    reporter.setSuccess(true);
    return reporter;
}
Also used : ProgressURIResolver(eu.esdihumboldt.hale.io.xsd.reader.internal.ProgressURIResolver) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) URI(java.net.URI) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) HumboldtURIResolver(eu.esdihumboldt.hale.io.xsd.reader.internal.HumboldtURIResolver) HashSet(java.util.HashSet)

Example 10 with IOProviderConfigurationException

use of eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException in project hale by halestudio.

the class HaleConnectProjectWriter method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    if (!haleConnect.isLoggedIn()) {
        reporter.error("Must be logged in to hale connect to upload project.");
        reporter.setSuccess(false);
        return reporter;
    }
    URI location = null;
    if (getTarget().getLocation() != null) {
        location = getTarget().getLocation();
    }
    progress.begin("Saving project to hale connect", ProgressIndicator.UNKNOWN);
    Project project = getProject();
    URI projectUrn;
    if (location == null) {
        // was not shared before or creation of new project requested by
        // user
        boolean enableVersioning = getParameter(ENABLE_VERSIONING).as(Boolean.class);
        boolean publicAccess = getParameter(SHARING_PUBLIC).as(Boolean.class);
        String ownerTypeParameter = getParameter(OWNER_TYPE).as(String.class);
        OwnerType ownerType;
        try {
            ownerType = OwnerType.fromJsonValue(ownerTypeParameter);
        } catch (IllegalArgumentException e) {
            throw new IOProviderConfigurationException(MessageFormat.format("Invalid owner type: {0}", ownerTypeParameter), e);
        }
        String ownerId;
        switch(ownerType) {
            case USER:
                ownerId = haleConnect.getSession().getUserId();
                break;
            case ORGANISATION:
                if (haleConnect.getSession().getOrganisationIds().isEmpty()) {
                    throw new IOProviderConfigurationException(MessageFormat.format("Owner type is set to ORGANISATION but user \"{0}\" is not associated with any organisation", haleConnect.getSession().getUsername()));
                }
                ownerId = haleConnect.getSession().getOrganisationIds().iterator().next();
                break;
            default:
                throw new IOProviderConfigurationException(MessageFormat.format("Unknown owner type: {0}", ownerType));
        }
        Owner owner = new Owner(ownerType, ownerId);
        String projectId;
        try {
            projectId = haleConnect.createProject(project.getName(), project.getAuthor(), owner, enableVersioning);
            haleConnect.setProjectSharingOptions(projectId, owner, new SharingOptions(publicAccess));
        } catch (HaleConnectException e) {
            reporter.error("Error creating hale connect project", e);
            reporter.setSuccess(false);
            return reporter;
        }
        projectUrn = HaleConnectUrnBuilder.buildProjectUrn(owner, projectId);
        if (reporter instanceof MutableTargetIOReport) {
            ((MutableTargetIOReport) reporter).setTarget(new LocatableURI(prettifyTarget(projectUrn)));
        }
    } else if (!HaleConnectUrnBuilder.isValidProjectUrn(location)) {
        throw new IOProviderConfigurationException(MessageFormat.format("Cannot write to location: {0}", location.toString()));
    } else {
        projectUrn = location;
        writerMode = ProjectWriterMode.SAVE;
    }
    this.setTarget(new NoStreamOutputSupplier(projectUrn));
    // save the hale connect project URN in the project properties
    getProject().getProperties().put(HaleConnectProjectReader.HALECONNECT_URN_PROPERTY, Value.of(projectUrn.toString()));
    // redirect project archive to temporary local file
    File projectArchive = Files.createTempFile("hc-arc", ".zip").toFile();
    IOReport report;
    try (final FileOutputStream archiveStream = new FileOutputStream(projectArchive)) {
        report = createProjectArchive(archiveStream, reporter, progress);
    }
    if (!report.isSuccess()) {
        // exit when creating project archive failed
        return report;
    }
    String projectId = HaleConnectUrnBuilder.extractProjectId(projectUrn);
    Owner owner = HaleConnectUrnBuilder.extractProjectOwner(projectUrn);
    boolean result;
    try {
        result = haleConnect.uploadProjectFile(projectId, owner, projectArchive, progress);
    } catch (HaleConnectException e) {
        switch(e.getStatusCode()) {
            case 403:
                /* Forbidden */
                reporter.error(MessageFormat.format("You are not authorized to access project {0}", projectId), e);
                break;
            default:
                reporter.error(MessageFormat.format("Error uploading hale connect project: {0}", e.getMessage()), e);
        }
        reporter.setSuccess(false);
        return reporter;
    }
    // name
    try {
        haleConnect.setProjectName(projectId, owner, project.getName());
    } catch (HaleConnectException e) {
        // This is non-fatal
        log.warn(MessageFormat.format("Unable to update project bucket name for project {0}: {1}", HaleConnectUrnBuilder.buildProjectUrn(owner, projectId).toString(), e.getMessage()), e);
    }
    try {
        HaleConnectProjectInfo hcProjectInfo = haleConnect.getProject(owner, projectId);
        if (hcProjectInfo != null) {
            getProject().getProperties().put(HaleConnectProjectReader.HALECONNECT_LAST_MODIFIED_PROPERTY, Value.of(hcProjectInfo.getLastModified()));
        }
    } catch (HaleConnectException e) {
        // This is non-fatal
        log.warn(MessageFormat.format("Unable to get lastUpdated property for project {0}: {1}", HaleConnectUrnBuilder.buildProjectUrn(owner, projectId).toString(), e.getMessage()), e);
    }
    this.clientAccessUrl = HaleConnectUrnBuilder.buildClientAccessUrl(haleConnect.getBasePathManager().getBasePath(HaleConnectServices.WEB_CLIENT), owner, projectId);
    this.projectUri = HaleConnectUrnBuilder.buildProjectUrn(owner, projectId);
    reporter.setSuccess(result);
    return reporter;
}
Also used : Owner(eu.esdihumboldt.hale.io.haleconnect.Owner) LocatableURI(eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) MutableTargetIOReport(eu.esdihumboldt.hale.common.core.io.report.MutableTargetIOReport) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) URI(java.net.URI) LocatableURI(eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) OwnerType(eu.esdihumboldt.hale.io.haleconnect.OwnerType) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) FileOutputStream(java.io.FileOutputStream) NoStreamOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.NoStreamOutputSupplier) File(java.io.File) MutableTargetIOReport(eu.esdihumboldt.hale.common.core.io.report.MutableTargetIOReport)

Aggregations

IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)44 IOException (java.io.IOException)38 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)27 InputStream (java.io.InputStream)14 URI (java.net.URI)13 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)6 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)6 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)6 File (java.io.File)6 OutputStream (java.io.OutputStream)6 ArrayList (java.util.ArrayList)6 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)5 QName (javax.xml.namespace.QName)5 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)4 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)4 InputStreamReader (java.io.InputStreamReader)4 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)3