Search in sources :

Example 56 with Value

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

the class DefaultCustomFunctionExplanation method findTemplate.

private Optional<Value> findTemplate(Locale locale) {
    ResourceBundle.Control control = ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_DEFAULT);
    List<Locale> candidateLocales = control.getCandidateLocales("baseName", locale);
    for (Locale specificLocale : candidateLocales) {
        Value candidate = templates.get(specificLocale);
        if (candidate != null) {
            return Optional.of(candidate);
        }
    }
    return Optional.empty();
}
Also used : Locale(java.util.Locale) Value(eu.esdihumboldt.hale.common.core.io.Value) ResourceBundle(java.util.ResourceBundle)

Example 57 with Value

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

the class ProjectTest method testSaveLoad.

/**
 * Test saving and loading an example project
 *
 * @throws Exception if an error occurs
 */
@Test
public void testSaveLoad() throws Exception {
    // populate project
    Project project = new Project();
    String author;
    project.setAuthor(author = "Simon");
    String name;
    project.setName(name = "Testprojekt");
    Date created;
    project.setCreated(created = new Date(0));
    Date modified;
    project.setModified(modified = new Date());
    Version haleVersion;
    project.setHaleVersion(haleVersion = new Version("2.2.0.alpha"));
    String desc;
    project.setDescription(desc = "Hallo Welt!\nBist Du auch hier?\nÖhm.");
    IOConfiguration conf1;
    project.getResources().add(conf1 = new IOConfiguration());
    String advisorId1;
    conf1.setActionId(advisorId1 = "some advisor");
    String providerId1;
    conf1.setProviderId(providerId1 = "some provider");
    String key1;
    Value value1;
    conf1.getProviderConfiguration().put(key1 = "some key", value1 = Value.of("some value"));
    Value value2;
    String key2;
    conf1.getProviderConfiguration().put(key2 = "some other key", value2 = Value.of("some other value"));
    IOConfiguration conf2;
    project.getResources().add(conf2 = new IOConfiguration());
    String advisorId2;
    conf2.setActionId(advisorId2 = "a certain advisor");
    String providerId2;
    conf2.setProviderId(providerId2 = "a special provider");
    // write project
    File projectFile = tmp.newFile("project.xml");
    System.out.println(projectFile.getAbsolutePath());
    Project.save(project, new FileOutputStream(projectFile));
    // load project
    Project p2 = Project.load(new FileInputStream(projectFile));
    // test project
    assertEquals(author, p2.getAuthor());
    assertEquals(name, p2.getName());
    assertEquals(created, p2.getCreated());
    assertEquals(modified, p2.getModified());
    assertEquals(haleVersion, p2.getHaleVersion());
    assertEquals(desc, p2.getDescription());
    assertEquals(2, p2.getResources().size());
    Iterator<IOConfiguration> it = p2.getResources().iterator();
    IOConfiguration c1 = it.next();
    assertNotNull(c1);
    assertEquals(advisorId1, c1.getActionId());
    assertEquals(providerId1, c1.getProviderId());
    assertEquals(2, c1.getProviderConfiguration().size());
    assertTrue(c1.getProviderConfiguration().get(key1).getValue().equals(value1.getValue()));
    assertTrue(c1.getProviderConfiguration().get(key2).getValue().equals(value2.getValue()));
    IOConfiguration c2 = it.next();
    assertNotNull(c2);
    assertEquals(advisorId2, c2.getActionId());
    assertEquals(providerId2, c2.getProviderId());
}
Also used : Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) Version(org.osgi.framework.Version) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) FileOutputStream(java.io.FileOutputStream) Value(eu.esdihumboldt.hale.common.core.io.Value) File(java.io.File) Date(java.util.Date) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 58 with Value

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

the class ProjectVariables method setVariable.

/**
 * Helper method to store a project variable value.
 *
 * @param name the variable name
 * @param value the variable value
 * @param projectConfiguration the project configuration service
 */
public static void setVariable(String name, Value value, ComplexConfigurationService projectConfiguration) {
    Value variables = projectConfiguration.getProperty(PROJECT_PROPERTY_VARIABLES);
    ValueProperties properties = variables.as(ValueProperties.class);
    if (properties == null) {
        properties = new ValueProperties();
    } else {
        if (variables.getValue() != null) {
            log.error("Unknown representation of project variables");
        }
    }
    properties.put(name, value);
    projectConfiguration.setProperty(PROJECT_PROPERTY_VARIABLES, Value.complex(properties));
}
Also used : ValueProperties(eu.esdihumboldt.hale.common.core.io.ValueProperties) Value(eu.esdihumboldt.hale.common.core.io.Value)

Example 59 with Value

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

the class ProjectVariables method getValue.

/**
 * Get the value for a given variable name.
 *
 * @param name the variable name
 * @return the project variable value or {@link Value#NULL}
 */
public Value getValue(String name) {
    // 1st priority: system property
    String value = System.getProperty(PREFIX_SYSTEM_PROPERTY + name);
    if (value != null) {
        return Value.of(value);
    }
    // 2nd priority: environment variable
    value = System.getenv(PREFIX_ENV + name);
    if (value != null) {
        return Value.of(value);
    }
    // 3rd priority: use value stored in project
    if (projectInfo != null) {
        Value variables = projectInfo.getProperty(PROJECT_PROPERTY_VARIABLES);
        ValueProperties properties = variables.as(ValueProperties.class);
        if (properties != null) {
            return properties.getSafe(name);
        } else {
            if (variables.getValue() != null) {
                log.error("Unknown representation of project variables");
            }
        }
    }
    return Value.NULL;
}
Also used : ValueProperties(eu.esdihumboldt.hale.common.core.io.ValueProperties) Value(eu.esdihumboldt.hale.common.core.io.Value)

Example 60 with Value

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

the class ArchiveProjectWriter method updateResources.

/**
 * Update the resources and copy them into the target directory
 *
 * @param targetDirectory target directory
 * @param includeWebResources whether to include web resources in the copy
 * @param progress the progress indicator
 * @param reporter the reporter to use for the execution report
 * @throws IOException if an I/O operation fails
 */
protected void updateResources(File targetDirectory, boolean includeWebResources, ProgressIndicator progress, IOReporter reporter) throws IOException {
    progress.begin("Copy resources", ProgressIndicator.UNKNOWN);
    try {
        List<IOConfiguration> resources = getProject().getResources();
        // every resource needs his own directory
        int count = 1;
        // true if excluded files should be skipped; false is default
        boolean excludeDataFiles = getParameter(EXLUDE_DATA_FILES).as(Boolean.class, false);
        // resource locations mapped to new resource path
        Map<URI, String> handledResources = new HashMap<>();
        Iterator<IOConfiguration> iter = resources.iterator();
        while (iter.hasNext()) {
            IOConfiguration resource = iter.next();
            // import not possible due to cycle errors
            if (excludeDataFiles && resource.getActionId().equals("eu.esdihumboldt.hale.io.instance.read.source")) {
                // delete reference in project file
                iter.remove();
                continue;
            }
            // get resource path
            Map<String, Value> providerConfig = resource.getProviderConfiguration();
            String path = providerConfig.get(ImportProvider.PARAM_SOURCE).toString();
            URI pathUri;
            try {
                pathUri = new URI(path);
            } catch (URISyntaxException e1) {
                reporter.error(new IOMessageImpl("Skipped resource because of invalid URI: " + path, e1));
                continue;
            }
            if (!pathUri.isAbsolute()) {
                if (getPreviousTarget() != null) {
                    pathUri = getPreviousTarget().resolve(pathUri);
                } else {
                    log.warn("Could not resolve relative path " + pathUri.toString());
                }
            }
            // check if path was already handled
            if (handledResources.containsKey(pathUri)) {
                providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(handledResources.get(pathUri)));
                // skip copying the resource
                continue;
            }
            String scheme = pathUri.getScheme();
            LocatableInputSupplier<? extends InputStream> input = null;
            if (scheme != null) {
                if (scheme.equals("http") || scheme.equals("https")) {
                    // web resource
                    if (includeWebResources) {
                        input = new DefaultInputSupplier(pathUri);
                    } else {
                        // web resource that should not be included this
                        // time
                        // but the resolved URI should be stored
                        // nevertheless
                        // otherwise the URI may be invalid if it was
                        // relative
                        providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(pathUri.toASCIIString()));
                        continue;
                    }
                } else if (scheme.equals("file") || scheme.equals("platform") || scheme.equals("bundle") || scheme.equals("jar")) {
                    // files need always to be included
                    // platform resources (or other internal resources)
                    // should be included as well
                    input = new DefaultInputSupplier(pathUri);
                } else {
                    // other type of URI, e.g. JDBC
                    // not to be included
                    providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(pathUri.toASCIIString()));
                    continue;
                }
            } else {
                // now can't open that, can we?
                reporter.error(new IOMessageImpl("Skipped resource because it cannot be loaded from " + pathUri.toString(), null));
                continue;
            }
            progress.setCurrentTask("Copying resource at " + path);
            // every resource file is copied into an own resource
            // directory in the target directory
            String resourceFolder = "resource" + count;
            File newDirectory = new File(targetDirectory, resourceFolder);
            try {
                newDirectory.mkdir();
            } catch (SecurityException e) {
                throw new IOException("Can not create directory " + newDirectory.toString(), e);
            }
            // Extract the file name from pathUri.getPath().
            // This will produce a non-URL-encoded file name to be used in
            // the File(File parent, String child) constructor below
            String fileName = FilenameUtils.getName(pathUri.getPath().toString());
            if (path.isEmpty()) {
                fileName = "file";
            }
            File newFile = new File(newDirectory, fileName);
            Path target = newFile.toPath();
            // retrieve the resource advisor
            Value ct = providerConfig.get(ImportProvider.PARAM_CONTENT_TYPE);
            IContentType contentType = null;
            if (ct != null) {
                contentType = HalePlatform.getContentTypeManager().getContentType(ct.as(String.class));
            }
            ResourceAdvisor ra = ResourceAdvisorExtension.getInstance().getAdvisor(contentType);
            // copy the resource
            progress.setCurrentTask("Copying resource at " + path);
            // Extract the URL-encoded file name of the copied resource and
            // build the new relative resource path
            String resourceName = FilenameUtils.getName(target.toUri().toString());
            String newPath = resourceFolder + "/" + resourceName;
            boolean skipCopy = getParameter(EXCLUDE_CACHED_RESOURCES).as(Boolean.class, false) && !resource.getCache().isEmpty();
            if (!skipCopy) {
                ra.copyResource(input, target, contentType, includeWebResources, reporter);
                // store new path for resource
                handledResources.put(pathUri, newPath);
            }
            // update the provider configuration
            providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(newPath));
            count++;
        }
    } finally {
        progress.end();
    }
}
Also used : Path(java.nio.file.Path) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) HashMap(java.util.HashMap) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IContentType(org.eclipse.core.runtime.content.IContentType) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ResourceAdvisor(eu.esdihumboldt.hale.common.core.io.ResourceAdvisor) Value(eu.esdihumboldt.hale.common.core.io.Value) File(java.io.File)

Aggregations

Value (eu.esdihumboldt.hale.common.core.io.Value)81 ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)12 LookupTable (eu.esdihumboldt.hale.common.lookup.LookupTable)12 HashMap (java.util.HashMap)12 ValueProperties (eu.esdihumboldt.hale.common.core.io.ValueProperties)11 LookupTableImpl (eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)11 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)10 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)9 ArrayList (java.util.ArrayList)9 URI (java.net.URI)8 Test (org.junit.Test)6 StyledString (org.eclipse.jface.viewers.StyledString)5 Composite (org.eclipse.swt.widgets.Composite)5 DefaultCustomPropertyFunction (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction)4 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)4 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)4 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)4 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)4 IOException (java.io.IOException)4 Locale (java.util.Locale)4