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

[LMS-1565] improved speed of updating property assignment with default value

SVN: 16457
parent 37cf1f00
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,6 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
......@@ -32,7 +31,6 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.IEntityPropertyTypeDAO
import ch.systemsx.cisd.openbis.generic.shared.dto.EntityPropertyPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePE;
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.PersonPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.PropertyTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.Session;
......@@ -93,8 +91,6 @@ public class EntityTypePropertyTypeBO extends AbstractBusinessObject implements
public void loadAssignment(String propertyTypeCode, String entityTypeCode)
{
StopWatch watch = new StopWatch();
watch.start();
EntityTypePE entityType = findEntityType(entityTypeCode);
PropertyTypePE propertyType = findPropertyType(propertyTypeCode);
IEntityPropertyTypeDAO entityPropertyTypeDAO = getEntityPropertyTypeDAO(entityKind);
......@@ -137,48 +133,6 @@ public class EntityTypePropertyTypeBO extends AbstractBusinessObject implements
return getEntityPropertyTypeDAO(entityKind).listEntityIds(entityType);
}
private void slowAddPropertyWithDefaultValue(EntityTypePE entityType,
PropertyTypePE propertyType, String defaultValue,
List<IEntityPropertiesHolder> entities, String errorMsgTemplate)
{
final int size = entities.size();
if (operationLog.isDebugEnabled())
{
operationLog.debug(getMemoryUsageMessage());
}
if (size > 0)
{
if (StringUtils.isEmpty(defaultValue))
{
throw new UserFailureException(String.format(errorMsgTemplate, size, entityKind
.getLabel(), createPlural(size), entityType.getCode()));
}
PersonPE registrator = findRegistrator();
String validatedValue =
propertiesConverter.tryCreateValidatedPropertyValue(propertyType, assignment,
defaultValue);
for (int i = 0; i < entities.size(); i++)
{
if (i > 0 && i % 1000 == 0)
{
if (operationLog.isDebugEnabled())
{
operationLog.info("entity " + i + ", " + getMemoryUsageMessage());
}
getSessionFactory().getCurrentSession().flush();
}
IEntityPropertiesHolder entity = entities.get(i);
final EntityPropertyPE property =
propertiesConverter.createValidatedProperty(propertyType, assignment,
registrator, validatedValue);
if (property != null)
{
entity.addProperty(property);
}
}
}
}
private void addPropertyWithDefaultValue(EntityTypePE entityType, PropertyTypePE propertyType,
String defaultValue, List<Long> entityIds, String errorMsgTemplate)
{
......@@ -202,8 +156,6 @@ public class EntityTypePropertyTypeBO extends AbstractBusinessObject implements
final EntityPropertyPE property =
propertiesConverter.createValidatedProperty(propertyType, assignment,
registrator, validatedValue);
getSessionFactory().getCurrentSession().flush();
getSessionFactory().getCurrentSession().clear();
entityPropertyTypeDAO.createProperties(property, entityIds);
......@@ -240,16 +192,15 @@ public class EntityTypePropertyTypeBO extends AbstractBusinessObject implements
{
final EntityTypePE entityType = assignment.getEntityType();
final PropertyTypePE propertyType = assignment.getPropertyType();
List<IEntityPropertiesHolder> entities =
getEntityPropertyTypeDAO(entityKind).listEntitiesWithoutPropertyValue(
entityType, propertyType);
String errorMsgTemplate =
"Cannot change assignment to mandatory. "
+ "Please specify 'Update Value', which will be used for %s %s%s "
+ "of type '%s' already existing in the database "
+ "without any value for this property.";
// TODO 2009-07-01, Piotr Buczek: switch to addPropertyWithDefaultValue
slowAddPropertyWithDefaultValue(entityType, propertyType, defaultValue, entities,
List<Long> entityIds =
getEntityPropertyTypeDAO(entityKind).listIdsOfEntitiesWithoutPropertyValue(
assignment);
addPropertyWithDefaultValue(entityType, propertyType, defaultValue, entityIds,
errorMsgTemplate);
}
assignment.setMandatory(isMandatory);
......
......@@ -73,12 +73,11 @@ public interface IEntityPropertyTypeDAO
public List<Long> listEntityIds(final EntityTypePE entityType) throws DataAccessException;
/**
* Returns a list of all entities of given <var>entityType</var> that don't have value with
* given <var>propertyType</var>.
* Returns a list of ids of all entities of type from specified assignment with that don't have
* any value for property assigned with the assignment.
*/
public List<IEntityPropertiesHolder> listEntitiesWithoutPropertyValue(
final EntityTypePE entityType, final PropertyTypePE propertyType)
throws DataAccessException;
public List<Long> listIdsOfEntitiesWithoutPropertyValue(
final EntityTypePropertyTypePE assignment) throws DataAccessException;
/**
* Fills term usage statistics for the entity kind represented by this class.
......
......@@ -119,20 +119,6 @@ final class EntityPropertyTypeDAO extends AbstractDAO implements IEntityProperty
final EntityTypePropertyTypePE etpt = (EntityTypePropertyTypePE) criteria.uniqueResult();
return etpt;
}
public int countAssignmentValues(String entityTypeCode, String propertyTypeCode)
{
assert entityTypeCode != null : "Unspecified entity type.";
assert propertyTypeCode != null : "Unspecified property type.";
String query =
String.format("SELECT count(pv) FROM %s pa join pa.propertyValues pv "
+ "WHERE pa.propertyTypeInternal.simpleCode = ? "
+ "AND pa.entityTypeInternal.code = ?", entityKind
.getEntityTypePropertyTypeAssignmentClass().getSimpleName());
return ((Long) (getHibernateTemplate().find(query,
toArray(propertyTypeCode, entityTypeCode)).get(0))).intValue();
}
public final void createEntityPropertyTypeAssignment(
final EntityTypePropertyTypePE entityPropertyTypeAssignement)
......@@ -202,6 +188,30 @@ final class EntityPropertyTypeDAO extends AbstractDAO implements IEntityProperty
return list;
}
public List<Long> listIdsOfEntitiesWithoutPropertyValue(
final EntityTypePropertyTypePE assignment) throws DataAccessException
{
assert assignment != null : "Unspecified assignment.";
String query =
String
.format(
"SELECT s.id FROM %s s WHERE s.sampleType = ? AND s not in (SELECT sp.entity FROM %s sp WHERE sp.entityTypePropertyType = ?)",
entityKind.getEntityClass().getSimpleName(), entityKind
.getEntityPropertyClass().getSimpleName());
final List<Long> list =
cast(getHibernateTemplate().find(query,
toArray(assignment.getEntityType(), assignment)));
if (operationLog.isInfoEnabled())
{
operationLog.info(String.format(
"LIST: found %s ids of entities of type '%s' assigned to property '%s'.", list
.size(), assignment.getEntityType(), assignment.getPropertyType()));
}
return list;
}
public void createProperties(final EntityPropertyPE property, final List<Long> entityIds)
{
assert property != null : "Given property data can not be null.";
......@@ -292,50 +302,6 @@ final class EntityPropertyTypeDAO extends AbstractDAO implements IEntityProperty
return "MEMORY (in MB): free:" + freeMemory + " total:" + totalMemory + " max:" + maxMemory;
}
public List<IEntityPropertiesHolder> listEntitiesWithoutPropertyValue(
final EntityTypePE entityType, final PropertyTypePE propertyType)
throws DataAccessException
{
assert entityType != null : "Unspecified entity type.";
assert propertyType != null : "Unspecified property type.";
final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityClass());
criteria.add(Restrictions.eq(entityKind.getEntityTypeFieldName(), entityType));
//
final List<IEntityPropertiesHolder> list =
cast(getHibernateTemplate().findByCriteria(criteria));
// TODO 2009-07-01, Piotr Buczek: filter results with criteria
// final DetachedCriteria propertyTypesCriteria =
// DetachedCriteria.forClass(entityKind.getEntityPropertyClass());
// propertyTypesCriteria.add(Restrictions.eq("entityTypePropertyType", propertyType));
// criteria.add(Subqueries.notExists(propertyTypesCriteria.setProjection(Projections
// .property(...))));
final List<IEntityPropertiesHolder> result =
new ArrayList<IEntityPropertiesHolder>(list.size());
for (IEntityPropertiesHolder entity : list)
{
if (isEntityWithoutPropertyValue(entity, propertyType))
{
result.add(entity);
}
}
return result;
}
private final boolean isEntityWithoutPropertyValue(final IEntityPropertiesHolder entity,
final PropertyTypePE propertyType)
{
for (EntityPropertyPE property : entity.getProperties())
{
if (propertyType.equals(property.getEntityTypePropertyType()))
{
return false;
}
}
return true;
}
public void fillTermUsageStatistics(List<VocabularyTermWithStats> termsWithStats,
VocabularyPE vocabulary)
{
......@@ -438,6 +404,20 @@ final class EntityPropertyTypeDAO extends AbstractDAO implements IEntityProperty
getHibernateTemplate().flush();
}
public int countAssignmentValues(String entityTypeCode, String propertyTypeCode)
{
assert entityTypeCode != null : "Unspecified entity type.";
assert propertyTypeCode != null : "Unspecified property type.";
String query =
String.format("SELECT count(pv) FROM %s pa join pa.propertyValues pv "
+ "WHERE pa.propertyTypeInternal.simpleCode = ? "
+ "AND pa.entityTypeInternal.code = ?", entityKind
.getEntityTypePropertyTypeAssignmentClass().getSimpleName());
return ((Long) (getHibernateTemplate().find(query,
toArray(propertyTypeCode, entityTypeCode)).get(0))).intValue();
}
public void delete(EntityTypePropertyTypePE assignment)
{
HibernateTemplate template = getHibernateTemplate();
......
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