Commit f80bd7c1 authored by 薛涛 Toby Xue's avatar 薛涛 Toby Xue 🆒

#1971 actionListener

parent c3f628bb
package logwire.web.bo;
import logwire.web.service.query.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BoService {
@Autowired
QueryService queryService;
}
package logwire.web.bo.bean;
import logwire.core.bo.handler.OperationHandler;
import logwire.core.bo.object.BizObject;
public class TypeOperationHandlerBean {
public TypeOperationHandlerBean(Class<? extends BizObject> bizObject, Class<? extends OperationHandler> handler) {
this.bizObject = bizObject;
this.handler = handler;
}
private Class<? extends BizObject> bizObject;
private Class<? extends OperationHandler> handler;
public Class getBizObject() {
return bizObject;
}
public void setBizObject(Class<? extends BizObject> bizObject) {
this.bizObject = bizObject;
}
public Class getHandler() {
return handler;
}
public void setHandler(Class<? extends OperationHandler> handler) {
this.handler = handler;
}
}
package logwire.web.bo.container;
public abstract class BoBeanContainer<T> {
public abstract T getBean(String id);
}
package logwire.web.bo.container;
import logwire.core.resource.*;
import logwire.web.bo.loader.BoRelationBeanDefinitionLoader;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class BoRelationBeanContainerBuilder implements JavaBeanContainerBuilder {
@Override
public JavaBeanContainer build(ApplicationContext applicationContext) {
return newContainer(applicationContext, BoRelationBeanDefinitionLoader.class);
}
@Override
public String getContainerName() {
return "BO_RELATION";
}
private JavaBeanContainer newContainer(ApplicationContext ctx, Class... classes) {
BeanLoader[] loaders = new BeanLoader[classes.length];
for (int i = 0; i < loaders.length; i++) {
loaders[i] = (BeanLoader) Util.getExactBean(ctx, classes[i]);
}
JavaBeanContainer container = new JavaBeanContainer(loaders);
return container;
}
}
package logwire.web.bo.container;
import logwire.web.bo.bean.TypeOperationHandlerBean;
import java.util.ArrayList;
import java.util.List;
public class OperationHandlerContainer {
private List<TypeOperationHandlerBean> typeOperationHandlerBeanList = new ArrayList<>();
public List<TypeOperationHandlerBean> getTypeOperationHandlerBeanList() {
return typeOperationHandlerBeanList;
}
public void setTypeOperationHandlerBeanList(List<TypeOperationHandlerBean> typeOperationHandlerBeanList) {
this.typeOperationHandlerBeanList = typeOperationHandlerBeanList;
}
public void addTypeOperationHandlerBean(TypeOperationHandlerBean typeOperationHandlerBean) {
this.typeOperationHandlerBeanList.add(typeOperationHandlerBean);
}
}
package logwire.web.bo.field.parse;
import logwire.core.meta.model.fields.BigIntegerField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class BigIntegerFieldParser extends FieldParser<BigIntegerField> {
@Override
protected BigIntegerField newField(Field field, Annotation[] annotations) {
return new BigIntegerField(field.getName());
}
@Override
protected void setField(BigIntegerField newField, Annotation[] annotations) {
}
private BigIntegerFieldParser() {
}
public static BigIntegerFieldParser getInstance() {
return BigIntegerFieldParser.SingletonHolder.instance;
}
private static class SingletonHolder {
private static BigIntegerFieldParser instance = new BigIntegerFieldParser();
}
}
package logwire.web.bo.field.parse;
import logwire.core.meta.model.fields.StringField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class BizArrayFieldParser extends FieldParser<StringField> {
@Override
protected StringField newField(Field field, Annotation[] annotations) {
return new StringField(field.getName(), 100);
}
private BizArrayFieldParser() {
}
public static BizArrayFieldParser getInstance() {
return BizArrayFieldParser.SingletonHolder.instance;
}
private static class SingletonHolder {
private static BizArrayFieldParser instance = new BizArrayFieldParser();
}
}
package logwire.web.bo.field.parse;
import logwire.core.meta.model.fields.BooleanField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class BooleanFieldParser extends FieldParser<BooleanField> {
@Override
protected BooleanField newField(Field field, Annotation[] annotations) {
return new BooleanField(field.getName());
}
private BooleanFieldParser() {
}
public static BooleanFieldParser getInstance() {
return BooleanFieldParser.SingletonHolder.instance;
}
private static class SingletonHolder {
private static BooleanFieldParser instance = new BooleanFieldParser();
}
}
package logwire.web.bo.field.parse;
import logwire.core.meta.model.fields.DateTimeField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class DateTimeFieldParser extends FieldParser<DateTimeField> {
@Override
protected DateTimeField newField(Field field, Annotation[] annotations) {
return new DateTimeField(field.getName());
}
private DateTimeFieldParser() {
}
public static DateTimeFieldParser getInstance() {
return DateTimeFieldParser.SingletonHolder.instance;
}
private static class SingletonHolder {
private static DateTimeFieldParser instance = new DateTimeFieldParser();
}
}
package logwire.web.bo.field.parse;
import logwire.core.meta.model.fields.DecimalField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class DecimalFieldParser extends FieldParser<DecimalField> {
@Override
protected DecimalField newField(Field field, Annotation[] annotations) {
return new DecimalField(field.getName(), 18, 6);
}
private DecimalFieldParser() {
}
public static DecimalFieldParser getInstance() {
return DecimalFieldParser.SingletonHolder.instance;
}
private static class SingletonHolder {
private static DecimalFieldParser instance = new DecimalFieldParser();
}
}
package logwire.web.bo.field.parse;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public abstract class FieldParser<T extends logwire.core.meta.model.fields.Field> {
protected abstract T newField(Field field, Annotation[] annotations);
public T parse(Field field, Annotation[] annotations) {
T newField = newField(field, annotations);
setField(newField, annotations);
return newField;
}
protected void setField(T newField, Annotation[] annotations) {
}
}
package logwire.web.bo.field.parse;
import logwire.core.bo.field.BizArray;
import java.lang.annotation.Annotation;
import java.time.OffsetDateTime;
public abstract class FieldParserUtil {
private FieldParserUtil() {
}
/**
* 获取解析注解的 parser
*
* @param field
* @param annotations
* @return
*/
public static FieldParser getParser(java.lang.reflect.Field field, Annotation[] annotations) {
return getParserByFieldType(field);
}
private static FieldParser getParserByFieldType(java.lang.reflect.Field field) {
// 此时一定是存在 Field 类型的 annotation
// 根据字段类型来推断 Parser
Class clazz = field.getType();
if (clazz.equals(String.class)) {
return StringFieldParser.getInstance();
} else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
return IntegerFieldParser.getInstance();
} else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
return SmallIntegerFieldParser.getInstance();
} else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
return BigIntegerFieldParser.getInstance();
} else if (clazz.equals(OffsetDateTime.class) || clazz.equals(java.util.Date.class)) {
return DateTimeFieldParser.getInstance();
} else if (clazz.equals(Boolean.class) || clazz.equals(boolean.class)) {
return BooleanFieldParser.getInstance();
} else if (clazz.equals(Double.class) || clazz.equals(double.class) || clazz.equals(float.class)) {
return DecimalFieldParser.getInstance();
} else if (clazz.equals(BizArray.class)) {
return BizArrayFieldParser.getInstance();
}
return null;
}
}
package logwire.web.bo.field.parse;
import logwire.core.meta.model.fields.IntegerField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class IntegerFieldParser extends FieldParser<IntegerField> {
@Override
protected IntegerField newField(Field field, Annotation[] annotations) {
return new IntegerField(field.getName());
}
private IntegerFieldParser() {
}
public static IntegerFieldParser getInstance() {
return IntegerFieldParser.SingletonHolder.instance;
}
private static class SingletonHolder {
private static IntegerFieldParser instance = new IntegerFieldParser();
}
}
package logwire.web.bo.field.parse;
import logwire.core.meta.model.fields.SmallIntegerField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class SmallIntegerFieldParser extends FieldParser<SmallIntegerField> {
@Override
protected SmallIntegerField newField(Field field, Annotation[] annotations) {
return new SmallIntegerField(field.getName());
}
private SmallIntegerFieldParser() {
}
public static SmallIntegerFieldParser getInstance() {
return SmallIntegerFieldParser.SingletonHolder.instance;
}
private static class SingletonHolder {
private static SmallIntegerFieldParser instance = new SmallIntegerFieldParser();
}
}
package logwire.web.bo.field.parse;
import logwire.core.meta.model.fields.StringField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class StringFieldParser extends FieldParser<StringField> {
@Override
protected StringField newField(Field field, Annotation[] annotations) {
return new StringField(field.getName(), 50);
}
private StringFieldParser() {
}
public static StringFieldParser getInstance() {
return StringFieldParser.SingletonHolder.instance;
}
private static class SingletonHolder {
private static StringFieldParser instance = new StringFieldParser();
}
}
package logwire.web.bo.handler;
import logwire.core.bo.handler.TypeOperationHandler;
public class CreateOperationHandler implements TypeOperationHandler {
@Override
public Object execute(Class aClass, Object... args) {
return null;
}
@Override
public String getOperation() {
return "create";
}
@Override
public boolean isEnabled() {
return true;
}
}
package logwire.web.bo.listener;
import logwire.core.meta.query.IQuery;
import logwire.core.meta.query.Query;
import logwire.web.action.listener.ActionListener;
import logwire.web.bo.query.BizModelQuery;
import logwire.web.bo.session.Session;
import logwire.web.bo.session.SessionHolder;
import logwire.web.service.ActionContext;
import logwire.web.service.query.QueryService;
import logwire.web.tenant.TenantProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BoActionListener implements ActionListener {
@Autowired
TenantProjectService projectService;
@Autowired
QueryService queryService;
@Override
public void beforeAction(ActionContext actionContext, String queryName) {
IQuery iQuery = projectService.getCurrentProject().findQuery(queryName);
if (iQuery == null) {
return;
}
if (!(iQuery instanceof Query)) {
return;
}
Query query = (Query) iQuery;
if (query instanceof BizModelQuery) {
SessionHolder.openSession(actionContext);
}
}
@Override
public void afterAction(ActionContext actionContext, String queryName) {
Session session = SessionHolder.getSession();
if (session != null) {
session.commit(queryService);
}
}
}
......@@ -10,11 +10,14 @@ import logwire.core.resource.BeanLoader;
import logwire.core.resource.loader.JavaModelBeanLoader;
import logwire.core.tenant.TenantClassLoader;
import logwire.core.tenant.TenantProject;
import logwire.web.bo.field.parse.FieldParser;
import logwire.web.bo.field.parse.FieldParserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.io.DefaultResourceLoader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
......@@ -26,6 +29,17 @@ public class BoModelBeanLoader extends BeanLoader<IQuery, TenantProject> impleme
@Autowired
LoaderUtil loaderUtil;
/**
* load BoModel 的逻辑:
* 1、先遍历出所有BizObject的子类
* 2、将直接继承BizObject的类初始化
* 3、剩余的类中,判断是否独立的model(包含BizModel注解),若是,生成Model;若不是,字段添加至父类的Model中
*
* @param project 当前租户
* @param input 待加载的资源
* @param consumer 处理加载结果
* @throws Exception
*/
@Override
public void load(TenantProject project, TenantProject input, Consumer<IQuery> consumer) throws Exception {
TenantClassLoader classLoader = project.getTenantBeanContext().getTenantClassLoader();
......@@ -44,33 +58,22 @@ public class BoModelBeanLoader extends BeanLoader<IQuery, TenantProject> impleme
}
}).collect(Collectors.toList());
for (BeanDefinition beanDefinition : beans) {
for (BeanDefinition beanDefinition : bizModelList) {
Class<?> clazz = classLoader.loadClass(beanDefinition.getBeanClassName());
//判断是否独立model
BizModel bizModelAnnotation = clazz.getAnnotation(BizModel.class);
if (bizModelAnnotation != null) {
//独立model
Model model = new Model();
model.setName(clazz.getSimpleName());
model.setName(clazz.getSimpleName());
model.setVerboseName(bizModelAnnotation.label());
model.setIncludeAuditFields(true);
model.setIncludeVersionField(true);
model.setIncludeDomainField(true);
initField(clazz, model);
} else {
//判断父类是否包含 @BizModel ,若包含则
}
/* this.initModel(clazz, model);
this.initFields(clazz, model);
this.initIndex(clazz, model);
this.initSign(model);
consumer.accept(model);*/
//独立model
Model model = new Model();
model.setName(clazz.getSimpleName());
model.setVerboseName(clazz.getAnnotation(BizModel.class).label());
model.setIncludeAuditFields(true);
model.setIncludeVersionField(true);
model.setIncludeDomainField(true);
initField(clazz, model);
/*
//todo
this.initIndex(clazz, model);
this.initSign(model);*/
consumer.accept(model);
}
......@@ -78,11 +81,18 @@ public class BoModelBeanLoader extends BeanLoader<IQuery, TenantProject> impleme
}
private void initField(Class<?> clazz, Model model) {
//获取所有带有Column注解的字段
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
Column column = f.getAnnotation(Column.class);
if (column != null) {
/* model.*/
Annotation[] annotations = f.getDeclaredAnnotations();
FieldParser parser = FieldParserUtil.getParser(f, annotations);
if (parser == null) {
continue;
}
logwire.core.meta.model.fields.Field field = parser.parse(f, annotations);
model.addField(field);
}
}
}
......
package logwire.web.bo.query;
public interface BizModelQuery {
}
package logwire.web.bo.session;
public class BizObjectStatus {
}
This diff is collapsed.
package logwire.web.bo.session;
import logwire.core.exceptions.ApplicationException;
import logwire.web.service.ActionContext;
import org.springframework.core.NamedThreadLocal;
/**
* SessionHolder
*
*/
public abstract class SessionHolder {
private static ThreadLocal<Session> sessionThreadLocal = new NamedThreadLocal<>("Biz context");
public static Session getSessionOrFail() {
Session session = sessionThreadLocal.get();
if (session != null) {
return session;
}
throw new ApplicationException("No session exists, please call method SessionHolder.openSession() to open a new session!");
}
public static Session getSession() {
return sessionThreadLocal.get();
}
public static Session openSession(ActionContext context) {
Session session = sessionThreadLocal.get();
if (session == null) {
session = new Session(context);
sessionThreadLocal.set(session);
}
return session;
}
public static void clear() {
sessionThreadLocal.remove();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment