diff --git a/openbis/.classpath b/openbis/.classpath index 704866c1ac9eba2501106eecde9a8b8715d70b76..0255cd3acbeb0a58e5100f4462992c9cd844e7f0 100644 --- a/openbis/.classpath +++ b/openbis/.classpath @@ -81,5 +81,6 @@ <classpathentry kind="lib" path="/libraries/poi/poi.jar"/> <classpathentry kind="lib" path="/libraries/jetty7/lib/server/jetty-deploy.jar"/> <classpathentry kind="lib" path="/libraries/jetty7/lib/server/jetty-webapp.jar"/> + <classpathentry kind="lib" path="/libraries/lucene-queries/lucene-queries.jar"/> <classpathentry kind="output" path="targets/www/WEB-INF/classes"/> </classpath> diff --git a/openbis/build/build.xml b/openbis/build/build.xml index aa416782eecefb611b46a8922f98e8bed21056d3..71b0f6c22cb72cd8eeddf7a08d509b677c5d8786 100644 --- a/openbis/build/build.xml +++ b/openbis/build/build.xml @@ -528,7 +528,10 @@ <fileset dir="${lib}/lucene-memory"> <include name="lucene-memory.jar" /> </fileset> - <fileset dir="${lib}/cisd-args4j"> + <fileset dir="${lib}/lucene-queries"> + <include name="lucene-queries.jar" /> + </fileset> + <fileset dir="${lib}/cisd-args4j"> <include name="cisd-args4j.jar" /> </fileset> <fileset dir="${lib}/jython"> @@ -844,7 +847,10 @@ <lib dir="${lib}/lucene-memory"> <include name="lucene-memory.jar" /> </lib> - <lib dir="${lib}/cisd-args4j"> + <lib dir="${lib}/lucene-queries"> + <include name="lucene-queries.jar" /> + </lib> + <lib dir="${lib}/cisd-args4j"> <include name="cisd-args4j.jar" /> </lib> <lib dir="${lib}/jython"> diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java index 42fa141801690c5e6025ca22e66e56678e389ff4..e5f348f411f462123ab37c65da669aecbc6f144e 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java @@ -3190,7 +3190,9 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt DynamicPropertyCalculator.create(info.getScript()); IDynamicPropertyEvaluator evaluator = new DynamicPropertyEvaluator(getDAOFactory(), null); - IEntityAdaptor adaptor = EntityAdaptorFactory.create(entity, evaluator); + IEntityAdaptor adaptor = + EntityAdaptorFactory.create(entity, evaluator, getDAOFactory() + .getSessionFactory().getCurrentSession()); calculator.setEntity(adaptor); return calculator.evalAsString(); } catch (Throwable e) @@ -3228,7 +3230,9 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt }); IDynamicPropertyEvaluator evaluator = new DynamicPropertyEvaluator(getDAOFactory(), null); - IEntityAdaptor adaptor = EntityAdaptorFactory.create(entity, evaluator); + IEntityAdaptor adaptor = + EntityAdaptorFactory.create(entity, evaluator, getDAOFactory() + .getSessionFactory().getCurrentSession()); calculator.setEntity(adaptor); calculator.setIsNewEntity(info.isNew()); String result = calculator.evalAsString(); diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityValidationInterceptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityValidationInterceptor.java index 413004a24b5857f887421fee2d01b26aaaed191a..98e725942c90394053e7c6fdc404c1ee91e38b4d 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityValidationInterceptor.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityValidationInterceptor.java @@ -27,9 +27,9 @@ import org.hibernate.Interceptor; import org.hibernate.Transaction; import org.hibernate.type.Type; +import ch.systemsx.cisd.common.exceptions.UserFailureException; import ch.systemsx.cisd.openbis.common.conversation.context.ServiceConversationsThreadContext; import ch.systemsx.cisd.openbis.common.conversation.progress.IServiceConversationProgressListener; -import ch.systemsx.cisd.common.exceptions.UserFailureException; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.DynamicPropertyEvaluator; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.IDynamicPropertyEvaluator; @@ -253,7 +253,9 @@ public class EntityValidationInterceptor extends EmptyInterceptor implements EntityValidationCalculator calculator = EntityValidationCalculator.create(script.getScript(), this); IDynamicPropertyEvaluator evaluator = new DynamicPropertyEvaluator(daoFactory, null); - IEntityAdaptor adaptor = EntityAdaptorFactory.create(entity, evaluator); + IEntityAdaptor adaptor = + EntityAdaptorFactory.create(entity, evaluator, daoFactory.getSessionFactory() + .getCurrentSession()); calculator.setEntity(adaptor); calculator.setIsNewEntity(isNewEntity); return calculator.evalAsString(); diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DefaultBatchDynamicPropertyEvaluator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DefaultBatchDynamicPropertyEvaluator.java index 3004dd5f587f66e0b55f47e9bc6960ae1d4ddd2b..b415001a1df42a6fe4da3e27c248a7240e0b4860 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DefaultBatchDynamicPropertyEvaluator.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DefaultBatchDynamicPropertyEvaluator.java @@ -213,7 +213,7 @@ final class DefaultBatchDynamicPropertyEvaluator implements IBatchDynamicPropert { for (T entity : entities) { - evaluator.evaluateProperties(entity); + evaluator.evaluateProperties(entity, session); } session.flush(); session.clear(); diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DynamicPropertyEvaluator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DynamicPropertyEvaluator.java index 8917897b6e8868266d24da2ec27304883b4081e0..eeeb998892acb260452e93e5a9b39264c6a750b6 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DynamicPropertyEvaluator.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DynamicPropertyEvaluator.java @@ -24,6 +24,7 @@ import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; +import org.hibernate.Session; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; @@ -92,14 +93,16 @@ public class DynamicPropertyEvaluator implements IDynamicPropertyEvaluator } @Override - public <T extends IEntityInformationWithPropertiesHolder> void evaluateProperties(T entity) + public <T extends IEntityInformationWithPropertiesHolder> void evaluateProperties(T entity, + Session session) { if (operationLog.isDebugEnabled()) { operationLog.debug(String.format("Evaluating dynamic properties of entity '%s'.", entity)); } - final IEntityAdaptor entityAdaptor = EntityAdaptorFactory.create(entity, this); + final IEntityAdaptor entityAdaptor = + EntityAdaptorFactory.create(entity, this, session); Set<EntityPropertyPE> propertiesToRemove = new HashSet<EntityPropertyPE>(); for (EntityPropertyPE property : entity.getProperties()) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/IDynamicPropertyEvaluator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/IDynamicPropertyEvaluator.java index 8496c56e67f692fbcc028642e32c3b07ff620617..a86739cf558003f913a9706676d218a2550952f4 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/IDynamicPropertyEvaluator.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/IDynamicPropertyEvaluator.java @@ -18,6 +18,8 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property; import java.util.List; +import org.hibernate.Session; + import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IEntityAdaptor; import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePropertyTypePE; import ch.systemsx.cisd.openbis.generic.shared.dto.IEntityInformationWithPropertiesHolder; @@ -34,7 +36,8 @@ public interface IDynamicPropertyEvaluator * Evaluates all dynamic properties of specified entity. Replaces placeholders with evaluated * values. */ - public <T extends IEntityInformationWithPropertiesHolder> void evaluateProperties(T entity); + public <T extends IEntityInformationWithPropertiesHolder> void evaluateProperties(T entity, + Session session); /** * Evaluates value of specified dynamic property on specified entity. diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/AbstractEntityAdaptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/AbstractEntityAdaptor.java index ce02a66c0b05c0cadeffad758d7686d1b16acdce..c4ef867c9bb8dc00ed0cb5cc00de59594c4a79fb 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/AbstractEntityAdaptor.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/AbstractEntityAdaptor.java @@ -20,6 +20,19 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.regex.RegexQuery; +import org.hibernate.ScrollMode; +import org.hibernate.ScrollableResults; +import org.hibernate.Session; +import org.hibernate.search.FullTextQuery; +import org.hibernate.search.FullTextSession; +import org.hibernate.search.Search; + +import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.LuceneQueryBuilder; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.IDynamicPropertyEvaluator; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IEntityAdaptor; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IEntityPropertyAdaptor; @@ -29,6 +42,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePropertyTypePE; import ch.systemsx.cisd.openbis.generic.shared.dto.IEntityPropertiesHolder; import ch.systemsx.cisd.openbis.generic.shared.dto.MaterialPE; import ch.systemsx.cisd.openbis.generic.shared.dto.PropertyTypePE; +import ch.systemsx.cisd.openbis.generic.shared.dto.hibernate.SearchFieldConstants; /** * Abstract {@link IEntityAdaptor} implementation. @@ -44,6 +58,9 @@ public class AbstractEntityAdaptor implements IEntityAdaptor protected final IDynamicPropertyEvaluator evaluator; + protected static String ENTITY_TYPE_CODE_FIELD = SearchFieldConstants.PREFIX_ENTITY_TYPE + + SearchFieldConstants.CODE; + public AbstractEntityAdaptor(String code) { this(code, null); @@ -135,4 +152,32 @@ public class AbstractEntityAdaptor implements IEntityAdaptor return propertiesByCode.values(); } + protected Query regexpConstraint(String field, String value) + { + return new RegexQuery(new Term(field, value.toLowerCase())); + } + + protected Query constraint(String field, String value) + { + return LuceneQueryBuilder.parseQuery(field, value, LuceneQueryBuilder + .createSearchAnalyzer()); + } + + protected Query and(Query... queries) + { + BooleanQuery query = new BooleanQuery(); + for (Query subquery : queries) + { + query.add(subquery, BooleanClause.Occur.MUST); + } + return query; + } + + protected ScrollableResults execute(Query query, Class<?> resultClass, Session session) + { + FullTextSession fullTextSession = Search.getFullTextSession(session); + FullTextQuery ftQuery = fullTextSession.createFullTextQuery(query, resultClass); + ftQuery.setFetchSize(10); + return ftQuery.scroll(ScrollMode.FORWARD_ONLY); + } } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityAdaptorFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityAdaptorFactory.java index aab59e2b4c0477d7bed363fb4aa231e35d89c92a..d09040484d3b46880ea36a953416771af950d2e7 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityAdaptorFactory.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityAdaptorFactory.java @@ -16,6 +16,8 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator; +import org.hibernate.Session; + import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.IDynamicPropertyEvaluator; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IDataAdaptor; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IEntityAdaptor; @@ -35,19 +37,23 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE; */ public class EntityAdaptorFactory { - /** Returns an adaptor for specified entity based on its kind. */ + /** + * Returns an adaptor for specified entity based on its kind. + * + * @param session + */ public static IEntityAdaptor create(IEntityInformationWithPropertiesHolder entity, - IDynamicPropertyEvaluator evaluator) + IDynamicPropertyEvaluator evaluator, Session session) { switch (entity.getEntityKind()) { case SAMPLE: - return new SampleAdaptor((SamplePE) entity, evaluator); + return new SampleAdaptor((SamplePE) entity, evaluator, session); case EXPERIMENT: - return new ExperimentAdaptor((ExperimentPE) entity, evaluator); + return new ExperimentAdaptor((ExperimentPE) entity, evaluator, session); case DATA_SET: - return new ExternalDataAdaptor((DataPE) entity, evaluator); + return new ExternalDataAdaptor((DataPE) entity, evaluator, session); case MATERIAL: return new MaterialAdaptor((MaterialPE) entity, evaluator); default: @@ -55,19 +61,22 @@ public class EntityAdaptorFactory } } - public static IExperimentAdaptor create(ExperimentPE entity, IDynamicPropertyEvaluator evaluator) + public static IExperimentAdaptor create(ExperimentPE entity, + IDynamicPropertyEvaluator evaluator, Session session) { - return new ExperimentAdaptor(entity, evaluator); + return new ExperimentAdaptor(entity, evaluator, session); } - public static ISampleAdaptor create(SamplePE entity, IDynamicPropertyEvaluator evaluator) + public static ISampleAdaptor create(SamplePE entity, IDynamicPropertyEvaluator evaluator, + Session session) { - return new SampleAdaptor(entity, evaluator); + return new SampleAdaptor(entity, evaluator, session); } - public static IDataAdaptor create(DataPE entity, IDynamicPropertyEvaluator evaluator) + public static IDataAdaptor create(DataPE entity, IDynamicPropertyEvaluator evaluator, + Session session) { - return new ExternalDataAdaptor(entity, evaluator); + return new ExternalDataAdaptor(entity, evaluator, session); } public static IMaterialAdaptor create(MaterialPE entity, IDynamicPropertyEvaluator evaluator) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityAdaptorIterator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityAdaptorIterator.java new file mode 100644 index 0000000000000000000000000000000000000000..da660755ec5c197d8ec509cd82f6a0616d943cfa --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityAdaptorIterator.java @@ -0,0 +1,61 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator; + +import org.hibernate.ScrollableResults; +import org.hibernate.Session; + +import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.IDynamicPropertyEvaluator; +import ch.systemsx.cisd.openbis.generic.shared.dto.DataPE; +import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE; + +/** + * @author anttil + */ +public class EntityAdaptorIterator<T> extends ScrollableResultsIterator<T> +{ + + private final IDynamicPropertyEvaluator evaluator; + + private final Session session; + + public EntityAdaptorIterator(ScrollableResults scroll, IDynamicPropertyEvaluator evaluator, + Session session) + { + super(scroll); + this.evaluator = evaluator; + this.session = session; + + } + + @SuppressWarnings("unchecked") + @Override + public T parseValue(Object[] result) + { + Object o = result[0]; + if (o instanceof SamplePE) + { + return (T) EntityAdaptorFactory.create((SamplePE) o, evaluator, session); + } else if (o instanceof DataPE) + { + return (T) EntityAdaptorFactory.create((DataPE) o, evaluator, session); + } else + { + throw new IllegalStateException("Unknown result type: " + o); + } + } +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ExperimentAdaptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ExperimentAdaptor.java index 915b42996f109b4575c6d33cdbb178f77f474bc5..7da2d2852505009edb7b3cdf3d0d77759fc4d094 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ExperimentAdaptor.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ExperimentAdaptor.java @@ -16,8 +16,9 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator; -import java.util.ArrayList; -import java.util.List; +import org.apache.lucene.search.Query; +import org.hibernate.ScrollableResults; +import org.hibernate.Session; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.IDynamicPropertyEvaluator; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IDataAdaptor; @@ -27,6 +28,7 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calcu import ch.systemsx.cisd.openbis.generic.shared.dto.DataPE; import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE; import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE; +import ch.systemsx.cisd.openbis.generic.shared.dto.hibernate.SearchFieldConstants; /** * {@link IEntityAdaptor} implementation for {@link ExperimentPE}. @@ -38,9 +40,13 @@ public class ExperimentAdaptor extends AbstractEntityAdaptor implements IExperim { private final ExperimentPE experimentPE; - public ExperimentAdaptor(ExperimentPE experimentPE, IDynamicPropertyEvaluator evaluator) + private final Session session; + + public ExperimentAdaptor(ExperimentPE experimentPE, IDynamicPropertyEvaluator evaluator, + Session session) { super(experimentPE.getCode(), evaluator); + this.session = session; initProperties(experimentPE); this.experimentPE = experimentPE; } @@ -57,25 +63,40 @@ public class ExperimentAdaptor extends AbstractEntityAdaptor implements IExperim } @Override - public List<ISampleAdaptor> samples() + public Iterable<ISampleAdaptor> samples() + { + return samplesOfType(".*"); + } + + @Override + public Iterable<ISampleAdaptor> samplesOfType(String typeRegexp) { - List<ISampleAdaptor> list = new ArrayList<ISampleAdaptor>(); - for (SamplePE sample : experimentPE.getSamples()) - { - list.add(EntityAdaptorFactory.create(sample, evaluator)); - } - return list; + Query typeConstraint = + regexpConstraint(ENTITY_TYPE_CODE_FIELD, typeRegexp.toLowerCase()); + Query experimentCodeConstraint = + constraint(SearchFieldConstants.EXPERIMENT_ID, Long.toString(experimentPE.getId())); + Query query = and(typeConstraint, experimentCodeConstraint); + + ScrollableResults results = execute(query, SamplePE.class, session); + return new EntityAdaptorIterator<ISampleAdaptor>(results, evaluator, session); } @Override - public List<IDataAdaptor> dataSets() + public Iterable<IDataAdaptor> dataSets() { - List<IDataAdaptor> list = new ArrayList<IDataAdaptor>(); - for (DataPE dataset : experimentPE.getDataSets()) - { - list.add(EntityAdaptorFactory.create(dataset, evaluator)); - } - return list; + return dataSetsOfType(".*"); } + @Override + public Iterable<IDataAdaptor> dataSetsOfType(String typeRegexp) + { + Query typeConstraint = + regexpConstraint(ENTITY_TYPE_CODE_FIELD, typeRegexp.toLowerCase()); + Query experimentCodeConstraint = + constraint(SearchFieldConstants.EXPERIMENT_ID, Long.toString(experimentPE.getId())); + Query query = and(typeConstraint, experimentCodeConstraint); + + ScrollableResults results = execute(query, DataPE.class, session); + return new EntityAdaptorIterator<IDataAdaptor>(results, evaluator, session); + } } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ExternalDataAdaptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ExternalDataAdaptor.java index 693bde2062ce6438d51be41f8a6a846cb6f5f3ea..c63475b6db0cc788c38704f6c2479bba57c96cd9 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ExternalDataAdaptor.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ExternalDataAdaptor.java @@ -19,6 +19,8 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calc import java.util.ArrayList; import java.util.List; +import org.hibernate.Session; + import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.IDynamicPropertyEvaluator; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IDataAdaptor; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IEntityAdaptor; @@ -37,9 +39,13 @@ public class ExternalDataAdaptor extends AbstractEntityAdaptor implements IDataA { private final DataPE externalDataPE; - public ExternalDataAdaptor(DataPE externalDataPE, IDynamicPropertyEvaluator evaluator) + private final Session session; + + public ExternalDataAdaptor(DataPE externalDataPE, IDynamicPropertyEvaluator evaluator, + Session session) { super(externalDataPE.getCode(), evaluator); + this.session = session; initProperties(externalDataPE); this.externalDataPE = externalDataPE; } @@ -58,7 +64,7 @@ public class ExternalDataAdaptor extends AbstractEntityAdaptor implements IDataA @Override public IExperimentAdaptor experiment() { - return EntityAdaptorFactory.create(externalDataPE.getExperiment(), evaluator); + return EntityAdaptorFactory.create(externalDataPE.getExperiment(), evaluator, session); } @Override @@ -68,7 +74,7 @@ public class ExternalDataAdaptor extends AbstractEntityAdaptor implements IDataA for (DataSetRelationshipPE relationship : externalDataPE.getParentRelationships()) { DataPE parent = relationship.getParentDataSet(); - list.add(EntityAdaptorFactory.create(parent, evaluator)); + list.add(EntityAdaptorFactory.create(parent, evaluator, session)); } return list; } @@ -80,7 +86,7 @@ public class ExternalDataAdaptor extends AbstractEntityAdaptor implements IDataA for (DataSetRelationshipPE relationship : externalDataPE.getChildRelationships()) { DataPE child = relationship.getChildDataSet(); - list.add(EntityAdaptorFactory.create(child, evaluator)); + list.add(EntityAdaptorFactory.create(child, evaluator, session)); } return list; } @@ -91,7 +97,7 @@ public class ExternalDataAdaptor extends AbstractEntityAdaptor implements IDataA List<IDataAdaptor> list = new ArrayList<IDataAdaptor>(); for (DataPE contained : externalDataPE.getContainedDataSets()) { - list.add(EntityAdaptorFactory.create(contained, evaluator)); + list.add(EntityAdaptorFactory.create(contained, evaluator, session)); } return list; } @@ -102,7 +108,7 @@ public class ExternalDataAdaptor extends AbstractEntityAdaptor implements IDataA DataPE container = externalDataPE.getContainer(); if (container != null) { - return EntityAdaptorFactory.create(container, evaluator); + return EntityAdaptorFactory.create(container, evaluator, session); } else { return null; diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/SampleAdaptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/SampleAdaptor.java index d2895cb73385a4622f0187bff757c989d47c30eb..2487d63405a37c42fb020993a2e9aedfeed6fa7e 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/SampleAdaptor.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/SampleAdaptor.java @@ -19,6 +19,8 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calc import java.util.ArrayList; import java.util.List; +import org.hibernate.Session; + import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.IDynamicPropertyEvaluator; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IEntityAdaptor; import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IExperimentAdaptor; @@ -36,9 +38,12 @@ public class SampleAdaptor extends AbstractEntityAdaptor implements ISampleAdapt { private final SamplePE samplePE; - public SampleAdaptor(SamplePE samplePE, IDynamicPropertyEvaluator evaluator) + private final Session session; + + public SampleAdaptor(SamplePE samplePE, IDynamicPropertyEvaluator evaluator, Session session) { super(samplePE.getCode(), evaluator); + this.session = session; initProperties(samplePE); this.samplePE = samplePE; } @@ -57,7 +62,7 @@ public class SampleAdaptor extends AbstractEntityAdaptor implements ISampleAdapt @Override public IExperimentAdaptor experiment() { - return EntityAdaptorFactory.create(samplePE.getExperiment(), evaluator); + return EntityAdaptorFactory.create(samplePE.getExperiment(), evaluator, session); } @Override @@ -66,7 +71,7 @@ public class SampleAdaptor extends AbstractEntityAdaptor implements ISampleAdapt List<ISampleAdaptor> list = new ArrayList<ISampleAdaptor>(); for (SamplePE parent : samplePE.getParents()) { - list.add(EntityAdaptorFactory.create(parent, evaluator)); + list.add(EntityAdaptorFactory.create(parent, evaluator, session)); } return list; } @@ -78,7 +83,7 @@ public class SampleAdaptor extends AbstractEntityAdaptor implements ISampleAdapt for (SampleRelationshipPE relationship : samplePE.getChildRelationships()) { SamplePE child = relationship.getChildSample(); - list.add(EntityAdaptorFactory.create(child, evaluator)); + list.add(EntityAdaptorFactory.create(child, evaluator, session)); } return list; } @@ -89,7 +94,7 @@ public class SampleAdaptor extends AbstractEntityAdaptor implements ISampleAdapt List<ISampleAdaptor> list = new ArrayList<ISampleAdaptor>(); for (SamplePE contained : samplePE.getContained()) { - list.add(EntityAdaptorFactory.create(contained, evaluator)); + list.add(EntityAdaptorFactory.create(contained, evaluator, session)); } return list; } @@ -100,7 +105,7 @@ public class SampleAdaptor extends AbstractEntityAdaptor implements ISampleAdapt SamplePE container = samplePE.getContainer(); if (container != null) { - return EntityAdaptorFactory.create(container, evaluator); + return EntityAdaptorFactory.create(container, evaluator, session); } else { return null; diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ScrollableResultsIterator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ScrollableResultsIterator.java new file mode 100644 index 0000000000000000000000000000000000000000..bdd9c4fe5ebd7c57fc180cea88aca2fccaf77033 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/ScrollableResultsIterator.java @@ -0,0 +1,88 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator; + +import java.util.Iterator; + +import org.hibernate.ScrollableResults; + +/** + * @author anttil + */ +public abstract class ScrollableResultsIterator<T> implements Iterable<T> +{ + + private final ScrollableResults scroll; + + public ScrollableResultsIterator(ScrollableResults scroll) + { + this.scroll = scroll; + } + + public abstract T parseValue(Object[] result); + + @Override + public Iterator<T> iterator() + { + return new Iterator<T>() + { + Object[] current = null; + + @Override + public boolean hasNext() + { + if (current != null) + { + return true; + } + + if (scroll.next()) + { + current = scroll.get(); + return true; + } else + { + return false; + } + + } + + @Override + public T next() + { + if (current == null) + { + if (scroll.next() == false) + { + return null; + } + current = scroll.get(); + } + + Object[] o = current; + current = null; + return parseValue(o); + } + + @Override + public void remove() + { + throw new UnsupportedOperationException(); + } + }; + } +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/api/IExperimentAdaptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/api/IExperimentAdaptor.java index 5650d2a3adc99a86033df49075aae2296c14da32..fe857c5363dd06b4a095bbb038cf9538639f13b4 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/api/IExperimentAdaptor.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/api/IExperimentAdaptor.java @@ -16,7 +16,6 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api; -import java.util.List; /** * @author Jakub Straszewski @@ -24,8 +23,12 @@ import java.util.List; public interface IExperimentAdaptor { /** Return the samples belonging to this experiment */ - List<ISampleAdaptor> samples(); + Iterable<ISampleAdaptor> samples(); + + Iterable<ISampleAdaptor> samplesOfType(String typeCodeRegexp); /** Return the datasets belonging to this experiment */ - List<IDataAdaptor> dataSets(); + Iterable<IDataAdaptor> dataSets(); + + Iterable<IDataAdaptor> dataSetsOfType(String typeCodeRegexp); } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DynamicPropertyEvaluatorTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DynamicPropertyEvaluatorTest.java index 0cde9d758ecef590ff006cf5ce648a808dda2848..3be836117145f0f3828ff17a6a3ad9dad99741fb 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DynamicPropertyEvaluatorTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/DynamicPropertyEvaluatorTest.java @@ -140,7 +140,7 @@ public class DynamicPropertyEvaluatorTest extends AbstractBOTest // create sample with all properties created above and evaluate dynamic properties final SamplePE sample = createSample("s1", properties); - evaluator.evaluateProperties(sample); + evaluator.evaluateProperties(sample, null); // check if evaluated values are correct final String expectedDp1Value = String.format("%s %s", p1.getValue(), p2.getValue()); @@ -200,7 +200,7 @@ public class DynamicPropertyEvaluatorTest extends AbstractBOTest // create sample with all properties created above and evaluate dynamic properties final SamplePE sample = createSample("s1", properties); - evaluator.evaluateProperties(sample); + evaluator.evaluateProperties(sample, null); // check if evaluated values are correct assertEquals(term2.getCode(), dpVarchar.getValue()); @@ -298,7 +298,7 @@ public class DynamicPropertyEvaluatorTest extends AbstractBOTest // create sample with all properties created above and evaluate dynamic properties final SamplePE sample = createSample("s1", properties); - evaluator.evaluateProperties(sample); + evaluator.evaluateProperties(sample, null); // check if evaluated values are correct final String materialIdentifier = @@ -350,7 +350,7 @@ public class DynamicPropertyEvaluatorTest extends AbstractBOTest properties.add(dp2); properties.add(dp3); SamplePE sample = createSample("s1", properties); - evaluator.evaluateProperties(sample); + evaluator.evaluateProperties(sample, null); // all values should be equal to value of p1 assertEquals(p1.getValue(), dp1.getValue()); assertEquals(p1.getValue(), dp2.getValue()); @@ -363,7 +363,7 @@ public class DynamicPropertyEvaluatorTest extends AbstractBOTest properties.add(p1); properties.add(dp1); sample = createSample("s1", properties); - evaluator.evaluateProperties(sample); + evaluator.evaluateProperties(sample, null); // cyclic dependency should be found assertEquals(expectedCyclicDependencyErrorMessage(dp1, dp1), dp1.getValue()); @@ -375,7 +375,7 @@ public class DynamicPropertyEvaluatorTest extends AbstractBOTest properties.add(dp1); properties.add(dp2); sample = createSample("s1", properties); - evaluator.evaluateProperties(sample); + evaluator.evaluateProperties(sample, null); // cyclic dependency should be found assertEquals(expectedCyclicDependencyErrorMessage(dp2, dp1, dp2), dp1.getValue()); assertEquals(expectedCyclicDependencyErrorMessage(dp2, dp1, dp2), dp2.getValue()); @@ -390,7 +390,7 @@ public class DynamicPropertyEvaluatorTest extends AbstractBOTest properties.add(dp2); properties.add(dp3); sample = createSample("s1", properties); - evaluator.evaluateProperties(sample); + evaluator.evaluateProperties(sample, null); // cyclic dependency should be found assertEquals(expectedCyclicDependencyErrorMessage(dp2, dp3, dp1, dp2), dp1.getValue()); assertEquals(expectedCyclicDependencyErrorMessage(dp2, dp3, dp1, dp2), dp2.getValue());