use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.
the class Transformation method transform.
/**
* Transform the instances provided through the given instance readers and
* supply the result to the given instance writer.
*
* @param sources the instance readers
* @param target the target instance writer
* @param environment the transformation environment
* @param reportHandler the report handler
* @param processId the identifier for the transformation process, may be
* <code>null</code> if grouping the jobs to a job family is not
* necessary
* @param validators the instance validators, may be <code>null</code> or
* empty
* @param filterDefinition {@link InstanceFilterDefinition} object as a
* filter may be <code>null</code>
* @return the future representing the successful completion of the
* transformation (note that a successful completion doesn't
* necessary mean there weren't any internal transformation errors)
*/
public static ListenableFuture<Boolean> transform(List<InstanceReader> sources, InstanceWriter target, final TransformationEnvironment environment, final ReportHandler reportHandler, Object processId, Collection<InstanceValidator> validators, InstanceFilterDefinition filterDefinition) {
final IOAdvisor<InstanceReader> loadDataAdvisor = new AbstractIOAdvisor<InstanceReader>() {
/**
* @see IOAdvisor#prepareProvider(IOProvider)
*/
@Override
public void prepareProvider(InstanceReader provider) {
super.prepareProvider(provider);
provider.setSourceSchema(environment.getSourceSchema());
}
/**
* @see AbstractIOAdvisor#updateConfiguration(IOProvider)
*/
@Override
public void updateConfiguration(InstanceReader provider) {
super.updateConfiguration(provider);
if (environment instanceof ProjectTransformationEnvironment) {
// set project CRS manager as CRS provider
/*
* Resource based CRS settings will however not work, as the
* resource identifiers will not match
*/
provider.setCRSProvider(new ProjectCRSManager(provider, null, ((ProjectTransformationEnvironment) environment).getProject()));
}
}
};
loadDataAdvisor.setServiceProvider(environment);
loadDataAdvisor.setActionId(InstanceIO.ACTION_LOAD_SOURCE_DATA);
List<InstanceCollection> sourceList = Lists.transform(sources, new Function<InstanceReader, InstanceCollection>() {
@Override
public InstanceCollection apply(@Nullable InstanceReader input) {
try {
HeadlessIO.executeProvider(input, loadDataAdvisor, null, reportHandler);
// XXX progress?!
} catch (IOException e) {
throw new IllegalStateException("Failed to load source data", e);
}
return input.getInstances();
}
});
// Apply Filter
InstanceCollection sourceCollection = applyFilter(sourceList, filterDefinition);
final TransformationSink targetSink;
try {
targetSink = TransformationSinkExtension.getInstance().createSink(!target.isPassthrough());
targetSink.setTypes(environment.getTargetSchema());
// add validation to sink
// XXX for now default validation if env variable is set
String env = System.getenv("HALE_TRANSFORMATION_INTERNAL_VALIDATION");
if (env != null && env.equalsIgnoreCase("true")) {
targetSink.addValidator(new DefaultTransformedInstanceValidator(reportHandler, environment));
}
} catch (Exception e) {
throw new IllegalStateException("Error creating target sink", e);
}
IOAdvisor<InstanceWriter> saveDataAdvisor = new AbstractIOAdvisor<InstanceWriter>() {
/**
* @see IOAdvisor#prepareProvider(IOProvider)
*/
@Override
public void prepareProvider(InstanceWriter provider) {
super.prepareProvider(provider);
// set target schema
provider.setTargetSchema(environment.getTargetSchema());
// set instances to export
provider.setInstances(targetSink.getInstanceCollection());
}
};
saveDataAdvisor.setServiceProvider(environment);
saveDataAdvisor.setActionId(InstanceIO.ACTION_SAVE_TRANSFORMED_DATA);
saveDataAdvisor.prepareProvider(target);
saveDataAdvisor.updateConfiguration(target);
ExportJob exportJob = new ExportJob(targetSink, target, saveDataAdvisor, reportHandler);
// no validation
ValidationJob validationJob = null;
if (validators != null && !validators.isEmpty()) {
validationJob = new ValidationJob(validators, reportHandler, target, environment);
}
return transform(sourceCollection, targetSink, exportJob, validationJob, environment.getAlignment(), environment.getSourceSchema(), reportHandler, environment, processId);
}
use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.
the class ProjectHandler method onSuccess.
@Override
protected void onSuccess(EnvironmentManager context, String projectId, File projectFile, Project project, ReportFile reportFile) {
super.onSuccess(context, projectId, projectFile, project, reportFile);
if (isEnabled()) {
// load transformation environment if not yet done
if (transformationEnvironment == null) {
try {
transformationEnvironment = new ProjectTransformationEnvironment(projectId, new FileIOSupplier(projectFile), reportFile);
// check alignment
if (transformationEnvironment.getAlignment() == null) {
throw new IllegalStateException("Alignment missing or failed to load");
}
if (transformationEnvironment.getAlignment().getActiveTypeCells().isEmpty()) {
throw new IllegalStateException("Alignment contains no active type relations");
}
} catch (Exception e) {
log.error("Could not load transformation environment for project " + projectId, e);
status = Status.BROKEN;
transformationEnvironment = null;
context.removeEnvironment(projectId);
// log the exception as report
Reporter<Message> report = new DefaultReporter<Message>("Load project transformation environment", ProjectIO.ACTION_LOAD_PROJECT, Message.class, false);
report.error(new MessageImpl(e.getMessage(), e));
reportFile.publishReport(report);
}
} else {
// XXX somehow check if project was changed?
}
if (transformationEnvironment != null) {
context.addEnvironment(transformationEnvironment);
status = Status.ACTIVE;
}
} else {
// clear transformation environment
status = Status.INACTIVE;
transformationEnvironment = null;
context.removeEnvironment(projectId);
}
}
use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.
the class GenerateDuplicates method loadProject.
private boolean loadProject() throws IOException {
final AtomicBoolean success = new AtomicBoolean(true);
LocatableInputSupplier<? extends InputStream> projectIn = new DefaultInputSupplier(context.getProject());
ProjectTransformationEnvironment env = new ProjectTransformationEnvironment("sample", projectIn, new ReportHandler() {
@Override
public void publishReport(Report<?> report) {
if (report.isSuccess() && report.getErrors().isEmpty()) {
System.out.println(report.getSummary());
} else {
System.err.println("Error loading project: " + report.getSummary());
success.set(false);
}
}
});
if (success.get()) {
this.sourceSchema = env.getSourceSchema();
this.targetSchema = env.getTargetSchema();
this.examples = env.getAlignment();
return true;
}
return false;
}
use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.
the class CityGMLPropagate method loadProject.
private boolean loadProject() throws IOException {
final AtomicBoolean success = new AtomicBoolean(true);
LocatableInputSupplier<? extends InputStream> projectIn = new DefaultInputSupplier(context.getProject());
ProjectTransformationEnvironment env = new ProjectTransformationEnvironment("sample", projectIn, new ReportHandler() {
@Override
public void publishReport(Report<?> report) {
if (report.isSuccess() && report.getErrors().isEmpty()) {
System.out.println(report.getSummary());
} else {
System.err.println("Error loading project: " + report.getSummary());
success.set(false);
}
}
});
if (success.get()) {
this.sourceSchema = env.getSourceSchema();
this.targetSchema = env.getTargetSchema();
this.examples = env.getAlignment();
return true;
}
return false;
}
use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.
the class ExecTransformation method loadProject.
private void loadProject() throws IOException {
status("Loading hale project...");
env = new ProjectTransformationEnvironment(id, new DefaultInputSupplier(context.getProject()), reportHandler);
}
Aggregations