Dictionary/Constraint improvements (required by MOB-1276)

- get constraints (for model)
- add optional title and/or description

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@15913 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jan Vonka
2009-08-25 21:29:27 +00:00
parent 9fff828051
commit 1bba527f56
16 changed files with 1680 additions and 1404 deletions

View File

@@ -35,10 +35,10 @@ import org.alfresco.service.cmr.dictionary.AspectDefinition;
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ClassDefinition;
import org.alfresco.service.cmr.dictionary.ConstraintDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryException;
import org.alfresco.service.cmr.dictionary.ModelDefinition;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.namespace.DynamicNamespacePrefixResolver;
import org.alfresco.service.namespace.NamespaceException;
@@ -414,6 +414,14 @@ import org.apache.commons.logging.LogFactory;
return associations.values();
}
/**
* @return the compiled constraints
*/
public Collection<ConstraintDefinition> getConstraints()
{
return constraints.values();
}
/* (non-Javadoc)
* @see org.alfresco.repo.dictionary.impl.ModelQuery#getConstraint(QName)

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -33,6 +33,7 @@ import org.alfresco.repo.tenant.TenantDeployer;
import org.alfresco.service.cmr.dictionary.AspectDefinition;
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ClassDefinition;
import org.alfresco.service.cmr.dictionary.ConstraintDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.ModelDefinition;
@@ -397,6 +398,11 @@ public class DictionaryComponent implements DictionaryService, TenantDeployer
return props;
}
public Collection<ConstraintDefinition> getConstraints(QName model)
{
return dictionaryDAO.getConstraints(model);
}
public void init()
{
dictionaryDAO.init();

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -28,8 +28,9 @@ import java.util.Collection;
import org.alfresco.service.cmr.dictionary.AspectDefinition;
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ModelDefinition;
import org.alfresco.service.cmr.dictionary.ConstraintDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.ModelDefinition;
import org.alfresco.service.cmr.dictionary.NamespaceDefinition;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
@@ -142,6 +143,12 @@ public interface DictionaryDAO extends ModelQuery
*/
public Collection<NamespaceDefinition> getNamespaces(QName modelName);
/**
* @param model the model to retrieve constraints for
* @return the constraints of the model
*/
public Collection<ConstraintDefinition> getConstraints(QName model);
/**
* validate against dictionary
*

View File

@@ -932,6 +932,15 @@ public class DictionaryDAOImpl implements DictionaryDAO
return namespaces;
}
/* (non-Javadoc)
* @see org.alfresco.repo.dictionary.DictionaryDAO#getConstraints(org.alfresco.service.namespace.QName)
*/
public Collection<ConstraintDefinition> getConstraints(QName modelName)
{
CompiledModel model = getCompiledModel(modelName);
return model.getConstraints();
}
// re-entrant (eg. via reset)
private DictionaryRegistry getDictionaryRegistry(String tenantDomain)
{

View File

@@ -29,6 +29,7 @@ import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -204,6 +205,35 @@ public class DictionaryDAOTest extends TestCase
assertNotNull("Constraint reg1 not registered", ConstraintRegistry.getInstance().getConstraint("cm:reg1"));
assertNotNull("Constraint reg2 not registered", ConstraintRegistry.getInstance().getConstraint("cm:reg2"));
QName model = QName.createQName(TEST_URL, "dictionarydaotest");
Collection<ConstraintDefinition> modelConstraints = service.getConstraints(model);
assertEquals(15, modelConstraints.size()); // 8 + 7
QName conRegExp1QName = QName.createQName(TEST_URL, "regex1");
boolean found1 = false;
QName conStrLen1QName = QName.createQName(TEST_URL, "stringLength1");
boolean found2 = false;
for (ConstraintDefinition constraintDef : modelConstraints)
{
if (constraintDef.getName().equals(conRegExp1QName))
{
assertEquals("Regex1 title", constraintDef.getTitle());
assertEquals("Regex1 description", constraintDef.getDescription());
found1 = true;
}
if (constraintDef.getName().equals(conStrLen1QName))
{
assertNull(constraintDef.getTitle());
assertNull(constraintDef.getDescription());
found2 = true;
}
}
assertTrue(found1);
assertTrue(found2);
// get the constraints for a property without constraints
QName propNoConstraintsQName = QName.createQName(TEST_URL, "fileprop");
PropertyDefinition propNoConstraintsDef = service.getProperty(propNoConstraintsQName);
@@ -222,6 +252,17 @@ public class DictionaryDAOTest extends TestCase
// check the individual constraints
ConstraintDefinition constraintDef = constraints.get(0);
assertTrue("Constraint anonymous name incorrect", constraintDef.getName().getLocalName().startsWith("prop1_anon"));
// inherit title / description for reference constraint
assertTrue("Constraint title incorrect", constraintDef.getTitle().equals("Regex1 title"));
assertTrue("Constraint description incorrect", constraintDef.getDescription().equals("Regex1 description"));
constraintDef = constraints.get(1);
assertTrue("Constraint anonymous name incorrect", constraintDef.getName().getLocalName().startsWith("prop1_anon"));
assertTrue("Constraint title incorrect", constraintDef.getTitle().equals("Prop1 Strlen1 title"));
assertTrue("Constraint description incorrect", constraintDef.getDescription().equals("Prop1 Strlen1 description"));
// check that the constraint implementation is valid (it used a reference)
Constraint constraint = constraintDef.getConstraint();
assertNotNull("Reference constraint has no implementation", constraint);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -37,6 +37,7 @@ public class M2Constraint
private String name;
private String ref;
private String type;
private String title;
private String description;
private List<M2NamedValue> parameters = new ArrayList<M2NamedValue>(2);
@@ -70,13 +71,63 @@ public class M2Constraint
return type;
}
public void setType(String type)
{
this.type = type;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public List<M2NamedValue> getParameters()
{
return parameters;
}
public M2NamedValue createParameter(String name, String simpleValue)
{
M2NamedValue param = new M2NamedValue();
param.setName(name);
param.setSimpleValue(simpleValue);
parameters.add(param);
return param;
}
public M2NamedValue createParameter(String name, List<String> listValue)
{
M2NamedValue param = new M2NamedValue();
param.setName(name);
param.setListValue(listValue);
parameters.add(param);
return param;
}
public void removeParameter(String name)
{
List<M2NamedValue> params = getParameters();
for (M2NamedValue param : params)
{
if (param.getName().equals(name))
{
parameters.remove(param);
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -50,6 +50,8 @@ import org.springframework.beans.PropertyAccessException;
/* package */class M2ConstraintDefinition implements ConstraintDefinition
{
private static final String PROP_SHORT_NAME = "shortName";
private static final String PROP_TITLE = "title";
private static final String PROP_DESCRIPTION = "description";
public static final String ERR_CYCLIC_REF = "d_dictionary.constraint.err.cyclic_ref";
public static final String ERR_TYPE_AND_REF = "d_dictionary.constraint.err.type_and_ref";
@@ -128,9 +130,14 @@ import org.springframework.beans.PropertyAccessException;
// already been resolved
return;
}
String shortName = name.toPrefixString();
String ref = m2Constraint.getRef();
String type = m2Constraint.getType();
String title = m2Constraint.getTitle();
String description = m2Constraint.getDescription();
if (ref != null && type != null)
{
throw new DictionaryException(ERR_TYPE_AND_REF, shortName);
@@ -153,6 +160,16 @@ import org.springframework.beans.PropertyAccessException;
constraintDef.resolveDependencies(query);
// just use the constraint provided by the referenced definition
this.constraint = constraintDef.getConstraint();
if (m2Constraint.getTitle() == null)
{
m2Constraint.setTitle(constraintDef.getTitle());
}
if (m2Constraint.getDescription() == null)
{
m2Constraint.setDescription(constraintDef.getDescription());
}
}
else
{
@@ -184,6 +201,7 @@ import org.springframework.beans.PropertyAccessException;
throw new DictionaryException(ERR_CONSTRUCT_FAILURE, type, shortName);
}
}
// property setters
BeanWrapper beanWrapper = new BeanWrapperImpl(constraint);
List<M2NamedValue> constraintNamedValues = m2Constraint.getParameters();
@@ -241,9 +259,19 @@ import org.springframework.beans.PropertyAccessException;
{
throw new DictionaryException(ERR_PROPERTY_MISMATCH, e, shortName, shortName);
}
}
if ((title != null) && (beanWrapper.isWritableProperty(PROP_TITLE)))
{
beanWrapper.setPropertyValue(PROP_TITLE, title);
}
if ((title != null) && (beanWrapper.isWritableProperty(PROP_DESCRIPTION)))
{
beanWrapper.setPropertyValue(PROP_DESCRIPTION, description);
}
}
// now initialize
constraint.initialize();
}
@@ -268,6 +296,26 @@ import org.springframework.beans.PropertyAccessException;
return name;
}
public String getTitle()
{
String value = M2Label.getLabel(model, "constraint", name, "title");
if (value == null)
{
value = m2Constraint.getTitle();
}
return value;
}
public String getDescription()
{
String value = M2Label.getLabel(model, "constraint", name, "description");
if (value == null)
{
value = m2Constraint.getDescription();
}
return value;
}
public Constraint getConstraint()
{
return constraint;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -392,6 +392,36 @@ public class M2Model
return Collections.unmodifiableList(constraints);
}
public M2Constraint getConstraint(String name)
{
for (M2Constraint candidate : constraints)
{
if (candidate.getName().equals(name))
{
return candidate;
}
}
return null;
}
public M2Constraint createConstraint(String name, String type)
{
M2Constraint constraint = new M2Constraint();
constraint.setName(name);
constraint.setType(type);
constraints.add(constraint);
return constraint;
}
public void removeConstraint(String name)
{
M2Constraint constraint = getConstraint(name);
if (constraint != null)
{
constraints.remove(name);
}
}
// Do not delete: referenced by m2binding.xml
@SuppressWarnings("unused")
private static List createList()

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -68,4 +68,30 @@ public class M2NamedValue
{
return listValue;
}
public void setName(String name)
{
this.name = name;
}
public void setSimpleValue(String simpleValue)
{
this.simpleValue = simpleValue;
}
public void setListValue(List<String> listValue)
{
this.listValue = listValue;
}
public boolean hasSimpleValue()
{
return (this.simpleValue != null);
}
public boolean hasListValue()
{
return (this.listValue != null);
}
}

View File

@@ -46,6 +46,8 @@ public abstract class AbstractConstraint implements Constraint
/** The constraint name. May be useful in error messages */
private String shortName;
private String title;
private ConstraintRegistry registry;
/**
@@ -95,6 +97,16 @@ public abstract class AbstractConstraint implements Constraint
return this.getClass().getName();
}
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return title;
}
public Map<String, Object> getParameters()
{
return new HashMap<String, Object>(3);

View File

@@ -109,6 +109,14 @@ public final class RegisteredConstraint implements Constraint
return getConstraint().getType();
}
/**
* Defers to the registered constraint
*/
public String getTitle()
{
return getConstraint().getTitle();
}
/**
* Defers to the registered constraint
*/

View File

@@ -30,6 +30,8 @@
<parameter name="registeredName"><value>cm:reg2</value></parameter>
</constraint>
<constraint name="test:regex1" type="REGEX">
<title>Regex1 title</title>
<description>Regex1 description</description>
<parameter name="expression"><value>[A-Z]*</value></parameter>
<parameter name="requiresMatch"><value>false</value></parameter>
</constraint>
@@ -74,7 +76,10 @@
<default></default>
<constraints>
<constraint ref="test:regex1"/>
<constraint ref="test:stringLength1"/>
<constraint ref="test:stringLength1">
<title>Prop1 Strlen1 title</title>
<description>Prop1 Strlen1 description</description>
</constraint>
<constraint ref="test:registered1"/>
</constraints>
</property>

View File

@@ -122,8 +122,8 @@
<mapping abstract="true" class="org.alfresco.repo.dictionary.M2NamedValue">
<value style="attribute" name="name" field="name" />
<value name="value" field="simpleValue" usage="optional" />
<structure name="list" usage="optional">
<value name="value" field="simpleValue" test-method="hasSimpleValue" usage="optional"/>
<structure name="list" test-method="hasListValue" usage="optional">
<collection field="listValue" factory="org.alfresco.repo.dictionary.M2Model.createList" usage="optional">
<value name="value" style="element" usage="optional" type="java.lang.String"/>
</collection>
@@ -134,6 +134,7 @@
<value style="attribute" name="name" field="name" usage="optional"/>
<value style="attribute" name="type" field="type" usage="optional"/>
<value style="attribute" name="ref" field="ref" usage="optional"/>
<value name="title" field="title" usage="optional"/>
<value name="description" field="description" usage="optional"/>
<collection field="parameters" factory="org.alfresco.repo.dictionary.M2Model.createList" usage="optional">
<structure name="parameter" type="org.alfresco.repo.dictionary.M2NamedValue" />

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -53,6 +53,11 @@ public interface Constraint
*/
public String getType();
/**
* @return the human-readable constraint title (optional)
*/
public String getTitle();
/**
* Returns the parameters passed to the instance of the constraint.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -43,6 +43,16 @@ public interface ConstraintDefinition
*/
public QName getName();
/**
* @return the human-readable class title
*/
public String getTitle();
/**
* @return the human-readable class description
*/
public String getDescription();
/**
* @return Returns the constraint implementation
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -25,6 +25,7 @@
package org.alfresco.service.cmr.dictionary;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.alfresco.service.NotAuditable;
@@ -262,6 +263,14 @@ public interface DictionaryService
@NotAuditable
Collection<QName> getAllAssociations();
/**
* Get constraints for the specified model
*
* @param model
* @return
*/
public Collection<ConstraintDefinition> getConstraints(QName model);
// TODO: Behaviour definitions
}