mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
RM-2319 Separate the field validation from ClassificationLevelValidation.
Create field validators that are small and potentially re-usable. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@106527 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -22,6 +22,8 @@ import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationLevelValidation;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
|
||||
|
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2015 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
|
||||
/**
|
||||
* Validator for the fields in {@link ClassificationLevel}.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ClassificationLevelFieldsValidator implements EntityFieldsValidator<ClassificationLevel>
|
||||
{
|
||||
/** The maximum number of characters allowed in a {@link ClassificationLevel#getId() level ID}. */
|
||||
private static final int ABBREVIATION_LENGTH_LIMIT = 10;
|
||||
/** Validator for the length of the level abbreviation. */
|
||||
private FieldValidator<String> lengthFieldValidator = new LengthFieldValidator(1, ABBREVIATION_LENGTH_LIMIT);
|
||||
/** Validator for the characters in the level abbreviation. */
|
||||
private FieldValidator<String> filenameFieldValidator = new FilenameFieldValidator();
|
||||
/** Validator that checks that the reserved symbol "U" is not configured as a classification level. */
|
||||
private FieldValidator<String> classificationLevelIsNotUnclassifiedValidator = new ClassificationLevelIsNotUnclassifiedValidator();
|
||||
|
||||
/**
|
||||
* Validates the provided {@link ClassificationLevel}.
|
||||
*
|
||||
* @param classificationLevel the level to validate.
|
||||
* @throws MissingConfiguration if the level abbreviation is missing.
|
||||
* @throws IllegalConfiguration if the level abbreviation violates the standard restrictions.
|
||||
* @throws IllegalAbbreviationChars if the level abbreviation contains illegal characters.
|
||||
*/
|
||||
@Override
|
||||
public void validate(ClassificationLevel classificationLevel)
|
||||
{
|
||||
final String levelId = classificationLevel.getId();
|
||||
|
||||
lengthFieldValidator.validate(levelId, "level abbreviation");
|
||||
filenameFieldValidator.validate(levelId, "level abbreviation");
|
||||
classificationLevelIsNotUnclassifiedValidator.validate(levelId, "level abbreviation");
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2015 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevelManager;
|
||||
|
||||
/**
|
||||
* A validator that checks that the reserved symbol "U" is not configured as a classification level.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ClassificationLevelIsNotUnclassifiedValidator implements FieldValidator<String>
|
||||
{
|
||||
@Override
|
||||
public void validate(String levelId, String fieldName) throws ClassificationException
|
||||
{
|
||||
if (levelId.equals(ClassificationLevelManager.UNCLASSIFIED_ID))
|
||||
{
|
||||
throw new IllegalConfiguration("The unclassified abbreviation '" +
|
||||
ClassificationLevelManager.UNCLASSIFIED_ID + "' is reserved for system use.");
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,14 +16,15 @@
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.getDuplicateElements;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||
@@ -36,47 +37,8 @@ import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationE
|
||||
*/
|
||||
public class ClassificationLevelValidation
|
||||
{
|
||||
/** The maximum number of characters allowed in a {@link ClassificationLevel#getId() level ID}. */
|
||||
private static final int ABBREVIATION_LENGTH_LIMIT = 10;
|
||||
|
||||
/**
|
||||
* Illegal characters in a {@link ClassificationLevel#getId() level ID}.
|
||||
* Equals to Alfresco's disallowed filename characters.
|
||||
*/
|
||||
// See <constraint name="cm:filename" type="REGEX"> in contentModel.xml
|
||||
static final List<Character> ILLEGAL_ABBREVIATION_CHARS = asList('"', '*', '\\', '>', '<', '?', '/', ':', '|');
|
||||
|
||||
/**
|
||||
* Validates the provided {@link ClassificationLevel}.
|
||||
* @param level the level to validate.
|
||||
* @throws MissingConfiguration if the level abbreviation is missing.
|
||||
* @throws IllegalConfiguration if the level abbreviation violates the standard restrictions.
|
||||
* @throws IllegalAbbreviationChars if the level abbreviation contains illegal characters.
|
||||
*/
|
||||
void validateLevel(ClassificationLevel level)
|
||||
{
|
||||
final String levelId = level.getId();
|
||||
|
||||
if (levelId == null || levelId.equals(""))
|
||||
{
|
||||
throw new MissingConfiguration("Classification level ID is missing.");
|
||||
}
|
||||
else if (levelId.equals(ClassificationLevelManager.UNCLASSIFIED_ID))
|
||||
{
|
||||
throw new IllegalConfiguration("The Unclassified ID abbreviation '" +
|
||||
ClassificationLevelManager.UNCLASSIFIED_ID + "' is reserved for system use.");
|
||||
}
|
||||
else if (levelId.length() > ABBREVIATION_LENGTH_LIMIT)
|
||||
{
|
||||
throw new IllegalConfiguration("Illegal classification level abbreviation. Length " +
|
||||
levelId.length() + " > " + ABBREVIATION_LENGTH_LIMIT);
|
||||
}
|
||||
else if (!getIllegalAbbreviationChars(levelId).isEmpty())
|
||||
{
|
||||
throw new IllegalAbbreviationChars("Illegal character(s) in level abbreviation",
|
||||
getIllegalAbbreviationChars(levelId));
|
||||
}
|
||||
}
|
||||
/** A validator for the fields within the classification level. */
|
||||
private EntityFieldsValidator<ClassificationLevel> classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator();
|
||||
|
||||
/**
|
||||
* Validates the provided {@link ClassificationLevel}s as a whole and individually.
|
||||
@@ -100,17 +62,7 @@ public class ClassificationLevelValidation
|
||||
|
||||
for (ClassificationLevel level : levels)
|
||||
{
|
||||
validateLevel(level);
|
||||
classificationLevelFieldsValidator.validate(level);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Character> getIllegalAbbreviationChars(String s)
|
||||
{
|
||||
final List<Character> result = new ArrayList<>();
|
||||
for (Character c : ILLEGAL_ABBREVIATION_CHARS)
|
||||
{
|
||||
if (s.contains(c.toString())) result.add(c);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2015 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeEntity;
|
||||
|
||||
/**
|
||||
* A validator for all the fields of a classification POJO.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface EntityFieldsValidator<T extends ClassificationSchemeEntity>
|
||||
{
|
||||
/**
|
||||
* Validate the fields of the given POJO.
|
||||
*
|
||||
* @param pojo The object to validate.
|
||||
*/
|
||||
void validate(T pojo);
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2015 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
|
||||
|
||||
/**
|
||||
* Validate a field.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface FieldValidator<T>
|
||||
{
|
||||
void validate(T field, String fieldName) throws ClassificationException;
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2015 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
|
||||
/**
|
||||
* Check that a field is suitable to be used as part of a filename.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 3.0
|
||||
*/
|
||||
public class FilenameFieldValidator implements FieldValidator<String>
|
||||
{
|
||||
/**
|
||||
* Illegal characters in a {@link ClassificationLevel#getId() level ID}.
|
||||
* Equals to Alfresco's disallowed filename characters.
|
||||
*/
|
||||
// See <constraint name="cm:filename" type="REGEX"> in contentModel.xml
|
||||
static final List<Character> ILLEGAL_ABBREVIATION_CHARS = asList('"', '*', '\\', '>', '<', '?', '/', ':', '|');
|
||||
|
||||
private List<Character> getIllegalAbbreviationChars(String s)
|
||||
{
|
||||
final List<Character> result = new ArrayList<>();
|
||||
for (Character c : ILLEGAL_ABBREVIATION_CHARS)
|
||||
{
|
||||
if (s.contains(c.toString())) result.add(c);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(String field, String fieldName) throws ClassificationException
|
||||
{
|
||||
List<Character> illegalAbbreviationChars = getIllegalAbbreviationChars(field);
|
||||
if (!illegalAbbreviationChars.isEmpty())
|
||||
{
|
||||
throw new IllegalAbbreviationChars("Illegal character(s) in " + fieldName, illegalAbbreviationChars);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2015 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||
|
||||
/**
|
||||
* Validate the length of a field.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 3.0
|
||||
*/
|
||||
public class LengthFieldValidator implements FieldValidator<String>
|
||||
{
|
||||
/** The minimum allowed length for the field. */
|
||||
private int minimumLength;
|
||||
/** The maximum allowed length for the field, or {@code null} if there is no maximum length. */
|
||||
private Integer maximumLength = null;
|
||||
|
||||
/**
|
||||
* Create a validator that only checks the minimum length.
|
||||
*
|
||||
* @param minimumLength The length of the shortest allowable field.
|
||||
*/
|
||||
public LengthFieldValidator(int minimumLength)
|
||||
{
|
||||
this.minimumLength = minimumLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a validator that checks the minimum and maximum length.
|
||||
*
|
||||
* @param minimumLength The length of the shortest allowable field.
|
||||
* @param maximumLength The length of the longest allowable field.
|
||||
*/
|
||||
public LengthFieldValidator(int minimumLength, int maximumLength)
|
||||
{
|
||||
if (minimumLength > maximumLength)
|
||||
{
|
||||
throw new IllegalArgumentException("The minimum length may not be larger than the maximum length.");
|
||||
}
|
||||
|
||||
this.minimumLength = minimumLength;
|
||||
this.maximumLength = maximumLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(String field, String fieldName) throws ClassificationException
|
||||
{
|
||||
if (field == null || (field.length() == 0 && minimumLength > 0))
|
||||
{
|
||||
throw new MissingConfiguration(fieldName + " is missing.");
|
||||
}
|
||||
else if (field.length() < minimumLength)
|
||||
{
|
||||
throw new IllegalConfiguration("Illegal " + fieldName + ": Length " + field.length() + " < " + minimumLength);
|
||||
}
|
||||
else if (maximumLength != null && field.length() > maximumLength)
|
||||
{
|
||||
throw new IllegalConfiguration("Illegal " + fieldName + ": Length " + field.length() + " > " + maximumLength);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user