add service hook
This commit is contained in:
parent
00fc0bc5f7
commit
d823b12ab1
5
pom.xml
5
pom.xml
@ -123,6 +123,11 @@
|
||||
<artifactId>tigon-codegen</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.0</version>
|
||||
</dependency>
|
||||
<!-- Spring Boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
|
@ -46,7 +46,7 @@
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.4</version>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
<!-- mybatis -->
|
||||
<dependency>
|
||||
|
@ -21,12 +21,12 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.1</version>
|
||||
<version>3.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.31</version>
|
||||
<version>1.2.47</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
|
@ -33,14 +33,14 @@ public class BaseForm implements BaseFormApi {
|
||||
*/
|
||||
@Override
|
||||
public <T> T copy(Class<T> clazz, boolean convert) {
|
||||
log.debug("Copy Form [{}] To Class [{}].", this, clazz);
|
||||
log.debug("Copy form [{}] to class [{}].", this, clazz);
|
||||
T obj = null;
|
||||
try {
|
||||
obj = clazz.newInstance();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(
|
||||
"Create [" + clazz + "] Object Error Caused", e);
|
||||
"Create [" + clazz + "] object error caused", e);
|
||||
}
|
||||
return copy(obj, convert);
|
||||
}
|
||||
@ -65,18 +65,18 @@ public class BaseForm implements BaseFormApi {
|
||||
public <T> T copy(T obj, boolean convert) {
|
||||
try {
|
||||
if (convert) {
|
||||
log.debug("Copy Form [{}] To [{}] With Type Converting.", this, obj);
|
||||
log.debug("Copy form [{}] to [{}] with type converting.", this, obj);
|
||||
org.apache.commons.beanutils.BeanUtils.copyProperties(obj, this);
|
||||
}
|
||||
else {
|
||||
log.debug("Copy Form [{}] To [{}].", this, obj);
|
||||
log.debug("Copy form [{}] to [{}].", this, obj);
|
||||
BeanUtils.copyProperties(this, obj);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(
|
||||
"Copy [" + this + "] Error Caused", e);
|
||||
"Copy [" + this + "] error caused", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ public interface BaseCrudByFormService
|
||||
FormForUpdate extends BaseFormForUpdateApi<PrimaryKey>>
|
||||
extends BaseCrudService<PrimaryKey, Model>,
|
||||
BaseQueryService<PrimaryKey, Model>,
|
||||
BaseDeleteService<PrimaryKey> {
|
||||
BaseDeleteService<PrimaryKey, Model> {
|
||||
|
||||
/**
|
||||
* @param form form
|
||||
|
@ -19,7 +19,7 @@ import me.chyxion.tigon.validation.annotation.NotNullOrBlank;
|
||||
public interface BaseCrudService
|
||||
<PrimaryKey, Model extends BaseModel<PrimaryKey>>
|
||||
extends BaseQueryService<PrimaryKey, Model>,
|
||||
BaseDeleteService<PrimaryKey> {
|
||||
BaseDeleteService<PrimaryKey, Model> {
|
||||
|
||||
/**
|
||||
* @param model form
|
||||
|
@ -1,5 +1,6 @@
|
||||
package me.chyxion.tigon.service;
|
||||
|
||||
import java.util.List;
|
||||
import me.chyxion.tigon.mybatis.Search;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@ -13,17 +14,19 @@ import me.chyxion.tigon.validation.annotation.NotNullOrBlank;
|
||||
* Nov 7, 2016 4:20:42 PM
|
||||
*/
|
||||
@Validated
|
||||
public interface BaseDeleteService<PrimaryKey> {
|
||||
public interface BaseDeleteService<PrimaryKey, Model> {
|
||||
|
||||
/**
|
||||
* delete by search
|
||||
* @param search search
|
||||
* @return models deleted
|
||||
*/
|
||||
int delete(@NotNull Search search);
|
||||
List<Model> delete(@NotNull Search search);
|
||||
|
||||
/**
|
||||
* delete by primaryKey
|
||||
* @param primaryKey primaryKey
|
||||
* @return model deleted
|
||||
*/
|
||||
int delete(@NotNullOrBlank PrimaryKey primaryKey);
|
||||
Model delete(@NotNullOrBlank PrimaryKey primaryKey);
|
||||
}
|
||||
|
@ -30,9 +30,11 @@ public class BaseCrudByFormServiceSupport
|
||||
*/
|
||||
@Override
|
||||
public ViewModel<Model> create(FormForCreate form) {
|
||||
log.debug("Create Form [{}].", form);
|
||||
log.debug("Create form [{}].", form);
|
||||
validate(form);
|
||||
return create(form.copy(modelType));
|
||||
final Model model = form.copy(modelType);
|
||||
beforeCreate(form, model);
|
||||
return create(model);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,13 +42,15 @@ public class BaseCrudByFormServiceSupport
|
||||
*/
|
||||
@Override
|
||||
public ViewModel<Model> update(FormForUpdate form) {
|
||||
log.debug("Update form [{}].", form);
|
||||
validate(form);
|
||||
PrimaryKey primaryKey = form.getId();
|
||||
Model model = find(primaryKey);
|
||||
Assert.state(model != null, "No Model [" + primaryKey + "] Found");
|
||||
return update(form.copy(model));
|
||||
Assert.state(model != null, "No model [" + primaryKey + "] found");
|
||||
form.copy(model);
|
||||
beforeUpdate(form, model);
|
||||
return update(model);
|
||||
}
|
||||
|
||||
|
||||
// --
|
||||
// private methods
|
||||
@ -66,4 +70,12 @@ public class BaseCrudByFormServiceSupport
|
||||
protected void validate(FormForUpdate form) {
|
||||
// For Override
|
||||
}
|
||||
|
||||
protected void beforeCreate(FormForCreate form, Model model) {
|
||||
|
||||
}
|
||||
|
||||
protected void beforeUpdate(FormForUpdate form, Model model) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -29,16 +29,41 @@ public class BaseCrudServiceSupport
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public int delete(Search search) {
|
||||
return mapper.delete(search);
|
||||
public List<Model> delete(final Search search) {
|
||||
log.info("Delete model by search [{}].", search);
|
||||
final List<Model> models = mapper.list(search);
|
||||
if (models != null && !models.isEmpty()) {
|
||||
for (Model model : models) {
|
||||
log.info("Delete model [{}].", model);
|
||||
beforeDelete(model);
|
||||
mapper.delete(model.primaryKeyValue());
|
||||
afterDelete(model);
|
||||
}
|
||||
}
|
||||
else {
|
||||
log.warn("No model found by search [{}] to delete.", search);
|
||||
}
|
||||
return models;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int delete(PrimaryKey primaryKey) {
|
||||
return mapper.delete(primaryKey);
|
||||
@Transactional
|
||||
public Model delete(final PrimaryKey primaryKey) {
|
||||
log.info("Delete model by PK [{}].", primaryKey);
|
||||
final Model model = mapper.find(primaryKey);
|
||||
if (model != null) {
|
||||
log.info("Delete model [{}].", model);
|
||||
beforeDelete(model);
|
||||
mapper.delete(primaryKey);
|
||||
afterDelete(model);
|
||||
}
|
||||
else {
|
||||
log.warn("No model found by PK [{}] to delete.", primaryKey);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -131,4 +156,12 @@ public class BaseCrudServiceSupport
|
||||
protected void afterUpdate(Model model) {
|
||||
// Hook
|
||||
}
|
||||
|
||||
protected void beforeDelete(Model model) {
|
||||
// Hook
|
||||
}
|
||||
|
||||
protected void afterDelete(Model model) {
|
||||
// Hook
|
||||
}
|
||||
}
|
||||
|
@ -35,10 +35,6 @@
|
||||
<artifactId>shiro-core</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>org.springframework.data</groupId>-->
|
||||
<!--<artifactId>spring-data-redis</artifactId>-->
|
||||
<!--</dependency>-->
|
||||
<!-- Test Dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
Loading…
x
Reference in New Issue
Block a user