class ArchiveUnitRulesUpdateActionPlugin extends ActionHandler implements VitamAutoCloseable {

    @Override
    public ItemStatus execute(WorkerParameters params, HandlerIO handler) {
        checkMandatoryParameters(params);
        LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin running ...");

        final ItemStatus itemStatus = new ItemStatus(UPDATE_UNIT_RULES_TASK_ID);
        // Get ArchiveUnit information
        try (MetaDataClient metaDataClient = metaDataClientFactory.getClient()) {
            String archiveUnitId = params.getObjectName().split("\\.")[0];
            final SelectParserMultiple selectRequest = new SelectParserMultiple();
            ObjectNode projectionNode = JsonHandler.createObjectNode();
            ObjectNode objectNode = JsonHandler.createObjectNode();
            objectNode.put(MANAGEMENT_KEY, 1);
            projectionNode.set(FIELDS_KEY, objectNode);
            final SelectMultiQuery request = selectRequest.getRequest().reset().addProjection(projectionNode);
            JsonNode jsonResponse =
                metaDataClient.selectUnitbyId(request.getFinalSelect(), archiveUnitId);

            JsonNode archiveUnitNode = jsonResponse.get(RESULTS);
            // if result = 0 then throw Exception
            if (archiveUnitNode.size() == 0) {
                throw new MetaDataNotFoundException(ARCHIVE_UNIT_NOT_FOUND);
            }
            archiveUnitNode = archiveUnitNode.get(0);
            JsonNode managementNode = archiveUnitNode.get(MANAGEMENT_KEY);

            UpdateMultiQuery query = new UpdateMultiQuery();

            Map<String, List<JsonNode>> updatedRulesByType = new HashMap<>();

            final JsonNode rulesForAU = handler.getJsonFromWorkspace(
                UpdateWorkflowConstants.UNITS_FOLDER + "/" + params.getObjectName());

            for (final JsonNode rule : rulesForAU) {
                if (!updatedRulesByType.containsKey(rule.get(RULE_TYPE).asText())) {
                    List<JsonNode> listRulesByType = new ArrayList<>();
                    listRulesByType.add(rule);
                    updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType);
                } else {
                    List<JsonNode> listRulesByType = updatedRulesByType.get(rule.get(RULE_TYPE).asText());
                    listRulesByType.add(rule);
                    updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType);
                }
            }
            int nbUpdates = 0;
            for (Map.Entry<String, List<JsonNode>> entry : updatedRulesByType.entrySet()) {
                JsonNode categoryNode = managementNode.get(entry.getKey());
                if (categoryNode != null && categoryNode.get(RULES_KEY) != null
                    && archiveUnitUpdateUtils.updateCategoryRules((ArrayNode) categoryNode.get(RULES_KEY),
                    entry.getValue(), query,
                    entry.getKey())) {
                    nbUpdates++;
                }
            }
            // if at least one action is set
            if (nbUpdates > 0) {
                query
                    .addActions(UpdateActionHelper.push(VitamFieldsHelper.operations(), params.getContainerName()));
                JsonNode updateResultJson = metaDataClient.updateUnitById(query.getFinalUpdate(), archiveUnitId);
                String diffMessage = archiveUnitUpdateUtils.getDiffMessageFor(updateResultJson, archiveUnitId);
                archiveUnitUpdateUtils.logLifecycle(params, archiveUnitId, StatusCode.OK, diffMessage,
                    handler.getLifecyclesClient());
                archiveUnitUpdateUtils.commitLifecycle(params.getContainerName(), archiveUnitId,
                    handler.getLifecyclesClient());
            }
            itemStatus.increment(StatusCode.OK);
        } catch (ProcessingException e) {
            LOGGER.error("Exception while getting AU information : ", e);
            itemStatus.increment(StatusCode.FATAL);
        } catch (InvalidParseOperationException e) {
            LOGGER.error("Exception while parsing json : ", e);
            itemStatus.increment(StatusCode.KO);
        } catch (MetaDataExecutionException | MetaDataDocumentSizeException | MetaDataClientServerException |
            InvalidCreateOperationException e) {
            LOGGER.error("Exception with metadata: ", e);
            itemStatus.increment(StatusCode.FATAL);
        } catch (MetaDataNotFoundException e) {
            LOGGER.error("Archive unit not found : ", e);
            itemStatus.increment(StatusCode.KO);
        }

        LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin response: " + itemStatus.getGlobalStatus());
        return new ItemStatus(UPDATE_UNIT_RULES_TASK_ID).setItemsStatus(UPDATE_UNIT_RULES_TASK_ID, itemStatus);
    }

    public  ArchiveUnitRulesUpdateActionPlugin();
    @VisibleForTesting public  ArchiveUnitRulesUpdateActionPlugin(MetaDataClientFactory metaDataClientFactory);

}