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.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
@Entity @Entity
@Table(name = "methodParamProperty") @Table(name = "methodParamProperty")
public class MethodParamProperty { public class MethodParamProperty {
private int id;
private String key; private String key;
private String value; private String value;
private MethodParam methodParam; 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") @Column(name = "key")
public String getKey() { public String getKey() {
return key; return key;
@ -30,6 +46,7 @@ public class MethodParamProperty {
public void setValue(String value) { public void setValue(String value) {
this.value = value; this.value = value;
} }
@ManyToOne @ManyToOne
@JoinColumn(name = "methodParamId") @JoinColumn(name = "methodParamId")
public MethodParam getMethodParam() { public MethodParam getMethodParam() {
@ -39,7 +56,5 @@ public class MethodParamProperty {
public void setMethodParam(MethodParam methodParam) { public void setMethodParam(MethodParam methodParam) {
this.methodParam = methodParam; this.methodParam = methodParam;
} }
} }

View File

@ -1,24 +1,21 @@
package org.bench4q.master.domain.entity.plugin; package org.bench4q.master.domain.entity.plugin;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType; import javax.persistence.EnumType;
import javax.persistence.Enumerated; import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Inheritance; import javax.persistence.Table;
import javax.persistence.InheritanceType;
import org.bench4q.share.models.master.plugin.ParamTypeEnum; import org.bench4q.share.models.master.plugin.ParamTypeEnum;
@Entity @Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Table(name = "paramType")
@DiscriminatorColumn(name = "paramType", discriminatorType = DiscriminatorType.STRING)
public class ParamType { public class ParamType {
private int id; private int id;
private ParamTypeEnum type;
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@ -32,7 +29,13 @@ public class ParamType {
} }
@Enumerated(EnumType.STRING) @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 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.Method;
import org.bench4q.master.domain.entity.plugin.MethodParam; 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.ParamType;
import org.bench4q.master.domain.entity.plugin.Plugin; import org.bench4q.master.domain.entity.plugin.Plugin;
import org.bench4q.master.domain.entity.plugin.Table; import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.share.models.master.plugin.CheckBoxModel;
import org.bench4q.share.models.master.plugin.MethodModel; import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.MethodParamModel; import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.MultiFieldModel; import org.bench4q.share.models.master.plugin.MethodParamPropertyModel;
import org.bench4q.share.models.master.plugin.ParamTypeModel; import org.bench4q.share.models.master.plugin.ParamTypeEnum;
import org.bench4q.share.models.master.plugin.ParamTypeString;
import org.bench4q.share.models.master.plugin.PluginModel; import org.bench4q.share.models.master.plugin.PluginModel;
import org.bench4q.share.models.master.plugin.FieldModel; import org.springframework.beans.factory.annotation.Autowired;
import org.bench4q.share.models.master.plugin.TableModel; import org.springframework.stereotype.Component;
import org.python.antlr.PythonParser.return_stmt_return; @Component
public class PluginFactory { 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 plugin = new Plugin();
plugin.setPluginMethods(new HashSet<Method>()); plugin.setPluginMethods(new HashSet<Method>());
if (pluginModel.getMethods() != null) { if (pluginModel.getMethods() != null) {
@ -38,8 +44,7 @@ public class PluginFactory {
} }
private static Method createMethodEntity(MethodModel methodModel, private Method createMethodEntity(MethodModel methodModel, Plugin plugin) {
Plugin plugin) {
Method method = new Method(); Method method = new Method();
method.setMethodParams(new HashSet<MethodParam>()); method.setMethodParams(new HashSet<MethodParam>());
method.setName(methodModel.getName()); method.setName(methodModel.getName());
@ -55,27 +60,39 @@ public class PluginFactory {
return method; return method;
} }
private static MethodParam createMethodParamEntity( private MethodParam createMethodParamEntity(
MethodParamModel methodParamModel, Method method) { MethodParamModel methodParamModel, Method method) {
MethodParam methodParam = new MethodParam(); MethodParam methodParam = new MethodParam();
methodParam.setLable(methodParamModel.getLable()); methodParam.setLable(methodParamModel.getLable());
methodParam.setName(methodParamModel.getName()); methodParam.setName(methodParamModel.getName());
methodParam.setMethod(method); methodParam.setMethod(method);
methodParam.setParamType(createParamTypeEntity( methodParam.setParamType(createPraParamTypeWithOutId(methodParamModel
methodParamModel.getParamTypeModel(), methodParam)); .getParamType()));
if (methodParamModel.getMethodParamProperties() != null) {
methodParam
.setMethodParamProperties(new HashSet<MethodParamProperty>());
for (MethodParamPropertyModel methodParamPropertyModel : methodParamModel
.getMethodParamProperties()) {
methodParam.getMethodParamProperties().add(
createMethodParamPropertyWithOutId(
methodParamPropertyModel, methodParam));
}
}
return methodParam; return methodParam;
} }
private static ParamType createParamTypeEntity( private ParamType createPraParamTypeWithOutId(ParamTypeEnum paramTypeEnum) {
ParamTypeModel paramTypeModel, MethodParam methodParam) { return this.getPluginRepository().getParamTypeByType(paramTypeEnum);
ParamType paramType = new ParamType(); }
paramType.setFieldSeperator(paramTypeModel.getFieldSeperator());
paramType.setValueSeperator(paramTypeModel.getFieldSeperator()); private MethodParamProperty createMethodParamPropertyWithOutId(
paramType.setType(paramTypeModel.getType()); MethodParamPropertyModel methodParamPropertyModel,
paramType.setCols(paramTypeModel.getCols()); MethodParam methodParam) {
paramType.setRows(paramType.getRows()); MethodParamProperty methodParamProperty = new MethodParamProperty();
paramType.setSize(paramType.getSize()); methodParamProperty.setKey(methodParamPropertyModel.getKey());
return paramType; methodParamProperty.setValue(methodParamPropertyModel.getValue());
methodParamProperty.setMethodParam(methodParam);
return methodParamProperty;
} }
public static MethodModel extractMethodModel(Method method) { public static MethodModel extractMethodModel(Method method) {
@ -96,68 +113,25 @@ public class PluginFactory {
MethodParamModel methodParamModel = new MethodParamModel(); MethodParamModel methodParamModel = new MethodParamModel();
methodParamModel.setLable(methodParam.getLable()); methodParamModel.setLable(methodParam.getLable());
methodParamModel.setName(methodParam.getName()); methodParamModel.setName(methodParam.getName());
methodParamModel.setParamTypeModel(extractParamTypeModel(methodParam methodParamModel.setParamType(methodParam.getParamType().getType());
.getParamType())); if (methodParam.getMethodParamProperties() != null) {
methodParamModel
.setMethodParamProperties(new HashSet<MethodParamPropertyModel>());
for (MethodParamProperty methodParamProperty : methodParam
.getMethodParamProperties()) {
methodParamModel.getMethodParamProperties().add(
extractMethodParamPropertyModel(methodParamProperty));
}
}
return methodParamModel; return methodParamModel;
} }
private static ParamTypeModel extractParamTypeModel(ParamType paramType) { private static MethodParamPropertyModel extractMethodParamPropertyModel(
ParamTypeModel paramTypeModel=new ParamTypeModel(); MethodParamProperty methodParamProperty) {
paramTypeModel.setCols(paramType.getCols()); MethodParamPropertyModel methodParamPropertyModel = new MethodParamPropertyModel();
paramTypeModel.setFieldSeperator(paramType.getFieldSeperator()); methodParamPropertyModel.setKey(methodParamProperty.getKey());
paramTypeModel.setRows(paramType.getRows()); methodParamPropertyModel.setValue(methodParamProperty.getValue());
paramTypeModel.setSize(paramType.getSize()); return methodParamPropertyModel;
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;
} }
} }

View File

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

View File

@ -22,6 +22,7 @@ public class PluginService {
private Set<Plugin> plugins; private Set<Plugin> plugins;
private List<String> pluginNameList; private List<String> pluginNameList;
private PluginRepository pluginRepository; private PluginRepository pluginRepository;
private PluginFactory pluginFactory;
private Logger logger = Logger.getLogger(PluginService.class); private Logger logger = Logger.getLogger(PluginService.class);
public PluginRepository getPluginRepository() { public PluginRepository getPluginRepository() {
@ -33,6 +34,15 @@ public class PluginService {
this.pluginRepository = pluginRepository; this.pluginRepository = pluginRepository;
} }
public PluginFactory getPluginFactory() {
return pluginFactory;
}
@Autowired
public void setPluginFactory(PluginFactory pluginFactory) {
this.pluginFactory = pluginFactory;
}
private Set<Plugin> getPlugins() throws Bench4QException { private Set<Plugin> getPlugins() throws Bench4QException {
List<Plugin> loadPluginList = this.getPluginRepository().loadPlugins(); List<Plugin> loadPluginList = this.getPluginRepository().loadPlugins();
if (loadPluginList != null) { if (loadPluginList != null) {
@ -47,7 +57,7 @@ public class PluginService {
public boolean addPlugin(PluginModel pluginModel) throws Bench4QException { public boolean addPlugin(PluginModel pluginModel) throws Bench4QException {
return this.getPluginRepository().attatch( return this.getPluginRepository().attatch(
PluginFactory.createPluginEntity(pluginModel)); this.getPluginFactory().createPluginEntity(pluginModel));
} }
public boolean deletePlugin(String pluginName) throws Bench4QException { 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.Method" />
<mapping class="org.bench4q.master.domain.entity.plugin.MethodParam" /> <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.ParamType" />
<mapping class="org.bench4q.master.domain.entity.plugin.Field" /> <mapping class="org.bench4q.master.domain.entity.plugin.MethodParamProperty" />
<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" />
</session-factory> </session-factory>
</hibernate-configuration> </hibernate-configuration>

View File

@ -1,59 +1,61 @@
package TestHelper; package TestHelper;
import java.util.HashSet;
import java.util.HashSet; import java.util.Set;
import java.util.Set; import java.util.UUID;
import java.util.UUID;
import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.FieldModel; import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.MethodModel; import org.bench4q.share.models.master.plugin.ParamTypeEnum;
import org.bench4q.share.models.master.plugin.MethodParamModel; import org.bench4q.share.models.master.plugin.PluginModel;
import org.bench4q.share.models.master.plugin.MultiFieldModel; import org.bench4q.share.models.master.plugin.MethodParamPropertyModel;
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 class Test_PlunginHelper {
public static PluginModel createOnePlugin() {
public static PluginModel createOnePlugin() { PluginModel plugin = new PluginModel();
PluginModel plugin = new PluginModel(); MethodModel method = new MethodModel();
MethodModel method = new MethodModel(); MethodParamModel methodParamModelUrl = new MethodParamModel();
MethodParamModel methodParamModel = new MethodParamModel(); methodParamModelUrl.setLable("input url");
methodParamModel.setLable("input url"); methodParamModelUrl.setName("url");
methodParamModel.setName("url"); methodParamModelUrl.setMethodParamProperties(generatePropertyForField());
methodParamModel.setParamTypeModel(generateUrlParamTypeModel()); methodParamModelUrl.setParamType(ParamTypeEnum.Field);
MethodParamModel methodParamsParamModel = new MethodParamModel(); MethodParamModel methodParamsParamModel = new MethodParamModel();
methodParamsParamModel.setName("queryparams"); methodParamsParamModel.setName("queryparams");
methodParamsParamModel.setLable("key=value"); methodParamsParamModel.setLable("key=value");
methodParamsParamModel.setParamTypeModel(generateParamsParamTypeModel()); methodParamsParamModel.setMethodParamProperties(generatePropertyForMulti());
Set<MethodParamModel> methodParams = new HashSet<MethodParamModel>(); methodParamsParamModel.setParamType(ParamTypeEnum.MultiField);
methodParams.add(methodParamModel); Set<MethodParamModel> methodParams = new HashSet<MethodParamModel>();
methodParams.add(methodParamsParamModel); methodParams.add(methodParamModelUrl);
methodParams.add(methodParamsParamModel);
method.setName("get"); method.setName("get");
method.setMethodParams(methodParams); method.setMethodParams(methodParams);
Set<MethodModel> methods = new HashSet<MethodModel>(); Set<MethodModel> methods = new HashSet<MethodModel>();
methods.add(method); methods.add(method);
plugin.setMethods(methods); plugin.setMethods(methods);
plugin.setName("http" + UUID.randomUUID()); plugin.setName("http" + UUID.randomUUID());
return plugin; return plugin;
} }
private static Set<MethodParamPropertyModel> generatePropertyForField() {
private static ParamTypeModel generateUrlParamTypeModel() { Set<MethodParamPropertyModel> metList = new HashSet<MethodParamPropertyModel>();
FieldModel paramPropertyUrl = new FieldModel(); MethodParamPropertyModel methodParamPropertyModel = new MethodParamPropertyModel();
paramPropertyUrl.setType(ParamTypeString.FIELD); methodParamPropertyModel.setKey("size");
paramPropertyUrl.setSize(20); methodParamPropertyModel.setValue("20");
return paramPropertyUrl; metList.add(methodParamPropertyModel);
} return metList;
private static ParamTypeModel generateParamsParamTypeModel() { }
MultiFieldModel multiFieldModel = new MultiFieldModel(); private static Set<MethodParamPropertyModel> generatePropertyForMulti() {
multiFieldModel.setSize(20); Set<MethodParamPropertyModel> metList = new HashSet<MethodParamPropertyModel>();
multiFieldModel.setType(ParamTypeString.MULTIFIELD); MethodParamPropertyModel methodParamPropertyModel = new MethodParamPropertyModel();
return multiFieldModel; 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 { public class Test_PluginRepository {
private PluginRepository pluginRepository; private PluginRepository pluginRepository;
private String pluginName; private String pluginName;
private PluginFactory pluginFactory;
public PluginRepository getPluginRepository() { public PluginRepository getPluginRepository() {
return pluginRepository; return pluginRepository;
@ -32,6 +33,15 @@ public class Test_PluginRepository {
this.pluginRepository = pluginRepository; this.pluginRepository = pluginRepository;
} }
public PluginFactory getPluginFactory() {
return pluginFactory;
}
@Autowired
public void setPluginFactory(PluginFactory pluginFactory) {
this.pluginFactory = pluginFactory;
}
@Before @Before
public void setUp() throws Bench4QException { public void setUp() throws Bench4QException {
pluginName = addPlugin(); pluginName = addPlugin();
@ -71,13 +81,13 @@ public class Test_PluginRepository {
@After @After
public void clear() throws Bench4QException { public void clear() throws Bench4QException {
/*this.getPluginRepository().detach(pluginName);*/ /* this.getPluginRepository().detach(pluginName); */
} }
private String addPlugin() throws Bench4QException { private String addPlugin() throws Bench4QException {
PluginModel pluginModel = TestPlunginHelper.createOnePlugin(); PluginModel pluginModel = TestPlunginHelper.createOnePlugin();
this.getPluginRepository().attatch( this.getPluginRepository().attatch(
PluginFactory.createPluginEntity(pluginModel)); this.getPluginFactory().createPluginEntity(pluginModel));
return pluginModel.getName(); return pluginModel.getName();
} }