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

Introduces "describeModel" interface. #270

Merged
merged 3 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,14 +1,19 @@
package de.terrestris.shogun2.service;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;

import de.terrestris.shogun2.model.PersistentObject;

/**
* Service class to describe classes model.
Expand All @@ -19,17 +24,48 @@
*
*/
@Service("modelDescriptionService")
public class ModelDescriptionService<E extends PersistentObject> {
public class ModelDescriptionService {

/**
* The LOGGER instance (that will be available in all subclasses)
*/
protected final Logger LOG = Logger.getLogger(getClass());

@Resource
@Qualifier("describeModelSearchPackages")
private List<String> describeModelSearchPackages;

public JsonSchema getJsonSchema(Class<?> clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
/**
*
*/
@Autowired
protected ObjectMapper objectMapper;

JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
public JsonSchema getJsonSchema(String className) throws IOException {

Class<?> foundClass = null;
for (String searchPackage : describeModelSearchPackages) {
LOG.debug(String.format("Search className %s in package %s.", className, searchPackage));
try{
boolean wasNull = foundClass == null;
foundClass = Class.forName(searchPackage + "." + className);
if (!wasNull) {
LOG.error(String.format("Modelname %s exists in multiple packages! Last one will win.", className));
}
} catch (ClassNotFoundException e){
//not in this package, try another
}
}

JsonSchema schema = schemaGen.generateSchema(clazz);
if (foundClass == null) {
LOG.warn(String.format("No class found for describing modelname %s", className));
return null;
}

JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(objectMapper);
JsonSchema schema = schemaGen.generateSchema(foundClass);

return schema;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
Expand All @@ -28,39 +27,14 @@
@RequestMapping("/describeModel")
public class ModelDescriptionController {

/**
* The LOGGER instance (that will be available in all subclasses)
*/
protected final Logger LOG = Logger.getLogger(getClass());

@Autowired
@Qualifier("modelDescriptionService")
private ModelDescriptionService<?> modelDescriptionService;
private ModelDescriptionService modelDescriptionService;

@RequestMapping(value = "/asJson/{className}", method = RequestMethod.GET)
@RequestMapping(value = "/asJson/{className}.action", method = RequestMethod.GET)
public @ResponseBody Map<String, Object> getJsonSchema(@PathVariable String className) {
try {

// TODO Get these from bean or on instantiation
String[] searchPackages = {
"de.terrestris.shogun2.model",
"de.terrestris.bismap.model"
};

// See https://stackoverflow.com/a/33111503
Class<?> foundClass = null;
for(int i=0; i < searchPackages.length; i++){
try{
boolean wasNull = foundClass == null;
foundClass = Class.forName(searchPackages[i] + "." + className);
if (!wasNull) throw new RuntimeException(className + " exists in multiple packages!");
} catch (ClassNotFoundException e){
//not in this package, try another
}
}

JsonSchema json = modelDescriptionService.getJsonSchema(foundClass);

JsonSchema json = modelDescriptionService.getJsonSchema(className);
return ResultSet.success(json);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to return a ResultSet.error() if the json is null. you decide.

} catch (Exception e) {
return ResultSet.error("Could not get description for " + className + " " + e.getMessage());
Expand Down