Commit 146c6d41 authored by Noel Alonso's avatar Noel Alonso
Browse files

Merge branch 'dev' into 'master'

Actualiza versión

See merge request redmic-project/server/library/commands-lib!2
parents 9e5898e1 ee6d2594
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ stages:

maven-build:
  stage: build
  image: redmic/maven-gitlab
  image: registry.gitlab.com/redmic-project/docker/maven
  variables:
    MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  only:
+14 −8
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@
	<parent>
		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-parent</artifactId>
    	<version>2.0.0.RELEASE</version>
    	<version>2.1.0.RELEASE</version>
		<relativePath/>
	</parent>

@@ -12,7 +12,7 @@
	<groupId>es.redmic.lib</groupId>
	<artifactId>commands-lib</artifactId>
	<packaging>jar</packaging>
	<version>0.6.0</version>
	<version>0.8.0</version>
	<name>commands-lib</name>
	
	<properties>
@@ -23,13 +23,13 @@
		<java.version>1.8</java.version>

		<!-- REDMIC -->
		<redmic.models.version>0.6.0</redmic.models.version>
		<redmic.broker-lib.version>0.6.0</redmic.broker-lib.version>
		<redmic.rest-lib.version>0.6.0</redmic.rest-lib.version>
		<redmic.models.version>0.8.0</redmic.models.version>
		<redmic.broker-lib.version>0.8.0</redmic.broker-lib.version>
		<redmic.rest-lib.version>0.8.0</redmic.rest-lib.version>

		<!-- OTHERS -->
		<kafka-stream.version>1.0.0</kafka-stream.version>
		<confluent.version>4.0.0</confluent.version>
		<kafka.version>2.0.1</kafka.version>
		<confluent.version>5.0.1</confluent.version>
		<powermock-tests-utils.version>1.6.6</powermock-tests-utils.version>
		<commons-io.version>1.3.2</commons-io.version>
		
@@ -44,7 +44,6 @@
		<dependency>
			<groupId>org.apache.kafka</groupId>
			<artifactId>kafka-streams</artifactId>
			<version>${kafka-stream.version}</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.kafka</groupId>
@@ -136,6 +135,13 @@
			<uniqueVersion>false</uniqueVersion>
		</snapshotRepository>
	</distributionManagement>
	<repositories>
		<repository>
			<id>confluent</id>
			<name>Confluent</name>
			<url>https://packages.confluent.io/maven/</url>
		</repository>
	</repositories>
	<build>
		<plugins>
			<plugin>
+73 −2
Original line number Diff line number Diff line
@@ -6,7 +6,10 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import es.redmic.brokerlib.avro.common.Event;
import es.redmic.brokerlib.avro.common.SimpleEvent;
import es.redmic.brokerlib.avro.common.EventTypes;
import es.redmic.commandslib.exceptions.HistoryNotFoundException;
import es.redmic.commandslib.exceptions.ItemLockedException;
import es.redmic.exception.data.ItemNotFoundException;

public abstract class Aggregate {

@@ -18,6 +21,64 @@ public abstract class Aggregate {

	protected static Logger logger = LogManager.getLogger();

	/*
	 * Función que comprueba si el item ya existe
	 */
	protected boolean exist(String id) {

		if (id != null) {
			// comprueba que el id no exista
			Event state = getItemFromStateStore(id);

			if (state != null) {

				loadFromHistory(state);

				if (!isDeleted()) {
					return true;
				}
				reset();
			}
		}
		return false;
	}

	/*
	 * Función para obtener el último estado del
	 */
	protected Event getStateFromHistory(String id) {

		Event state = getItemFromStateStore(id);

		if (state == null) {
			logger.error("Intentando modificar(editar o eliminar) un elemento del cual no se tiene historial, ", id);
			throw new HistoryNotFoundException(EventTypes.UPDATE + " | " + EventTypes.DELETE, id);
		}

		return state;
	}

	protected void checkState(String id, String eventType) {

		if (this.deleted) {
			logger.error("Intentando modificar un elemento eliminado, ", id);
			throw new ItemNotFoundException("id", id);
		}
	}

	protected void check(Event event) {

		if (isLocked(event.getType()) && !event.getType().equals(EventTypes.DELETED)) {

			// TODO: Si el tiempo entre el último y el actual es superior a el máximo del
			// ciclo, compensar.

			logger.error("Intentando modificar un elemento bloqueado por una edición en curso, ",
					event.getAggregateId());
			throw new ItemLockedException("id", event.getAggregateId());
		}
	}

	public String getAggregateId() {
		return aggregateId;
	}
@@ -34,11 +95,21 @@ public abstract class Aggregate {
		this.version = version;
	}

	protected void _apply(SimpleEvent event) {
	public void apply(Event event) {
		setVersion(event.getVersion());
		setAggregateId(event.getAggregateId());
	}

	/*
	 * Función que devuelve si el item específico está bloqueado
	 */
	protected abstract boolean isLocked(String eventType);

	/*
	 * Función que obtiene el item con id pasado en caso de existir.
	 */
	protected abstract Event getItemFromStateStore(String id);

	/*
	 * Función que a partir de todos los eventos generados sobre un item, aplica
	 * todos los cambios para restaurar el estado actual del item. Si queremos
+12 −8
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@ import es.redmic.exception.common.BaseException;

public abstract class CommandHandler implements ApplicationEventPublisherAware {

	@Value("${eventsource.timeout.ms}")
	private long timeoutms;
	@Value("${rest.eventsource.timeout.ms}")
	private long timeoutMS;

	protected static Logger logger = LogManager.getLogger();

@@ -44,7 +44,6 @@ public abstract class CommandHandler implements ApplicationEventPublisherAware {
	/* Handler por defecto para descartar los mensajes que no queremos */
	@KafkaHandler(isDefault = true)
	public void defaultListen(Object event) {
		logger.info("Mensaje descartado: " + event.getClass());
	}

	// Resuelve el CompletableFuture con el evento recibido
@@ -62,7 +61,7 @@ public abstract class CommandHandler implements ApplicationEventPublisherAware {
			if (future != null) {
				future.complete(ex);// future.complete(ex);
			} else {
				logger.info("Petición asíncrona no resgistrada");
				logger.warn("Petición asíncrona no resgistrada para sessionId: " + sessionId);
			}
			return;
		});
@@ -90,13 +89,18 @@ public abstract class CommandHandler implements ApplicationEventPublisherAware {
	}

	// Resuelve el CompletableFuture controlando posibles fallos y borrando la
	// entrada
	protected <T> T getResult(String sessionId, CompletableFuture<T> completableFeature) {
	// entrada. El timeout es configurable dependiendo de la función llamada
	protected <T> T getResult(String sessionId, CompletableFuture<T> completableFuture) {
		return getResult(timeoutMS, sessionId, completableFuture);
	}

	protected <T> T getResult(long timeoutMS, String sessionId, CompletableFuture<T> completableFuture) {

		try {
			return completableFeature.get(timeoutms, TimeUnit.MILLISECONDS);
			return completableFuture.get(timeoutMS, TimeUnit.MILLISECONDS);
		} catch (InterruptedException | TimeoutException e) {
			logger.error(e.getMessage());
			// TODO: Enviar alerta ya que ha quedado un evento sin acabar el ciclo
			logger.error("Error. No se ha recibido confirmación de la acción realizada.");
			throw new ConfirmationTimeoutException();
		} catch (ExecutionException e) {
			if (e.getCause() instanceof BaseException)
+6 −4
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;
import com.kjetland.jackson.jsonSchema.JsonSchemaResources;

import es.redmic.commandslib.controller.CommandController;
import es.redmic.commandslib.controller.CommandBaseController;
import es.redmic.exception.mediastorage.MSFileUploadException;

public class GenerateJsonSchemaScanBean implements ApplicationContextAware {
@@ -64,9 +64,10 @@ public class GenerateJsonSchemaScanBean implements ApplicationContextAware {
		 * CommandController
		 */
		@SuppressWarnings("rawtypes")
		final Map<String, CommandController> controllers = applicationContext.getBeansOfType(CommandController.class);
		final Map<String, CommandBaseController> controllers = applicationContext
				.getBeansOfType(CommandBaseController.class);
		for (@SuppressWarnings("rawtypes")
		final CommandController controller : controllers.values()) {
		final CommandBaseController controller : controllers.values()) {

			try {
				Class<?> typeOfTDTO = (Class<?>) ((ParameterizedType) controller.getClass().getGenericSuperclass())
@@ -129,7 +130,8 @@ public class GenerateJsonSchemaScanBean implements ApplicationContextAware {
	public Map<String, Object> getProperties() {

		if (properties.isEmpty()) {
			String serverPath = env.getProperty("server.servlet.context-path") + env.getProperty("server.servlet.path");
			String serverPath = env.getProperty("server.servlet.context-path")
					+ env.getProperty("spring.mvc.servlet.path");
			for (Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext();) {
				PropertySource propertySource = (PropertySource) it.next();
				if (propertySource instanceof MapPropertySource) {
Loading