AttributeService - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

This provides services for reading, writing, and querying global attributes. The AttributeService is used to get and set global, arbitrary attributes. Attributes typically have a key and a value, where the key consists of three segments (known as a key set) and a value. Attributes are stored in the database so they persist over server restarts.

An example of use is for persisting system-wide JMX configuration properties in Alfresco Content Services. The AttributeService class provides a Java interface for creating and managing attributes, including methods such as:

  • Serializable getAttribute(Serializable ... keys) - get an attribute using a list of unique keys
  • getAttributes(AttributeQueryCallback callback, Serializable ... keys) - Getting a collection of attributes
  • Serializable getAttribute(Serializable ... keys) - Getting a single attribute
  • setAttribute(Serializable value, Serializable ... keys) - Set attribute or create attribute if doesn’t exist
  • removeAttribute(Serializable ... keys) - Removing an attribute
  • removeAttributes(Serializable ... keys) - Removing a collection of attributes

Collections of Attributes can be processed on retrieval by implementing a callback handler object. The callback handler object’s handleAttribute method is invoked for each attribute retrieved.

Note: The AttributeService is not what you would use to get the attributes (more correctly, “properties”) of a node. Use the NodeService class for that. For more information, see NodeService.

The following example shows how you could map a unique document identifier to an Alfresco node reference independently of nodes:

publicclassDocId2NodeRefMapper {
/**
* The Alfresco Service Registry that gives access to all public content services in Alfresco.
*/
private ServiceRegistry serviceRegistry;

    publicvoidsetServiceRegistry(ServiceRegistry serviceRegistry) {
        this.serviceRegistry = serviceRegistry;
    }

    publicstaticfinal String ROOT_ATTR_PATH = "docId2NodeRefMappings";
    publicstaticfinal String DOC_ID_ATTR_NAME = "documentId";

    publicvoidmapDocId2NodeRef(String doc_id, NodeRef nodeRef) {
 
        // Check if mapping to node ref is already set up
        if (this.serviceRegistry.getAttributeService().exists(ROOT_ATTR_PATH, DOC_ID_ATTR_NAME, doc_id)) {
 
            // Check to see if this node has already been registered
            if (!this.serviceRegistry.getAttributeService().getAttribute(ROOT_ATTR_PATH, DOC_ID_ATTR_NAME, doc_id).equals(nodeRef)) {
                thrownew RuntimeException("Duplicate entry id:" + doc_id);
            }
        }

        // Register node reference under document identifier
        this.serviceRegistry.getAttributeService().setAttribute(nodeRef, ROOT_ATTR_PATH, DOC_ID_ATTR_NAME, doc_id);
    }
}

Notice how when you set the attribute value the value is the first parameter of the setAttribute method.