com.reprezen.genflow.rapidml.nodejs.CustomCodeGenerator Maven / Gradle / Ivy
package com.reprezen.genflow.rapidml.nodejs;
import com.reprezen.genflow.api.GenerationException;
import com.reprezen.genflow.rapidml.nodejs.NodejsGenerator;
import com.reprezen.rapidml.CollectionResource;
import com.reprezen.rapidml.HTTPMethods;
import com.reprezen.rapidml.Method;
import com.reprezen.rapidml.ResourceDefinition;
import com.reprezen.rapidml.ServiceDataResource;
import com.reprezen.rapidml.Structure;
import com.reprezen.rapidml.TypedResponse;
import com.reprezen.rapidml.URI;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
@SuppressWarnings("all")
public class CustomCodeGenerator implements NodejsGenerator.Generator {
private final ServiceDataResource resource;
public CustomCodeGenerator(final ServiceDataResource resource) {
this.resource = resource;
}
@Override
public String generate() throws GenerationException {
StringConcatenation _builder = new StringConcatenation();
_builder.append("const CustomImpl = require(\'../../../lib/CustomImpl\');");
_builder.newLine();
_builder.newLine();
_builder.append("class ");
String _name = this.resource.getName();
_builder.append(_name);
_builder.append("Impl extends CustomImpl {");
_builder.newLineIfNotEmpty();
{
EList _methods = this.resource.getMethods();
boolean _hasElements = false;
for(final Method method : _methods) {
if (!_hasElements) {
_hasElements = true;
} else {
_builder.appendImmediate("\n", "\t");
}
_builder.append("\t");
_builder.append("// ");
HTTPMethods _httpMethod = method.getHttpMethod();
_builder.append(_httpMethod, "\t");
_builder.append(" ");
String _expressify = this.expressify(this.resource.getURI());
_builder.append(_expressify, "\t");
_builder.newLineIfNotEmpty();
{
EList _responses = method.getResponses();
for(final TypedResponse response : _responses) {
_builder.append("\t");
_builder.append("// ");
String _status = this.getStatus(response);
_builder.append(_status, "\t");
_builder.append(" => ");
CharSequence _type = this.getType(response);
_builder.append(_type, "\t");
_builder.append(" ");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
String _name_1 = method.getName();
_builder.append(_name_1, "\t");
_builder.append("(dao, enrichments, req, res, next) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("res.status(500).send(\"Method not implemented\");");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
}
}
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("module.exports = ");
String _name_2 = this.resource.getName();
_builder.append(_name_2);
_builder.append("Impl; ");
_builder.newLineIfNotEmpty();
return _builder.toString();
}
private String getStatus(final TypedResponse response) {
return String.format("%03d", Integer.valueOf(response.getStatusCode()));
}
private CharSequence getType(final TypedResponse response) {
CharSequence _xblockexpression = null;
{
Structure _elvis = null;
ResourceDefinition _resourceType = response.getResourceType();
Structure _dataType = null;
if (((ServiceDataResource) _resourceType)!=null) {
_dataType=((ServiceDataResource) _resourceType).getDataType();
}
if (_dataType != null) {
_elvis = _dataType;
} else {
Structure _dataType_1 = response.getDataType();
_elvis = _dataType_1;
}
final Structure struct = _elvis;
CharSequence _xifexpression = null;
if ((struct != null)) {
CharSequence _xblockexpression_1 = null;
{
String _xifexpression_1 = null;
if (((response.getResourceType() != null) &&
(response.getResourceType() instanceof CollectionResource))) {
_xifexpression_1 = "*";
} else {
_xifexpression_1 = "";
}
final String card = _xifexpression_1;
String _xifexpression_2 = null;
ResourceDefinition _resourceType_1 = response.getResourceType();
boolean _tripleNotEquals = (_resourceType_1 != null);
if (_tripleNotEquals) {
StringConcatenation _builder = new StringConcatenation();
_builder.append(" ");
_builder.append("as ");
String _name = response.getResourceType().getName();
_builder.append(_name, " ");
_xifexpression_2 = _builder.toString();
} else {
_xifexpression_2 = "";
}
final String res = _xifexpression_2;
StringConcatenation _builder_1 = new StringConcatenation();
String _name_1 = struct.getName();
_builder_1.append(_name_1);
_builder_1.append(card);
_builder_1.append(res);
_xblockexpression_1 = _builder_1;
}
_xifexpression = _xblockexpression_1;
} else {
_xifexpression = "no response data";
}
_xblockexpression = _xifexpression;
}
return _xblockexpression;
}
private String expressify(final URI uri) {
return uri.toString().replaceAll("/\\{([^/]+)\\}((?=/)|$)", "/:$1");
}
}