Commit 34680a59 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub
Browse files

fix(bdm) add a specific db vendor validator (#2518)

* Those keywords triggers warnings in the BDM Editor

Closes BS-19520
parent 7149f34d
......@@ -332,3 +332,4 @@ exploreTooltip=Explore the BDM schema with a graphQL voyager. BDM must have been
bdmVoyager=Business Data Model voyager
deployCancelTitle=Deploy cancel
deployCancel=Unable to save the model, the deploy operation has been cancelled.
discouragedUsageOfKnownReservedKeyword=`{0}` usage is discouraged as it is a known reserved identifier for some Database vendor.
/**
* Copyright (C) 2020 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.businessobject.validator;
import static org.bonitasoft.studio.assertions.StatusAssert.assertThat;
import org.eclipse.core.runtime.IStatus;
import org.junit.Test;
public class CustomSQLNameValidatorTest {
@Test
public void should_validate_db_specific_keyword() throws Exception {
CustomSQLNameValidator customSQLNameValidator = new CustomSQLNameValidator(20);
IStatus status = customSQLNameValidator.validate("Index");
assertThat(status).isWarning();
}
@Test
public void should_validate_sql_keyword() throws Exception {
CustomSQLNameValidator customSQLNameValidator = new CustomSQLNameValidator(20);
IStatus status = customSQLNameValidator.validate("Table");
assertThat(status).isError();
}
}
......@@ -508,6 +508,7 @@ public class Messages extends NLS {
public static String bdmVoyager;
public static String deployCancelTitle;
public static String deployCancel;
public static String discouragedUsageOfKnownReservedKeyword;
private Messages() {
}
......
......@@ -30,17 +30,18 @@ import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.jdt.core.JavaConventions;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.osgi.util.NLS;
public class BusinessObjectNameValidator implements IBDMValidator<BusinessObject> {
public static final int MAX_TABLE_NAME_LENGTH = 30;
private IObservableValue<BusinessObjectModel> modelObservable;
private SQLNameValidator sqlNameValidator;
private CustomSQLNameValidator sqlNameValidator;
public BusinessObjectNameValidator(IObservableValue<BusinessObjectModel> modelObservable) {
this.modelObservable = modelObservable;
this.sqlNameValidator = new SQLNameValidator(MAX_TABLE_NAME_LENGTH);
this.sqlNameValidator = new CustomSQLNameValidator(MAX_TABLE_NAME_LENGTH);
}
@Override
......@@ -56,7 +57,7 @@ public class BusinessObjectNameValidator implements IBDMValidator<BusinessObject
status.add(validateUniqueness((businessObject), name));
status.add(validateWhiteSpaceCharacter(name));
status.add(validateUnderscoreCharacter(name));
status.add(validateSqlValidity(name));
status.add(sqlNameValidator.validate(name));
status.add(validateJavaConvention(name));
return status;
......@@ -66,14 +67,6 @@ public class BusinessObjectNameValidator implements IBDMValidator<BusinessObject
return JavaConventions.validateJavaTypeName(name, JavaCore.VERSION_1_8, JavaCore.VERSION_1_8);
}
private IStatus validateSqlValidity(String name) {
return sqlNameValidator.isValid(name)
? ValidationStatus.ok()
: sqlNameValidator.isSQLKeyword(name)
? ValidationStatus.error(Messages.bind(Messages.reservedKeyWord, name))
: ValidationStatus.error(Messages.bind(Messages.invalidSQLIdentifier, name));
}
private IStatus validateWhiteSpaceCharacter(String name) {
return name.contains(" ")
? ValidationStatus.error(Messages.errorMessageNoWhitespaceInDataTypeNames)
......
/**
* Copyright (C) 2020 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.businessobject.validator;
import java.util.HashSet;
import java.util.Set;
import org.bonitasoft.engine.bdm.validator.SQLNameValidator;
import org.bonitasoft.studio.businessobject.i18n.Messages;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.osgi.util.NLS;
public class CustomSQLNameValidator {
private static final Set<String> VENDOR_SPECIFIC_KEYWORDS = new HashSet<>();
static {
VENDOR_SPECIFIC_KEYWORDS.add("index");
}
private SQLNameValidator engineValidator;
public CustomSQLNameValidator(int maxLength) {
this.engineValidator = new SQLNameValidator(maxLength);
}
public IStatus validate(String name) {
IStatus result = engineValidator.isValid(name)
? ValidationStatus.ok()
: engineValidator.isSQLKeyword(name)
? ValidationStatus.error(NLS.bind(Messages.reservedKeyWord, name))
: ValidationStatus.error(NLS.bind(Messages.invalidSQLIdentifier, name));
if (result.isOK() && VENDOR_SPECIFIC_KEYWORDS.contains(name.toLowerCase())) {
return ValidationStatus.warning(NLS.bind(Messages.discouragedUsageOfKnownReservedKeyword, name));
}
return result;
}
}
......@@ -16,7 +16,6 @@ package org.bonitasoft.studio.businessobject.validator;
import java.util.Objects;
import org.bonitasoft.engine.bdm.validator.SQLNameValidator;
import org.bonitasoft.studio.businessobject.BusinessObjectPlugin;
import org.bonitasoft.studio.businessobject.editor.model.BusinessObject;
import org.bonitasoft.studio.businessobject.editor.model.Field;
......@@ -27,15 +26,16 @@ import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.jdt.core.JavaConventions;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.osgi.util.NLS;
public class FieldNameValidator implements IBDMValidator<Field> {
public static final int MAX_COLUMN_NAME_LENGTH = 50;
private SQLNameValidator sqlNameValidator;
private CustomSQLNameValidator sqlNameValidator;
public FieldNameValidator() {
this.sqlNameValidator = new SQLNameValidator(MAX_COLUMN_NAME_LENGTH);
this.sqlNameValidator = new CustomSQLNameValidator(MAX_COLUMN_NAME_LENGTH);
}
@Override
......@@ -48,7 +48,7 @@ public class FieldNameValidator implements IBDMValidator<Field> {
status.add(validateJavaConvention(name));
status.add(validateNameLength(name));
status.add(validateSqlValidity(name));
status.add(sqlNameValidator.validate(name));
status.add(validateReservedFieldNames(name));
status.add(validateUniqueness(field));
status.add(validateFirstCharacter(name));
......@@ -75,22 +75,14 @@ public class FieldNameValidator implements IBDMValidator<Field> {
private IStatus validateReservedFieldNames(String name) {
if (name.equalsIgnoreCase(org.bonitasoft.engine.bdm.model.field.Field.PERSISTENCE_ID)) {
return ValidationStatus.error(
Messages.bind(Messages.reservedKeyWord, org.bonitasoft.engine.bdm.model.field.Field.PERSISTENCE_ID));
NLS.bind(Messages.reservedKeyWord, org.bonitasoft.engine.bdm.model.field.Field.PERSISTENCE_ID));
} else if (name.equalsIgnoreCase(org.bonitasoft.engine.bdm.model.field.Field.PERSISTENCE_VERSION)) {
return ValidationStatus.error(Messages.bind(Messages.reservedKeyWord,
return ValidationStatus.error(NLS.bind(Messages.reservedKeyWord,
org.bonitasoft.engine.bdm.model.field.Field.PERSISTENCE_VERSION));
}
return ValidationStatus.ok();
}
private IStatus validateSqlValidity(String name) {
return sqlNameValidator.isValid(name)
? ValidationStatus.ok()
: sqlNameValidator.isSQLKeyword(name)
? ValidationStatus.error(Messages.bind(Messages.reservedKeyWord, name))
: ValidationStatus.error(Messages.bind(Messages.invalidSQLIdentifier, name));
}
private IStatus validateNameLength(String name) {
return new InputLengthValidator(name, MAX_COLUMN_NAME_LENGTH).validate(name);
}
......
......@@ -17,7 +17,6 @@ package org.bonitasoft.studio.businessobject.validator;
import java.util.Collection;
import java.util.Objects;
import org.bonitasoft.engine.bdm.validator.SQLNameValidator;
import org.bonitasoft.studio.businessobject.BusinessObjectPlugin;
import org.bonitasoft.studio.businessobject.editor.model.BusinessObject;
import org.bonitasoft.studio.businessobject.editor.model.BusinessObjectModel;
......@@ -36,13 +35,13 @@ public class IndexNameValidator implements IBDMValidator<Index> {
public static final int MAX_INDEX_NAME_LENGTH = 20;
private SQLNameValidator sqlNameValidator;
private CustomSQLNameValidator sqlNameValidator;
private IObservableValue<BusinessObjectModel> modelObservable;
public IndexNameValidator(IObservableValue<BusinessObjectModel> modelObservable) {
this.modelObservable = modelObservable;
this.sqlNameValidator = new SQLNameValidator(MAX_INDEX_NAME_LENGTH);
this.sqlNameValidator = new CustomSQLNameValidator(MAX_INDEX_NAME_LENGTH);
}
@Override
......@@ -55,7 +54,7 @@ public class IndexNameValidator implements IBDMValidator<Index> {
status.add(validateJavaConvention(name));
status.add(validateNameLength(name));
status.add(validateSqlValidity(name));
status.add(sqlNameValidator.validate(name));
status.add(validateUniqueness(index));
return status;
......@@ -75,14 +74,6 @@ public class IndexNameValidator implements IBDMValidator<Index> {
return ValidationStatus.ok();
}
private IStatus validateSqlValidity(String name) {
return sqlNameValidator.isValid(name)
? ValidationStatus.ok()
: sqlNameValidator.isSQLKeyword(name)
? ValidationStatus.error(Messages.bind(Messages.reservedKeyWord, name))
: ValidationStatus.error(Messages.bind(Messages.invalidSQLIdentifier, name));
}
private IStatus validateNameLength(String name) {
return new InputLengthValidator(name, MAX_INDEX_NAME_LENGTH).validate(name);
}
......
......@@ -16,7 +16,6 @@ package org.bonitasoft.studio.businessobject.validator;
import java.util.Objects;
import org.bonitasoft.engine.bdm.validator.SQLNameValidator;
import org.bonitasoft.studio.businessobject.BusinessObjectPlugin;
import org.bonitasoft.studio.businessobject.editor.model.BusinessObject;
import org.bonitasoft.studio.businessobject.editor.model.UniqueConstraint;
......@@ -32,10 +31,10 @@ public class UniqueConstraintNameValidator implements IBDMValidator<UniqueConstr
public static final int MAX_COLUMN_NAME_LENGTH = 20;
private SQLNameValidator sqlNameValidator;
private CustomSQLNameValidator sqlNameValidator;
public UniqueConstraintNameValidator() {
this.sqlNameValidator = new SQLNameValidator(MAX_COLUMN_NAME_LENGTH);
this.sqlNameValidator = new CustomSQLNameValidator(MAX_COLUMN_NAME_LENGTH);
}
@Override
......@@ -48,7 +47,7 @@ public class UniqueConstraintNameValidator implements IBDMValidator<UniqueConstr
status.add(validateJavaConvention(name));
status.add(validateNameLength(name));
status.add(validateSqlValidity(name));
status.add(sqlNameValidator.validate(name));
status.add(validateUniqueness(constraint));
return status;
......@@ -72,14 +71,6 @@ public class UniqueConstraintNameValidator implements IBDMValidator<UniqueConstr
return new InputLengthValidator(name, MAX_COLUMN_NAME_LENGTH).validate(name);
}
private IStatus validateSqlValidity(String name) {
return sqlNameValidator.isValid(name)
? ValidationStatus.ok()
: sqlNameValidator.isSQLKeyword(name)
? ValidationStatus.error(Messages.bind(Messages.reservedKeyWord, name))
: ValidationStatus.error(Messages.bind(Messages.invalidSQLIdentifier, name));
}
@Override
public String getValidatorType() {
return Messages.constraints;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment