Search in sources :

Example 1 with M2Type

use of org.alfresco.repo.dictionary.M2Type 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 2 with M2Type

use of org.alfresco.repo.dictionary.M2Type in project alfresco-repository by Alfresco.

the class SiteServiceImplTest method testCustomSiteType.

/**
 * Creates a site with a custom type, and ensures that
 *  it behaves correctly.
 */
@SuppressWarnings("deprecation")
@Test
public void testCustomSiteType() {
    final String CS_URI = "http://example.com/site";
    final String CS_PFX = "cs";
    // Setup our custom site type
    DictionaryDAO dictionaryDAO = (DictionaryDAO) this.applicationContext.getBean("dictionaryDAO");
    M2Model model = M2Model.createModel("cm:CustomSiteModel");
    model.createNamespace(CS_URI, CS_PFX);
    // Import the usual suspects too
    model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
    model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
    model.createImport(SiteModel.SITE_MODEL_URL, SiteModel.SITE_MODEL_PREFIX);
    // Custom type
    M2Type customType = model.createType("cs:customSite");
    customType.setTitle("customSite");
    customType.setParentName(SiteModel.SITE_MODEL_PREFIX + ":" + SiteModel.TYPE_SITE.getLocalName());
    M2Property customProp = customType.createProperty("cs:customSiteProp");
    customProp.setTitle("customSiteProp");
    customProp.setType("d:text");
    dictionaryDAO.putModel(model);
    // Get our custom type, to check it's in there properly
    final QName customTypeQ = QName.createQName("cs", "customSite", namespaceService);
    TypeDefinition td = dictionaryService.getType(customTypeQ);
    assertNotNull(td);
    // Create a site
    SiteInfo site = siteService.createSite("custom", "custom", "Custom", "Custom", SiteVisibility.PUBLIC);
    // Check the roles on it
    List<String> roles = siteService.getSiteRoles();
    assertEquals(7, roles.size());
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONSUMER));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONTRIBUTOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_COLLABORATOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_MANAGER));
    roles = siteService.getSiteRoles(site.getShortName());
    assertEquals(7, roles.size());
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONSUMER));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONTRIBUTOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_COLLABORATOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_MANAGER));
    // Swap the type
    nodeService.setType(site.getNodeRef(), customTypeQ);
    // Check again
    roles = siteService.getSiteRoles();
    assertEquals(7, roles.size());
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONSUMER));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONTRIBUTOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_COLLABORATOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_MANAGER));
    roles = siteService.getSiteRoles(site.getShortName());
    assertEquals(7, roles.size());
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONSUMER));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONTRIBUTOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_COLLABORATOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_MANAGER));
    // Alter the permissions for the custom site
    PermissionService testPermissionService = spy((PermissionService) this.applicationContext.getBean("permissionServiceImpl"));
    Set<String> customPerms = new HashSet<String>();
    customPerms.add(SiteServiceImpl.SITE_MANAGER);
    customPerms.add("CUSTOM");
    when(testPermissionService.getSettablePermissions(customTypeQ)).thenReturn(customPerms);
    // Check it changed for the custom site, but not normal
    SiteServiceImpl siteServiceImpl = (SiteServiceImpl) this.applicationContext.getBean("siteService");
    siteServiceImpl.setPermissionService(testPermissionService);
    roles = siteService.getSiteRoles();
    assertEquals(7, roles.size());
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONSUMER));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONTRIBUTOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_COLLABORATOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_MANAGER));
    roles = siteService.getSiteRoles(site.getShortName());
    assertEquals(2, roles.size());
    assertEquals(true, roles.contains("CUSTOM"));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_MANAGER));
    // Put the permissions back
    siteServiceImpl.setPermissionService(permissionService);
    roles = siteService.getSiteRoles();
    assertEquals(7, roles.size());
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONSUMER));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONTRIBUTOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_COLLABORATOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_MANAGER));
    roles = siteService.getSiteRoles(site.getShortName());
    assertEquals(7, roles.size());
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONSUMER));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_CONTRIBUTOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_COLLABORATOR));
    assertEquals(true, roles.contains(SiteServiceImpl.SITE_MANAGER));
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) M2Property(org.alfresco.repo.dictionary.M2Property) QName(org.alfresco.service.namespace.QName) M2Model(org.alfresco.repo.dictionary.M2Model) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) PermissionService(org.alfresco.service.cmr.security.PermissionService) M2Type(org.alfresco.repo.dictionary.M2Type) DictionaryDAO(org.alfresco.repo.dictionary.DictionaryDAO) HashSet(java.util.HashSet) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 3 with M2Type

use of org.alfresco.repo.dictionary.M2Type in project alfresco-repository by Alfresco.

the class FileFolderServiceImplTest method testCreateFolder.

public void testCreateFolder() throws Exception {
    // we are testing failures as well
    txn.commit();
    // start a new one
    txn = transactionService.getNonPropagatingUserTransaction();
    txn.begin();
    FileInfo parentFolderInfo = getByName(NAME_L0_FOLDER_A, true);
    assertNotNull(parentFolderInfo);
    NodeRef parentFolderRef = parentFolderInfo.getNodeRef();
    // create a file that already exists
    UserTransaction rollbackTxn = null;
    try {
        rollbackTxn = transactionService.getNonPropagatingUserTransaction();
        rollbackTxn.begin();
        fileFolderService.create(parentFolderRef, NAME_L1_FILE_A, ContentModel.TYPE_CONTENT);
        fail("Failed to detect duplicate filename");
    } catch (FileExistsException e) {
    // expected
    } finally {
        rollbackTxn.rollback();
    }
    // create folder of illegal type
    try {
        rollbackTxn = transactionService.getNonPropagatingUserTransaction();
        rollbackTxn.begin();
        fileFolderService.create(parentFolderRef, "illegal folder", ContentModel.TYPE_SYSTEM_FOLDER);
        fail("Illegal type not detected");
    } catch (RuntimeException e) {
    // expected
    } finally {
        rollbackTxn.rollback();
    }
    // Create a cm:folder derived type
    try {
        rollbackTxn = transactionService.getNonPropagatingUserTransaction();
        rollbackTxn.begin();
        // Create a new model
        String testNs = "http://www.alfresco.org/model/test111/1.0";
        M2Model testModel = M2Model.createModel("t111:filefolderserviceimpltest");
        testModel.createNamespace(testNs, "t111");
        testModel.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
        testModel.createImport(NamespaceService.SYSTEM_MODEL_1_0_URI, NamespaceService.SYSTEM_MODEL_PREFIX);
        testModel.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
        M2Type testType = testModel.createType("t111:subfolder");
        testType.setParentName("cm:" + ContentModel.TYPE_FOLDER.getLocalName());
        dictionaryDAO.putModel(testModel);
        fileFolderService.create(parentFolderRef, "Legal subtype of folder", QName.createQName(testNs, "subfolder"));
    } catch (Throwable e) {
        throw new Exception("Legal subtype of cm:folder not allowed.", e);
    } finally {
        rollbackTxn.rollback();
    }
    // create a file
    FileInfo fileInfo = fileFolderService.create(parentFolderRef, "newFile", ContentModel.TYPE_CONTENT);
    // check
    assertTrue("Node not created", nodeService.exists(fileInfo.getNodeRef()));
    assertFalse("File type expected", fileInfo.isFolder());
}
Also used : UserTransaction(javax.transaction.UserTransaction) NodeRef(org.alfresco.service.cmr.repository.NodeRef) M2Type(org.alfresco.repo.dictionary.M2Type) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) FileInfo(org.alfresco.service.cmr.model.FileInfo) M2Model(org.alfresco.repo.dictionary.M2Model) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) InvalidTypeException(org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) CyclicChildRelationshipException(org.alfresco.service.cmr.repository.CyclicChildRelationshipException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException)

Example 4 with M2Type

use of org.alfresco.repo.dictionary.M2Type in project alfresco-remote-api by Alfresco.

the class CustomModelImportTest method testUploadModel_UnsupportedModelElements.

public void testUploadModel_UnsupportedModelElements() throws Exception {
    // Note: here we only test a couple of not-supported model elements to check for the correct status code.
    // This test should be removed when we implement the required support
    long timestamp = System.currentTimeMillis();
    final String modelName = getClass().getSimpleName() + timestamp;
    final String prefix = "prefix" + timestamp;
    final String uri = "uriNamespace" + timestamp;
    final String aspectName = prefix + QName.NAMESPACE_PREFIX + "testAspec";
    final String typeName = prefix + QName.NAMESPACE_PREFIX + "testType";
    final String associationName = prefix + QName.NAMESPACE_PREFIX + "testAssociation";
    M2Model model = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(uri, prefix);
    model.setAuthor("John Doe");
    model.createAspect(aspectName);
    model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
    M2Type type = model.createType(typeName);
    // Add 'association' not supported yet.
    M2Association association = type.createAssociation(associationName);
    association.setSourceMandatory(false);
    association.setSourceMany(false);
    association.setTargetMandatory(false);
    association.setTargetClassName("cm:content");
    ByteArrayOutputStream xml = new ByteArrayOutputStream();
    model.toXML(xml);
    ZipEntryContext context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
    File zipFile = createZip(context);
    PostRequest postRequest = buildMultipartPostRequest(zipFile);
    // <associations> element is not supported yet
    sendRequest(postRequest, 409);
    type.removeAssociation(associationName);
    // Add 'mandatory-aspect' not supported yet.
    type.addMandatoryAspect(aspectName);
    xml = new ByteArrayOutputStream();
    model.toXML(xml);
    context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
    zipFile = createZip(context);
    postRequest = buildMultipartPostRequest(zipFile);
    // <mandatory-aspects> element is not supported yet
    sendRequest(postRequest, 409);
}
Also used : M2Association(org.alfresco.repo.dictionary.M2Association) M2Type(org.alfresco.repo.dictionary.M2Type) 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 5 with M2Type

use of org.alfresco.repo.dictionary.M2Type in project alfresco-repository by Alfresco.

the class CopyServiceImplTest method createTestModel.

/**
 * Creates the test model used by the tests
 */
private void createTestModel() {
    M2Model model = M2Model.createModel("test:nodeoperations");
    model.createNamespace(TEST_TYPE_NAMESPACE, "test");
    model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
    model.createImport(NamespaceService.SYSTEM_MODEL_1_0_URI, NamespaceService.SYSTEM_MODEL_PREFIX);
    model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
    M2Type testType = model.createType("test:" + TEST_TYPE_QNAME.getLocalName());
    testType.setParentName("cm:" + ContentModel.TYPE_CONTENT.getLocalName());
    M2Property prop1 = testType.createProperty("test:" + PROP1_QNAME_MANDATORY.getLocalName());
    prop1.setMandatory(true);
    prop1.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop1.setMultiValued(false);
    M2Property prop2 = testType.createProperty("test:" + PROP2_QNAME_OPTIONAL.getLocalName());
    prop2.setMandatory(false);
    prop2.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop2.setMandatory(false);
    M2Property propNodeRef = testType.createProperty("test:" + PROP_QNAME_MY_NODE_REF.getLocalName());
    propNodeRef.setMandatory(false);
    propNodeRef.setType("d:" + DataTypeDefinition.NODE_REF.getLocalName());
    propNodeRef.setMandatory(false);
    M2Property propAnyNodeRef = testType.createProperty("test:" + PROP_QNAME_MY_ANY.getLocalName());
    propAnyNodeRef.setMandatory(false);
    propAnyNodeRef.setType("d:" + DataTypeDefinition.ANY.getLocalName());
    propAnyNodeRef.setMandatory(false);
    M2ChildAssociation childAssoc = testType.createChildAssociation("test:" + TEST_CHILD_ASSOC_TYPE_QNAME.getLocalName());
    childAssoc.setTargetClassName("sys:base");
    childAssoc.setTargetMandatory(false);
    M2Association assoc = testType.createAssociation("test:" + TEST_ASSOC_TYPE_QNAME.getLocalName());
    assoc.setTargetClassName("sys:base");
    assoc.setTargetMandatory(false);
    M2Aspect testAspect = model.createAspect("test:" + TEST_ASPECT_QNAME.getLocalName());
    M2Property prop3 = testAspect.createProperty("test:" + PROP3_QNAME_MANDATORY.getLocalName());
    prop3.setMandatory(true);
    prop3.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop3.setMultiValued(false);
    M2Property prop4 = testAspect.createProperty("test:" + PROP4_QNAME_OPTIONAL.getLocalName());
    prop4.setMandatory(false);
    prop4.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop4.setMultiValued(false);
    M2Aspect testMandatoryAspect = model.createAspect("test:" + TEST_MANDATORY_ASPECT_QNAME.getLocalName());
    M2Property prop5 = testMandatoryAspect.createProperty("test:" + PROP5_QNAME_MANDATORY.getLocalName());
    prop5.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop5.setMandatory(true);
    testType.addMandatoryAspect("test:" + TEST_MANDATORY_ASPECT_QNAME.getLocalName());
    dictionaryDAO.putModel(model);
}
Also used : M2Association(org.alfresco.repo.dictionary.M2Association) M2Type(org.alfresco.repo.dictionary.M2Type) M2Property(org.alfresco.repo.dictionary.M2Property) M2Model(org.alfresco.repo.dictionary.M2Model) M2Aspect(org.alfresco.repo.dictionary.M2Aspect) M2ChildAssociation(org.alfresco.repo.dictionary.M2ChildAssociation)

Aggregations

M2Model (org.alfresco.repo.dictionary.M2Model)8 M2Type (org.alfresco.repo.dictionary.M2Type)8 M2Property (org.alfresco.repo.dictionary.M2Property)4 QName (org.alfresco.service.namespace.QName)3 Test (org.junit.Test)3 M2Aspect (org.alfresco.repo.dictionary.M2Aspect)2 M2Association (org.alfresco.repo.dictionary.M2Association)2 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 ZipFile (java.util.zip.ZipFile)1 UserTransaction (javax.transaction.UserTransaction)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 DictionaryDAO (org.alfresco.repo.dictionary.DictionaryDAO)1 M2ChildAssociation (org.alfresco.repo.dictionary.M2ChildAssociation)1 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)1