# Document Selection Plugin



The Document Selection Plugin allows you to determine whether a document will be generated and attached to a quote or policy.

This plugin will be automatically executed as part of the [policy document workflow](/configuration/resources/documents#policy_document_workflow). Document workflows can be customized through <ApiLink name="DocumentConfigRef" /> configuration objects.

This plugin is executed asynchronously.

Supported Entity Types [#supported-entity-types]

The Document Selection Plugin supports the following entity types:

* Quotes
* Policy Transactions

<span id="document_selection_actions" />

Actions [#actions]

Actions determine how the Document Selection Plugin handles a given document. For each document, one of the following actions can be specified:

* `generate` - Generate and attach a new document instance, and replace any currently attached instances of the same document within the same document `scope`
* `noAction` - No new document instances will be generated and attached, and any existing instances of the same document within the same document `scope` will remain attached
* `generateIfAbsent` - Generate and attach a new document instance if no instances of the same document within the same document `scope` are currently attached
* `remove` - Remove any currently attached instances of the same document within the same document `scope`

<Callout>
  The `remove` action does not affect documents if their `scope` value is set to `transaction`, since documents with this scope are associated with a transaction rather than a quote or policy.
</Callout>

When executing actions, the plugin considers two document instances to be instances of the same document if both document instances have the same `staticName` and `scope`. For multiple [versions](/configuration/resources/versioned-resource-selection) of the same document, all versions are considered to be instances of the same document.

Implementation [#implementation]

Create a new Java class in the `src/main/java/com/socotra/deployment/customer` folder. All plugin code must be contained within this folder. We named our class `DocumentSelectionPluginImpl.java` in the example below, but you can name your class whatever you'd like.

Implement the `DocumentSelectionPlugin` interface, and override the method corresponding to your target entity type.

For example, the following class contains a method that selects documents for commercial auto quotes:

```java
public class DocumentSelectionPluginImpl implements DocumentSelectionPlugin {
    private static final Logger log = LoggerFactory.getLogger(DocumentSelectionPluginImpl.class);

    // Select documents for commercial auto quotes

    @Override
    public Map<String, DocumentSelectionAction> selectDocuments(CommercialAutoQuoteRequest commercialAutoQuoteRequest) {
        return Map.of("TermsAndConditions", DocumentSelectionAction.generate);
    }
}
```

The method argument contains the entity. In this case, the entity is a commercial auto quote.

The method returns a map of document names to [actions](#document_selection_actions).

Examples [#examples]

All examples are based on the Prism configuration. Contact your Socotra representative for more information.

Quote [#quote]

```java
// Generate and attach documents listed in the document configuration for the commercial auto product if they’re not currently attached to commercial auto quotes

@Override
public Map<String, DocumentSelectionAction> selectDocuments(CommercialAutoQuoteRequest commercialAutoQuoteRequest) {
    Map<String, DocumentSelectionAction> response = new HashMap<>();

    commercialAutoQuoteRequest.documentConfigs()
            .forEach(config ->
                    response.put(config.name(), DocumentSelectionAction.generateIfAbsent)
            );

    return response;
}
```

Policy Transaction [#policy-transaction]

```java
// Generate and attach a document upon renewal of commercial auto policies

@Override
public Map<String, DocumentSelectionAction> selectDocuments(CommercialAutoRequest commercialAutoRequest) {
    Map<String, DocumentSelectionAction> response = new HashMap<>();

    if (commercialAutoRequest.transaction().transactionCategory().equals(TransactionCategory.renewal)) {
        response.put("TermsAndConditions", DocumentSelectionAction.generate);
    }

    return response;
}
```

Next Steps [#next-steps]

* [Installments Plugin](/configuration/plugins/installments)

See Also [#see-also]

* [Plugins Overview](/configuration/plugins/overview)
* [Documents](/configuration/resources/documents)
* [Policy Document Workflow](/configuration/resources/documents#policy_document_workflow)
* <ApiLink name="DocumentConfigRef" />
* [Versioned Resource Selection](/configuration/resources/versioned-resource-selection)
