cancel
Showing results for 
Search instead for 
Did you mean: 

How to change a node's name before it gets created?

johannii
Champ in-the-making
Champ in-the-making
Hello,
due to a character constraint certain characters are not allowed in the file/ folder names. Our users have Macs that allow "<" and ">" characters in the file/ folder names and the email program they use exports emails in a way that the sender of the email is part of the folder name the email is being exported to. The sender usually appears in the following format: last name, first name <firstname.lastname@domain.com>. Now, the user is supposed to export an email directly into a webdav drive, so that the email is being uploaded into Alfresco. Because of these < > characters, the upload fails of course.

I was considering using a policy - "BeforeCreateNode" to be precise. So far, I have been able to read the name of that uploaded folder from one of the parameters of the beforeCreateNode method. But I have not yet found a way to change it. The idea was to get rid of those invalid characters, before the node actually gets created. But the to-be-created node name is coming over in a QName object, which does not have any setter or other manipulation methods. I have a feeling that this policy won't help me solve this problem.

Would be great, if someone could point me into the right direction.

Thanks!
3 REPLIES 3

mitpatoliya
Star Collaborator
Star Collaborator
I think you could use that.
If you are able to get the name of the content for which you are going to create node then you can rename it before it is actually being created could you post the code so that I can guide you more?

johannii
Champ in-the-making
Champ in-the-making
Here is the simplified code of the the policy behavior implementation:

public class MySecondPolicy extends TransactionListenerAdapter implements BeforeCreateNodePolicy{
    private PolicyComponent policyComponent;
    private static final Logger LOGGER = Logger.getLogger(MySecondPolicy.class);
    /**
     * Bind custom behavior to policy component
     */
    public void init() {

        this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "beforeCreateNode"),
            QName.createQName("http://www.alfresco.org/model/content/1.0", "folder"),
            new JavaBehaviour(this, "beforeCreateNode", NotificationFrequency.TRANSACTION_COMMIT));
    }

    public void setPolicyComponent(PolicyComponent policyComponent) {
        this.policyComponent = policyComponent;
    }

   @Override
   public void beforeCreateNode(NodeRef parentRef, QName arg1, QName folderName, QName arg3) {
      // TODO Auto-generated method stub
      LOGGER.info("Method 'beforeCreateNode' invoked");
      LOGGER.info(folderName.getLocalName());
      …
   }
}

jhesparrach
Champ in-the-making
Champ in-the-making
Hi,

I think an option to consider would be to use this policy: NodeServicePolicies.OnUpdatePropertiesPolicy.

This behaviour will be triggered every time a node is updated. When you create a node this behaviour is going to be triggered (after the other ones like onCreateNode) and you can access to the properties and modify whatever you want.

This is the signature of the method:

public void onUpdateProperties(NodeRef nodeRef, Map<QName, Serializable> before,
            Map<QName, Serializable> after)

When you create a node the param before will be empty.

Regards