Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK-5556 - OpenCGA CLI --json-data-model does not produce parseable JSONS #2394

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by imedina on 19/04/16.
Expand Down Expand Up @@ -291,72 +294,61 @@ public CommandExecutor setSessionManager(SessionManager sessionManager) {
return this;
}


public String getObjectAsJSON(String objectCategory, String objectPath, OpenCGAClient openCGAClient) throws Exception {
StringBuilder jsonInString = new StringBuilder("\n");
try {
ObjectMap queryParams = new ObjectMap();
queryParams.putIfNotEmpty("category", objectCategory);
RestResponse<List> response = openCGAClient.getMetaClient().api(queryParams);
ObjectMapper jsonObjectMapper = new ObjectMapper();
boolean found = false;
for (List list : response.getResponses().get(0).getResults()) {
List<RestCategory> categories = jsonObjectMapper.convertValue(list, new TypeReference<List<RestCategory>>() {});
for (RestCategory category : categories) {
for (RestEndpoint endpoint : category.getEndpoints()) {
if (objectPath.equals(endpoint.getPath())) {
boolean enc = false;
for (RestParameter parameter : endpoint.getParameters()) {
//jsonInString += parameter.getName()+":"+parameter.getAllowedValues()+"\n";
if (parameter.getData() != null) {
enc = true;
jsonInString.append(printBody(parameter.getData(), ""));
found = true;
Map<String, Object> map = getExampleBody(parameter.getData());
jsonInString.append(jsonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(map));
}
}
if (!enc) {
jsonInString.append("No model available");
}
//
}
}
}
}
if (!found) {
jsonInString.append("No model available");
}
} catch (Exception e) {
jsonInString = new StringBuilder("Data model not found.");
CommandLineUtils.error(e);
}
return jsonInString.toString();
}

private String printBody(List<RestParameter> data, String tabs) {
String res = "";
res += "{\n";
String tab = " " + tabs;
private Map<String, Object> getExampleBody(List<RestParameter> data) {
Map<String, Object> result = new HashMap<>();
for (RestParameter parameter : data) {
if (parameter.getData() == null) {
res += printParameter(parameter, tab);
result.put(parameter.getName(), getParameterExampleValue(parameter));
} else {
res += tab + "\"" +parameter.getName() + "\"" + ": [" + printBody(parameter.getData(), tab) + "],\n";
result.put(parameter.getName(), getExampleBody(parameter.getData()));
}
}
res += tabs + "}";
return res;

}

private String printParameter(RestParameter parameter, String tab) {

return tab + "\"" + parameter.getName() + "\"" + ":" + printParameterValue(parameter) + ",\n";
return result;
}

private String printParameterValue(RestParameter parameter) {

private Object getParameterExampleValue(RestParameter parameter) {
if(!StringUtils.isEmpty(parameter.getAllowedValues())){
return parameter.getAllowedValues().replace(" ", "|");
}

switch (parameter.getType()) {
case "Boolean":
case "java.lang.Boolean":
return "false";
return false;
case "Long":
case "Float":
case "Double":
Expand All @@ -365,21 +357,21 @@ private String printParameterValue(RestParameter parameter) {
case "double":
case "float":
case "long":
return "0";
return 0;
case "List":
return "[\"\"]";
return Collections.singletonList("");
case "Date":
return "\"dd/mm/yyyy\"";
return "dd/mm/yyyy";
case "Map":
return "{\"key\": \"value\"}";
return Collections.singletonMap("key", "value");
case "String":
return "\"\"";
return "";
default:
return "\"-\"";
logger.debug("Unknown type: " + parameter.getType() + " for parameter: " + parameter.getName());
return "-";
}
}


public Logger getLogger() {
return logger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ private RestResponse<SampleAclEntryList> updateAcl() throws Exception {
queryParams.putIfNotEmpty("study", sessionManager.getSession().getCurrentStudy());
}


SampleAclUpdateParams sampleAclUpdateParams = null;
if (commandOptions.jsonDataModel) {
RestResponse<SampleAclEntryList> res = new RestResponse<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,104 +99,117 @@ public String getName() {
return name;
}

public void setName(String name) {
public RestParameter setName(String name) {
this.name = name;
return this;
}

public RestParamType getParam() {
return param;
}

public void setParam(RestParamType param) {
public RestParameter setParam(RestParamType param) {
this.param = param;
return this;
}

public String getParentName() {
return parentName;
}

public void setParentName(String parentName) {
public RestParameter setParentName(String parentName) {
this.parentName = parentName;
return this;
}

public String getType() {
return type;
}

public void setType(String type) {
public RestParameter setType(String type) {
this.type = type;
return this;
}

public String getTypeClass() {
return typeClass;
}

public void setTypeClass(String typeClass) {
public RestParameter setTypeClass(String typeClass) {
this.typeClass = typeClass;
return this;
}

public boolean isRequired() {
return required;
}

public void setRequired(boolean required) {
public RestParameter setRequired(boolean required) {
this.required = required;
return this;
}

public String getDefaultValue() {
return defaultValue;
}

public void setDefaultValue(String defaultValue) {
public RestParameter setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
return this;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
public RestParameter setDescription(String description) {
this.description = description;
return this;
}

public List<RestParameter> getData() {
return data;
}

public void setData(List<RestParameter> data) {
public RestParameter setData(List<RestParameter> data) {
this.data = data;
return this;
}

public String getAllowedValues() {
return allowedValues;
}

public void setAllowedValues(String allowedValues) {
public RestParameter setAllowedValues(String allowedValues) {
this.allowedValues = allowedValues;
return this;
}

public boolean isComplex() {
return complex;
}

public void setComplex(boolean complex) {
public RestParameter setComplex(boolean complex) {
this.complex = complex;
return this;
}

public String getGenericType() {
return genericType;
}

public void setGenericType(String genericType) {
public RestParameter setGenericType(String genericType) {
this.genericType = genericType;
return this;
}

public boolean isInnerParam() {
return innerParam;
}

public void setInnerParam(boolean innerParam) {
public RestParameter setInnerParam(boolean innerParam) {
this.innerParam = innerParam;
return this;
}

public boolean isEnum() {
Expand Down
Loading