-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #000 feat: rename ISchema to ISchemaValidator
- Loading branch information
Mahesh Kumar Gangula
committed
Aug 21, 2019
1 parent
5290f2f
commit 05d3acc
Showing
11 changed files
with
120 additions
and
79 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
ontology-engine/graph-engine/src/main/java/org/sunbird/graph/engine/BaseManager.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 org.sunbird.graph.engine; | ||
|
||
import akka.actor.ActorRef; | ||
import org.sunbird.actor.core.BaseActor; | ||
import org.sunbird.common.JsonUtils; | ||
import org.sunbird.common.dto.Response; | ||
import org.sunbird.common.dto.ResponseParams; | ||
import org.sunbird.common.exception.ResponseCode; | ||
import org.sunbird.schema.ISchemaValidator; | ||
import org.sunbird.schema.SchemaValidatorFactory; | ||
import org.sunbird.schema.dto.ValidationResult; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class BaseManager extends BaseActor { | ||
|
||
/** | ||
* This fetches the schema for given objectType and version and validates data against the schema. | ||
* | ||
* @param objectType | ||
* @param version | ||
* @param request | ||
* @return ValidationResult | ||
* @throws Exception | ||
*/ | ||
|
||
public ValidationResult validate(String objectType, String version, Map<String, Object> request) throws Exception { | ||
ISchemaValidator schema = SchemaValidatorFactory.getInstance(objectType, version); | ||
return schema.validate(JsonUtils.serialize(request)); | ||
} | ||
|
||
public void ERROR(String errorCode, String errorMessage, ResponseCode code, String responseIdentifier, Object vo) { | ||
Response response = new Response(); | ||
response.put(responseIdentifier, vo); | ||
response.setParams(getErrorStatus(errorCode, errorMessage)); | ||
response.setResponseCode(code); | ||
sender().tell(response, getSelf()); | ||
} | ||
|
||
private ResponseParams getErrorStatus(String errorCode, String errorMessage) { | ||
ResponseParams params = new ResponseParams(); | ||
params.setErr(errorCode); | ||
params.setStatus(ResponseParams.StatusType.failed.name()); | ||
params.setErrmsg(errorMessage); | ||
return params; | ||
} | ||
} |
41 changes: 17 additions & 24 deletions
41
ontology-engine/graph-engine/src/main/java/org/sunbird/graph/engine/actor/NodeManager.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 |
---|---|---|
@@ -1,46 +1,39 @@ | ||
package org.sunbird.graph.engine.actor; | ||
|
||
import org.sunbird.actor.core.BaseActor; | ||
import org.sunbird.actor.router.ActorConfig; | ||
import org.sunbird.common.JsonUtils; | ||
import org.sunbird.common.dto.Request; | ||
import org.sunbird.common.dto.Response; | ||
import org.sunbird.common.dto.ResponseParams; | ||
import org.sunbird.common.exception.ResponseCode; | ||
import org.sunbird.schema.ISchema; | ||
import org.sunbird.schema.SchemaFactory; | ||
import org.sunbird.schema.dto.Result; | ||
import org.sunbird.graph.engine.BaseManager; | ||
import org.sunbird.schema.dto.ValidationResult; | ||
|
||
import java.util.Map; | ||
|
||
@ActorConfig(tasks = {"createDataNode"}) | ||
public class NodeManager extends BaseActor { | ||
public class NodeManager extends BaseManager { | ||
|
||
@Override | ||
public void onReceive(Request request) throws Throwable { | ||
String action = request.getOperation(); | ||
switch (action) { | ||
String operation = request.getOperation(); | ||
switch (operation) { | ||
case "createDataNode": | ||
Result result = validate("content", "1.0", request.getRequest()); | ||
Response response = new Response("org.sunbird.content.create"); | ||
if (result.isValid()) { | ||
Map<String, Object> inputWithDefault = JsonUtils.deserialize(result.getData(), Map.class); | ||
|
||
response.getResult().put("content", inputWithDefault); | ||
} else { | ||
response.setParams(new ResponseParams()); | ||
response.setResponseCode(ResponseCode.CLIENT_ERROR); | ||
response.getResult().put("messages", result.getMessages()); | ||
} | ||
OK(response, self()); | ||
createDataNode(request); | ||
break; | ||
default: | ||
ERROR(action); | ||
ERROR(operation); | ||
} | ||
} | ||
|
||
public Result validate(String objectType, String version, Map<String, Object> request) throws Exception { | ||
ISchema schema = SchemaFactory.getInstance(objectType, version); | ||
return schema.validate(JsonUtils.serialize(request)); | ||
private void createDataNode(Request request) throws Exception { | ||
ValidationResult result = validate("content", "1.0", request.getRequest()); | ||
Response response = new Response("org.sunbird.content.create"); | ||
if (result.isValid()) { | ||
Map<String, Object> inputWithDefault = JsonUtils.deserialize(result.getData(), Map.class); | ||
response.getResult().put("content", inputWithDefault); | ||
OK(response, self()); | ||
} else { | ||
ERROR("NODE_VALIDATION_FAILED", "Validation errors.", ResponseCode.CLIENT_ERROR, "messages", result.getMessages()); | ||
} | ||
} | ||
} |
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
19 changes: 0 additions & 19 deletions
19
platform-core/schema-validator/src/main/java/org/sunbird/schema/ISchema.java
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
platform-core/schema-validator/src/main/java/org/sunbird/schema/ISchemaValidator.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,15 @@ | ||
package org.sunbird.schema; | ||
|
||
import org.sunbird.schema.dto.ValidationResult; | ||
|
||
import java.io.StringReader; | ||
import java.util.List; | ||
|
||
public interface ISchemaValidator { | ||
|
||
ValidationResult validate(String data); | ||
|
||
List<String> validate(StringReader input); | ||
|
||
String withDefaultValues(String data); | ||
} |
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
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
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