/*
* Copyright (C) 2019 fastjrun, Inc. All Rights Reserved.
*/
package com.fastjrun.codeg.generator.method;
import com.fastjrun.codeg.common.*;
import com.fastjrun.codeg.generator.BaseServiceGenerator;
import com.fastjrun.codeg.helper.StringHelper;
import com.helger.jcodemodel.*;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import java.util.Map;
@Setter
@Getter
public abstract class BaseServiceMethodGenerator extends AbstractMethodGenerator {
protected CommonMethod commonMethod;
protected String methodName;
protected JMethod jServiceMethod;
protected AbstractJType requestBodyClass;
protected AbstractJType responseBodyClass;
protected AbstractJClass elementClass;
protected BaseServiceGenerator serviceGenerator;
protected JFieldVar fieldVar;
protected JMethod jServiceMockMethod;
public abstract void doParse();
public void processServiceMethod() {
if (!this.commonMethod.isNeedApi()) {
return;
}
this.jServiceMethod =
this.serviceGenerator
.getServiceClass()
.method(JMod.NONE, this.responseBodyClass, this.methodName);
String methodRemark = commonMethod.getRemark();
this.jServiceMethod.javadoc().append(methodRemark);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMethod, this.commonMethod.getHeadVariables(), this.cm, this.packageNamePrefix);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMethod, this.commonMethod.getPathVariables(), this.cm, this.packageNamePrefix);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMethod, this.commonMethod.getParameters(), this.cm, this.packageNamePrefix);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMethod,
this.commonMethod.getCookieVariables(),
this.cm,
this.packageNamePrefix);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMethod, this.commonMethod.getWebParameters(), this.cm, this.packageNamePrefix);
if (this.requestBodyClass != null) {
String varName=this.commonMethod.getRequestName();
if(StringUtils.isBlank(varName)){
if(this.requestBodyClass.isArray()){
varName = StringHelper.toLowerCaseFirstOne(this.requestBodyClass.elementType().name())+"s";
}else{
varName = StringHelper.toLowerCaseFirstOne(this.requestBodyClass.name());
}
}
this.jServiceMethod.param(this.requestBodyClass, varName);
}
}
public void processServiceMockMethod() {
this.jServiceMockMethod =
this.serviceGenerator.getServiceMockClass()
.method(JMod.PUBLIC, this.responseBodyClass, this.methodName);
String methodRemark = this.commonMethod.getRemark();
this.jServiceMockMethod.javadoc().append(methodRemark);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMockMethod,
this.commonMethod.getHeadVariables(),
this.cm,
this.packageNamePrefix);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMockMethod,
this.commonMethod.getPathVariables(),
this.cm,
this.packageNamePrefix);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMockMethod,
this.commonMethod.getParameters(),
this.cm,
this.packageNamePrefix);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMockMethod,
this.commonMethod.getCookieVariables(),
this.cm,
this.packageNamePrefix);
MethodGeneratorHelper.processServiceMethodVariables(
this.jServiceMockMethod,
this.commonMethod.getWebParameters(),
this.cm,
this.packageNamePrefix);
if (this.requestBodyClass != null) {
String varName=this.commonMethod.getRequestName();
if(StringUtils.isBlank(varName)){
if(this.requestBodyClass.isArray()){
varName = StringHelper.toLowerCaseFirstOne(this.requestBodyClass.elementType().name())+"s";
}else{
varName = StringHelper.toLowerCaseFirstOne(this.requestBodyClass.name());
}
}
this.jServiceMockMethod.param(this.requestBodyClass, varName);
}
this.jServiceMockMethod.annotate(cm.ref("java.lang.Override"));
JBlock serviceMockMethodBlock = this.jServiceMockMethod.body();
if (this.responseBodyClass != cm.VOID) {
if (this.commonMethod.isResponseIsArray()) {
if (this.elementClass.name().endsWith("Boolean")) {
serviceMockMethodBlock._return(cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geBooleanList").arg(JExpr.lit(10)));
} else if (this.elementClass.name().endsWith("Integer")) {
serviceMockMethodBlock._return(
cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geIntegerList").arg(JExpr.lit(10)));
} else if (this.elementClass.name().endsWith("Long")) {
serviceMockMethodBlock._return(
cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geLongList").arg(JExpr.lit(10)));
} else if (this.elementClass.name().endsWith("Double")) {
serviceMockMethodBlock._return(
cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geDoubleList").arg(JExpr.lit(10)));
} else if (this.elementClass.name().endsWith("String")) {
serviceMockMethodBlock._return(
cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geStringListWithAscii").arg(JExpr.lit(10)));
} else if (this.elementClass.name().endsWith("Date")) {
serviceMockMethodBlock._return(
cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geDateList").arg(JExpr.lit(10)));
} else {
JVar responseVar =
serviceMockMethodBlock.decl(
this.responseBodyClass, "response", JExpr._new(cm.ref("java.util.ArrayList")));
JVar responseBodyVar =
this.composeResponseBody(
0, serviceMockMethodBlock, this.commonMethod.getResponse(), this.elementClass);
serviceMockMethodBlock.add(responseVar.invoke("add").arg(responseBodyVar));
serviceMockMethodBlock._return(responseVar);
}
} else if (this.commonMethod.isResponseIsPage()) {
if (this.elementClass.name().endsWith("Boolean")) {
serviceMockMethodBlock._return(
cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geBooleanPage").arg(JExpr.lit(10)));
} else if (this.elementClass.name().endsWith("Integer")) {
serviceMockMethodBlock._return(
cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geIntegerPage").arg(JExpr.lit(10)));
} else if (this.elementClass.name().endsWith("Long")) {
serviceMockMethodBlock._return(
cm.ref(this.serviceGenerator.getMockHelperName()).staticInvoke("geLongPage").arg(JExpr.li