@Transactional @Override @CacheEvict(value = "hooks", allEntries = true) public CommandProcessingResult createHook(final JsonCommand command) { try { this.context.authenticatedUser(); this.fromApiJsonDeserializer.validateForCreate(command.json()); final HookTemplate template = retrieveHookTemplateBy(command.stringValueOfParameterNamed(nameParamName)); final String configJson = command.jsonFragment(configParamName); final Set<HookConfiguration> config = assembleConfig(command.mapValueOfParameterNamed(configJson), template); final JsonArray events = command.arrayOfParameterNamed(eventsParamName); final Set<HookResource> allEvents = assembleSetOfEvents(events); Template ugdTemplate = null; if (command.hasParameter(templateIdParamName)) { final Long ugdTemplateId = command.longValueOfParameterNamed(templateIdParamName); ugdTemplate = this.ugdTemplateRepository.findOne(ugdTemplateId); if (ugdTemplate == null) { throw new TemplateNotFoundException(ugdTemplateId); } } final Hook hook = Hook.fromJson(command, template, config, allEvents, ugdTemplate); validateHookRules(template, config, allEvents); this.hookRepository.save(hook); return new CommandProcessingResultBuilder() .withCommandId(command.commandId()) .withEntityId(hook.getId()) .build(); } catch (final DataIntegrityViolationException dve) { handleHookDataIntegrityIssues(command, dve); return CommandProcessingResult.empty(); } }