Skip to content
Snippets Groups Projects
Commit fc225732 authored by buczekp's avatar buczekp
Browse files

[LMS-1863] better code reuse

SVN: 18578
parent 9ec75cda
No related branches found
No related tags found
No related merge requests found
Showing
with 89 additions and 72 deletions
......@@ -56,7 +56,7 @@ public class DynamicPropertyEvaluator implements IDynamicPropertyEvaluator
/** Returns a calculator for given script (creates a new one if nothing is found in cache). */
private DynamicPropertyCalculator getCalculator(ScriptPE scriptPE)
{
// Creation of a calculator involves takes time because of compilation of the script.
// Creation of a calculator takes some time because of compilation of the script.
// That is why a cache is used.
DynamicPropertyCalculator result = calculatorsByScript.get(scriptPE);
if (result == null)
......@@ -74,27 +74,41 @@ public class DynamicPropertyEvaluator implements IDynamicPropertyEvaluator
operationLog.debug(String.format("Evaluating dynamic properties of entity '%s'.",
entity));
}
final IEntityAdaptor entityAdaptor = EntityAdaptorFactory.create(entity, this);
for (EntityPropertyPE property : entity.getProperties())
{
EntityTypePropertyTypePE etpt = property.getEntityTypePropertyType();
if (etpt.isDynamic())
{
try
{
final DynamicPropertyCalculator calculator = getCalculator(etpt.getScript());
final IEntityAdaptor entityAdaptor = EntityAdaptorFactory.create(entity);
calculator.setEntity(entityAdaptor);
final String dynamicValue = calculator.evalAsString();
final String validatedValue =
validator.validatePropertyValue(etpt.getPropertyType(), dynamicValue);
property.setValue(validatedValue);
} catch (Exception e)
{
String errorMsg = ERROR_PREFIX + e.getMessage();
operationLog.info(errorMsg);
property.setValue(BasicConstant.ERROR_PROPERTY_PREFIX + errorMsg);
}
final String dynamicValue = evaluateProperty(entityAdaptor, etpt);
property.setValue(dynamicValue);
}
}
}
public String evaluateProperty(IEntityAdaptor entityAdaptor, EntityTypePropertyTypePE etpt)
{
assert etpt.isDynamic() == true : "expected dynamic property";
try
{
final DynamicPropertyCalculator calculator = getCalculator(etpt.getScript());
calculator.setEntity(entityAdaptor);
final String dynamicValue = calculator.evalAsString();
final String validatedValue =
validator.validatePropertyValue(etpt.getPropertyType(), dynamicValue);
return validatedValue;
} catch (Exception e)
{
final String errorValue = errorPropertyValue(e.getMessage());
return errorValue;
}
}
/** @return value for property storing specified error message */
public static String errorPropertyValue(String error)
{
String errorMsg = ERROR_PREFIX + error;
operationLog.info(errorMsg);
return BasicConstant.ERROR_PROPERTY_PREFIX + errorMsg;
}
}
......@@ -16,10 +16,12 @@
package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.calculator.IEntityAdaptor;
import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePropertyTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.IEntityInformationWithPropertiesHolder;
/**
* Interface for evaluation of all dynamic properties of an entity.
* Interface for evaluation of dynamic properties of an entity.
*
* @author Piotr Buczek
*/
......@@ -32,4 +34,12 @@ public interface IDynamicPropertyEvaluator
*/
public <T extends IEntityInformationWithPropertiesHolder> void evaluateProperties(T entity);
/**
* Evaluates value of specified dynamic property on specified entity.
*
* @return computed value
*/
public String evaluateProperty(IEntityAdaptor entityAdaptor,
EntityTypePropertyTypePE dynamicPropertyETPT);
}
......@@ -20,6 +20,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.IDynamicPropertyEvaluator;
import ch.systemsx.cisd.openbis.generic.shared.dto.EntityPropertyPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePropertyTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.IEntityPropertiesHolder;
......@@ -42,7 +43,8 @@ public class AbstractEntityAdaptor implements IEntityAdaptor
this.code = code;
}
protected void initProperties(IEntityPropertiesHolder propertiesHolder)
protected void initProperties(IEntityPropertiesHolder propertiesHolder,
IDynamicPropertyEvaluator evaluator)
{
for (EntityPropertyPE property : propertiesHolder.getProperties())
{
......@@ -51,7 +53,7 @@ public class AbstractEntityAdaptor implements IEntityAdaptor
final String propertyTypeCode = propertyType.getCode();
if (etpt.isDynamic())
{
addProperty(new DynamicPropertyAdaptor(propertyTypeCode, this, property));
addProperty(new DynamicPropertyAdaptor(propertyTypeCode, this, property, evaluator));
} else
{
final String value;
......
......@@ -16,9 +16,8 @@
package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.calculator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IPropertyValueValidator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.PropertyValidator;
import ch.systemsx.cisd.openbis.generic.shared.basic.BasicConstant;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.DynamicPropertyEvaluator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.IDynamicPropertyEvaluator;
import ch.systemsx.cisd.openbis.generic.shared.dto.EntityPropertyPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePropertyTypePE;
......@@ -28,17 +27,20 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePropertyTypePE;
*
* @author Piotr Buczek
*/
// TODO 2010-11-05, Piotr Buczek: refactor to use DynamicPropertyEvaluator
class DynamicPropertyAdaptor implements IEntityPropertyAdaptor
{
/** state of lazy evaluation of the property value (analogy to graph search) */
private enum State
{
EMPTY, EVALUATING, EVALUATED
}
/** initial state before evaluation has been triggered */
EMPTY,
private static final IPropertyValueValidator validator = new PropertyValidator();
/** state reached when evaluation is triggered */
EVALUATING,
private static final String ERROR_PREFIX = "ERROR: ";
/** state reached when evaluation has been finished */
EVALUATED
}
private State state = State.EMPTY;
......@@ -50,20 +52,18 @@ class DynamicPropertyAdaptor implements IEntityPropertyAdaptor
private final IEntityAdaptor entityAdaptor;
private final IDynamicPropertyEvaluator evaluator;
public DynamicPropertyAdaptor(String code, IEntityAdaptor entityAdaptor,
EntityPropertyPE propertyPE)
EntityPropertyPE propertyPE, IDynamicPropertyEvaluator evaluator)
{
this.code = code;
this.entityAdaptor = entityAdaptor;
this.propertyPE = propertyPE;
this.evaluator = evaluator;
}
public String propertyTypeCode()
{
return code;
}
private String doGetValue()
public String valueAsString()
{
switch (state)
{
......@@ -71,14 +71,15 @@ class DynamicPropertyAdaptor implements IEntityPropertyAdaptor
// start evaluation
state = State.EVALUATING;
value = doEvaluate();
state = State.EVALUATED;
break;
case EVALUATING:
// cycle found - return an error
state = State.EVALUATED;
String errorMsg =
ERROR_PREFIX + "cycle found in dependencies of property "
+ propertyTypeCode();
value = BasicConstant.ERROR_PROPERTY_PREFIX + errorMsg;
String.format("cycle of dependencies found for dynamic property '%s'",
propertyTypeCode());
value = DynamicPropertyEvaluator.errorPropertyValue(errorMsg);
state = State.EVALUATED;
break;
case EVALUATED:
// value was already computed
......@@ -90,33 +91,17 @@ class DynamicPropertyAdaptor implements IEntityPropertyAdaptor
private String doEvaluate()
{
EntityTypePropertyTypePE etpt = propertyPE.getEntityTypePropertyType();
assert etpt != null;
try
{
// TODO 2010-11-05, Piotr Buczek: use cache
final DynamicPropertyCalculator calculator =
DynamicPropertyCalculator.create(etpt.getScript().getScript());
calculator.setEntity(entityAdaptor);
final String dynamicValue = calculator.evalAsString();
final String validatedValue =
validator.validatePropertyValue(etpt.getPropertyType(), dynamicValue);
return validatedValue;
} catch (Exception e)
{
String errorMsg = ERROR_PREFIX + e.getMessage();
// operationLog.info(errorMsg);
return BasicConstant.ERROR_PROPERTY_PREFIX + errorMsg;
}
return evaluator.evaluateProperty(entityAdaptor, etpt);
}
public String valueAsString()
public String renderedValue()
{
return doGetValue();
return valueAsString();
}
public String renderedValue()
public String propertyTypeCode()
{
return valueAsString();
return code;
}
public EntityPropertyPE getPropertyPE()
......
......@@ -16,6 +16,7 @@
package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.calculator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.IDynamicPropertyEvaluator;
import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.ExternalDataPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.IEntityInformationWithPropertiesHolder;
......@@ -30,18 +31,19 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
public class EntityAdaptorFactory
{
/** Returns an adaptor for specified entity based on its kind. */
public static IEntityAdaptor create(IEntityInformationWithPropertiesHolder entity)
public static IEntityAdaptor create(IEntityInformationWithPropertiesHolder entity,
IDynamicPropertyEvaluator evaluator)
{
switch (entity.getEntityKind())
{
case SAMPLE:
return new SampleAdaptor((SamplePE) entity);
return new SampleAdaptor((SamplePE) entity, evaluator);
case EXPERIMENT:
return new ExperimentAdaptor((ExperimentPE) entity);
return new ExperimentAdaptor((ExperimentPE) entity, evaluator);
case DATA_SET:
return new ExternalDataAdaptor((ExternalDataPE) entity);
return new ExternalDataAdaptor((ExternalDataPE) entity, evaluator);
case MATERIAL:
return new MaterialAdaptor((MaterialPE) entity);
return new MaterialAdaptor((MaterialPE) entity, evaluator);
default:
throw new UnsupportedOperationException(""); // can't happen
}
......
......@@ -16,6 +16,7 @@
package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.calculator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.IDynamicPropertyEvaluator;
import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;
/**
......@@ -27,10 +28,10 @@ public class ExperimentAdaptor extends AbstractEntityAdaptor
{
private final ExperimentPE experimentPE;
public ExperimentAdaptor(ExperimentPE experimentPE)
public ExperimentAdaptor(ExperimentPE experimentPE, IDynamicPropertyEvaluator evaluator)
{
super(experimentPE.getCode());
initProperties(experimentPE);
initProperties(experimentPE, evaluator);
this.experimentPE = experimentPE;
}
......
......@@ -16,6 +16,7 @@
package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.calculator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.IDynamicPropertyEvaluator;
import ch.systemsx.cisd.openbis.generic.shared.dto.ExternalDataPE;
/**
......@@ -27,10 +28,10 @@ public class ExternalDataAdaptor extends AbstractEntityAdaptor
{
private final ExternalDataPE externalDataPE;
public ExternalDataAdaptor(ExternalDataPE externalDataPE)
public ExternalDataAdaptor(ExternalDataPE externalDataPE, IDynamicPropertyEvaluator evaluator)
{
super(externalDataPE.getCode());
initProperties(externalDataPE);
initProperties(externalDataPE, evaluator);
this.externalDataPE = externalDataPE;
}
......
......@@ -16,6 +16,7 @@
package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.calculator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.IDynamicPropertyEvaluator;
import ch.systemsx.cisd.openbis.generic.shared.dto.MaterialPE;
/**
......@@ -27,10 +28,10 @@ public class MaterialAdaptor extends AbstractEntityAdaptor
{
private final MaterialPE MaterialPE;
public MaterialAdaptor(MaterialPE MaterialPE)
public MaterialAdaptor(MaterialPE MaterialPE, IDynamicPropertyEvaluator evaluator)
{
super(MaterialPE.getCode());
initProperties(MaterialPE);
initProperties(MaterialPE, evaluator);
this.MaterialPE = MaterialPE;
}
......
......@@ -16,6 +16,7 @@
package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.calculator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.IDynamicPropertyEvaluator;
import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
/**
......@@ -27,10 +28,10 @@ public class SampleAdaptor extends AbstractEntityAdaptor
{
private final SamplePE samplePE;
public SampleAdaptor(SamplePE samplePE)
public SampleAdaptor(SamplePE samplePE, IDynamicPropertyEvaluator evaluator)
{
super(samplePE.getCode());
initProperties(samplePE);
initProperties(samplePE, evaluator);
this.samplePE = samplePE;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment