Skip to content

Commit

Permalink
#36 - add configuration, rename log table
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Aug 2, 2015
1 parent a8275f3 commit 0257340
Show file tree
Hide file tree
Showing 22 changed files with 229 additions and 79 deletions.
10 changes: 5 additions & 5 deletions src/main/java/alfio/config/DataSourceConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import alfio.plugin.PluginDataStorageProvider;
import alfio.plugin.mailchimp.MailChimpPlugin;
import alfio.repository.plugin.PluginConfigurationRepository;
import alfio.repository.plugin.PluginEventRepository;
import alfio.repository.plugin.PluginLogRepository;
import alfio.util.TemplateManager;
import ch.digitalfondue.npjt.QueryFactory;
import ch.digitalfondue.npjt.QueryRepositoryScanner;
Expand Down Expand Up @@ -157,13 +157,13 @@ public JMustacheTemplateLoader getTemplateLoader() {
}

@Bean
public MailChimpPlugin getMailChimpPlugin(PluginConfigurationRepository pluginConfigurationRepository, PluginEventRepository pluginEventRepository) {
return new MailChimpPlugin(pluginDataStorageProvider(pluginConfigurationRepository, pluginEventRepository));
public MailChimpPlugin getMailChimpPlugin(PluginConfigurationRepository pluginConfigurationRepository, PluginLogRepository pluginLogRepository) {
return new MailChimpPlugin(pluginDataStorageProvider(pluginConfigurationRepository, pluginLogRepository));
}

@Bean
public PluginDataStorageProvider pluginDataStorageProvider(PluginConfigurationRepository pluginConfigurationRepository, PluginEventRepository pluginEventRepository) {
return new PluginDataStorageProvider(pluginConfigurationRepository, pluginEventRepository);
public PluginDataStorageProvider pluginDataStorageProvider(PluginConfigurationRepository pluginConfigurationRepository, PluginLogRepository pluginLogRepository) {
return new PluginDataStorageProvider(pluginConfigurationRepository, pluginLogRepository);
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/alfio/config/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package alfio.config;

import alfio.filter.RedirectToHttpsFilter;
import alfio.util.DefaultExceptionHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.apache.logging.log4j.Level;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.web.context.ConfigurableWebApplicationContext;
Expand Down Expand Up @@ -48,6 +50,8 @@ public class Initializer extends AbstractAnnotationConfigDispatcherServletInitia
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);

Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler());

configureSessionCookie(servletContext);

CharacterEncodingFilter cef = new CharacterEncodingFilter();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/alfio/config/SpringBootLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
*/
package alfio.config;

import alfio.util.DefaultExceptionHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.apache.logging.log4j.Level;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
Expand All @@ -32,6 +34,8 @@ public class SpringBootLauncher {
*/
public static void main(String[] args) {

Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler());

String profiles = System.getProperty("spring.profiles.active", "");
log.info("requested profiles {}", profiles);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
*/
package alfio.controller.api.admin;

import alfio.manager.plugin.PluginManager;
import alfio.manager.system.ConfigurationManager;
import alfio.model.modification.ConfigurationModification;
import alfio.model.modification.PluginConfigOptionModification;
import alfio.model.plugin.PluginConfigOption;
import alfio.model.system.Configuration;
import alfio.model.system.ConfigurationKeys;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -39,10 +42,12 @@
public class SettingsApiController {

private final ConfigurationManager configurationManager;
private final PluginManager pluginManager;

@Autowired
public SettingsApiController(ConfigurationManager configurationManager) {
public SettingsApiController(ConfigurationManager configurationManager, PluginManager pluginManager) {
this.configurationManager = configurationManager;
this.pluginManager = pluginManager;
}

@RequestMapping(value = "/configuration/load", method = GET)
Expand All @@ -64,6 +69,18 @@ public Map<ConfigurationKeys.SettingCategory, List<Configuration>> updateConfigu
return loadConfiguration();
}

@RequestMapping(value = "/configuration/plugin/load", method = GET)
public List<PluginConfigOption> loadPluginConfiguration() {
return pluginManager.loadAllConfigOptions();
}

@RequestMapping(value = "/configuration/plugin/update-bulk", method = POST)
public List<PluginConfigOption> updatePluginConfiguration(@RequestBody List<PluginConfigOptionModification> input) {
Objects.requireNonNull(input);
pluginManager.saveAllConfigOptions(input);
return loadPluginConfiguration();
}

@RequestMapping(value = "/configuration/key/{key}", method = DELETE)
public boolean deleteKey(@PathVariable("key") String key) {
configurationManager.deleteKey(key);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/alfio/manager/TicketReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public PaymentResult confirm(String gatewayToken, Event event, String reservatio
} else {
paymentResult = PaymentResult.successful(NOT_YET_PAID_TRANSACTION_ID);
}
completeReservation(reservationId, email, fullName, userLanguage, billingAddress, specialPriceSessionId, paymentProxy);
completeReservation(event.getId(), reservationId, email, fullName, userLanguage, billingAddress, specialPriceSessionId, paymentProxy);
return paymentResult;
} catch(Exception ex) {
//it is guaranteed that in this case we're dealing with "local" error (e.g. database failure),
Expand Down Expand Up @@ -318,7 +318,7 @@ public void confirmOfflinePayment(Event event, String reservationId) {
Locale language = findReservationLanguage(reservationId);
notificationManager.sendSimpleEmail(event, ticketReservation.getEmail(), messageSource.getMessage("reservation-email-subject",
new Object[]{ getShortReservationID(reservationId), event.getDisplayName()}, language), () -> templateManager.renderClassPathResource("/alfio/templates/confirmation-email-txt.ms", prepareModelForReservationEmail(event, ticketReservation), language, TemplateOutput.TEXT));
pluginManager.handleReservationConfirmation(ticketReservationRepository.findReservationById(reservationId));
pluginManager.handleReservationConfirmation(ticketReservationRepository.findReservationById(reservationId), event.getId());
}

private Locale findReservationLanguage(String reservationId) {
Expand Down Expand Up @@ -423,11 +423,11 @@ public Optional<Triple<Event, TicketReservation, Ticket>> from(String eventName,
* @param billingAddress
* @param specialPriceSessionId
*/
private void completeReservation(String reservationId, String email, String fullName, Locale userLanguage, String billingAddress, Optional<String> specialPriceSessionId, PaymentProxy paymentProxy) {
private void completeReservation(int eventId, String reservationId, String email, String fullName, Locale userLanguage, String billingAddress, Optional<String> specialPriceSessionId, PaymentProxy paymentProxy) {
if(paymentProxy != PaymentProxy.OFFLINE) {
TicketStatus ticketStatus = paymentProxy.isDeskPaymentRequired() ? TicketStatus.TO_BE_PAID : TicketStatus.ACQUIRED;
acquireTickets(ticketStatus, paymentProxy, reservationId, email, fullName, userLanguage.getLanguage(), billingAddress);
pluginManager.handleReservationConfirmation(ticketReservationRepository.findReservationById(reservationId));
pluginManager.handleReservationConfirmation(ticketReservationRepository.findReservationById(reservationId), eventId);
}
//cleanup unused special price codes...
specialPriceSessionId.ifPresent(specialPriceRepository::unbindFromSession);
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/alfio/manager/plugin/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@

import alfio.model.Ticket;
import alfio.model.TicketReservation;
import alfio.model.modification.PluginConfigOptionModification;
import alfio.model.plugin.PluginConfigOption;
import alfio.model.system.ComponentType;
import alfio.plugin.Plugin;
import alfio.plugin.ReservationConfirmationPlugin;
import alfio.plugin.TicketAssignmentPlugin;
import alfio.repository.plugin.PluginConfigurationRepository;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;

Expand All @@ -39,22 +41,28 @@ public class PluginManager implements ApplicationListener<ContextRefreshedEvent>

private final List<Plugin> plugins;
private final PluginConfigurationRepository pluginConfigurationRepository;
private final Executor executor = Executors.newCachedThreadPool();
private final ExecutorService executor = Executors.newCachedThreadPool();

@Autowired
public PluginManager(List<Plugin> plugins, PluginConfigurationRepository pluginConfigurationRepository) {
this.plugins = plugins;
this.pluginConfigurationRepository = pluginConfigurationRepository;
}

public void handleReservationConfirmation(TicketReservation reservation) {
filterPlugins(plugins, ReservationConfirmationPlugin.class)
.forEach(p -> executor.execute(() -> p.onReservationConfirmation(reservation)));
public void handleReservationConfirmation(TicketReservation reservation, int eventId) {
executor.submit(() -> filterPlugins(plugins, ReservationConfirmationPlugin.class).forEach(p -> p.onReservationConfirmation(reservation, eventId)));
}

public void handleTicketAssignment(Ticket ticket) {
filterPlugins(plugins, TicketAssignmentPlugin.class)
.forEach(p -> executor.execute(() -> p.onTicketAssignment(ticket)));
executor.submit(() -> filterPlugins(plugins, TicketAssignmentPlugin.class).forEach(p -> p.onTicketAssignment(ticket)));
}

public List<PluginConfigOption> loadAllConfigOptions() {
return pluginConfigurationRepository.loadAll();
}

public void saveAllConfigOptions(List<PluginConfigOptionModification> input) {
input.forEach(m -> pluginConfigurationRepository.update(m.getPluginId(), m.getName(), m.getValue()));
}

private static <T extends Plugin> Stream<T> filterPlugins(List<Plugin> plugins, Class<T> type) {
Expand All @@ -68,6 +76,9 @@ private static <T extends Plugin> Stream<T> filterPlugins(List<Plugin> plugins,
public void onApplicationEvent(ContextRefreshedEvent event) {
plugins.stream()
.filter(p -> !pluginConfigurationRepository.loadSingleOption(p.getId(), Plugin.ENABLED_CONF_NAME).isPresent())
.forEach(p -> pluginConfigurationRepository.insert(p.getId(), Plugin.ENABLED_CONF_NAME, "false", "Enabled", ComponentType.BOOLEAN));
.forEach(p -> {
pluginConfigurationRepository.insert(p.getId(), Plugin.ENABLED_CONF_NAME, "false", "Enabled", ComponentType.BOOLEAN);
p.install();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.model.modification;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

@Getter
public class PluginConfigOptionModification {
private final String pluginId;
private final String name;
private final String value;

public PluginConfigOptionModification(@JsonProperty("pluginId") String pluginId,
@JsonProperty("optionName") String name,
@JsonProperty("optionValue") String value) {
this.pluginId = pluginId;
this.name = name;
this.value = value;
}
}
8 changes: 8 additions & 0 deletions src/main/java/alfio/model/plugin/PluginConfigOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public PluginConfigOption(@Column("plugin_id") String pluginId,
this.description = description;
this.componentType = componentType;
}

public String getKey() {
return optionName;
}

public String getValue() {
return optionValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,27 @@
import java.time.ZonedDateTime;

@Getter
public class PluginEvent {
public class PluginLog {

public enum Type {
SUCCESS, ERROR, WARNING
}

private final int id;
private final int eventId;
private final String pluginId;
private final String description;
private final Type type;
private final ZonedDateTime timestamp;

public PluginEvent(@Column("id") int id,
@Column("plugin_id") String pluginId,
@Column("description") String description,
@Column("type") Type type,
@Column("event_ts") ZonedDateTime timestamp) {
public PluginLog(@Column("id") int id,
@Column("event_id") int eventId,
@Column("plugin_id") String pluginId,
@Column("description") String description,
@Column("type") Type type,
@Column("event_ts") ZonedDateTime timestamp) {
this.id = id;
this.eventId = eventId;
this.pluginId = pluginId;
this.description = description;
this.type = type;
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/alfio/plugin/PluginDataStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package alfio.plugin;

import alfio.model.plugin.PluginConfigOption;
import alfio.model.plugin.PluginEvent;
import alfio.model.plugin.PluginLog;
import alfio.model.system.ComponentType;
import alfio.repository.plugin.PluginConfigurationRepository;
import alfio.repository.plugin.PluginEventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import alfio.repository.plugin.PluginLogRepository;

import java.time.Clock;
import java.time.ZonedDateTime;
Expand All @@ -30,30 +29,30 @@
public class PluginDataStorageProvider {

private final PluginConfigurationRepository pluginConfigurationRepository;
private final PluginEventRepository pluginEventRepository;
private final PluginLogRepository pluginLogRepository;

public PluginDataStorageProvider(PluginConfigurationRepository pluginConfigurationRepository,
PluginEventRepository pluginEventRepository) {
PluginLogRepository pluginLogRepository) {
this.pluginConfigurationRepository = pluginConfigurationRepository;
this.pluginEventRepository = pluginEventRepository;
this.pluginLogRepository = pluginLogRepository;
}

public PluginDataStorage getDataStorage(String pluginId) {
return new PluginDataStorage(pluginId, pluginConfigurationRepository, pluginEventRepository);
return new PluginDataStorage(pluginId, pluginConfigurationRepository, pluginLogRepository);
}


public static class PluginDataStorage {
private final String pluginId;
private final PluginConfigurationRepository pluginConfigurationRepository;
private final PluginEventRepository pluginEventRepository;
private final PluginLogRepository pluginLogRepository;

private PluginDataStorage(String pluginId,
PluginConfigurationRepository pluginConfigurationRepository,
PluginEventRepository pluginEventRepository) {
PluginLogRepository pluginLogRepository) {
this.pluginId = pluginId;
this.pluginConfigurationRepository = pluginConfigurationRepository;
this.pluginEventRepository = pluginEventRepository;
this.pluginLogRepository = pluginLogRepository;
}

public Optional<String> getConfigValue(String name) {
Expand All @@ -64,12 +63,12 @@ public void insertConfigValue(String name, String value, String description, Com
pluginConfigurationRepository.insert(pluginId, name, value, description, componentType);
}

public void registerSuccess(String description) {
pluginEventRepository.insertEvent(pluginId, description, PluginEvent.Type.SUCCESS, ZonedDateTime.now(Clock.systemUTC()));
public void registerSuccess(String description, int eventId) {
pluginLogRepository.insertEvent(pluginId, eventId, description, PluginLog.Type.SUCCESS, ZonedDateTime.now(Clock.systemUTC()));
}

public void registerFailure(String description) {
pluginEventRepository.insertEvent(pluginId, description, PluginEvent.Type.ERROR, ZonedDateTime.now(Clock.systemUTC()));
public void registerFailure(String description, int eventId) {
pluginLogRepository.insertEvent(pluginId, eventId, description, PluginLog.Type.ERROR, ZonedDateTime.now(Clock.systemUTC()));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public interface ReservationConfirmationPlugin extends Plugin {
/**
* Does something immediately after the confirmation of a reservation.
* @param ticketReservation the confirmed reservation
* @param eventId the id of the event
*/
void onReservationConfirmation(TicketReservation ticketReservation);
void onReservationConfirmation(TicketReservation ticketReservation, int eventId);
}
Loading

0 comments on commit 0257340

Please sign in to comment.