cancel
Showing results for 
Search instead for 
Did you mean: 

Access vocabulary from Java

hgonzalez_
Confirmed Champ
Confirmed Champ

Is there any way of accessing a vocabulary via Java code?

1 ACCEPTED ANSWER

Clément_Lardeur
Confirmed Champ
Confirmed Champ

You need to use a Session (org.nuxeo.ecm.directory.Session) connected to the SQL repository which contains all directories and the DirectoryService (org.nuxeo.ecm.directory.api.DirectoryService).

This is a snippet of my code to create a new directory entry :

    DirectoryService dirService = DirectoryHelper.getDirectoryService();
    if (dirService == null) {
        return;
    }

    DocumentModel entry = null;
    Session dirSession = null;
    int id;
    try {
        dirSession = dirService.open(<VOCABULARY_ID>);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", "1");
        map.put("label", "label.country.france");
        map.put("obsolete", "0");
        map.put("ordering", "0");
        entry = dirSession.createEntry(map);
    } finally {
        if (entry == null) {
            dirSession.close();
            throw new ApplicationException("Can't add reference to the directory <VOCABULARY_ID>");
        } else {
            dirSession.commit();
            dirSession.close();
        }
    }

Just take care to close (or commit) the Session correctly.

View answer in original post

2 REPLIES 2

Clément_Lardeur
Confirmed Champ
Confirmed Champ

You need to use a Session (org.nuxeo.ecm.directory.Session) connected to the SQL repository which contains all directories and the DirectoryService (org.nuxeo.ecm.directory.api.DirectoryService).

This is a snippet of my code to create a new directory entry :

    DirectoryService dirService = DirectoryHelper.getDirectoryService();
    if (dirService == null) {
        return;
    }

    DocumentModel entry = null;
    Session dirSession = null;
    int id;
    try {
        dirSession = dirService.open(<VOCABULARY_ID>);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", "1");
        map.put("label", "label.country.france");
        map.put("obsolete", "0");
        map.put("ordering", "0");
        entry = dirSession.createEntry(map);
    } finally {
        if (entry == null) {
            dirSession.close();
            throw new ApplicationException("Can't add reference to the directory <VOCABULARY_ID>");
        } else {
            dirSession.commit();
            dirSession.close();
        }
    }

Just take care to close (or commit) the Session correctly.

Merci, that's it .

Getting started

Attention: Blogs & Forums are in READ-ONLY mode from Oct 9-19 as we transition to our new platform. Learn more.

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.