use of org.alfresco.rest.api.model.CustomModelConstraint in project alfresco-remote-api by Alfresco.
the class CustomModelsImpl method setM2Properties.
private void setM2Properties(M2Class m2Class, List<CustomModelProperty> properties, String namespacePrefix, Set<Pair<String, String>> namespacesToImport) {
if (properties != null) {
for (CustomModelProperty prop : properties) {
validateName(prop.getName(), "cmm.rest_api.property_name_null");
M2Property m2Property = m2Class.createProperty(constructName(prop.getName(), namespacePrefix));
m2Property.setTitle(prop.getTitle());
m2Property.setDescription(prop.getDescription());
m2Property.setMandatory(prop.isMandatory());
m2Property.setMandatoryEnforced(prop.isMandatoryEnforced());
m2Property.setMultiValued(prop.isMultiValued());
String dataType = prop.getDataType();
// Default type is d:text
if (StringUtils.isBlank(dataType)) {
dataType = DEFAULT_DATA_TYPE;
} else {
if (!dataType.contains(":")) {
throw new InvalidArgumentException("cmm.rest_api.property_datatype_invalid", new Object[] { dataType });
}
}
namespacesToImport.add(resolveToUriAndPrefix(dataType));
try {
// validate default values
this.valueDataTypeValidator.validateValue(dataType, prop.getDefaultValue());
} catch (Exception ex) {
throw new InvalidArgumentException(ex.getMessage());
}
m2Property.setType(dataType);
m2Property.setDefaultValue(prop.getDefaultValue());
// Set indexing options
m2Property.setIndexed(prop.isIndexed());
// so it can support boolean data type.
if (!BOOLEAN_DATA_TYPE.equals(dataType)) {
if (Facetable.TRUE == prop.getFacetable()) {
m2Property.setFacetable(true);
} else if (Facetable.FALSE == prop.getFacetable()) {
m2Property.setFacetable(false);
}
}
m2Property.setIndexTokenisationMode(prop.getIndexTokenisationMode());
// Check for constraints
List<String> constraintRefs = prop.getConstraintRefs();
List<CustomModelConstraint> constraints = prop.getConstraints();
if (constraintRefs.size() > 0) {
for (String ref : constraintRefs) {
Pair<String, String> prefixLocalName = splitPrefixedQName(ref);
if (!namespacePrefix.equals(prefixLocalName.getFirst())) {
throw new ConstraintViolatedException(I18NUtil.getMessage("cmm.rest_api.constraint_ref_not_defined", ref));
}
m2Property.addConstraintRef(ref);
}
}
if (constraints.size() > 0) {
for (CustomModelConstraint modelConstraint : constraints) {
String constraintName = null;
if (modelConstraint.getName() != null) {
validateName(modelConstraint.getName(), CONSTRAINT_NAME_NULL_ERR);
constraintName = constructName(modelConstraint.getName(), namespacePrefix);
}
M2Constraint m2Constraint = m2Property.addConstraint(constraintName, modelConstraint.getType());
// Set title, desc and parameters
setConstraintOtherData(modelConstraint, m2Constraint, dataType);
}
}
}
}
}
use of org.alfresco.rest.api.model.CustomModelConstraint in project alfresco-remote-api by Alfresco.
the class CustomModelsImpl method getCustomModelConstraints.
@Override
public CollectionWithPagingInfo<CustomModelConstraint> getCustomModelConstraints(String modelName, Parameters parameters) {
CustomModelDefinition modelDef = getCustomModelImpl(modelName);
Collection<ConstraintDefinition> constraintDefinitions = modelDef.getModelDefinedConstraints();
// TODO Should we support paging?
Paging paging = Paging.DEFAULT;
List<CustomModelConstraint> customModelConstraints = convertToCustomModelConstraints(constraintDefinitions);
return CollectionWithPagingInfo.asPaged(paging, customModelConstraints, false, constraintDefinitions.size());
}
use of org.alfresco.rest.api.model.CustomModelConstraint in project alfresco-remote-api by Alfresco.
the class CustomModelsImpl method createCustomModel.
@Override
public CustomModel createCustomModel(M2Model m2Model) {
// Check the current user is authorised to import the custom model
validateCurrentUser();
validateImportedM2Model(m2Model);
CompiledModel compiledModel = null;
try {
compiledModel = customModelService.compileModel(m2Model);
} catch (CustomModelConstraintException mce) {
throw new ConstraintViolatedException(mce.getMessage());
} catch (InvalidCustomModelException iex) {
throw new InvalidArgumentException(iex.getMessage());
}
ModelDefinition modelDefinition = compiledModel.getModelDefinition();
CustomModel customModel = new CustomModel();
customModel.setName(modelDefinition.getName().getLocalName());
customModel.setAuthor(modelDefinition.getAuthor());
customModel.setDescription(modelDefinition.getDescription(dictionaryService));
customModel.setStatus(ModelStatus.DRAFT);
NamespaceDefinition nsd = modelDefinition.getNamespaces().iterator().next();
customModel.setNamespaceUri(nsd.getUri());
customModel.setNamespacePrefix(nsd.getPrefix());
List<CustomType> customTypes = convertToCustomTypes(compiledModel.getTypes(), false);
List<CustomAspect> customAspects = convertToCustomAspects(compiledModel.getAspects(), false);
List<ConstraintDefinition> constraintDefinitions = CustomModelDefinitionImpl.removeInlineConstraints(compiledModel);
List<CustomModelConstraint> customModelConstraints = convertToCustomModelConstraints(constraintDefinitions);
customModel.setTypes(customTypes);
customModel.setAspects(customAspects);
customModel.setConstraints(customModelConstraints);
return createCustomModelImpl(customModel, false);
}
use of org.alfresco.rest.api.model.CustomModelConstraint in project alfresco-remote-api by Alfresco.
the class TestCustomConstraint method testCreateListConstraintInvalid.
@Test
public void testCreateListConstraintInvalid() throws Exception {
setRequestContext(customModelAdmin);
String modelName = "testModelConstraintInvalid" + System.currentTimeMillis();
final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
// Create aspect
String aspectName = "testAspect" + System.currentTimeMillis();
createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
// Update the Aspect by adding property
CustomAspect aspectPayload = new CustomAspect();
aspectPayload.setName(aspectName);
final String aspectPropName = "testAspect1Prop" + System.currentTimeMillis();
CustomModelProperty aspectProp = new CustomModelProperty();
aspectProp.setName(aspectPropName);
aspectProp.setTitle("property title");
aspectProp.setDataType("d:int");
// Create LIST constraint
String inlineListConstraintName = "testListConstraint" + System.currentTimeMillis();
CustomModelConstraint inlineListConstraint = new CustomModelConstraint();
inlineListConstraint.setName(inlineListConstraintName);
inlineListConstraint.setType("LIST");
inlineListConstraint.setTitle("test List title");
inlineListConstraint.setDescription("test List desc");
// Create the List constraint's parameters
List<CustomModelNamedValue> parameters = new ArrayList<>(3);
// text list value, but the the property data type is d:int
parameters.add(buildNamedValue("allowedValues", null, "a", "b", "c"));
parameters.add(buildNamedValue("sorted", "false"));
// Add the parameters into the constraint
inlineListConstraint.setParameters(parameters);
// Add inline constraint
aspectProp.setConstraints(Arrays.asList(inlineListConstraint));
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(aspectProp);
aspectPayload.setProperties(props);
// Try to create the property - Invalid LIST values
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
// Test d:double LIST values with d:int property data type
parameters = new ArrayList<>(3);
// double list value, but the the property data type is d:int
parameters.add(buildNamedValue("allowedValues", null, "1.0", "2.0", "3.0"));
parameters.add(buildNamedValue("sorted", "false"));
// Add the parameters into the constraint
inlineListConstraint.setParameters(parameters);
// Add inline constraint
aspectProp.setConstraints(Arrays.asList(inlineListConstraint));
props = new ArrayList<>(1);
props.add(aspectProp);
aspectPayload.setProperties(props);
// Try to create the property - Invalid LIST values
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
}
use of org.alfresco.rest.api.model.CustomModelConstraint in project alfresco-remote-api by Alfresco.
the class TestCustomConstraint method testPropDefaultValueWithConstraintRef.
@Test
public void testPropDefaultValueWithConstraintRef() throws Exception {
setRequestContext(customModelAdmin);
String modelName = "testModelConstraintRef" + System.currentTimeMillis();
final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
{
// Create List constraint
String listConstraintName = "testListConstraint" + System.currentTimeMillis();
CustomModelConstraint listConstraint = new CustomModelConstraint();
listConstraint.setName(listConstraintName);
listConstraint.setType("LIST");
// Create the List constraint's parameters
List<CustomModelNamedValue> parameters = new ArrayList<>(3);
// list value
parameters.add(buildNamedValue("allowedValues", null, "London", "Paris", "New York"));
parameters.add(buildNamedValue("sorted", "false"));
// Add the parameters into the constraint
listConstraint.setParameters(parameters);
// Create constraint as a Model Administrator
post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(listConstraint), 201);
// Retrieve the created List constraint
HttpResponse response = getSingle("cmm/" + modelName + "/constraints", listConstraintName, 200);
CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
// Create aspect
String aspectName = "testAspect" + System.currentTimeMillis();
createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
// Update the Aspect by adding property
CustomAspect aspectPayload = new CustomAspect();
aspectPayload.setName(aspectName);
final String aspectPropName = "testAspect1Prop" + System.currentTimeMillis();
CustomModelProperty aspectProp = new CustomModelProperty();
aspectProp.setName(aspectPropName);
aspectProp.setTitle("property with LIST constraint ref");
aspectProp.setDataType("d:text");
// Not in the list
aspectProp.setDefaultValue("Berlin");
// constrain ref
aspectProp.setConstraintRefs(Arrays.asList(returnedConstraint.getPrefixedName()));
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(aspectProp);
aspectPayload.setProperties(props);
// Try to create the property - constraint violation
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 409);
}
{
// Create MINMAX constraint
String minMaxConstraintName = "testMinMaxConstraint" + System.currentTimeMillis();
CustomModelConstraint minMaxConstraint = new CustomModelConstraint();
minMaxConstraint.setName(minMaxConstraintName);
minMaxConstraint.setType("MINMAX");
// Create the MinMax constraint's parameters
List<CustomModelNamedValue> parameters = new ArrayList<>(2);
parameters.add(buildNamedValue("maxValue", "100"));
parameters.add(buildNamedValue("minValue", "50"));
// Add the parameters into the constraint
minMaxConstraint.setParameters(parameters);
// Create constraint as a Model Administrator
post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(minMaxConstraint), 201);
// Retrieve the created MinMax constraint
HttpResponse response = getSingle("cmm/" + modelName + "/constraints", minMaxConstraintName, 200);
CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
// Create type
String typeName = "testType1" + System.currentTimeMillis();
createTypeAspect(CustomType.class, modelName, typeName, "test type1 title", "test type1 Desc", "cm:content");
// Update the Type by adding property
CustomType typePayload = new CustomType();
typePayload.setName(typeName);
String typePropName = "testType1Prop1" + System.currentTimeMillis();
CustomModelProperty typeProp = new CustomModelProperty();
typeProp.setName(typePropName);
typeProp.setTitle("property with MINMAX constraint ref");
typeProp.setDataType("d:int");
// Not in the defined range [50,100]
typeProp.setDefaultValue("35");
// constrain ref
typeProp.setConstraintRefs(Arrays.asList(returnedConstraint.getPrefixedName()));
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(typeProp);
typePayload.setProperties(props);
// Try to create the property - constraint violation
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 409);
}
}
Aggregations