Java Code Examples for org.alfresco.service.cmr.repository.NodeService#createNode()

The following examples show how to use org.alfresco.service.cmr.repository.NodeService#createNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentEmailMessageHandler.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
     * Adds forum node
     * 
     * @param nodeRef Paren node
     * @return Reference to created node
     */
    private NodeRef addForumNode(NodeRef nodeRef)
    {
        NodeService nodeService = getNodeService();
        
//        //Add discussable aspect to content node
//        if (!nodeService.hasAspect(nodeRef, ForumModel.ASPECT_DISCUSSABLE))
//        {
//            nodeService.addAspect(nodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
//        }

        //Create forum node and associate it with content node
        Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
        properties.put(ContentModel.PROP_NAME, forumNodeName);
        ChildAssociationRef childAssoc = nodeService.createNode(nodeRef, ForumModel.ASSOC_DISCUSSION, ForumModel.ASSOC_DISCUSSION, ForumModel.TYPE_FORUM, properties);
        NodeRef forumNode = childAssoc.getChildRef();        

        //Add necessary aspects to forum node
        properties.clear();
        properties.put(ApplicationModel.PROP_ICON, "forum");
        nodeService.addAspect(forumNode, ApplicationModel.ASPECT_UIFACETS, properties);
        
        return forumNode;
    }
 
Example 2
Source File: AbstractForumEmailMessageHandler.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Adds topic node into Alfresco repository
 * 
 * @param parentNode        Parent node
 * @param name              Topic name
 * @return                  Reference to created node
 */
protected NodeRef addTopicNode(NodeRef parentNode, String name)
{
    String workingName = encodeSubject(name);
    
    NodeService nodeService = getNodeService();
    Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
    properties.put(ContentModel.PROP_NAME, workingName);

    NodeRef topicNode = nodeService.getChildByName(parentNode, ContentModel.ASSOC_CONTAINS, workingName);
    if (topicNode == null)
    {
        ChildAssociationRef association = nodeService.createNode(
                parentNode,
                ContentModel.ASSOC_CONTAINS,
                QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, workingName),
                ForumModel.TYPE_TOPIC,
                properties);
        topicNode = association.getChildRef();
    }

    // Add necessary aspects
    properties.clear();
    properties.put(ApplicationModel.PROP_ICON, "topic");
    getNodeService().addAspect(topicNode, ApplicationModel.ASPECT_UIFACETS, properties);

    return topicNode;
}
 
Example 3
Source File: RoutingContentServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void setUp() throws Exception
{
    ctx = ApplicationContextHelper.getApplicationContext();
    transactionService = (TransactionService) ctx.getBean("TransactionService");
    nodeService = (NodeService) ctx.getBean("NodeService");
    contentService = (ContentService) ctx.getBean(ServiceRegistry.CONTENT_SERVICE.getLocalName());
    copyService = (CopyService) ctx.getBean("CopyService");
    this.policyComponent = (PolicyComponent) ctx.getBean("policyComponent");
    this.authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
    
    // authenticate
    this.authenticationComponent.setSystemUserAsCurrentUser();
    
    // start the transaction
    txn = getUserTransaction();
    txn.begin();
    
    // create a store and get the root node
    StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, getName());
    if (!nodeService.exists(storeRef))
    {
        storeRef = nodeService.createStore(storeRef.getProtocol(), storeRef.getIdentifier());
    }
    rootNodeRef = nodeService.getRootNode(storeRef);

    ChildAssociationRef assocRef = nodeService.createNode(
            rootNodeRef,
            ContentModel.ASSOC_CHILDREN,
            QName.createQName(TEST_NAMESPACE, GUID.generate()),
            ContentModel.TYPE_CONTENT);
    contentNodeRef = assocRef.getChildRef();

    ContentWriter writer = contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
    writer.setEncoding("UTF-16");
    writer.setLocale(Locale.CHINESE);
    writer.setMimetype("text/plain");
    writer.putContent("sample content");
}
 
Example 4
Source File: TestWithUserUtils.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void createUser(
        String userName, 
        String password, 
        String email,
        NodeRef rootNodeRef,
        NodeService nodeService,
        MutableAuthenticationService authenticationService)
{    
    // ignore if the user's authentication already exists
    if (authenticationService.authenticationExists(userName))
    {
        // ignore
        return;
    }
    QName children = ContentModel.ASSOC_CHILDREN;
    QName system = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "system");
    QName container = ContentModel.TYPE_CONTAINER;
    QName types = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "people");
    
    NodeRef systemNodeRef = nodeService.createNode(rootNodeRef, children, system, container).getChildRef();
    NodeRef typesNodeRef = nodeService.createNode(systemNodeRef, children, types, container).getChildRef();
    
    HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>();
    properties.put(ContentModel.PROP_USERNAME, userName);
    if (email != null && email.length() != 0)
    {
        properties.put(ContentModel.PROP_EMAIL, email);
    }
    nodeService.createNode(typesNodeRef, children, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, userName) , container, properties);
    
    // Create the users
    authenticationService.createAuthentication(userName, password.toCharArray()); 
}
 
Example 5
Source File: TemporaryNodes.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private NodeRef createNode(String cmName, NodeRef parentNode, QName nodeType)
{
    final NodeService nodeService = (NodeService) appContextRule.getApplicationContext().getBean("nodeService");
    
    Map<QName, Serializable> props = new HashMap<QName, Serializable>();
    props.put(ContentModel.PROP_NAME, cmName);
    ChildAssociationRef childAssoc = nodeService.createNode(parentNode,
                ContentModel.ASSOC_CONTAINS,
                ContentModel.ASSOC_CONTAINS,
                nodeType,
                props);
    
    return childAssoc.getChildRef();
}
 
Example 6
Source File: InvitationWebScriptTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String makeAvatar(final NodeService nodeService, final NodeRef person)
{
    nodeService.addAspect(person, ContentModel.ASPECT_PREFERENCES, null);
    ChildAssociationRef assoc = nodeService.createNode(person, ContentModel.ASSOC_PREFERENCE_IMAGE, avatarQName,
                ContentModel.TYPE_CONTENT);
    NodeRef avatar = assoc.getChildRef();
    nodeService.createAssociation(person, avatar, ContentModel.ASSOC_AVATAR);
    return "api/node/" + avatar + "/content/thumbnails/avatar";
}
 
Example 7
Source File: AbstractEmailMessageHandler.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Add new node into Alfresco repository with specified parameters. Node content isn't added.
 * 
 * @param nodeService Alfresco Node Service
 * @param parent Parent node
 * @param name Name of the new node
 * @param overwrite if true then overwrite an existing node with the same name.   if false the name is changed to make it unique.
 * @param assocType Association type that should be set between parent node and the new one.
 * @return Reference to created node
 */
protected NodeRef addContentNode(NodeService nodeService, NodeRef parent, String name, QName assocType, boolean overwrite)
{
    String workingName =  encodeSubject(name);
    
    // Need to work out a new safe name.
    String baseName = FilenameUtils.getBaseName(workingName);
    String extension = FilenameUtils.getExtension(workingName);
    
    if(logger.isDebugEnabled())
    {
        logger.debug("addContentNode name:" + workingName);
    }
      
    for(int counter = 1; counter < 10000; counter++)
    {
        QName safeQName = QName.createQNameWithValidLocalName(NamespaceService.CONTENT_MODEL_1_0_URI, workingName);

        NodeRef childNodeRef = nodeService.getChildByName(parent, ContentModel.ASSOC_CONTAINS, workingName);
        
        if (childNodeRef != null)
        {
            if(overwrite)
            {
                if(logger.isDebugEnabled())
                {
                    logger.debug("overwriting existing node :" + workingName);
                }
            
                // Node already exists
                // The node is present already.  Make sure the name case is correct
                nodeService.setProperty(childNodeRef, ContentModel.PROP_NAME, baseName);
                return childNodeRef;
            }
            
            // Node already exists and not overwrite
            String postFix = "(" + counter + ")";
        
            if(baseName.length() + extension.length() + postFix.length() > QName.MAX_LENGTH )
            {
                // Need to truncate base name   
                workingName =  baseName.substring(0, QName.MAX_LENGTH - postFix.length() - extension.length() -1) + postFix;  
            }
            else
            {
                workingName = baseName + postFix ;
            }
            if(extension.length() > 0)
            {
                workingName = workingName + "." + extension;
            }
        }
        else
        {
            // Here if child node ref does not already exist
            if(logger.isDebugEnabled())
            {
                logger.debug("child node ref does not already exist :" + workingName);
            }
            Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
            contentProps.put(ContentModel.PROP_NAME, workingName);
                
            ChildAssociationRef associationRef = nodeService.createNode(
                parent,
                assocType,
                safeQName,
                ContentModel.TYPE_CONTENT,
                contentProps);
            childNodeRef = associationRef.getChildRef();
                
            return childNodeRef;
        }
    }
    throw new AlfrescoRuntimeException("Unable to add new file");
}
 
Example 8
Source File: AbstractForumEmailMessageHandler.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Posts content
 * 
 * @param nodeRef   Reference to node
 * @param message    Mail parser
 * @return          Returns the new post node
 */
protected NodeRef addPostNode(NodeRef nodeRef, EmailMessage message)
{
    NodeService nodeService = getNodeService();
    Date now = new Date();
    String nodeName = "posted-" + new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss").format(now) + ".html";

    PropertyMap properties = new PropertyMap(3);
    properties.put(ContentModel.PROP_NAME, nodeName);

    NodeRef postNodeRef = nodeService.getChildByName(nodeRef, ContentModel.ASSOC_CONTAINS, nodeName);
    if (postNodeRef == null)
    {
        ChildAssociationRef childAssoc = nodeService.createNode(
                nodeRef,
                ContentModel.ASSOC_CONTAINS,
                QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, nodeName),
                ForumModel.TYPE_POST,
                properties);
        postNodeRef = childAssoc.getChildRef();
    }

    // Add necessary aspects
    properties.clear();
    properties.put(ContentModel.PROP_TITLE, nodeName);
    nodeService.addAspect(postNodeRef, ContentModel.ASPECT_TITLED, properties);
    properties.clear();
    properties.put(ApplicationModel.PROP_EDITINLINE, true);
    nodeService.addAspect(postNodeRef, ApplicationModel.ASPECT_INLINEEDITABLE, properties);

    // Write content
    if (message.getBody() != null)
    {
        writeContent(
                postNodeRef,
                message.getBody().getContent(),
                message.getBody().getContentType(),
                message.getBody().getEncoding());
    }
    else
    {
        writeContent(postNodeRef, "<The message was empty>", MimetypeMap.MIMETYPE_TEXT_PLAIN);
    }
    addEmailedAspect(postNodeRef, message);
    
    // Done
    return postNodeRef;
}
 
Example 9
Source File: FFCLoadsOfFiles.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void doExample(ServiceRegistry serviceRegistry) throws Exception
    {
        //
        // locate the company home node
        //
        SearchService searchService = serviceRegistry.getSearchService();
        NodeService nodeService = serviceRegistry.getNodeService();
        NamespaceService namespaceService = serviceRegistry.getNamespaceService();
        StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
        NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
        List<NodeRef> results = searchService.selectNodes(rootNodeRef, "/app:company_home", null, namespaceService, false);
        if (results.size() == 0)
        {
            throw new AlfrescoRuntimeException("Can't find /app:company_home");
        }
        NodeRef companyHomeNodeRef = results.get(0);
        results = searchService.selectNodes(companyHomeNodeRef, "./cm:LoadTest", null, namespaceService, false);
        final NodeRef loadTestHome;
        if (results.size() == 0)
        {
            loadTestHome = nodeService.createNode(
                    companyHomeNodeRef,
                    ContentModel.ASSOC_CHILDREN,
                    QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "LoadTest"),
                    ContentModel.TYPE_FOLDER).getChildRef();
        }
        else
        {
            loadTestHome = results.get(0);
        }

        if ((currentDoc + docsPerTx) > totalNumDocs)
        {
        	docsPerTx = totalNumDocs - currentDoc;
        }
        // Create new Space
        String spaceName = "Bulk Load Space (" + System.currentTimeMillis() + ") from " + currentDoc + " to " + (currentDoc + docsPerTx - 1) + " of " + totalNumDocs;
        Map<QName, Serializable> spaceProps = new HashMap<QName, Serializable>();
    	spaceProps.put(ContentModel.PROP_NAME, spaceName);
        NodeRef  newSpace = nodeService.createNode(loadTestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, spaceName),ContentModel.TYPE_FOLDER,spaceProps).getChildRef();
        

        // create new content node within new Space home
        for (int k = 1;k<=docsPerTx;k++)
        {
    		currentDoc++;
    		System.out.println("About to start document " + currentDoc);
        	// assign name
        	String name = "BulkLoad (" + System.currentTimeMillis() + ") " + currentDoc ;
        	Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
        	contentProps.put(ContentModel.PROP_NAME, name);
        	
        	// create content node
        	// NodeService nodeService = serviceRegistry.getNodeService();
        	ChildAssociationRef association = nodeService.createNode(newSpace, 
        			ContentModel.ASSOC_CONTAINS, 
        			QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, name),
        			ContentModel.TYPE_CONTENT,
        			contentProps);
        	NodeRef content = association.getChildRef();
        
        	// add titled aspect (for Web Client display)
        	Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>();
        	titledProps.put(ContentModel.PROP_TITLE, name);
        	titledProps.put(ContentModel.PROP_DESCRIPTION, name);
        	nodeService.addAspect(content, ContentModel.ASPECT_TITLED, titledProps);
        	
        	//
        	// write some content to new node
        	//

        	ContentService contentService = serviceRegistry.getContentService();
        	ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true);
        	writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
        	writer.setEncoding("UTF-8");
        	String text = "This is some text in a doc";
        	writer.putContent(text);
    		System.out.println("About to get child assocs ");        	
        	//Circa
//        	nodeService.getChildAssocs(newSpace);
    	       for (int count=0;count<=10000;count++)
    	        {
    	        	nodeService.getChildAssocs(newSpace);
    	        }
       	
        }
    	//doSearch(searchService);
 		System.out.println("About to end transaction " );

    }
 
Example 10
Source File: AbstractSignatureActionExecuter.java    From CounterSign with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Creates a "signature" object and associates it with the signed doc
 * @param node
 * @param location
 * @param reason
 */
protected NodeRef addSignatureNodeAssociation(NodeRef node, String location, String reason, 
		String signatureField, java.util.Date sigDate, String geolocation, int page, String position)
{
	NodeService nodeService = serviceRegistry.getNodeService();
	
	String userId = AuthenticationUtil.getRunAsUser();
	NodeRef person = serviceRegistry.getPersonService().getPerson(userId);
	
	// if page is -1, then this was a signature field, set position to "none"
	if(page == -1) position = "none";
	
	HashMap<QName, Serializable> props = new HashMap<QName, Serializable>();
	props.put(CounterSignSignatureModel.PROP_REASON, reason);
	props.put(CounterSignSignatureModel.PROP_LOCATION, location);
	props.put(CounterSignSignatureModel.PROP_SIGNATUREDATE, sigDate);
	props.put(CounterSignSignatureModel.PROP_SIGNATUREFIELD, signatureField);
	props.put(CounterSignSignatureModel.PROP_SIGNATUREPAGE, page);
	props.put(CounterSignSignatureModel.PROP_SIGNATUREPOSITION, position);
	props.put(CounterSignSignatureModel.PROP_EXTERNALSIGNER, userId);
	
	// check the geolocation data, if it is valid, split it out and add
	if(geolocation.indexOf(",") != -1)
	{
		String[] latLong = geolocation.split(",");
		props.put(ContentModel.PROP_LATITUDE, latLong[0]);
		props.put(ContentModel.PROP_LONGITUDE, latLong[1]);
	}
	else
	{
		props.put(ContentModel.PROP_LATITUDE, -1);
		props.put(ContentModel.PROP_LONGITUDE, -1);
	}
	
	QName assocQName = QName.createQName(
			CounterSignSignatureModel.COUNTERSIGN_SIGNATURE_MODEL_1_0_URI,
			QName.createValidLocalName(userId + "-" + sigDate.getTime()));
		
	ChildAssociationRef sigChildRef = nodeService.createNode(
			node, 
			CounterSignSignatureModel.ASSOC_SIGNATURES, 
			assocQName, 
			CounterSignSignatureModel.TYPE_SIGNATURE, 
			props);
	
	NodeRef signature = sigChildRef.getChildRef();
	
	// add hidden aspect to signature nodes, these should not be 
	// shown in any document lists or other Share views
	HashMap<QName, Serializable> aspectProps = new HashMap<QName, Serializable>();
	aspectProps.put(ContentModel.PROP_VISIBILITY_MASK, HiddenAspect.Visibility.NotVisible.getMask());
	nodeService.addAspect(signature, ContentModel.ASPECT_HIDDEN, aspectProps);

	nodeService.createAssociation(signature, person, CounterSignSignatureModel.ASSOC_SIGNEDBY);
	
	return signature;
}