use of org.alfresco.repo.dictionary.M2Model in project alfresco-remote-api by Alfresco.
the class CustomModelImportTest method testUploadModel_Invalid.
public void testUploadModel_Invalid() throws Exception {
long timestamp = System.currentTimeMillis();
final String modelName = getClass().getSimpleName() + timestamp;
final String prefix = "prefix" + timestamp;
final String uri = "uriNamespace" + timestamp;
M2Model model = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
model.setAuthor("Admin");
model.setDescription("Desc");
ByteArrayOutputStream xml = new ByteArrayOutputStream();
model.toXML(xml);
ZipEntryContext context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
File zipFile = createZip(context);
PostRequest postRequest = buildMultipartPostRequest(zipFile);
// no namespace has been defined
sendRequest(postRequest, 409);
// Create two namespaces
model.createNamespace(uri, prefix);
model.createNamespace(uri + "anotherUri", prefix + "anotherPrefix");
xml = new ByteArrayOutputStream();
model.toXML(xml);
context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
zipFile = createZip(context);
postRequest = buildMultipartPostRequest(zipFile);
// custom model can only have one namespace
sendRequest(postRequest, 409);
}
use of org.alfresco.repo.dictionary.M2Model in project alfresco-remote-api by Alfresco.
the class AbstractEnterpriseOpenCMISTCKTest method overrideVersionableAspectProperties.
protected void overrideVersionableAspectProperties(ApplicationContext ctx) {
final DictionaryDAO dictionaryDAO = (DictionaryDAO) ctx.getBean("dictionaryDAO");
dictionaryDAO.removeModel(QName.createQName("cm:contentmodel"));
M2Model contentModel = M2Model.createModel(getClass().getClassLoader().getResourceAsStream("alfresco/model/contentModel.xml"));
M2Aspect versionableAspect = contentModel.getAspect("cm:versionable");
M2Property prop = versionableAspect.getProperty("cm:initialVersion");
prop.setDefaultValue(Boolean.FALSE.toString());
prop = versionableAspect.getProperty("cm:autoVersion");
prop.setDefaultValue(Boolean.FALSE.toString());
prop = versionableAspect.getProperty("cm:autoVersionOnUpdateProps");
prop.setDefaultValue(Boolean.FALSE.toString());
dictionaryDAO.putModel(contentModel);
}
use of org.alfresco.repo.dictionary.M2Model in project alfresco-remote-api by Alfresco.
the class CustomModelsImpl method convertToM2Model.
/**
* Converts the given {@code org.alfresco.rest.api.model.CustomModel}
* object, a collection of {@code org.alfresco.rest.api.model.CustomType}
* objects, a collection of
* {@code org.alfresco.rest.api.model.CustomAspect} objects, and a collection of
* {@code org.alfresco.rest.api.model.CustomModelConstraint} objects into a {@link M2Model} object
*
* @param customModel the custom model
* @param types the custom types
* @param aspects the custom aspects
* @param constraints the custom constraints
* @return {@link M2Model} object
*/
private M2Model convertToM2Model(CustomModel customModel, Collection<CustomType> types, Collection<CustomAspect> aspects, Collection<CustomModelConstraint> constraints) {
validateBasicModelInput(customModel);
Set<Pair<String, String>> namespacesToImport = new LinkedHashSet<>();
final String namespacePrefix = customModel.getNamespacePrefix();
final String namespaceURI = customModel.getNamespaceUri();
// Construct the model name
final String name = constructName(customModel.getName(), namespacePrefix);
M2Model model = M2Model.createModel(name);
model.createNamespace(namespaceURI, namespacePrefix);
model.setDescription(customModel.getDescription());
String author = customModel.getAuthor();
if (author == null) {
author = getCurrentUserFullName();
}
model.setAuthor(author);
// Types
if (types != null) {
for (CustomType type : types) {
validateName(type.getName(), TYPE_NAME_NULL_ERR);
M2Type m2Type = model.createType(constructName(type.getName(), namespacePrefix));
m2Type.setDescription(type.getDescription());
m2Type.setTitle(type.getTitle());
setParentName(m2Type, type.getParentName(), namespacesToImport, namespacePrefix);
setM2Properties(m2Type, type.getProperties(), namespacePrefix, namespacesToImport);
}
}
// Aspects
if (aspects != null) {
for (CustomAspect aspect : aspects) {
validateName(aspect.getName(), ASPECT_NAME_NULL_ERR);
M2Aspect m2Aspect = model.createAspect(constructName(aspect.getName(), namespacePrefix));
m2Aspect.setDescription(aspect.getDescription());
m2Aspect.setTitle(aspect.getTitle());
setParentName(m2Aspect, aspect.getParentName(), namespacesToImport, namespacePrefix);
setM2Properties(m2Aspect, aspect.getProperties(), namespacePrefix, namespacesToImport);
}
}
// Constraints
if (constraints != null) {
for (CustomModelConstraint constraint : constraints) {
validateName(constraint.getName(), CONSTRAINT_NAME_NULL_ERR);
final String constraintName = constructName(constraint.getName(), namespacePrefix);
M2Constraint m2Constraint = model.createConstraint(constraintName, constraint.getType());
// Set title, desc and parameters
setConstraintOtherData(constraint, m2Constraint, null);
}
}
// Add imports
for (Pair<String, String> uriPrefix : namespacesToImport) {
// Don't import the already defined namespace
if (!namespaceURI.equals(uriPrefix.getFirst())) {
model.createImport(uriPrefix.getFirst(), uriPrefix.getSecond());
}
}
return model;
}
use of org.alfresco.repo.dictionary.M2Model in project alfresco-remote-api by Alfresco.
the class CustomModelsImpl method createCustomModelImpl.
private CustomModel createCustomModelImpl(CustomModel model, boolean basicModelOnly) {
M2Model m2Model = null;
if (basicModelOnly) {
m2Model = convertToM2Model(model, null, null, null);
} else {
m2Model = convertToM2Model(model, model.getTypes(), model.getAspects(), model.getConstraints());
}
boolean activate = ModelStatus.ACTIVE.equals(model.getStatus());
try {
CustomModelDefinition modelDefinition = customModelService.createCustomModel(m2Model, activate);
return new CustomModel(modelDefinition);
} catch (ModelExistsException me) {
throw new ConstraintViolatedException(me.getMessage());
} catch (CustomModelConstraintException ncx) {
throw new ConstraintViolatedException(ncx.getMessage());
} catch (InvalidCustomModelException iex) {
throw new InvalidArgumentException(iex.getMessage());
} catch (Exception e) {
throw new ApiException("cmm.rest_api.model_invalid", e);
}
}
use of org.alfresco.repo.dictionary.M2Model in project records-management by Alfresco.
the class RecordsManagementAdminServiceImpl method removeCustomPropertyDefinition.
/**
* @see org.alfresco.module.org_alfresco_module_rm.RecordsManagementAdminService#removeCustomPropertyDefinition(org.alfresco.service.namespace.QName)
*/
public void removeCustomPropertyDefinition(QName propQName) {
mandatory("propQName", propQName);
NodeRef modelRef = getCustomModelRef(propQName.getNamespaceURI());
M2Model deserializedModel = readCustomContentModel(modelRef);
String propQNameAsString = propQName.toPrefixString(getNamespaceService());
String aspectName = null;
boolean found = false;
// attempt to delete the property definition.
for (QName customisableType : getCustomisable()) {
aspectName = getCustomAspect(customisableType).toPrefixString(getNamespaceService());
M2Aspect customPropsAspect = deserializedModel.getAspect(aspectName);
if (customPropsAspect == null) {
throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_UNKNOWN_ASPECT, aspectName));
}
M2Property prop = customPropsAspect.getProperty(propQNameAsString);
if (prop != null) {
if (logger.isDebugEnabled()) {
StringBuilder msg = new StringBuilder();
msg.append("Attempting to delete custom property: ");
msg.append(propQNameAsString);
logger.debug(msg.toString());
}
found = true;
customPropsAspect.removeProperty(propQNameAsString);
break;
}
}
if (!found) {
throw new AlfrescoRuntimeException(I18NUtil.getMessage(MSG_PROP_EXIST, propQNameAsString));
}
writeCustomContentModel(modelRef, deserializedModel);
if (logger.isInfoEnabled()) {
logger.info("deleteCustomPropertyDefinition: " + propQNameAsString + " from aspect: " + aspectName);
}
}
Aggregations