refact the plugin

This commit is contained in:
fanfuxiaoran 2014-03-20 09:39:01 +08:00
parent ad1e957642
commit 0f56415e15
8 changed files with 186 additions and 162 deletions

View File

@ -2,17 +2,33 @@ package org.bench4q.master.domain.entity.plugin;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "methodParamProperty")
public class MethodParamProperty {
private int id;
private String key;
private String value;
private MethodParam methodParam;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "key")
public String getKey() {
return key;
@ -30,6 +46,7 @@ public class MethodParamProperty {
public void setValue(String value) {
this.value = value;
}
@ManyToOne
@JoinColumn(name = "methodParamId")
public MethodParam getMethodParam() {
@ -39,7 +56,5 @@ public class MethodParamProperty {
public void setMethodParam(MethodParam methodParam) {
this.methodParam = methodParam;
}
}

View File

@ -1,24 +1,21 @@
package org.bench4q.master.domain.entity.plugin;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.bench4q.share.models.master.plugin.ParamTypeEnum;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "paramType", discriminatorType = DiscriminatorType.STRING)
@Table(name = "paramType")
public class ParamType {
private int id;
private ParamTypeEnum type;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ -32,7 +29,13 @@ public class ParamType {
}
@Enumerated(EnumType.STRING)
ParamTypeEnum type;
@Column(columnDefinition = "ENUM('Field', 'MultiField', 'Table','CheckBox')")
public ParamTypeEnum getType() {
return type;
}
public void setType(ParamTypeEnum type) {
this.type = type;
}
}

View File

@ -2,28 +2,34 @@ package org.bench4q.master.domain.factory;
import java.util.HashSet;
import org.bench4q.master.domain.entity.plugin.CheckBox;
import org.bench4q.master.domain.entity.plugin.Field;
import org.bench4q.master.domain.entity.plugin.Method;
import org.bench4q.master.domain.entity.plugin.MethodParam;
import org.bench4q.master.domain.entity.plugin.MultiField;
import org.bench4q.master.domain.entity.plugin.MethodParamProperty;
import org.bench4q.master.domain.entity.plugin.ParamType;
import org.bench4q.master.domain.entity.plugin.Plugin;
import org.bench4q.master.domain.entity.plugin.Table;
import org.bench4q.share.models.master.plugin.CheckBoxModel;
import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.MultiFieldModel;
import org.bench4q.share.models.master.plugin.ParamTypeModel;
import org.bench4q.share.models.master.plugin.ParamTypeString;
import org.bench4q.share.models.master.plugin.MethodParamPropertyModel;
import org.bench4q.share.models.master.plugin.ParamTypeEnum;
import org.bench4q.share.models.master.plugin.PluginModel;
import org.bench4q.share.models.master.plugin.FieldModel;
import org.bench4q.share.models.master.plugin.TableModel;
import org.python.antlr.PythonParser.return_stmt_return;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class PluginFactory {
public static Plugin createPluginEntity(PluginModel pluginModel) {
private PluginRepository pluginRepository;
public PluginRepository getPluginRepository() {
return pluginRepository;
}
@Autowired
public void setPluginRepository(PluginRepository pluginRepository) {
this.pluginRepository = pluginRepository;
}
public Plugin createPluginEntity(PluginModel pluginModel) {
Plugin plugin = new Plugin();
plugin.setPluginMethods(new HashSet<Method>());
if (pluginModel.getMethods() != null) {
@ -38,8 +44,7 @@ public class PluginFactory {
}
private static Method createMethodEntity(MethodModel methodModel,
Plugin plugin) {
private Method createMethodEntity(MethodModel methodModel, Plugin plugin) {
Method method = new Method();
method.setMethodParams(new HashSet<MethodParam>());
method.setName(methodModel.getName());
@ -55,27 +60,39 @@ public class PluginFactory {
return method;
}
private static MethodParam createMethodParamEntity(
private MethodParam createMethodParamEntity(
MethodParamModel methodParamModel, Method method) {
MethodParam methodParam = new MethodParam();
methodParam.setLable(methodParamModel.getLable());
methodParam.setName(methodParamModel.getName());
methodParam.setMethod(method);
methodParam.setParamType(createParamTypeEntity(
methodParamModel.getParamTypeModel(), methodParam));
methodParam.setParamType(createPraParamTypeWithOutId(methodParamModel
.getParamType()));
if (methodParamModel.getMethodParamProperties() != null) {
methodParam
.setMethodParamProperties(new HashSet<MethodParamProperty>());
for (MethodParamPropertyModel methodParamPropertyModel : methodParamModel
.getMethodParamProperties()) {
methodParam.getMethodParamProperties().add(
createMethodParamPropertyWithOutId(
methodParamPropertyModel, methodParam));
}
}
return methodParam;
}
private static ParamType createParamTypeEntity(
ParamTypeModel paramTypeModel, MethodParam methodParam) {
ParamType paramType = new ParamType();
paramType.setFieldSeperator(paramTypeModel.getFieldSeperator());
paramType.setValueSeperator(paramTypeModel.getFieldSeperator());
paramType.setType(paramTypeModel.getType());
paramType.setCols(paramTypeModel.getCols());
paramType.setRows(paramType.getRows());
paramType.setSize(paramType.getSize());
return paramType;
private ParamType createPraParamTypeWithOutId(ParamTypeEnum paramTypeEnum) {
return this.getPluginRepository().getParamTypeByType(paramTypeEnum);
}
private MethodParamProperty createMethodParamPropertyWithOutId(
MethodParamPropertyModel methodParamPropertyModel,
MethodParam methodParam) {
MethodParamProperty methodParamProperty = new MethodParamProperty();
methodParamProperty.setKey(methodParamPropertyModel.getKey());
methodParamProperty.setValue(methodParamPropertyModel.getValue());
methodParamProperty.setMethodParam(methodParam);
return methodParamProperty;
}
public static MethodModel extractMethodModel(Method method) {
@ -96,68 +113,25 @@ public class PluginFactory {
MethodParamModel methodParamModel = new MethodParamModel();
methodParamModel.setLable(methodParam.getLable());
methodParamModel.setName(methodParam.getName());
methodParamModel.setParamTypeModel(extractParamTypeModel(methodParam
.getParamType()));
methodParamModel.setParamType(methodParam.getParamType().getType());
if (methodParam.getMethodParamProperties() != null) {
methodParamModel
.setMethodParamProperties(new HashSet<MethodParamPropertyModel>());
for (MethodParamProperty methodParamProperty : methodParam
.getMethodParamProperties()) {
methodParamModel.getMethodParamProperties().add(
extractMethodParamPropertyModel(methodParamProperty));
}
}
return methodParamModel;
}
private static ParamTypeModel extractParamTypeModel(ParamType paramType) {
ParamTypeModel paramTypeModel=new ParamTypeModel();
paramTypeModel.setCols(paramType.getCols());
paramTypeModel.setFieldSeperator(paramType.getFieldSeperator());
paramTypeModel.setRows(paramType.getRows());
paramTypeModel.setSize(paramType.getSize());
paramTypeModel.setValueSeperator(paramType.getValueSeperator());
paramTypeModel.setType(paramType.getType());
return paramTypeModel;
/*
switch (paramType.getType()) {
case ParamTypeString.FIELD:
return extractFieldModel(paramType);
case ParamTypeString.MULTIFIELD:
return extractMultFieldiModel(paramType);
case ParamTypeString.TABLE:
return extractTableMode(paramType);
case ParamTypeString.CHECKBOX:
return extractCheckBoxModel(paramType);
default:
return null;
}*/
}
private static ParamTypeModel extractFieldModel(ParamType paramType) {
FieldModel fieldModel = new FieldModel();
fieldModel.setSize(paramType.getSize());
fieldModel.setType(paramType.getType());
return fieldModel;
}
private static ParamTypeModel extractMultFieldiModel(ParamType paramType) {
MultiFieldModel multiFieldModel = new MultiFieldModel();
multiFieldModel.setFieldSeperator(paramType.getFieldSeperator());
multiFieldModel.setSize(paramType.getSize());
multiFieldModel.setType(paramType.getType());
multiFieldModel.setValueSeperator(paramType.getValueSeperator());
return multiFieldModel;
}
private static TableModel extractTableMode(ParamType paramType) {
TableModel tableModel = new TableModel();
tableModel.setFieldSeperator(paramType.getFieldSeperator());
tableModel.setType(paramType.getType());
tableModel.setValueSeperator(paramType.getValueSeperator());
tableModel.setCols(paramType.getCols());
return tableModel;
}
private static CheckBoxModel extractCheckBoxModel(ParamType paramType) {
CheckBoxModel checkBoxModel = new CheckBoxModel();
checkBoxModel.setFieldSeperator(paramType.getFieldSeperator());
checkBoxModel.setType(paramType.getType());
checkBoxModel.setValueSeperator(paramType.getValueSeperator());
checkBoxModel.setRows(paramType.getRows());
return checkBoxModel;
private static MethodParamPropertyModel extractMethodParamPropertyModel(
MethodParamProperty methodParamProperty) {
MethodParamPropertyModel methodParamPropertyModel = new MethodParamPropertyModel();
methodParamPropertyModel.setKey(methodParamProperty.getKey());
methodParamPropertyModel.setValue(methodParamProperty.getValue());
return methodParamPropertyModel;
}
}

View File

@ -2,9 +2,11 @@ package org.bench4q.master.domain.repository;
import java.util.List;
import org.bench4q.master.domain.entity.plugin.ParamType;
import org.bench4q.master.domain.entity.plugin.Plugin;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.exception.ExceptionUtils.EntityUniqueAlReadyExistException;
import org.bench4q.share.models.master.plugin.ParamTypeEnum;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
@ -13,6 +15,16 @@ import org.springframework.stereotype.Component;
@Component
public class PluginRepository extends AbstractRepositoty {
public ParamType getParamTypeByType(ParamTypeEnum paramTypeEnum) {
ParamType paramType = null;
Session session = this.getSessionHelper().openSession();
paramType = (ParamType) session.createCriteria(Plugin.class)
.add(Restrictions.eq("type", paramTypeEnum)).uniqueResult();
releaseSession(session);
return paramType;
}
public boolean attatch(Plugin plugin) throws Bench4QException {
Session session = this.getSessionHelper().openSession();
@ -98,6 +110,7 @@ public class PluginRepository extends AbstractRepositoty {
releaseSession(session);
return pluginAlreadyExist != null;
}
public Plugin getPlugin(String pluginName) {
Plugin plugin = null;
Session session = this.getSessionHelper().openSession();

View File

@ -22,6 +22,7 @@ public class PluginService {
private Set<Plugin> plugins;
private List<String> pluginNameList;
private PluginRepository pluginRepository;
private PluginFactory pluginFactory;
private Logger logger = Logger.getLogger(PluginService.class);
public PluginRepository getPluginRepository() {
@ -33,6 +34,15 @@ public class PluginService {
this.pluginRepository = pluginRepository;
}
public PluginFactory getPluginFactory() {
return pluginFactory;
}
@Autowired
public void setPluginFactory(PluginFactory pluginFactory) {
this.pluginFactory = pluginFactory;
}
private Set<Plugin> getPlugins() throws Bench4QException {
List<Plugin> loadPluginList = this.getPluginRepository().loadPlugins();
if (loadPluginList != null) {
@ -47,7 +57,7 @@ public class PluginService {
public boolean addPlugin(PluginModel pluginModel) throws Bench4QException {
return this.getPluginRepository().attatch(
PluginFactory.createPluginEntity(pluginModel));
this.getPluginFactory().createPluginEntity(pluginModel));
}
public boolean deletePlugin(String pluginName) throws Bench4QException {

View File

@ -33,9 +33,6 @@
<mapping class="org.bench4q.master.domain.entity.plugin.Method" />
<mapping class="org.bench4q.master.domain.entity.plugin.MethodParam" />
<mapping class="org.bench4q.master.domain.entity.plugin.ParamType" />
<mapping class="org.bench4q.master.domain.entity.plugin.Field" />
<mapping class="org.bench4q.master.domain.entity.plugin.MultiField" />
<mapping class="org.bench4q.master.domain.entity.plugin.CheckBox" />
<mapping class="org.bench4q.master.domain.entity.plugin.Table" />
<mapping class="org.bench4q.master.domain.entity.plugin.MethodParamProperty" />
</session-factory>
</hibernate-configuration>

View File

@ -1,59 +1,61 @@
package TestHelper;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.bench4q.share.models.master.plugin.FieldModel;
import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.MultiFieldModel;
import org.bench4q.share.models.master.plugin.ParamTypeModel;
import org.bench4q.share.models.master.plugin.ParamTypeString;
import org.bench4q.share.models.master.plugin.PluginModel;
public class Test_PlunginHelper {
public static PluginModel createOnePlugin() {
PluginModel plugin = new PluginModel();
MethodModel method = new MethodModel();
MethodParamModel methodParamModel = new MethodParamModel();
methodParamModel.setLable("input url");
methodParamModel.setName("url");
methodParamModel.setParamTypeModel(generateUrlParamTypeModel());
MethodParamModel methodParamsParamModel = new MethodParamModel();
methodParamsParamModel.setName("queryparams");
methodParamsParamModel.setLable("key=value");
methodParamsParamModel.setParamTypeModel(generateParamsParamTypeModel());
Set<MethodParamModel> methodParams = new HashSet<MethodParamModel>();
methodParams.add(methodParamModel);
methodParams.add(methodParamsParamModel);
method.setName("get");
method.setMethodParams(methodParams);
Set<MethodModel> methods = new HashSet<MethodModel>();
methods.add(method);
plugin.setMethods(methods);
plugin.setName("http" + UUID.randomUUID());
return plugin;
}
private static ParamTypeModel generateUrlParamTypeModel() {
FieldModel paramPropertyUrl = new FieldModel();
paramPropertyUrl.setType(ParamTypeString.FIELD);
paramPropertyUrl.setSize(20);
return paramPropertyUrl;
}
private static ParamTypeModel generateParamsParamTypeModel() {
MultiFieldModel multiFieldModel = new MultiFieldModel();
multiFieldModel.setSize(20);
multiFieldModel.setType(ParamTypeString.MULTIFIELD);
return multiFieldModel;
}
}
package TestHelper;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.ParamTypeEnum;
import org.bench4q.share.models.master.plugin.PluginModel;
import org.bench4q.share.models.master.plugin.MethodParamPropertyModel;
;
public class Test_PlunginHelper {
public static PluginModel createOnePlugin() {
PluginModel plugin = new PluginModel();
MethodModel method = new MethodModel();
MethodParamModel methodParamModelUrl = new MethodParamModel();
methodParamModelUrl.setLable("input url");
methodParamModelUrl.setName("url");
methodParamModelUrl.setMethodParamProperties(generatePropertyForField());
methodParamModelUrl.setParamType(ParamTypeEnum.Field);
MethodParamModel methodParamsParamModel = new MethodParamModel();
methodParamsParamModel.setName("queryparams");
methodParamsParamModel.setLable("key=value");
methodParamsParamModel.setMethodParamProperties(generatePropertyForMulti());
methodParamsParamModel.setParamType(ParamTypeEnum.MultiField);
Set<MethodParamModel> methodParams = new HashSet<MethodParamModel>();
methodParams.add(methodParamModelUrl);
methodParams.add(methodParamsParamModel);
method.setName("get");
method.setMethodParams(methodParams);
Set<MethodModel> methods = new HashSet<MethodModel>();
methods.add(method);
plugin.setMethods(methods);
plugin.setName("http" + UUID.randomUUID());
return plugin;
}
private static Set<MethodParamPropertyModel> generatePropertyForField() {
Set<MethodParamPropertyModel> metList = new HashSet<MethodParamPropertyModel>();
MethodParamPropertyModel methodParamPropertyModel = new MethodParamPropertyModel();
methodParamPropertyModel.setKey("size");
methodParamPropertyModel.setValue("20");
metList.add(methodParamPropertyModel);
return metList;
}
private static Set<MethodParamPropertyModel> generatePropertyForMulti() {
Set<MethodParamPropertyModel> metList = new HashSet<MethodParamPropertyModel>();
MethodParamPropertyModel methodParamPropertyModel = new MethodParamPropertyModel();
methodParamPropertyModel.setKey("size");
methodParamPropertyModel.setValue("20");
metList.add(methodParamPropertyModel);
return metList;
}
}

View File

@ -22,6 +22,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class Test_PluginRepository {
private PluginRepository pluginRepository;
private String pluginName;
private PluginFactory pluginFactory;
public PluginRepository getPluginRepository() {
return pluginRepository;
@ -32,6 +33,15 @@ public class Test_PluginRepository {
this.pluginRepository = pluginRepository;
}
public PluginFactory getPluginFactory() {
return pluginFactory;
}
@Autowired
public void setPluginFactory(PluginFactory pluginFactory) {
this.pluginFactory = pluginFactory;
}
@Before
public void setUp() throws Bench4QException {
pluginName = addPlugin();
@ -71,13 +81,13 @@ public class Test_PluginRepository {
@After
public void clear() throws Bench4QException {
/*this.getPluginRepository().detach(pluginName);*/
/* this.getPluginRepository().detach(pluginName); */
}
private String addPlugin() throws Bench4QException {
PluginModel pluginModel = TestPlunginHelper.createOnePlugin();
this.getPluginRepository().attatch(
PluginFactory.createPluginEntity(pluginModel));
this.getPluginFactory().createPluginEntity(pluginModel));
return pluginModel.getName();
}