Access vocabulary from Java

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

2 votes

1 answers

4272 views

ANSWER



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.

4 votes



Merci, that's it .
11/10/2011