Search in sources :

Example 41 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class SiteServiceImpl method createSite.

public SiteInfo createSite(final String sitePreset, String passedShortName, final String title, final String description, final SiteVisibility visibility, final QName siteType) {
    // Check that the provided site type is a subtype of TYPE_SITE
    if (SiteModel.TYPE_SITE.equals(siteType) == false && dictionaryService.isSubClass(siteType, TYPE_SITE) == false) {
        throw new SiteServiceException(MSG_INVALID_SITE_TYPE, new Object[] { siteType });
    }
    // Remove spaces from shortName
    final String shortName = passedShortName.replaceAll(" ", "");
    // Check to see if we already have a site of this name
    NodeRef existingSite = getSiteNodeRef(shortName, false);
    if (existingSite != null || authorityService.authorityExists(getSiteGroup(shortName, true))) {
        // Throw an exception since we have a duplicate site name
        throw new SiteServiceException(MSG_UNABLE_TO_CREATE, new Object[] { shortName });
    }
    // Check that the site name isn't too long
    // Authorities are limited to 100 characters by the PermissionService
    int longestPermissionLength = 0;
    for (String permission : permissionService.getSettablePermissions(siteType)) {
        if (permission.length() > longestPermissionLength)
            longestPermissionLength = permission.length();
    }
    int maximumPermisionGroupLength = 99 - longestPermissionLength;
    if (getSiteGroup(shortName, true).length() > maximumPermisionGroupLength) {
        throw new SiteServiceException(MSG_SITE_SHORT_NAME_TOO_LONG, new Object[] { shortName, maximumPermisionGroupLength - getSiteGroup("", true).length() });
    }
    // Get the site parent node reference
    final NodeRef siteParent = getSiteParent(shortName);
    if (siteParent == null) {
        throw new SiteServiceException("No root sites folder exists");
    }
    // Create the site node
    final PropertyMap properties = new PropertyMap(4);
    properties.put(ContentModel.PROP_NAME, shortName);
    properties.put(SiteModel.PROP_SITE_PRESET, sitePreset);
    properties.put(SiteModel.PROP_SITE_VISIBILITY, visibility.toString());
    properties.put(ContentModel.PROP_TITLE, title);
    properties.put(ContentModel.PROP_DESCRIPTION, description);
    final NodeRef siteNodeRef = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

        @Override
        public NodeRef doWork() throws Exception {
            behaviourFilter.disableBehaviour(siteParent, ContentModel.ASPECT_AUDITABLE);
            try {
                return nodeService.createNode(siteParent, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, shortName), siteType, properties).getChildRef();
            } finally {
                behaviourFilter.enableBehaviour(siteParent, ContentModel.ASPECT_AUDITABLE);
            }
        }
    }, AuthenticationUtil.getSystemUserName());
    // Make the new site a tag scope
    this.taggingService.addTagScope(siteNodeRef);
    // Clear the sites inherited permissions
    this.permissionService.setInheritParentPermissions(siteNodeRef, false);
    // Create the relevant groups and assign permissions
    setupSitePermissions(siteNodeRef, shortName, visibility, null);
    eventPublisher.publishEvent(new EventPreparator() {

        @Override
        public Event prepareEvent(String user, String networkId, String transactionId) {
            return new SiteManagementEvent("site.create", transactionId, networkId, new Date().getTime(), user, shortName, title, description, visibility.toString(), sitePreset);
        }
    });
    // Return created site information
    Map<QName, Serializable> customProperties = getSiteCustomProperties(siteNodeRef);
    SiteInfo siteInfo = new SiteInfoImpl(sitePreset, shortName, title, description, visibility, customProperties, siteNodeRef);
    return siteInfo;
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) SiteManagementEvent(org.alfresco.sync.events.types.SiteManagementEvent) FilterTypeString(org.alfresco.repo.node.getchildren.FilterPropString.FilterTypeString) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString) JSONException(org.json.JSONException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) NoSuchPersonException(org.alfresco.service.cmr.security.NoSuchPersonException) Date(java.util.Date) NodeRef(org.alfresco.service.cmr.repository.NodeRef) PropertyMap(org.alfresco.util.PropertyMap) EventPreparator(org.alfresco.sync.repo.events.EventPreparator) Event(org.alfresco.sync.events.types.Event) SiteManagementEvent(org.alfresco.sync.events.types.SiteManagementEvent) ApplicationEvent(org.springframework.context.ApplicationEvent)

Example 42 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class RegistryServiceImpl method getPath.

/**
 * Get the node-qname pair for the key.  If the key doesn't have a value element,
 * i.e. if it is purely path-based, then the QName will be null.
 *
 * @return Returns the node and property name represented by the key or <tt>null</tt>
 *      if it doesn't exist and was not allowed to be created.
 */
private Pair<NodeRef, QName> getPath(RegistryKey key, boolean create) {
    // Get the root
    NodeRef currentNodeRef = getRegistryRootNodeRef();
    // Get the key and property
    String namespaceUri = key.getNamespaceUri();
    String[] pathElements = key.getPath();
    String property = key.getProperty();
    // Find the node and property to put the value
    for (String pathElement : pathElements) {
        QName assocQName = QName.createQName(namespaceUri, QName.createValidLocalName(pathElement));
        // Find the node
        List<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(currentNodeRef, ContentModel.ASSOC_CHILDREN, assocQName);
        int size = childAssocRefs.size();
        if (// Found nothing with that path
        size == 0) {
            if (// Must create the path
            create) {
                // Create the node (with a name)
                PropertyMap properties = new PropertyMap();
                properties.put(ContentModel.PROP_NAME, pathElement);
                currentNodeRef = nodeService.createNode(currentNodeRef, ContentModel.ASSOC_CHILDREN, assocQName, ContentModel.TYPE_CONTAINER, properties).getChildRef();
            } else {
                // There is no node and we are not allowed to create it
                currentNodeRef = null;
                break;
            }
        } else // Found some results for that path
        {
            if (// More than one association by that name
            size > 1 && create) {
                // Too many, so trim it down
                boolean first = true;
                for (ChildAssociationRef assocRef : childAssocRefs) {
                    if (first) {
                        first = false;
                        continue;
                    }
                    // Remove excess assocs
                    nodeService.removeChildAssociation(assocRef);
                }
            }
            // Use the first one
            currentNodeRef = childAssocRefs.get(0).getChildRef();
        }
    }
    // Cater for null properties, i.e. path-based keys
    QName propertyQName = null;
    if (property != null) {
        propertyQName = QName.createQName(namespaceUri, QName.createValidLocalName(property));
    }
    // Create the result
    Pair<NodeRef, QName> resultPair = new Pair<NodeRef, QName>(currentNodeRef, propertyQName);
    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Converted registry key: \n" + "   Key:      " + key + "\n" + "   Result:   " + resultPair);
    }
    if (resultPair.getFirst() == null) {
        return null;
    } else {
        return resultPair;
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) PropertyMap(org.alfresco.util.PropertyMap) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Pair(org.alfresco.util.Pair)

Example 43 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class DictionaryModelTypeTest method testIsActiveFlagAndDelete.

@Test
public void testIsActiveFlagAndDelete() throws Exception {
    try {
        // Check that the model has not yet been loaded into the dictionary
        this.dictionaryService.getModel(TEST_MODEL_ONE);
        fail("This model has not yet been loaded into the dictionary service");
    } catch (DictionaryException exception) {
    // We expect this exception
    }
    // Create a model node
    PropertyMap properties = new PropertyMap(1);
    final NodeRef modelNode = this.nodeService.createNode(this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"), ContentModel.TYPE_DICTIONARY_MODEL, properties).getChildRef();
    assertNotNull(modelNode);
    // Add the model content to the model node
    ContentWriter contentWriter = this.contentService.getWriter(modelNode, ContentModel.PROP_CONTENT, true);
    contentWriter.setEncoding("UTF-8");
    contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
    contentWriter.putContent(MODEL_ONE_XML);
    // End the transaction to force update
    TestTransaction.flagForCommit();
    TestTransaction.end();
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // The model should not yet be loaded
            try {
                // Check that the model has not yet been loaded into the dictionary
                DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE);
                fail("This model has not yet been loaded into the dictionary service");
            } catch (DictionaryException exception) {
            // We expect this exception
            }
            // Set the isActive flag
            DictionaryModelTypeTest.this.nodeService.setProperty(modelNode, ContentModel.PROP_MODEL_ACTIVE, true);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // The model should now be loaded
            assertNotNull(DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE));
            // Set the isActive flag
            DictionaryModelTypeTest.this.nodeService.setProperty(modelNode, ContentModel.PROP_MODEL_ACTIVE, false);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // The model should not be loaded
            try {
                // Check that the model has not yet been loaded into the dictionary
                DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE);
                fail("This model has not yet been loaded into the dictionary service");
            } catch (DictionaryException exception) {
            // We expect this exception
            }
            // Set the isActive flag
            DictionaryModelTypeTest.this.nodeService.setProperty(modelNode, ContentModel.PROP_MODEL_ACTIVE, true);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // The model should now be loaded
            assertNotNull(DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE));
            // Delete the model
            DictionaryModelTypeTest.this.nodeService.deleteNode(modelNode);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // The model should not be loaded
            try {
                // Check that the model has not yet been loaded into the dictionary
                DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE);
                fail("This model has not yet been loaded into the dictionary service");
            } catch (DictionaryException exception) {
            // We expect this exception
            }
            return null;
        }
    });
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) ExpectedException(org.junit.rules.ExpectedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 44 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class DictionaryModelTypeTest method createActiveModel.

private NodeRef createActiveModel(String contentFile) {
    PropertyMap properties = new PropertyMap(1);
    properties.put(ContentModel.PROP_MODEL_ACTIVE, true);
    NodeRef modelNode = this.nodeService.createNode(this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"), ContentModel.TYPE_DICTIONARY_MODEL, properties).getChildRef();
    assertNotNull(modelNode);
    ContentWriter contentWriter = this.contentService.getWriter(modelNode, ContentModel.PROP_CONTENT, true);
    contentWriter.setEncoding("UTF-8");
    contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
    contentWriter.putContent(Thread.currentThread().getContextClassLoader().getResourceAsStream(contentFile));
    return modelNode;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap)

Example 45 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class DictionaryModelTypeTest method testCreateAndUpdateDictionaryModelNodeContent.

/**
 * Test the creation of dictionary model nodes
 */
@Test
public void testCreateAndUpdateDictionaryModelNodeContent() throws Exception {
    // just to make sure we don't regress ACE-5852, some tests should run as System
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
    try {
        // Check that the model has not yet been loaded into the dictionary
        this.dictionaryService.getModel(TEST_MODEL_ONE);
        fail("This model has not yet been loaded into the dictionary service");
    } catch (DictionaryException exception) {
    // We expect this exception
    }
    // Check that the namespace is not yet in the namespace service
    String uri = this.namespaceService.getNamespaceURI("test1");
    assertNull(uri);
    // Create a model node
    final NodeRef modelNode = transactionService.getRetryingTransactionHelper().doInTransaction(() -> {
        PropertyMap properties = new PropertyMap(1);
        properties.put(ContentModel.PROP_MODEL_ACTIVE, true);
        NodeRef model = this.nodeService.createNode(this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"), ContentModel.TYPE_DICTIONARY_MODEL, properties).getChildRef();
        assertNotNull(model);
        // Add the model content to the model node
        ContentWriter contentWriter = this.contentService.getWriter(model, ContentModel.PROP_CONTENT, true);
        contentWriter.setEncoding("UTF-8");
        contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
        contentWriter.putContent(MODEL_ONE_XML);
        return model;
    }, false, true);
    final NodeRef workingCopy = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Exception {
            // Check that the namespace is in the namespace service
            String uri = namespaceService.getNamespaceURI("test1");
            assertNotNull(uri);
            // Check that the meta data has been extracted from the model
            assertEquals(QName.createQName("{http://www.alfresco.org/test/testmodel1/1.0}testModelOne"), DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_NAME));
            assertEquals("Test model one", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_DESCRIPTION));
            assertEquals("Alfresco", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_AUTHOR));
            // System.out.println(this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_PUBLISHED_DATE));
            assertEquals("1.0", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_VERSION));
            // Check that the model is now available from the dictionary
            ModelDefinition modelDefinition2 = DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE);
            assertNotNull(modelDefinition2);
            assertEquals("Test model one", modelDefinition2.getDescription(DictionaryModelTypeTest.this.dictionaryService));
            // Check that the namespace has been added to the namespace service
            String uri2 = DictionaryModelTypeTest.this.namespaceService.getNamespaceURI("test1");
            assertEquals(uri2, "http://www.alfresco.org/test/testmodel1/1.0");
            // Lets check the node out and update the content
            NodeRef workingCopy = DictionaryModelTypeTest.this.cociService.checkout(modelNode);
            ContentWriter contentWriter2 = DictionaryModelTypeTest.this.contentService.getWriter(workingCopy, ContentModel.PROP_CONTENT, true);
            contentWriter2.putContent(MODEL_ONE_MODIFIED_XML);
            return workingCopy;
        }
    }, false, true);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // Check that the policy has not been fired since we have updated a working copy
            assertEquals("1.0", DictionaryModelTypeTest.this.nodeService.getProperty(workingCopy, ContentModel.PROP_MODEL_VERSION));
            // Check-in the model change
            DictionaryModelTypeTest.this.cociService.checkin(workingCopy, null);
            return null;
        }
    }, false, true);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // Now check that the model has been updated
            assertEquals("1.1", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_VERSION));
            return null;
        }
    }, false, true);
    // create node using new type
    final NodeRef node1 = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Exception {
            NodeRef node = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("http://www.alfresco.org/model/system/1.0", "node1"), QName.createQName("http://www.alfresco.org/test/testmodel1/1.0", "base"), null).getChildRef();
            assertNotNull(node);
            return node;
        }
    }, false, true);
    try {
        transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

            public Object execute() throws Exception {
                DictionaryModelTypeTest.this.nodeService.deleteNode(modelNode);
                return null;
            }
        }, false, true);
        fail("Unexpected - should not be able to delete model");
    } catch (AlfrescoRuntimeException are) {
    // expected
    }
    // delete node
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            nodeService.deleteNode(node1);
            return null;
        }
    }, false, true);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            DictionaryModelTypeTest.this.nodeService.deleteNode(modelNode);
            return null;
        }
    }, false, true);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap) ModelDefinition(org.alfresco.service.cmr.dictionary.ModelDefinition) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) ExpectedException(org.junit.rules.ExpectedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Aggregations

PropertyMap (org.alfresco.util.PropertyMap)111 NodeRef (org.alfresco.service.cmr.repository.NodeRef)43 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)15 Test (org.junit.Test)14 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)13 QName (org.alfresco.service.namespace.QName)11 Serializable (java.io.Serializable)9 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)8 ContentReader (org.alfresco.service.cmr.repository.ContentReader)8 BaseSpringTest (org.alfresco.util.BaseSpringTest)8 FileContentReader (org.alfresco.repo.content.filestore.FileContentReader)7 DictionaryException (org.alfresco.service.cmr.dictionary.DictionaryException)7 ExpectedException (org.junit.rules.ExpectedException)7 ApplicationContext (org.springframework.context.ApplicationContext)7 StoreRef (org.alfresco.service.cmr.repository.StoreRef)6 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)5 NodeService (org.alfresco.service.cmr.repository.NodeService)5 Date (java.util.Date)4 HashMap (java.util.HashMap)4 ServiceRegistry (org.alfresco.service.ServiceRegistry)4