blob: ad5191d7e95c512746eb3d53dd5a5e2405169613 [file] [log] [blame]
/*
* generated by Xtext 2.11.0
*/
package org.eclipse.osbp.xtext.datainterchange.validation
import java.net.MalformedURLException
import java.net.URL
import java.nio.file.Paths
import javax.inject.Inject
import org.eclipse.osbp.xtext.datainterchange.DataDSLPackage
import org.eclipse.osbp.xtext.datainterchange.DataInterchange
import org.eclipse.osbp.xtext.datainterchange.DataInterchangeExportFilter
import org.eclipse.osbp.xtext.datainterchange.DataInterchangeFile
import org.eclipse.osbp.xtext.datainterchange.DataInterchangeFileCSV
import org.eclipse.osbp.xtext.datainterchange.DataInterchangeFileEDI
import org.eclipse.osbp.xtext.datainterchange.DataInterchangeFileFixed
import org.eclipse.osbp.xtext.datainterchange.DataInterchangeFileXML
import org.eclipse.osbp.xtext.datainterchange.DataInterchangeLookupFilterCondition
import org.eclipse.osbp.xtext.datainterchange.DataInterchangePostFunction
import org.eclipse.osbp.xtext.datainterchange.Operator
import org.eclipse.osbp.xtext.datainterchange.jvmmodel.DataDSLJvmModelInferrer
import org.eclipse.xtext.common.types.JvmParameterizedTypeReference
import org.eclipse.xtext.validation.Check
/**
* This class contains custom validation rules.
*
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
*/
class DataDSLValidator extends AbstractDataDSLValidator {
@Inject extension DataDSLJvmModelInferrer df
public val CSV_EXTENSION = ".csv";
public val XML_EXTENSION = ".xml";
public val EDI_EXTENSION = ".edi";
public val FIX_EXTENSION = ".txt";
static val ERROR_MESSAGE = "The directory you've chosen does not exist or is not accessible. Please adapt the url."
@Check
def checkPostFunctionInterchange(DataInterchangePostFunction post){
if(post !== null && post.doExecuteFunction !== null && post.doExecuteFunction.params !== null){
if(!post.doExecuteFunction.params.empty){
if(post.doExecuteFunction.params.size >=2){
var params = post.doExecuteFunction.params.get(0)
var type1 = params.parameterType.type.qualifiedName
var type2 = post.doExecuteFunction.params.get(1).parameterType.type.qualifiedName
if(!type1.equals("java.util.HashMap")){
error("The second parameter of the chosen post function must be from type HashMap<String,Parameter>,
\nwith Parameter from org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable.Parameter .",
post, DataDSLPackage.Literals.DATA_INTERCHANGE_POST_FUNCTION__DO_EXECUTE_FUNCTION)
}
if(!type2.equals("java.util.List")){
error("The first parameter of the chosen post function must be from type java.util.List<java.nio.file.Path>", post, DataDSLPackage.Literals.DATA_INTERCHANGE_POST_FUNCTION__DO_EXECUTE_FUNCTION)
}
else{
// check the 2 type of the HashMap, the second parameter of the post function
var param0 = (params.parameterType.eContents.get(0) as JvmParameterizedTypeReference).qualifiedName // String
var param1 = (params.parameterType.eContents.get(1) as JvmParameterizedTypeReference).qualifiedName // org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable.Parameter
if(!param0.equals("java.lang.String")){
error("The first parameter of your HashMap<> has to be from type java.lang.String .",
post, DataDSLPackage.Literals.DATA_INTERCHANGE_POST_FUNCTION__DO_EXECUTE_FUNCTION)
}
if(!param1.equals("org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable$Parameter")){
error("The second parameter of your HashMap<> has to be from type org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable.Parameter .",
post, DataDSLPackage.Literals.DATA_INTERCHANGE_POST_FUNCTION__DO_EXECUTE_FUNCTION)
}
}
}
else{
error("The chosen post function must have at least tree parameters from types HashMap<String, org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable.Parameter> and List<java.nio.file.Path> in that order!
\nWith Parameter from org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable.Parameter.",
post, DataDSLPackage.Literals.DATA_INTERCHANGE_POST_FUNCTION__DO_EXECUTE_FUNCTION)
}
}
else{
error("The chosen post function must at least have two parameters from types HashMap<String, org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable.Parameter> and List<java.nio.file.Path> in that order!
\nWith Parameter from org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable.Parameter.",
post, DataDSLPackage.Literals.DATA_INTERCHANGE_POST_FUNCTION__DO_EXECUTE_FUNCTION )
}
}
}
@Check
def checkFilterConditionValidity2(DataInterchangeLookupFilterCondition condition){
if(condition !== null && condition.getRefProperty !== null && condition.getOperator !== null){
if(condition.getOperator.equals(Operator.ISNOTNULL)
|| condition.getOperator.equals(Operator.ISNULL)){
if(condition.operand !== null) {
error("no value option expected/allowed for this condition.", DataDSLPackage.Literals.DATA_INTERCHANGE_LOOKUP_FILTER_CONDITION__OPERAND)
}
}
else if(condition.operand === null && !(condition.getOperator.equals(Operator.ISNOTNULL)
|| condition.getOperator.equals(Operator.ISNULL))){
error("value option missing for this condition.", DataDSLPackage.Literals.DATA_INTERCHANGE_LOOKUP_FILTER_CONDITION__OPERAND)
}
}
}
@Check
def void checkFileNameValidity(DataInterchange interchange) {
if (interchange !== null && interchange.fileEndpoint !== null){
var allowed_extension = getValidExtensionToInterchange(interchange.fileEndpoint)
var filename = interchange.getDataInterchangeFileName
if (allowed_extension !== null) {
if(allowed_extension.equals(CSV_EXTENSION) && !isExtensionAllowedForInterchange(filename, allowed_extension)){
error("The file name ["+filename+"] you have entered is not valid. Only file names with the extension "+allowed_extension+" are allowed for this interchange unit!",
interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE_FILE_CSV__FILE_URL)
}
else if (allowed_extension.equals(EDI_EXTENSION) && !isExtensionAllowedForInterchange(filename, allowed_extension)){
error("The file name ["+filename+"] you have entered is not valid. Only file names with the extension "+allowed_extension+" are allowed for this interchange unit!",
interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE_FILE_EDI__FILE_URL)
}
else if (allowed_extension.equals(XML_EXTENSION) && !isExtensionAllowedForInterchange(filename, allowed_extension)){
error("The file name ["+filename+"] you have entered is not valid. Only file names with the extension "+allowed_extension+" are allowed for this interchange unit!",
interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE_FILE_XML__FILE_URL)
}
else if (allowed_extension.equals(FIX_EXTENSION) && !isExtensionAllowedForInterchange(filename, allowed_extension)){
error("The file name ["+filename+"] you have entered is not valid. Only file names with the extension "+allowed_extension+" are allowed for this interchange unit!",
interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE_FILE_FIXED__FILE_URL)
}
}
else {
error("The file name extension you have entered is not currently supported. Only file names with either one of following extension are allowed: '.csv' '.edi' '.txt' or '.xml' .",
interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE__FILE_ENDPOINT)
}
}
}
def boolean isExtensionValid(String filename) {
return (filename.endsWith(CSV_EXTENSION) || filename.endsWith(CSV_EXTENSION.toUpperCase) ||
filename.endsWith(EDI_EXTENSION) || filename.endsWith(EDI_EXTENSION.toUpperCase) ||
filename.endsWith(FIX_EXTENSION) || filename.endsWith(FIX_EXTENSION.toUpperCase) ||
filename.endsWith(XML_EXTENSION) || filename.endsWith(XML_EXTENSION.toUpperCase)
)
}
def boolean isExtensionAllowedForInterchange(String filename, String allowedextension) {
return (filename.endsWith(allowedextension) || filename.endsWith(allowedextension.toUpperCase))
}
def String getValidExtensionToInterchange(DataInterchangeFile file) {
if (file instanceof DataInterchangeFileCSV) {
return CSV_EXTENSION
} else if (file instanceof DataInterchangeFileEDI) {
return EDI_EXTENSION
} else if (file instanceof DataInterchangeFileXML) {
return XML_EXTENSION
} else if (file instanceof DataInterchangeFileFixed) {
return FIX_EXTENSION
}
return null
}
@Check
def checkDirectoryOrURLValidity(DataInterchange interchange) {
var directory = interchange.getDataInterchangeDirectoryUrlPath
if(directory.trim.length !== directory.length){
error("The path you've given shouldn't contains blank space.", interchange, DataDSLPackage.Literals.DATA_INTERCHANGE__FILE_ENDPOINT)
}
else{
try {
checkDirectoryURL(new URL(directory), interchange)
}
catch (MalformedURLException e1) {
if(e1.getMessage().startsWith("unknown protocol") || e1.getMessage().startsWith("no protocol")) {
try {
// check for relative path
if(!Paths.get(directory).absolute){
var projectname = interchange.eResource.URI.segment(1)
if(!existsDirectory(projectname, directory)){
showDirectoryError(interchange)
// var os = FileSystems.^default.class.canonicalName
// if(os.endsWith("WindowsFileSystem")){
// // TODO fill appropriate tool tip or info message
// }
// else if(os.endsWith("MacOSXFileSystem")){
// // TODO fill appropriate tool tip or info message
// }
// else{
// // TODO fill appropriate tool tip or info message
// }
}
}
else{
// check for absolute path
checkDirectoryURL(Paths.get(directory).toUri().toURL(), interchange)
}
} catch (Exception e2) {
showDirectoryError(interchange)
}
}
}
}
}
def boolean existsDirectory(String projectname, String directory) {
var path = findProjectFileOrFolderLocation(projectname, directory, false).toOSString
var file = Paths.get(path).toFile
if(file !== null && file.exists){
if(!file.directory){
return false
}
return true
}
return false
}
def boolean existsDirectory(String path) {
var file = Paths.get(path).toFile
if(file !== null && file.exists){
if(!file.directory){
return false
}
return true
}
return false
}
def checkDirectoryURL(URL url, DataInterchange interchange){
if(url !== null && url.protocol.equals("file") && !existsDirectory(interchange.getDataInterchangeDirectoryUrlPath)){
showDirectoryError(interchange)
}
//TODO add additional url protocol validity check here for later
}
private def showDirectoryError(DataInterchange interchange){
if(interchange.fileEndpoint instanceof DataInterchangeFileCSV){
warning(ERROR_MESSAGE, interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE_FILE_CSV__FILE_URL)
}
else if(interchange.fileEndpoint instanceof DataInterchangeFileEDI){
warning(ERROR_MESSAGE, interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE_FILE_EDI__FILE_URL)
}
else if(interchange.fileEndpoint instanceof DataInterchangeFileXML){
warning(ERROR_MESSAGE, interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE_FILE_XML__FILE_URL)
}
else if(interchange.fileEndpoint instanceof DataInterchangeFileFixed){
warning(ERROR_MESSAGE, interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE_FILE_FIXED__FILE_URL)
}
else{
warning(ERROR_MESSAGE, interchange.fileEndpoint, DataDSLPackage.Literals.DATA_INTERCHANGE__FILE_ENDPOINT)
}
}
@Check
def checkExportFilterValidity(DataInterchangeExportFilter filter) {
// TODO Check value based on attribute type, and value convertion in term of criteria building
// if(filter !== null && filter.refProperty !== null){
// if(filter.subcondition !== null){
//
// }
// }
// TODO Converter and Parser Test for all primitive attributes DataType String Value Converter
// TODO Test for Date Values with Criteria Builder / Integer / Boolean ...
// Test For DTO's
}
@Check
def checkCSVInterchange(DataInterchange interchange){
if(interchange !== null && interchange.path !== null && interchange.path.size >=2 && interchange.fileEndpoint instanceof DataInterchangeFileCSV){
var firstBean = interchange.path.get(0);
if(firstBean.fileName !== null){
warning("This entry won't be evaluated", interchange, DataDSLPackage.Literals.DATA_INTERCHANGE_BEAN__FILE_NAME)
}
// for(bean : interchange.path){
// if(bean !== firstBean && (bean.fileName === null || bean.fileName.empty)){
// warning("The file name of this entity must be defined.", bean, DataDSLPackage.Literals.DATA_INTERCHANGE_BEAN__FILE_NAME)
// }
// }
}
}
// @Check
// def void checkDateValue(DataInterchange interchange) {
// // check Date value
// }
}