-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from KaiVolland/decribe-model
Introduces "describeModel" interface.
- Loading branch information
Showing
6 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/shogun2-core/src/main/java/de/terrestris/shogun2/service/ModelDescriptionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
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; | ||
|
||
|
||
/** | ||
* Service class to describe classes model. | ||
* | ||
* terrestris GmbH & Co. KG | ||
* @author Kai Volland | ||
* @date 04.09.2017 | ||
* | ||
*/ | ||
@Service("modelDescriptionService") | ||
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; | ||
|
||
/** | ||
* | ||
*/ | ||
@Autowired | ||
protected ObjectMapper objectMapper; | ||
|
||
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 | ||
} | ||
} | ||
|
||
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; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/shogun2-core/src/main/java/de/terrestris/shogun2/web/ModelDescriptionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.terrestris.shogun2.web; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; | ||
|
||
import de.terrestris.shogun2.service.ModelDescriptionService; | ||
import de.terrestris.shogun2.util.data.ResultSet; | ||
|
||
/** | ||
* | ||
* terrestris GmbH & Co. KG | ||
* @author Kai Volland | ||
* @date 04.09.2017 | ||
* | ||
* | ||
*/ | ||
@Controller | ||
@RequestMapping("/describeModel") | ||
public class ModelDescriptionController { | ||
|
||
@Autowired | ||
@Qualifier("modelDescriptionService") | ||
private ModelDescriptionService modelDescriptionService; | ||
|
||
@RequestMapping(value = "/asJson/{className}.action", method = RequestMethod.GET) | ||
public @ResponseBody Map<String, Object> getJsonSchema(@PathVariable String className) { | ||
try { | ||
JsonSchema json = modelDescriptionService.getJsonSchema(className); | ||
if (json == null) { | ||
return ResultSet.error("Modeldescription (json) for model " + className + "is null."); | ||
} | ||
return ResultSet.success(json); | ||
} catch (Exception e) { | ||
return ResultSet.error("Could not get description for " + className + " " + e.getMessage()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters