Java Code Examples for org.alfresco.service.namespace.QName#createQNameWithValidLocalName()

The following examples show how to use org.alfresco.service.namespace.QName#createQNameWithValidLocalName() . 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: 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 2
Source File: VirtualNodeServiceExtension.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<ChildAssociationRef> getParentAssocs(NodeRef nodeRef, QNamePattern typeQNamePattern,
            QNamePattern qnamePattern)
{
    NodeServiceTrait theTrait = getTrait();
    Reference reference = Reference.fromNodeRef(nodeRef);
    if (reference != null)
    {
        Reference parent = reference.execute(new GetParentReferenceMethod());
        if (parent == null)
        {
            return theTrait.getParentAssocs(reference.execute(new GetActualNodeRefMethod(environment)),
                                            typeQNamePattern,
                                            qnamePattern);
        }
        else
        {
            if (typeQNamePattern.isMatch(ContentModel.ASSOC_CONTAINS))
            {
                Reference parentsParent = parent.execute(new GetParentReferenceMethod());

                NodeRef parentNodeRef = parent.toNodeRef();
                if (parentsParent == null)
                {
                    parentNodeRef = parent.execute(new GetActualNodeRefMethod(environment));

                }

                NodeRef referenceNodeRef = reference.toNodeRef();
                Map<QName, Serializable> properties = smartStore.getProperties(reference);
                Serializable name = properties.get(ContentModel.PROP_NAME);
                QName assocChildName = QName
                            .createQNameWithValidLocalName(VirtualContentModel.VIRTUAL_CONTENT_MODEL_1_0_URI,
                                                           name.toString());
                if (qnamePattern.isMatch(assocChildName))
                {
                    ChildAssociationRef assoc = new ChildAssociationRef(ContentModel.ASSOC_CONTAINS,
                                                                        parentNodeRef,
                                                                        assocChildName,
                                                                        referenceNodeRef);
                    return Arrays.asList(assoc);
                }
                else
                {
                    return Collections.emptyList();
                }
            }
            else
            {
                return Collections.emptyList();
            }
        }
    }
    else
    {
        return theTrait.getParentAssocs(nodeRef,
                                        typeQNamePattern,
                                        qnamePattern);
    }
}
 
Example 3
Source File: VirtualNodeServiceExtension.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ChildAssociationRef getPrimaryParent(NodeRef nodeRef)
{
	Reference reference = Reference.fromNodeRef(nodeRef);
    if (reference != null)
    {  
        Reference parent = reference.execute(new GetParentReferenceMethod());
        if (parent == null)
        {
            return getTrait().getPrimaryParent(reference.execute(new GetActualNodeRefMethod(environment)));
        }
        else
        {
            Reference parentsParent = parent.execute(new GetParentReferenceMethod());

            NodeRef parentNodeRef = parent.toNodeRef();
            if (parentsParent == null)
            {
                parentNodeRef = parent.execute(new GetActualNodeRefMethod(environment));

            }

            NodeRef referenceNodeRef = reference.toNodeRef();
            Map<QName, Serializable> refProperties = smartStore.getProperties(reference);
            Serializable childName = refProperties.get(ContentModel.PROP_NAME);
            QName childAssocQName = QName
                        .createQNameWithValidLocalName(VirtualContentModel.VIRTUAL_CONTENT_MODEL_1_0_URI,
                                                       childName.toString());
            ChildAssociationRef assoc = new ChildAssociationRef(ContentModel.ASSOC_CONTAINS,
                                                                parentNodeRef,
                                                                childAssocQName,
                                                                referenceNodeRef,
                                                                true,
                                                                -1);
            return assoc;
        }
    }
    else
    {
        return getTrait().getPrimaryParent(nodeRef);
    }
}
 
Example 4
Source File: GetChildAssocsMethod.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<ChildAssociationRef> execute(VirtualProtocol virtualProtocol, Reference reference)
            throws ProtocolMethodException
{
    if (typeQNamePattern.isMatch(ContentModel.ASSOC_CONTAINS))
    {
        List<ChildAssociationRef> childAssocs = new LinkedList<>();
        List<Reference> children = smartStore.list(reference);
        NodeRef nodeRefReference = reference.toNodeRef();
        int count = 0;
        for (Reference child : children)
        {
            if (count >= maxResults)
            {
                break;
            }

            NodeRef childNodeRef = child.toNodeRef();
            Serializable childName = environment.getProperty(childNodeRef,
                                                             ContentModel.PROP_NAME);
            QName childAssocQName = QName
                        .createQNameWithValidLocalName(VirtualContentModel.VIRTUAL_CONTENT_MODEL_1_0_URI,
                                                       childName.toString());
            if (qnamePattern.isMatch(childAssocQName))
            {

                ChildAssociationRef childAssoc = new ChildAssociationRef(ContentModel.ASSOC_CONTAINS,
                                                                         nodeRefReference,
                                                                         childAssocQName,
                                                                         childNodeRef,
                                                                         true,
                                                                         -1);
                childAssocs.add(childAssoc);
                count++;
            }
        }

        return childAssocs;
    }
    else
    {
        return Collections.emptyList();
    }
}
 
Example 5
Source File: EmailServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * MNT-9289
 * 
 * Change in case in email Subject causes DuplicateChildNodeNameException
 */
public void testCaseSensitivity() throws Exception
{
 NodeRef person = personService.getPerson(TEST_USER);
 String TEST_EMAIL="[email protected]";
 NodeRef testUserHomeFolder = (NodeRef)nodeService.getProperty(person, ContentModel.PROP_HOMEFOLDER);
    if(person == null)
    {
        logger.debug("new person created");
        Map<QName, Serializable> props = new HashMap<QName, Serializable>();
        props.put(ContentModel.PROP_USERNAME, TEST_USER);
        props.put(ContentModel.PROP_EMAIL, TEST_EMAIL);
        person = personService.createPerson(props);
    }
    
    nodeService.setProperty(person, ContentModel.PROP_EMAIL, TEST_EMAIL);

    Set<String> auths = authorityService.getContainedAuthorities(null, "GROUP_EMAIL_CONTRIBUTORS", true);
    if(!auths.contains(TEST_USER))
    {
        authorityService.addAuthority("GROUP_EMAIL_CONTRIBUTORS", TEST_USER);
    }
    
    String companyHomePathInStore = "/app:company_home"; 
    String storePath = "workspace://SpacesStore";
    StoreRef storeRef = new StoreRef(storePath);

    NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
    List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, companyHomePathInStore, null, namespaceService, false);
    NodeRef companyHomeNodeRef = nodeRefs.get(0);
    assertNotNull("company home is null", companyHomeNodeRef);
 
    String TEST_CASE_SENSITIVITY_SUBJECT = "Test (Mail)";
    String testUserHomeDBID = ((Long)nodeService.getProperty(testUserHomeFolder, ContentModel.PROP_NODE_DBID)).toString() + "@Alfresco.com";
 
    String from = TEST_EMAIL;
    String to = testUserHomeDBID;
    String content = "hello world";

    Session sess = Session.getDefaultInstance(new Properties());
    assertNotNull("sess is null", sess);
    SMTPMessage msg = new SMTPMessage(sess);
    InternetAddress[] toa =  { new InternetAddress(to) };
    
    EmailDelivery delivery = new EmailDelivery(to, from, null);

    msg.setFrom(new InternetAddress(TEST_EMAIL));
    msg.setRecipients(Message.RecipientType.TO, toa);
    msg.setContent(content, "text/plain");
 
    msg.setSubject(TEST_CASE_SENSITIVITY_SUBJECT);
    ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
    msg.writeTo(bos1);
    InputStream is = IOUtils.toInputStream(bos1.toString());
    assertNotNull("is is null", is);
    SubethaEmailMessage m = new SubethaEmailMessage(is);  
    folderEmailMessageHandler.setOverwriteDuplicates(false);
    emailService.importMessage(delivery, m);
    
    QName safeQName = QName.createQNameWithValidLocalName(NamespaceService.CONTENT_MODEL_1_0_URI, TEST_CASE_SENSITIVITY_SUBJECT);
    List<ChildAssociationRef> assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, safeQName);
    assertEquals(1, assocs.size());
    
    msg.setSubject(TEST_CASE_SENSITIVITY_SUBJECT.toUpperCase());
    ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
    msg.writeTo(bos2);
    is = IOUtils.toInputStream(bos2.toString());
    assertNotNull("is is null", is);
    m = new SubethaEmailMessage(is);  
    folderEmailMessageHandler.setOverwriteDuplicates(false);
    emailService.importMessage(delivery, m);
    
    safeQName = QName.createQNameWithValidLocalName(NamespaceService.CONTENT_MODEL_1_0_URI, TEST_CASE_SENSITIVITY_SUBJECT.toUpperCase() +  "(1)");
    assocs = nodeService.getChildAssocs(testUserHomeFolder, ContentModel.ASSOC_CONTAINS, safeQName);
    assertEquals(1, assocs.size());
}
 
Example 6
Source File: VirtualNodeServiceExtensionTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void setUpTestAssociations(NodeRef actualNodeRef)
{
    rootChildrenQNames = new QName[13];
    rootChildrenQNames[0] = QName.createQNameWithValidLocalName(VirtualContentModel.VIRTUAL_CONTENT_MODEL_1_0_URI,
                                                                "Node2");
    rootChildrenQNames[1] = QName.createQNameWithValidLocalName(VirtualContentModel.VIRTUAL_CONTENT_MODEL_1_0_URI,
                                                                "Node1");

    NodeRef node2 = nodeService.getChildByName(virtualFolder1NodeRef,
                                               ContentModel.ASSOC_CONTAINS,
                                               "Node2");

    String node2ChildNameString = "test1_2.txt";
    ChildAssociationRef node2ChildAssoc = createContent(node2,
                                                        node2ChildNameString);
    node2Test1_2_TXTNodeRef = node2ChildAssoc.getChildRef();
    rootChildrenQNames[2] = QName.createQNameWithValidLocalName(NamespaceService.CONTENT_MODEL_1_0_URI,
                                                                node2ChildNameString);

    nodeService.setProperty(node2ChildAssoc.getChildRef(),
                            ContentModel.PROP_TITLE,
                            NODE2TEST1_2_TXT);

    node2ChildrenQNames = new QName[2];
    node2ChildrenQNames[0] = QName.createQNameWithValidLocalName(VirtualContentModel.VIRTUAL_CONTENT_MODEL_1_0_URI,
                                                                 "Node2_1");
    node2ChildrenQNames[1] = node2ChildAssoc.getQName();

    NodeRef node2_1 = nodeService.getChildByName(node2,
                                                 ContentModel.ASSOC_CONTAINS,
                                                 "Node2_1");

    node2_1ChildrenQNames = new QName[10];
    for (int i = 1; i <= 10; i++)
    {
        ChildAssociationRef childAssoc = createContent(node2_1,
                                                       "test" + i + "_2_1.txt");
        rootChildrenQNames[2 + i] = QName.createQNameWithValidLocalName(NamespaceService.CONTENT_MODEL_1_0_URI,
                                                                        childAssoc.getQName().getLocalName());
        node2_1ChildrenQNames[i - 1] = childAssoc.getQName();
    }
}