Search in sources :

Example 6 with IOReport

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

the class XLSReaderTest method testReadEmptySheet.

/**
 * Test - Check empty table
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testReadEmptySheet() throws Exception {
    int sheetIndex = 0;
    XLSSchemaReader schemaReader = new XLSSchemaReader();
    schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/data/emptyAndNormalSheet.xls").toURI()));
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
    schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of(typeName));
    // Try read the empty sheet
    IOReport report = schemaReader.execute(null);
    assertFalse(report.isSuccess());
    // Read the correct sheet
    sheetIndex = 1;
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
    report = schemaReader.execute(null);
    assertTrue(report.isSuccess());
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) XLSSchemaReader(eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) Test(org.junit.Test)

Example 7 with IOReport

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

the class XLSInstanceWriterTest method testWriteNotNestedProperties.

/**
 * Test - write data of complex schema and analyze result The
 * implementation, this test based on, does not work correctly at the
 * moment.
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testWriteNotNestedProperties() throws Exception {
    TransformationExample example = TransformationExamples.getExample(TransformationExamples.SIMPLE_COMPLEX);
    // alternative the data could be generated by iterating through the
    // exempleproject's sourcedata
    List<String> header = Arrays.asList("id", "name");
    List<String> firstDataRow = Arrays.asList("id0", "name0");
    // set instances to xls instance writer
    XLSInstanceWriter writer = new XLSInstanceWriter();
    IContentType contentType = HalePlatform.getContentTypeManager().getContentType("eu.esdihumboldt.hale.io.xls.xls");
    writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    File tmpFile = tmpFolder.newFile("excelNotNestedProperties.xls");
    writer.setInstances(example.getSourceInstances());
    // write instances to a temporary XLS file
    writer.setTarget(new FileIOSupplier(tmpFile));
    writer.setContentType(contentType);
    IOReport report = writer.execute(null);
    assertTrue(report.isSuccess());
    Workbook wb = WorkbookFactory.create(tmpFile);
    Sheet sheet = wb.getSheetAt(0);
    checkHeader(sheet, header);
    checkSheetName(sheet, "person");
    checkFirstDataRow(sheet, firstDataRow);
}
Also used : XLSInstanceWriter(eu.esdihumboldt.hale.io.xls.writer.XLSInstanceWriter) TransformationExample(eu.esdihumboldt.cst.test.TransformationExample) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) IContentType(org.eclipse.core.runtime.content.IContentType) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 8 with IOReport

use of eu.esdihumboldt.hale.common.core.io.report.IOReport 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)

Example 9 with IOReport

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

the class MsAccessDataReaderTestSuit method schemaReaderTest.

/**
 * Test - reads a sample MsAccess Database schema. UCanAccess lib should not
 * throw any error.
 *
 * @throws Exception if an error occurs
 */
public void schemaReaderTest() throws Exception {
    MsAccessSchemaReader schemaReader = new MsAccessSchemaReader();
    schemaReader.setSource(new FileIOSupplier(getSourceTempFilePath()));
    schemaReader.setParameter(JDBCSchemaReader.PARAM_USER, Value.of(USER_NAME));
    schemaReader.setParameter(JDBCSchemaReader.PARAM_PASSWORD, Value.of(PASSWORD));
    IOReport report = schemaReader.execute(new LogProgressIndicator());
    assertTrue(report.isSuccess());
    TEMP_SOURCE_FILE_NAME = null;
    Schema schema = schemaReader.getSchema();
    assertTrue(schema != null);
    Collection<? extends TypeDefinition> k = schema.getMappingRelevantTypes();
    for (TypeDefinition def : k) System.out.println(def.getDisplayName());
    checkTables(k);
}
Also used : Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) MsAccessSchemaReader(eu.esdihumboldt.hale.io.jdbc.msaccess.MsAccessSchemaReader) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 10 with IOReport

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

the class MsAccessDataReaderTestSuit method readInstances.

/**
 * Reads instances from from a MsAccess database file with the provided
 * schema.
 *
 * @param sourceSchema the schema of the source database
 * @param sourceFile the file of the source database.
 * @return the read instances
 * @throws Exception any exception thrown by {@link MsAccessInstanceReader}
 */
public InstanceCollection readInstances(Schema sourceSchema, File sourceFile) throws Exception {
    MsAccessInstanceReader instanceReader = new MsAccessInstanceReader();
    instanceReader.setSource(new FileIOSupplier(sourceFile));
    instanceReader.setSourceSchema(sourceSchema);
    instanceReader.setParameter(JDBCInstanceReader.PARAM_USER, Value.of(USER_NAME));
    instanceReader.setParameter(JDBCInstanceReader.PARAM_PASSWORD, Value.of(PASSWORD));
    // Test instances
    IOReport report = instanceReader.execute(new LogProgressIndicator());
    assertTrue("Data import was not successfull.", report.isSuccess());
    return instanceReader.getInstances();
}
Also used : MsAccessInstanceReader(eu.esdihumboldt.hale.io.jdbc.msaccess.MsAccessInstanceReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)

Aggregations

IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)102 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)34 Test (org.junit.Test)33 LogProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)25 List (java.util.List)23 QName (javax.xml.namespace.QName)23 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)22 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)22 HashMap (java.util.HashMap)22 File (java.io.File)14 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)11 XmlSchemaReader (eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader)11 SchemaReader (eu.esdihumboldt.hale.common.schema.io.SchemaReader)10 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)8 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)8 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)8 IOException (java.io.IOException)8 Ignore (org.junit.Ignore)8 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)7 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)7