use of eu.esdihumboldt.util.groovy.sandbox.GroovyService in project hale by halestudio.
the class GroovyCreatePage method validate.
@Override
protected boolean validate(String document) {
super.validate(document);
Type typeEntity = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
if (typeEntity == null) {
// not yet selected (NewRelationWizard)
return false;
}
InstanceBuilder builder = new InstanceBuilder(false);
Cell cell = getWizard().getUnfinishedCell();
CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
Binding binding = GroovyUtil.createBinding(builder, cell, cell, log, context, typeEntity.getDefinition().getDefinition());
binding.setProperty(BINDING_INDEX, 0);
GroovyService service = HaleUI.getServiceProvider().getService(GroovyService.class);
Script script = null;
try {
script = service.parseScript(document, binding);
GroovyUtil.evaluateAll(script, builder, typeEntity.getDefinition().getDefinition(), service, log);
} catch (final Exception e) {
return handleValidationResult(script, e);
}
return handleValidationResult(script, null);
}
use of eu.esdihumboldt.util.groovy.sandbox.GroovyService in project hale by halestudio.
the class GroovyJoinPage method validate.
@Override
protected boolean validate(String document) {
ParameterValue param = CellUtil.getFirstParameter(getWizard().getUnfinishedCell(), JoinFunction.PARAMETER_JOIN);
JoinParameter joinParameter = param.as(JoinParameter.class);
// check Join parameter
if (joinParameter == null) {
// setValidationError("Missing join configuration");
return false;
} else {
String error = joinParameter.validate();
if (!setValidationError(error)) {
return false;
}
}
// target type
Type targetType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
if (targetType == null) {
// not yet selected (NewRelationWizard)
return false;
}
/*
* FIXME use JoinParameter to fake joined instances!
*
* XXX for now just base instance
*/
TypeEntityDefinition sourceType = joinParameter.getTypes().get(0);
InstanceBuilder builder = new InstanceBuilder(false);
Instance instance = getTestValues().get(sourceType);
if (instance == null) {
// use an empty instance as input for the script
instance = new DefaultInstance(sourceType.getDefinition(), DataSet.SOURCE);
}
FamilyInstance source = new FamilyInstanceImpl(instance);
// prepare binding
Cell cell = getWizard().getUnfinishedCell();
CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
Binding binding = GroovyRetype.createBinding(source, cell, builder, log, context, targetType.getDefinition().getDefinition());
GroovyService service = HaleUI.getServiceProvider().getService(GroovyService.class);
Script script = null;
try {
script = service.parseScript(document, binding);
GroovyUtil.evaluateAll(script, builder, targetType.getDefinition().getDefinition(), service, log);
} catch (final Exception e) {
return handleValidationResult(script, e);
}
return handleValidationResult(script, null);
}
use of eu.esdihumboldt.util.groovy.sandbox.GroovyService in project hale by halestudio.
the class ToggleRestrictionHandler method updateElement.
@Override
public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
GroovyService gs = HaleUI.getServiceProvider().getService(GroovyService.class);
element.setChecked(gs.isRestrictionActive());
}
use of eu.esdihumboldt.util.groovy.sandbox.GroovyService in project hale by halestudio.
the class CustomGroovyTransformation method evaluate.
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
// store script as parameter
if (!CustomPropertyFunctionType.GROOVY.equals(customFunction.getFunctionType())) {
throw new TransformationException("Custom function is not of type groovy");
}
Value scriptValue = customFunction.getFunctionDefinition();
if (scriptValue.isEmpty()) {
throw new NoResultException("Script not defined");
}
ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
params.put(GroovyTransformation.PARAMETER_SCRIPT, new ParameterValue(scriptValue));
setParameters(params);
// instance builder
InstanceBuilder builder = GroovyTransformation.createBuilder(resultProperty);
// create the script binding
Binding binding = createGroovyBinding(variables, getCell(), getTypeCell(), builder, log, getExecutionContext(), resultProperty.getDefinition().getPropertyType());
Object result;
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script groovyScript = GroovyUtil.getScript(this, binding, service, true);
// evaluate the script
result = GroovyTransformation.evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
} catch (TransformationException | NoResultException e) {
throw e;
} catch (Throwable e) {
throw new TransformationException("Error evaluating the custom function script", e);
}
if (result == null) {
throw new NoResultException();
}
return result;
}
use of eu.esdihumboldt.util.groovy.sandbox.GroovyService in project hale by halestudio.
the class GroovyCreate method createInstances.
private Iterable<MutableInstance> createInstances(TypeDefinition type, TransformationLog log, Cell cell, int index) throws TransformationException {
InstanceBuilder builder = new InstanceBuilder(false);
Binding binding = GroovyUtil.createBinding(builder, cell, cell, log, getExecutionContext(), type);
binding.setProperty(BINDING_INDEX, index);
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script script = GroovyUtil.getScript(this, binding, service);
return GroovyUtil.evaluateAll(script, builder, type, service, log);
} catch (TransformationException e) {
throw e;
} catch (NoResultException e) {
log.info(log.createMessage("Skipping target instance because received NoResultException from script", null));
return Collections.emptyList();
} catch (Exception e) {
throw new TransformationException(e.getMessage(), e);
}
}
Aggregations