<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic adding computed groups on document creation in Nuxeo Forum</title>
    <link>https://connect.hyland.com/t5/nuxeo-forum/adding-computed-groups-on-document-creation/m-p/316567#M3568</link>
    <description>&lt;P&gt;Hi There,&lt;/P&gt;
&lt;P&gt;I would like to add a computed group to a workspace at creation.
I understand that I need to create an eventListener for this for the event documentCreated:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;component name="WorkspaceCreationListener"&amp;gt;
    &amp;lt;extension target="org.nuxeo.ecm.core.event.EventServiceComponent" point="listener"&amp;gt;
        &amp;lt;listener name="documentCreated" async="false" postCommit="false" priority="120"
                  class="WorkspaceCreationListener"&amp;gt;
        &amp;lt;/listener&amp;gt;
    &amp;lt;/extension&amp;gt;
&amp;lt;/component&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My Listener class looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;public class WorkspaceCreationListener implements EventListener {


    private static final String TENANT_GROUP_PREFIX = "tenant-" ;
    private static final String TENANT_ADMINISTRATORS_GROUP_SUFFIX = "_tenantAdministrators";
    private CoreSession session;

    @Override
    public void handleEvent(Event event) throws ClientException {
            EventContext ctx = event.getContext();

            if (ctx != null)
                session = ctx.getCoreSession();

            if (!(ctx instanceof DocumentEventContext)) {
                return;
            }
            DocumentModel doc = ((DocumentEventContext) ctx).getSourceDocument();
            if (doc == null) {
                return;
            }
            if (doc.getType().equals("Workspace")){
                String tenantId = ((NuxeoPrincipal) ctx.getPrincipal()).getTenantId();
                process(doc,ctx.getPrincipal().toString(),tenantId);
            }
    }

    public void process(DocumentModel doc, String userId, String tenantId) throws ClientException {
        setACEs(doc,userId);
        setACEs(doc,computeTenantAdministratorsGroup(tenantId));

    /* doc.setPropertyValue("creator", emet);  perhap to position, i'm not enough sure */
    }

    public static String computeTenantAdministratorsGroup(String tenantId) {
        return TENANT_GROUP_PREFIX + tenantId
                + TENANT_ADMINISTRATORS_GROUP_SUFFIX;
    }

    private void setACEs(DocumentModel doc, String userId) throws ClientException {
        ACP acp = doc.getACP();
        ACL acl = acp.getOrCreateACL();
        int WorkspaceAdminACEIndex = acl.indexOf(new ACE(userId, SecurityConstants.EVERYTHING, true));
        if (WorkspaceAdminACEIndex == -1) {
            ACE ace = new ACE(userId, SecurityConstants.EVERYTHING, true);
            acl.add(0,ace);
            acp.addACL(acl);
            doc.setACP(acp, true);
        }
        session.saveDocument(doc);
        session.save();
    }
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However It seems like I'm getting an infinite loop.
Any ideas on this?&lt;/P&gt;
&lt;P&gt;Thanks,
Bauke Roo&lt;/P&gt;</description>
    <pubDate>Wed, 07 May 2014 09:34:35 GMT</pubDate>
    <dc:creator>Bauke_Roo</dc:creator>
    <dc:date>2014-05-07T09:34:35Z</dc:date>
    <item>
      <title>adding computed groups on document creation</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/adding-computed-groups-on-document-creation/m-p/316567#M3568</link>
      <description>&lt;P&gt;Hi There,&lt;/P&gt;
&lt;P&gt;I would like to add a computed group to a workspace at creation.
I understand that I need to create an eventListener for this for the event documentCreated:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;component name="WorkspaceCreationListener"&amp;gt;
    &amp;lt;extension target="org.nuxeo.ecm.core.event.EventServiceComponent" point="listener"&amp;gt;
        &amp;lt;listener name="documentCreated" async="false" postCommit="false" priority="120"
                  class="WorkspaceCreationListener"&amp;gt;
        &amp;lt;/listener&amp;gt;
    &amp;lt;/extension&amp;gt;
&amp;lt;/component&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My Listener class looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;public class WorkspaceCreationListener implements EventListener {


    private static final String TENANT_GROUP_PREFIX = "tenant-" ;
    private static final String TENANT_ADMINISTRATORS_GROUP_SUFFIX = "_tenantAdministrators";
    private CoreSession session;

    @Override
    public void handleEvent(Event event) throws ClientException {
            EventContext ctx = event.getContext();

            if (ctx != null)
                session = ctx.getCoreSession();

            if (!(ctx instanceof DocumentEventContext)) {
                return;
            }
            DocumentModel doc = ((DocumentEventContext) ctx).getSourceDocument();
            if (doc == null) {
                return;
            }
            if (doc.getType().equals("Workspace")){
                String tenantId = ((NuxeoPrincipal) ctx.getPrincipal()).getTenantId();
                process(doc,ctx.getPrincipal().toString(),tenantId);
            }
    }

    public void process(DocumentModel doc, String userId, String tenantId) throws ClientException {
        setACEs(doc,userId);
        setACEs(doc,computeTenantAdministratorsGroup(tenantId));

    /* doc.setPropertyValue("creator", emet);  perhap to position, i'm not enough sure */
    }

    public static String computeTenantAdministratorsGroup(String tenantId) {
        return TENANT_GROUP_PREFIX + tenantId
                + TENANT_ADMINISTRATORS_GROUP_SUFFIX;
    }

    private void setACEs(DocumentModel doc, String userId) throws ClientException {
        ACP acp = doc.getACP();
        ACL acl = acp.getOrCreateACL();
        int WorkspaceAdminACEIndex = acl.indexOf(new ACE(userId, SecurityConstants.EVERYTHING, true));
        if (WorkspaceAdminACEIndex == -1) {
            ACE ace = new ACE(userId, SecurityConstants.EVERYTHING, true);
            acl.add(0,ace);
            acp.addACL(acl);
            doc.setACP(acp, true);
        }
        session.saveDocument(doc);
        session.save();
    }
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However It seems like I'm getting an infinite loop.
Any ideas on this?&lt;/P&gt;
&lt;P&gt;Thanks,
Bauke Roo&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2014 09:34:35 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/adding-computed-groups-on-document-creation/m-p/316567#M3568</guid>
      <dc:creator>Bauke_Roo</dc:creator>
      <dc:date>2014-05-07T09:34:35Z</dc:date>
    </item>
    <item>
      <title>Re: adding computed groups on document creation</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/adding-computed-groups-on-document-creation/m-p/316568#M3569</link>
      <description>&lt;P&gt;Oke, I got this working, it still needs some optimalization.&lt;/P&gt;
&lt;P&gt;Two things needed to be done:&lt;/P&gt;
&lt;P&gt;one add the event in the contribution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;component name="WorkspaceCreationListener"&amp;gt;
    &amp;lt;extension target="org.nuxeo.ecm.core.event.EventServiceComponent" point="listener"&amp;gt;
        &amp;lt;listener name="documentCreated" async="false" postCommit="false" priority="120"
                  class="WorkspaceCreationListener"&amp;gt;
            &amp;lt;event&amp;gt;documentCreated&amp;lt;/event&amp;gt;
        &amp;lt;/listener&amp;gt;
    &amp;lt;/extension&amp;gt;
&amp;lt;/component&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Two:
Make use of the unrestrictedRunner&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;public class WorkspaceCreationListener implements EventListener {


private static final String TENANT_GROUP_PREFIX = "tenant-" ;
private static final String TENANT_ADMINISTRATORS_GROUP_SUFFIX = "_tenantAdministrators";


@Override
public void handleEvent(Event event) throws ClientException {
    if (event.getName().equals("documentCreated")){
        EventContext ctx = event.getContext();



        if (!(ctx instanceof DocumentEventContext)) {
            return;
        }
        DocumentModel doc = ((DocumentEventContext) ctx).getSourceDocument();
        if (doc == null) {
            return;
        }
        if (doc.getType().equals("Workspace")){
            String tenantId = ((NuxeoPrincipal) ctx.getPrincipal()).getTenantId();
            try {
                process(doc,ctx.getPrincipal().toString(),tenantId);
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        }
    }
}

public void process(DocumentModel doc, String userId, String tenantId) throws Exception {
    setACEs(doc,userId,tenantId);
    setACEs(doc,computeTenantAdministratorsGroup(tenantId),tenantId);

/* doc.setPropertyValue("creator", emet);  perhap to position, i'm not enough sure */
}

public static String computeTenantAdministratorsGroup(String tenantId) {
    return TENANT_GROUP_PREFIX + tenantId
            + TENANT_ADMINISTRATORS_GROUP_SUFFIX;
}

private void setACEs(DocumentModel doc, String userId, String tenantID) throws Exception {
    AceSetter setter = new AceSetter(doc,userId,tenantID);
    setter.runUnrestricted();
}

protected class AceSetter extends UnrestrictedSessionRunner {

    private final DocumentModel doc;
    private final String userId;


    public IterableQueryResult ids = null;


    private String username;

    protected AceSetter(DocumentModel doc, String userId,String repositoryName)
            throws Exception {
        super("default");
        this.doc = doc;
        this.userId = userId;
    }


    @Override
    public void run() throws ClientException {

        ACP acp =null;
        DocumentModel doc2 = session.getDocument(new PathRef(doc.getPath().toString()));
        try{
            acp = doc2.getACP();
        }
        catch (Exception e){

        }

        if (acp==null){
            acp =new ACPImpl();
        }
        ACL acl = acp.getOrCreateACL();
        int WorkspaceAdminACEIndex = acl.indexOf(new ACE(this.userId, SecurityConstants.EVERYTHING, true));
        if (WorkspaceAdminACEIndex == -1) {
            ACE ace = new ACE(this.userId, SecurityConstants.EVERYTHING, true);
            acl.add(0,ace);
            acp.addACL(acl);
            doc2.setACP(acp, true);
        }
    this.session.saveDocument(doc2);
    this.session.save();
    }
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;I hope this helps someone &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Bauke Roo&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2014 14:10:42 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/adding-computed-groups-on-document-creation/m-p/316568#M3569</guid>
      <dc:creator>Bauke_Roo</dc:creator>
      <dc:date>2014-05-07T14:10:42Z</dc:date>
    </item>
  </channel>
</rss>

