Today's question is raised by bledi who asks how to create a group on domain creation. That seems like a legitimate request. His first thought was to create an event handler that would run an operation chain each time a new domain document is created. This is a good solution, providing that you have an operation to allow group creation. Unfortunately, there is no operation that lets you create a group right now. So today I'm going to show you how to add one and use it in Nuxeo Studio.
Creating a new operation in Nuxeo is dead simple, especially using Nuxeo IDE. Just use the operation wizard and it will generate a nice operation skeleton. All you have left to do is fill it with the important business logic. If you have no idea what I m blabbering about, take some time to read the Nuxeo IDE documentation.
When creating a group in Nuxeo, or a user, you need to use the UserManager service. Looking at it, you'll find the createGroup method. But it takes a DocumentModel as parameter. At first glance it does not sound intuitive. One would have expected string parameters for the group id, label and description. The reason why you are asked for a DocumentModel is because the UserManager service relies on directories. If you're familiar with Nuxeo you might know that users and groups are stored in what we call directories.
In the Nuxeo Platform, a directory is a source of (mostly) table-like data that lives outside of the VCS document storage database. A directory is typically a connection to an external data source that is also accessed by other processes than the Nuxeo Platform itself (therefore allowing shared management and usage).
A directory may have different implementations (SQL table, LDAP, both) but a directory entry is always represented by a DocumentModel. This is why the UserManager wants a DocumentModel. The group directory is represented by the group.xsd schema. It has several fields like the groupname, grouplabel, description, members, etc. If you want to set the group name and label, you should do something like this:
groupDocModel.setProperty("group", "groupname", groupId);
groupDocModel.setProperty("group", "grouplabel", goupLabel);
groupDocModel.setProperty("group", "description", description);
Now the question you should be asking yourself is how do we create that groupDocMdel? What's its type? This question is answered by the UserManager service again, with the getBareGroupModel. This method will give you the DocumentModel you need.
Here's the full operation:
Once you've coded your operation, export it to Studio and add it to your operation chain.
This operation will be run by an event handler. In the enablement tab, I have selected the Document Created event and the Domain document type. It will run the chain each time a Domain document is created.
Now deploy your IDE and Studio projects and that's it, you're all set.