Init Commit

This commit is contained in:
uPagge 2020-11-12 02:03:10 +03:00
commit 34e72edbe0
No known key found for this signature in database
GPG Key ID: A76EB343414E022E
42 changed files with 1352 additions and 0 deletions

51
.gitignore vendored Normal file
View File

@ -0,0 +1,51 @@
# Created by .ignore support plugin (hsz.mobi)
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/contentModel.xml
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
cmake-build-*/
.idea/**/mongoSettings.xml
*.iws
out/
.idea_modules/
atlassian-ide-plugin.xml
.idea/replstate.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.idea/httpRequests
.idea/caches/build_file_checksums.ser

51
haiti-context/.gitignore vendored Normal file
View File

@ -0,0 +1,51 @@
# Created by .ignore support plugin (hsz.mobi)
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/contentModel.xml
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
cmake-build-*/
.idea/**/mongoSettings.xml
*.iws
out/
.idea_modules/
atlassian-ide-plugin.xml
.idea/replstate.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.idea/httpRequests
.idea/caches/build_file_checksums.ser

22
haiti-context/pom.xml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sadtech.haiti</groupId>
<artifactId>haiti</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>haiti-context</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,18 @@
package org.sadtech.haiti.context.anotation;
/**
* Маркер для разграничения политик валидации.
*
* @author upagge 26.05.20
*/
public interface Marker {
interface Create {
}
interface Update {
}
}

View File

@ -0,0 +1,12 @@
package org.sadtech.haiti.context.enums;
/**
* Допустимые типы сортировки.
*
* @author upagge 28.01.2020
*/
public enum TypeSort {
ASC, DESC
}

View File

@ -0,0 +1,13 @@
package org.sadtech.haiti.context.exception;
/**
* Исключения связанные с доступом.
*/
public class AccessException extends BasicException {
public AccessException(String message) {
super(message);
}
}

View File

@ -0,0 +1,18 @@
package org.sadtech.haiti.context.exception;
/**
* Общий класс для всех ошибок приложения.
*
* @author upagge 17.12.2019
*/
public abstract class BasicException extends RuntimeException {
protected BasicException(String message) {
super(message);
}
protected BasicException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,16 @@
package org.sadtech.haiti.context.exception;
/**
* Исключение при конвертации.
*/
public class ConvertException extends BasicException {
public ConvertException(String message) {
super(message);
}
public ConvertException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,14 @@
package org.sadtech.haiti.context.exception;
/**
* Исключения, возникающиек, когда необходимые данные не были найдены.
*
* @author upagge 17.12.2019
*/
public class NotFoundException extends BasicException {
public NotFoundException(String message) {
super(message);
}
}

View File

@ -0,0 +1,15 @@
package org.sadtech.haiti.context.page;
import org.sadtech.haiti.context.enums.TypeSort;
public interface Pagination {
int getPage();
int getSize();
TypeSort getTypeSort();
String getFieldSort();
}

View File

@ -0,0 +1,22 @@
package org.sadtech.haiti.context.page;
import java.util.List;
import java.util.function.Function;
public interface Sheet<T> {
int getNumber();
int getSize();
long getTotalElement();
int getTotalPage();
List<T> getContent();
boolean hasContent();
<U> Sheet<U> map(Function<? super T, ? extends U> converter);
}

View File

@ -0,0 +1,8 @@
package org.sadtech.haiti.context.repository;
import org.sadtech.haiti.context.repository.simple.CrudOperation;
import org.sadtech.haiti.context.repository.simple.MultipleOperation;
import org.sadtech.haiti.context.repository.simple.PagingOperation;
public interface SimpleManagerRepository<T, K> extends CrudOperation<T, K>, MultipleOperation<T, K>, PagingOperation<T> {
}

View File

@ -0,0 +1,31 @@
package org.sadtech.haiti.context.repository.simple;
import lombok.NonNull;
import java.util.Optional;
/**
* Контракт для реализации объекта, который будет взаимодействовать с хранилищем данных.
*
* @param <T> Класс сущности
* @param <K> Класс идентификатора
* @author upagge
*/
public interface CrudOperation<T, K> {
/**
* Сохраняет сущность в базу данных. Не проверяет на наличие в хранилище.
* Если сущность с подобным идентификатором уже есть, то необходимо ее перезаписать.
*
* @param entity Сущность для сохранения
* @return Сохраненная сущность
*/
T save(@NonNull T entity);
Optional<T> findById(@NonNull K primaryKey);
boolean existsById(@NonNull K primaryKey);
void deleteById(@NonNull K primaryKey);
}

View File

@ -0,0 +1,14 @@
package org.sadtech.haiti.context.repository.simple;
import lombok.NonNull;
import java.util.Collection;
import java.util.List;
public interface MultipleOperation<T, K> {
List<T> saveAll(@NonNull Collection<T> entities);
void deleteAllById(@NonNull Collection<K> primaryKeys);
}

View File

@ -0,0 +1,11 @@
package org.sadtech.haiti.context.repository.simple;
import lombok.NonNull;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
public interface PagingOperation<T> {
Sheet<T> findAll(@NonNull Pagination pagination);
}

View File

@ -0,0 +1,9 @@
package org.sadtech.haiti.context.service;
import org.sadtech.haiti.context.service.simple.MultipleService;
import org.sadtech.haiti.context.service.simple.PagingService;
import org.sadtech.haiti.context.service.simple.SimpleService;
public interface SimpleManagerService<T, K> extends SimpleService<T, K>, MultipleService<T, K>, PagingService<T> {
}

View File

@ -0,0 +1,16 @@
package org.sadtech.haiti.context.service.simple;
import lombok.NonNull;
import java.util.Collection;
import java.util.List;
public interface MultipleService<T, K> {
List<T> createAll(@NonNull Collection<T> entities);
List<T> updateAll(@NonNull Collection<T> entities);
void deleteAllById(@NonNull Collection<K> ids);
}

View File

@ -0,0 +1,17 @@
package org.sadtech.haiti.context.service.simple;
import lombok.NonNull;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
/**
* Расширение базового контракта сервисов с добавлением пагинации.
*
* @param <T> Сущность сервиса.
* @author upagge 26.05.20
*/
public interface PagingService<T> {
Sheet<T> getAll(@NonNull Pagination pagination);
}

View File

@ -0,0 +1,24 @@
package org.sadtech.haiti.context.service.simple;
import lombok.NonNull;
import java.util.Optional;
/**
* Базовый контракт для сервисов.
*
* @param <T> Сущность сервиса.
*/
public interface SimpleService<T, K> {
T create(@NonNull T entity);
T update(@NonNull T entity);
Optional<T> getById(@NonNull K primaryKey);
boolean existsById(@NonNull K primaryKey);
void deleteById(@NonNull K primaryKey);
}

51
haiti-core/.gitignore vendored Normal file
View File

@ -0,0 +1,51 @@
# Created by .ignore support plugin (hsz.mobi)
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/contentModel.xml
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
cmake-build-*/
.idea/**/mongoSettings.xml
*.iws
out/
.idea_modules/
atlassian-ide-plugin.xml
.idea/replstate.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.idea/httpRequests
.idea/caches/build_file_checksums.ser

32
haiti-core/pom.xml Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sadtech.haiti</groupId>
<artifactId>haiti</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>haiti-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.sadtech.haiti</groupId>
<artifactId>haiti-context</artifactId>
</dependency>
<dependency>
<groupId>org.sadtech.haiti.filter</groupId>
<artifactId>filter-api</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,35 @@
package org.sadtech.haiti.core.page;
import lombok.Getter;
import org.sadtech.haiti.context.enums.TypeSort;
import org.sadtech.haiti.context.page.Pagination;
@Getter
public class PaginationImpl implements Pagination {
private final int page;
private final int size;
private String fieldSort;
private TypeSort typeSort;
private PaginationImpl(int page, int size) {
this.page = page;
this.size = size;
}
public PaginationImpl(int page, int size, String fieldSort, TypeSort typeSort) {
this.page = page;
this.size = size;
this.fieldSort = fieldSort;
this.typeSort = typeSort;
}
public static Pagination of(int page, int size) {
return new PaginationImpl(page, size);
}
public static Pagination of(int page, int size, String fieldSort, TypeSort typeSort) {
return new PaginationImpl(page, size, fieldSort, typeSort);
}
}

View File

@ -0,0 +1,40 @@
package org.sadtech.haiti.core.page;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.sadtech.haiti.context.page.Sheet;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
@Getter
@Builder
@AllArgsConstructor
public class SheetImpl<T> implements Sheet<T> {
private final int number;
private final int size;
private final long totalElement;
private final int totalPage;
private final List<T> content;
@Override
public boolean hasContent() {
return content != null && !content.isEmpty();
}
@Override
public <U> Sheet<U> map(Function<? super T, ? extends U> function) {
return new SheetImpl<>(
this.number,
this.size,
this.totalElement,
this.totalPage,
this.content.stream().map(function).collect(Collectors.toList())
);
}
}

View File

@ -0,0 +1,40 @@
package org.sadtech.haiti.core.service;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.filter.Filter;
import org.sadtech.haiti.filter.FilterOperation;
import org.sadtech.haiti.filter.FilterService;
import java.util.Optional;
@RequiredArgsConstructor
public abstract class AbstractFilterService<T, F> implements FilterService<T, F> {
private final FilterOperation<T> filterOperation;
@Override
public Sheet<T> getAll(@NonNull F filter, Pagination pagination) {
return filterOperation.findAll(createFilter(filter), pagination);
}
@Override
public Optional<T> getFirst(@NonNull F filter) {
return filterOperation.findFirst(createFilter(filter));
}
@Override
public boolean exists(@NonNull F filter) {
return filterOperation.exists(createFilter(filter));
}
@Override
public long count(@NonNull F filter) {
return filterOperation.count(createFilter(filter));
}
protected abstract Filter createFilter(@NonNull F filter);
}

View File

@ -0,0 +1,34 @@
package org.sadtech.haiti.core.service;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.repository.simple.MultipleOperation;
import org.sadtech.haiti.context.service.simple.MultipleService;
import org.sadtech.haiti.context.service.simple.SimpleService;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public abstract class AbstractMultipleService<T, K> implements MultipleService<T, K> {
private final SimpleService<T, K> simpleService;
private final MultipleOperation<T, K> multipleOperation;
@Override
public void deleteAllById(@NonNull Collection<K> ids) {
multipleOperation.deleteAllById(ids);
}
@Override
public List<T> createAll(@NonNull Collection<T> entities) {
return entities.stream().map(simpleService::create).collect(Collectors.toList());
}
@Override
public List<T> updateAll(@NonNull Collection<T> entities) {
return entities.stream().map(simpleService::update).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,20 @@
package org.sadtech.haiti.core.service;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.repository.simple.PagingOperation;
import org.sadtech.haiti.context.service.simple.PagingService;
@RequiredArgsConstructor
public abstract class AbstractPagingService<T> implements PagingService<T> {
private final PagingOperation<T> pagingOperation;
@Override
public Sheet<T> getAll(@NonNull Pagination pagination) {
return pagingOperation.findAll(pagination);
}
}

View File

@ -0,0 +1,56 @@
package org.sadtech.haiti.core.service;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.repository.SimpleManagerRepository;
import org.sadtech.haiti.context.service.SimpleManagerService;
import org.sadtech.haiti.core.util.ServiceOperation;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public abstract class AbstractSimpleManagerService<T, K> implements SimpleManagerService<T, K> {
private final SimpleManagerRepository<T, K> repository;
@Override
public void deleteAllById(@NonNull Collection<K> primaryKeys) {
ServiceOperation.deleteAllById(repository, primaryKeys);
}
@Override
public Sheet<T> getAll(@NonNull Pagination pagination) {
return ServiceOperation.getAll(repository, pagination);
}
@Override
public Optional<T> getById(@NonNull K primaryKey) {
return ServiceOperation.getById(repository, primaryKey);
}
@Override
public boolean existsById(@NonNull K primaryKey) {
return ServiceOperation.existsById(repository, primaryKey);
}
@Override
public void deleteById(@NonNull K primaryKey) {
ServiceOperation.deleteById(repository, primaryKey);
}
@Override
public List<T> createAll(@NonNull Collection<T> entities) {
return entities.stream().map(this::create).collect(Collectors.toList());
}
@Override
public List<T> updateAll(@NonNull Collection<T> entities) {
return entities.stream().map(this::update).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,30 @@
package org.sadtech.haiti.core.service;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.repository.simple.CrudOperation;
import org.sadtech.haiti.context.service.simple.SimpleService;
import java.util.Optional;
@RequiredArgsConstructor
public abstract class AbstractSimpleService<T, K> implements SimpleService<T, K> {
private final CrudOperation<T, K> crudOperation;
@Override
public Optional<T> getById(@NonNull K primaryKey) {
return crudOperation.findById(primaryKey);
}
@Override
public boolean existsById(@NonNull K primaryKey) {
return crudOperation.existsById(primaryKey);
}
@Override
public void deleteById(@NonNull K primaryKey) {
crudOperation.deleteById(primaryKey);
}
}

View File

@ -0,0 +1,23 @@
package org.sadtech.haiti.core.util;
/**
* // TODO: 06.09.2020 Добавить описание.
*
* @author upagge 06.09.2020
*/
public final class Assert {
public static void isNull(Object object, String message) {
if (object != null) {
throw new IllegalStateException(message);
}
}
public static void isNotNull(Object object, String message) {
if (object == null) {
throw new IllegalStateException(message);
}
}
}

View File

@ -0,0 +1,37 @@
package org.sadtech.haiti.core.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.repository.simple.CrudOperation;
import org.sadtech.haiti.context.repository.simple.MultipleOperation;
import org.sadtech.haiti.context.repository.simple.PagingOperation;
import java.util.Collection;
import java.util.Optional;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ServiceOperation {
public static <T, K> void deleteAllById(MultipleOperation<T, K> repository, Collection<K> primaryKeys) {
repository.deleteAllById(primaryKeys);
}
public static <T> Sheet<T> getAll(PagingOperation<T> repository, Pagination pagination) {
return repository.findAll(pagination);
}
public static <T, K> Optional<T> getById(CrudOperation<T, K> repository, K primaryKey) {
return repository.findById(primaryKey);
}
public static <T, K> boolean existsById(CrudOperation<T, K> repository, K primaryKey) {
return repository.existsById(primaryKey);
}
public static <T, K> void deleteById(CrudOperation<T, K> repository, K primaryKey) {
repository.deleteById(primaryKey);
}
}

51
haiti-database/.gitignore vendored Normal file
View File

@ -0,0 +1,51 @@
# Created by .ignore support plugin (hsz.mobi)
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/contentModel.xml
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
cmake-build-*/
.idea/**/mongoSettings.xml
*.iws
out/
.idea_modules/
atlassian-ide-plugin.xml
.idea/replstate.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.idea/httpRequests
.idea/caches/build_file_checksums.ser

37
haiti-database/pom.xml Normal file
View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sadtech.haiti</groupId>
<artifactId>haiti</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>haiti-database</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.sadtech.haiti</groupId>
<artifactId>haiti-core</artifactId>
</dependency>
<dependency>
<groupId>org.sadtech.haiti.filter</groupId>
<artifactId>criteria-filter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,24 @@
package org.sadtech.haiti.database.entity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@Getter
@Setter
@MappedSuperclass
@EqualsAndHashCode(of = "id")
public abstract class BasicEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
protected Long id;
}

View File

@ -0,0 +1,35 @@
package org.sadtech.haiti.database.repository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.repository.simple.CrudOperation;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
@RequiredArgsConstructor
public abstract class AbstractCrudOperation<T, K> implements CrudOperation<T, K> {
private final JpaRepository<T, K> jpaRepository;
@Override
public T save(@NonNull T entity) {
return OperationJpa.save(jpaRepository, entity);
}
@Override
public Optional<T> findById(@NonNull K primaryKey) {
return OperationJpa.findById(jpaRepository, primaryKey);
}
@Override
public boolean existsById(@NonNull K primaryKey) {
return OperationJpa.existsById(jpaRepository, primaryKey);
}
@Override
public void deleteById(@NonNull K primaryKey) {
OperationJpa.deleteById(jpaRepository, primaryKey);
}
}

View File

@ -0,0 +1,38 @@
package org.sadtech.haiti.database.repository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.filter.Filter;
import org.sadtech.haiti.filter.FilterOperation;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import java.util.Optional;
@RequiredArgsConstructor
public abstract class AbstractFilterOperation<T, K> implements FilterOperation<T> {
private final JpaRepositoryImplementation<T, K> jpaRepository;
@Override
public Sheet<T> findAll(@NonNull Filter filter, @NonNull Pagination pagination) {
return OperationJpa.findAll(jpaRepository, filter.build(), pagination);
}
@Override
public Optional<T> findFirst(@NonNull Filter filter) {
return OperationJpa.findFirst(jpaRepository, filter.build());
}
@Override
public boolean exists(@NonNull Filter filter) {
return OperationJpa.exists(jpaRepository, filter.build());
}
@Override
public long count(@NonNull Filter filter) {
return OperationJpa.count(jpaRepository, filter.build());
}
}

View File

@ -0,0 +1,26 @@
package org.sadtech.haiti.database.repository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.repository.simple.MultipleOperation;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Collection;
import java.util.List;
@RequiredArgsConstructor
public abstract class AbstractMultipleOperation<T, K> implements MultipleOperation<T, K> {
private final JpaRepository<T, K> jpaRepository;
@Override
public List<T> saveAll(@NonNull Collection<T> entities) {
return OperationJpa.saveAll(jpaRepository, entities);
}
@Override
public void deleteAllById(@NonNull Collection<K> primaryKeys) {
OperationJpa.deleteAllById(jpaRepository, primaryKeys);
}
}

View File

@ -0,0 +1,20 @@
package org.sadtech.haiti.database.repository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.repository.simple.PagingOperation;
import org.springframework.data.jpa.repository.JpaRepository;
@RequiredArgsConstructor
public abstract class AbstractPagingOperation<T, K> implements PagingOperation<T> {
private final JpaRepository<T, K> jpaRepository;
@Override
public Sheet<T> findAll(@NonNull Pagination pagination) {
return OperationJpa.findAll(jpaRepository, pagination);
}
}

View File

@ -0,0 +1,73 @@
package org.sadtech.haiti.database.repository;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.database.util.Converter;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OperationJpa {
public static <T, K> T save(JpaRepository<T, K> jpaRepository, T entity) {
return jpaRepository.save(entity);
}
public static <T, K> Optional<T> findById(JpaRepository<T, K> jpaRepository, K primaryKey) {
return jpaRepository.findById(primaryKey);
}
public static <K, T> boolean existsById(JpaRepository<T, K> jpaRepository, K primaryKey) {
return jpaRepository.existsById(primaryKey);
}
public static <K, T> void deleteById(JpaRepository<T, K> jpaRepository, K primaryKey) {
jpaRepository.deleteById(primaryKey);
}
public static <T, K> Sheet<T> findAll(JpaRepository<T, K> jpaRepository, Pagination pagination) {
return Converter.page(
jpaRepository.findAll(
Converter.pagination(pagination)
)
);
}
public static <T, K> List<T> saveAll(JpaRepository<T, K> jpaRepository, Collection<T> entities) {
return jpaRepository.saveAll(entities);
}
public static <K, T> void deleteAllById(JpaRepository<T, K> jpaRepository, Collection<K> primaryKeys) {
primaryKeys.forEach(jpaRepository::deleteById);
}
public static <T, K> Optional<T> findFirst(JpaRepositoryImplementation<T, K> jpaRepository, Specification<T> specification) {
return jpaRepository.findOne(specification);
}
public static <K, T> boolean exists(JpaRepositoryImplementation<T, K> jpaRepository, Specification<T> specification) {
return jpaRepository.count(specification) > 0;
}
public static <T, K> List<T> findAll(JpaRepositoryImplementation<T, K> jpaRepository, Specification<T> specification) {
return jpaRepository.findAll(specification);
}
public static <T, K> Sheet<T> findAll(JpaRepositoryImplementation<T, K> jpaRepository, Specification<T> specification, Pagination pagination) {
return Converter.page(
jpaRepository.findAll(specification, Converter.pagination(pagination))
);
}
public static <T, K> long count(JpaRepositoryImplementation<T, K> jpaRepository, Specification<T> specification) {
return jpaRepository.count(specification);
}
}

View File

@ -0,0 +1,55 @@
package org.sadtech.haiti.database.repository.manager;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.haiti.database.repository.OperationJpa;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.repository.SimpleManagerRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@RequiredArgsConstructor
public abstract class AbstractSimpleManagerRepository<T, K> implements SimpleManagerRepository<T, K> {
private final JpaRepository<T, K> jpaRepository;
@Override
public T save(@NonNull T entity) {
return OperationJpa.save(jpaRepository, entity);
}
@Override
public Optional<T> findById(@NonNull K primaryKey) {
return OperationJpa.findById(jpaRepository, primaryKey);
}
@Override
public boolean existsById(@NonNull K primaryKey) {
return OperationJpa.existsById(jpaRepository, primaryKey);
}
@Override
public void deleteById(@NonNull K primaryKey) {
OperationJpa.deleteById(jpaRepository, primaryKey);
}
@Override
public List<T> saveAll(@NonNull Collection<T> entities) {
return OperationJpa.saveAll(jpaRepository, entities);
}
@Override
public void deleteAllById(@NonNull Collection<K> primaryKeys) {
OperationJpa.deleteAllById(jpaRepository, primaryKeys);
}
@Override
public Sheet<T> findAll(@NonNull Pagination pagination) {
return OperationJpa.findAll(jpaRepository, pagination);
}
}

View File

@ -0,0 +1,59 @@
package org.sadtech.haiti.database.repository.manager;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.database.repository.OperationJpa;
import org.sadtech.haiti.filter.Filter;
import org.sadtech.haiti.filter.FilterOperation;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import java.util.Optional;
@Slf4j
public abstract class FilterManagerRepository<T, K> extends AbstractSimpleManagerRepository<T, K> implements FilterOperation<T> {
private final JpaRepositoryImplementation<T, K> jpaRepositoryImplementation;
public FilterManagerRepository(JpaRepositoryImplementation<T, K> jpaRepository) {
super(jpaRepository);
this.jpaRepositoryImplementation = jpaRepository;
}
@Override
public Sheet<T> findAll(@NonNull Filter filter, @NonNull Pagination pagination) {
return OperationJpa.findAll(jpaRepositoryImplementation, filter.build(), pagination);
}
@Override
public Optional<T> findFirst(@NonNull Filter filter) {
try {
return OperationJpa.findFirst(jpaRepositoryImplementation, filter.build());
} catch (InvalidDataAccessResourceUsageException e) {
log.error(e.getMessage());
}
return Optional.empty();
}
@Override
public boolean exists(@NonNull Filter filter) {
try {
return OperationJpa.exists(jpaRepositoryImplementation, filter.build());
} catch (InvalidDataAccessResourceUsageException e) {
log.error(e.getMessage());
}
return false;
}
@Override
public long count(@NonNull Filter filter) {
try {
return OperationJpa.count(jpaRepositoryImplementation, filter.build());
} catch (InvalidDataAccessResourceUsageException e) {
log.error(e.getMessage());
}
return 0;
}
}

View File

@ -0,0 +1,41 @@
package org.sadtech.haiti.database.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.sadtech.haiti.context.enums.TypeSort;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.core.page.SheetImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Converter {
public static Pageable pagination(@NonNull Pagination pagination) {
if (pagination.getTypeSort() != null) {
return PageRequest.of(
pagination.getPage(),
pagination.getSize(),
TypeSort.ASC.equals(pagination.getTypeSort()) ? Sort.Direction.ASC : Sort.Direction.DESC,
pagination.getFieldSort()
);
}
return PageRequest.of(pagination.getPage(), pagination.getSize());
}
public static <T> Sheet<T> page(@NonNull Page<T> page) {
return SheetImpl.<T>builder()
.totalPage(page.getTotalPages())
.totalElement(page.getTotalElements())
.size(page.getSize())
.number(page.getNumber())
.content(page.getContent())
.build();
}
}

113
pom.xml Normal file
View File

@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sadtech.haiti</groupId>
<artifactId>haiti</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>haiti-context</module>
<module>haiti-core</module>
<module>haiti-database</module>
</modules>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<sadtech.haiti.context.version>0.0.1-SNAPSHOT</sadtech.haiti.context.version>
<sadtech.haiti.core.version>0.0.1-SNAPSHOT</sadtech.haiti.core.version>
<sadtech.haiti.filter.api.version>0.0.1-SNAPSHOT</sadtech.haiti.filter.api.version>
<sadtech.haiti.filter.criteria.version>0.0.1-SNAPSHOT</sadtech.haiti.filter.criteria.version>
<spring.data.elasticsearch.version>4.1.0</spring.data.elasticsearch.version>
<spring.data.jpa.version>2.4.0</spring.data.jpa.version>
<spring.data.commons.version>2.4.0</spring.data.commons.version>
<elasticsearch.version>7.9.3</elasticsearch.version>
<lombok.version>1.18.16</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.sadtech.haiti</groupId>
<artifactId>haiti-context</artifactId>
<version>${sadtech.haiti.context.version}</version>
</dependency>
<dependency>
<groupId>org.sadtech.haiti</groupId>
<artifactId>haiti-core</artifactId>
<version>${sadtech.haiti.core.version}</version>
</dependency>
<dependency>
<groupId>org.sadtech.haiti.filter</groupId>
<artifactId>criteria-filter</artifactId>
<version>${sadtech.haiti.filter.criteria.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sadtech.haiti.filter</groupId>
<artifactId>filter-api</artifactId>
<version>${sadtech.haiti.filter.api.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>${spring.data.elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${spring.data.commons.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.jpa.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<developers>
<developer>
<id>uPagge</id>
<name>Struchkov Mark</name>
<email>upagge@ya.ru</email>
<organization>SADTECH</organization>
<url>https://t.me/uPagge</url>
</developer>
</developers>
</project>