Search in sources :

Example 1 with M2Model

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);
}
Also used : PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) M2Model(org.alfresco.repo.dictionary.M2Model) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with M2Model

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);
}
Also used : DictionaryDAO(org.alfresco.repo.dictionary.DictionaryDAO) M2Property(org.alfresco.repo.dictionary.M2Property) M2Model(org.alfresco.repo.dictionary.M2Model) M2Aspect(org.alfresco.repo.dictionary.M2Aspect)

Example 3 with M2Model

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CustomType(org.alfresco.rest.api.model.CustomType) M2Constraint(org.alfresco.repo.dictionary.M2Constraint) M2Type(org.alfresco.repo.dictionary.M2Type) M2Model(org.alfresco.repo.dictionary.M2Model) CustomAspect(org.alfresco.rest.api.model.CustomAspect) M2Aspect(org.alfresco.repo.dictionary.M2Aspect) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) Pair(org.alfresco.util.Pair)

Example 4 with M2Model

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);
    }
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ModelExistsException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelExistsException) InvalidCustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException) M2Model(org.alfresco.repo.dictionary.M2Model) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) CustomModel(org.alfresco.rest.api.model.CustomModel) CustomModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ActiveModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException) ModelDoesNotExistException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException) CustomModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException) InvalidCustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) CustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException) ModelExistsException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelExistsException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Example 5 with M2Model

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);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) QName(org.alfresco.service.namespace.QName) M2Property(org.alfresco.repo.dictionary.M2Property) M2Model(org.alfresco.repo.dictionary.M2Model) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ParameterCheck.mandatoryString(org.springframework.extensions.surf.util.ParameterCheck.mandatoryString) M2Aspect(org.alfresco.repo.dictionary.M2Aspect)

Aggregations

M2Model (org.alfresco.repo.dictionary.M2Model)32 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)18 NodeRef (org.alfresco.service.cmr.repository.NodeRef)15 M2Aspect (org.alfresco.repo.dictionary.M2Aspect)10 QName (org.alfresco.service.namespace.QName)10 ParameterCheck.mandatoryString (org.springframework.extensions.surf.util.ParameterCheck.mandatoryString)10 M2Property (org.alfresco.repo.dictionary.M2Property)8 InputStream (java.io.InputStream)6 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)6 File (java.io.File)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 M2ClassAssociation (org.alfresco.repo.dictionary.M2ClassAssociation)4 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ArrayList (java.util.ArrayList)3 ZipFile (java.util.zip.ZipFile)3 AlfrescoModel (org.alfresco.solr.client.AlfrescoModel)3 HashSet (java.util.HashSet)2 M2Namespace (org.alfresco.repo.dictionary.M2Namespace)2