# Dynamic decorators

## Dynamic decorators

MOLGENIS decorators are used to add functionality, like for example security checks, to repository functionality. Most of these decorators are applied to all repositories, with the dynamic decorators it is possible to configure for which entity types a decorator should be used.

## Creating a dynamic decorator

To create a dynamic decorator two classes are needed:

### The decorator

This class must extend AbstractRepositoryDecorator, and can be used to override the add, update and delete functions in this class.

Example decorator that logs the add and update of an entity:

```
package org.molgenis.app;

import org.molgenis.data.AbstractRepositoryDecorator;
import org.molgenis.data.Entity;
import org.molgenis.data.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggingRepositoryDecorator extends AbstractRepositoryDecorator<Entity>
{
  private static final Logger LOG = LoggerFactory.getLogger(LoggingRepositoryDecorator.class);

  public LoggingRepositoryDecorator(Repository<Entity> delegateRepository)
  {
    super(delegateRepository);
  }

  @Override
  public void add(Entity entity)
  {
    LOG.info("adding entity:'{}' of type '{}'",entity.getIdValue(),entity.getEntityType().getId());
    super.add(entity);
  }

  @Override
  public void update(Entity entity)
  {
    LOG.info("updating entity:'{}' of type '{}'",entity.getIdValue(),entity.getEntityType().getId());
    super.update(entity);
  }
}
```

### The factory for this decorator

This class must implement DynamicRepositoryDecoratorFactory

Example:

```
package org.molgenis.app;

import org.molgenis.data.Repository;
import org.molgenis.data.decorator.DynamicRepositoryDecoratorFactory;
import org.springframework.stereotype.Component;

@Component
public class LoggingRepositoryDecoratorFactory implements DynamicRepositoryDecoratorFactory
{
  private static final String ID = "log";

  @Override
  @SuppressWarnings("unchecked")
  public Repository createDecoratedRepository(Repository repository)
  {
    return new LoggingRepositoryDecorator(repository);
  }

  @Override
  public String getId()
  {
    return ID;
  }

  @Override
  public String getLabel()
  {
    return "Logging Decorator";
  }

  @Override
  public String getDescription()
  {
    return "This is a demo decorator that logs the add(Entity entity) and update(Entity entity)";
  }
}
```

## Configuring a dynamic decorator for an entityType

To apply a dynamic decorator to an entity type, a row should be added to the "sys\_dec\_DecoratorConfiguration" via the dataexplorer, this entity contains:

* The identifier of the entitytype.
* an mref to the decorators to be applied

Once this row is added the decorators are applied to the specified entity type.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://molgenis.gitbook.io/molgenis/8.1/for-developers/guide-dynamic-decorators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
