Commit 20e5624a authored by Nacho's avatar Nacho
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+4 −0
Original line number Diff line number Diff line
.*
!.gitignore
!.gitlab-ci.yml
target

.gitlab-ci.yml

0 → 100644
+24 −0
Original line number Diff line number Diff line
stages:
  - build

maven-build:
  stage: build
  image: redmic/maven-gitlab
  variables:
    MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  only:
    - branches
  cache:
    paths:
      - .m2/repository/
  script:
    - mvn deploy -B
    - "COVERAGE=$(xmllint --html --xpath '//table[@id=\"coveragetable\"]/tfoot//td[@class=\"ctr2\"][1]/text()' target/site/jacoco/index.html)"
    - 'echo "Coverage: $COVERAGE"'
  after_script:
    - rm -r .m2/repository/es
  artifacts:
    name: "$CI_PROJECT_NAME"
    expire_in: '6 months'
    paths:
      - target/*.jar

README.md

0 → 100644
+2 −0
Original line number Diff line number Diff line
[![pipeline status](https://git.redmic.net/redmic-server/commands-lib/badges/dev/pipeline.svg)](https://git.redmic.net/redmic-server/commands-lib/commits/dev) [![coverage report](https://git.redmic.net/redmic-server/commands-lib/badges/dev/coverage.svg)](https://git.redmic.net/redmic-server/commands-lib/commits/dev)
 No newline at end of file

pom.xml

0 → 100644
+163 −0
Original line number Diff line number Diff line
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<parent>
		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-parent</artifactId>
    	<version>2.0.0.RELEASE</version>
		<relativePath/>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<groupId>es.redmic.lib</groupId>
	<artifactId>commands-lib</artifactId>
	<packaging>jar</packaging>
	<version>0.6.0</version>
	<name>commands-lib</name>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<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>

		<!-- OTHERS -->
		<kafka-stream.version>1.0.0</kafka-stream.version>
		<confluent.version>4.0.0</confluent.version>
		<powermock-tests-utils.version>1.6.6</powermock-tests-utils.version>
		<commons-io.version>1.3.2</commons-io.version>
		
		<!-- Version plugins -->
		<jacoco.version>0.8.1</jacoco.version>
		
		<!-- Environment variables -->
		<env.MAVEN_REPO_URL>https://artifactory.redmic.net/artifactory</env.MAVEN_REPO_URL>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.kafka</groupId>
			<artifactId>kafka-streams</artifactId>
			<version>${kafka-stream.version}</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.kafka</groupId>
					<artifactId>kafka-clients</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-api</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.fasterxml.jackson.core</groupId>
		    		<artifactId>jackson-databind</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>io.confluent</groupId>
			<artifactId>kafka-streams-avro-serde</artifactId>
			<version>${confluent.version}</version>
			<exclusions>
				<exclusion>
					<groupId>io.confluent</groupId>
					<artifactId>kafka-schema-registry-client</artifactId>
				</exclusion>
				<exclusion>
					<groupId>io.confluent</groupId>
					<artifactId>kafka-avro-serializer</artifactId>
				</exclusion>
			</exclusions>
		</dependency>


		<!-- Redmic -->

		<dependency>
			<groupId>es.redmic.lib</groupId>
			<artifactId>models</artifactId>
			<version>${redmic.models.version}</version>
		</dependency>

		<dependency>
			<groupId>es.redmic.lib</groupId>
			<artifactId>broker-lib</artifactId>
			<version>${redmic.broker-lib.version}</version>
		</dependency>

		<dependency>
			<groupId>es.redmic.lib</groupId>
			<artifactId>rest-lib</artifactId>
			<version>${redmic.rest-lib.version}</version>
		</dependency>
		
		<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-io</artifactId>
		    <version>${commons-io.version}</version>
		</dependency>
		
		<!-- Test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
		    <groupId>org.powermock.tests</groupId>
		    <artifactId>powermock-tests-utils</artifactId>
		    <version>${powermock-tests-utils.version}</version>
		    <scope>test</scope>
		</dependency>
		
	</dependencies>
	<distributionManagement>
		<repository>
			<id>central</id>
			<name>redmic-releases</name>
			<url>${env.MAVEN_REPO_URL}/libs-release-local</url>
		</repository>
		<snapshotRepository>
			<id>snapshots</id>
			<name>redmic-snapshots</name>
			<url>${env.MAVEN_REPO_URL}/libs-snapshot-local</url>
			<uniqueVersion>false</uniqueVersion>
		</snapshotRepository>
	</distributionManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>${jacoco.version}</version>
				<executions>
					<execution>
						<id>pre-unit-test</id>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<execution>
						<id>post-unit-test</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
+69 −0
Original line number Diff line number Diff line
package es.redmic.commandslib.aggregate;

import java.util.List;

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;

public abstract class Aggregate {

	protected boolean deleted = false;

	private String aggregateId;

	private Integer version;

	protected static Logger logger = LogManager.getLogger();

	public String getAggregateId() {
		return aggregateId;
	}

	public void setAggregateId(String aggregateId) {
		this.aggregateId = aggregateId;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

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

	/*
	 * 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
	 * obtener todos los cambios debemos usar KStream
	 */
	public void loadFromHistory(List<Event> history) {

		for (Event evt : history) {
			loadFromHistory(evt);
		}
	}

	/*
	 * Función que a partir de un evento generado sobre un item, aplica dicho
	 * evento, seteando un estado válido.
	 */
	public abstract void loadFromHistory(Event event);

	protected void reset() {

		setVersion(null);
		setAggregateId(null);
	}

	public boolean isDeleted() {
		return deleted;
	}
}