use of alfio.model.ExtensionSupport.ExtensionMetadataValue in project alf.io by alfio-event.
the class EventApiV1Controller method insertEvent.
private Optional<Event> insertEvent(EventCreationRequest request, Principal user, String imageRef) {
Organization organization = userManager.findUserOrganizations(user.getName()).get(0);
EventModification em = request.toEventModification(organization, eventNameManager::generateShortName, imageRef);
eventManager.createEvent(em, user.getName());
Optional<Event> event = eventManager.getOptionalByName(em.getShortName(), user.getName());
event.ifPresent(e -> {
Optional.ofNullable(request.getTickets().getPromoCodes()).ifPresent(promoCodes -> promoCodes.forEach(// TODO add ref to categories
pc -> eventManager.addPromoCode(pc.getName(), e.getId(), organization.getId(), ZonedDateTime.of(pc.getValidFrom(), e.getZoneId()), ZonedDateTime.of(pc.getValidTo(), e.getZoneId()), pc.getDiscount(), pc.getDiscountType(), Collections.emptyList(), null, null, null, PromoCodeDiscount.CodeType.DISCOUNT, null)));
// link categories to groups, if any
request.getTickets().getCategories().stream().filter(cr -> cr.getGroupLink() != null && cr.getGroupLink().getGroupId() != null).map(cr -> Pair.of(cr, groupManager.findById(cr.getGroupLink().getGroupId(), organization.getId()))).forEach(link -> {
if (link.getRight().isPresent()) {
Group group = link.getRight().get();
EventCreationRequest.CategoryRequest categoryRequest = link.getLeft();
findCategoryByName(e, categoryRequest.getName()).ifPresent(category -> {
EventCreationRequest.GroupLinkRequest groupLinkRequest = categoryRequest.getGroupLink();
LinkedGroupModification modification = new LinkedGroupModification(null, group.getId(), e.getId(), category.getId(), groupLinkRequest.getType(), groupLinkRequest.getMatchType(), groupLinkRequest.getMaxAllocation());
groupManager.createLink(group.getId(), e.getId(), modification);
});
}
});
if (!CollectionUtils.isEmpty(request.getExtensionSettings())) {
request.getExtensionSettings().stream().collect(Collectors.groupingBy(EventCreationRequest.ExtensionSetting::getExtensionId)).forEach((id, settings) -> {
List<ExtensionSupport.ExtensionMetadataIdAndName> metadata = extensionService.getSingle(organization, e, id).map(es -> extensionRepository.findAllParametersForExtension(es.getId())).orElseGet(Collections::emptyList);
List<ExtensionMetadataValue> values = settings.stream().map(es -> Pair.of(es, metadata.stream().filter(mm -> mm.getName().equals(es.getKey())).findFirst())).filter(pair -> {
if (pair.getRight().isEmpty()) {
log.warn("ignoring non-existent extension setting key {}", pair.getLeft().getKey());
}
return pair.getRight().isPresent();
}).map(pair -> new ExtensionMetadataValue(pair.getRight().get().getId(), pair.getLeft().getValue())).collect(Collectors.toList());
extensionService.bulkUpdateEventSettings(organization, e, values);
});
}
});
return event;
}
Aggregations