All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jaxio.celerio.model.support.SuffixPrefixPredicates Maven / Gradle / Ivy

There is a newer version: 4.0.23
Show newest version
/*
 * Copyright 2015 JAXIO http://www.jaxio.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jaxio.celerio.model.support;

import com.google.common.base.Predicate;
import com.jaxio.celerio.model.Attribute;
import com.jaxio.celerio.util.MiscUtil;

import java.util.List;
import java.util.Locale;

import static com.google.common.base.Predicates.and;
import static com.google.common.collect.Lists.newArrayList;
import static com.jaxio.celerio.model.support.AttributePredicates.*;

public class SuffixPrefixPredicates {

    private static abstract class PatternPredicate implements Predicate {
        private Iterable patterns;

        public PatternPredicate(Iterable patterns) {
            this.patterns = patterns;
        }

        public Iterable getPatterns() {
            return patterns;
        }

        @Override
        public abstract boolean apply(Attribute input);
    }

    public static class HasSuffixPredicate extends PatternPredicate {
        public HasSuffixPredicate(Iterable patterns) {
            super(patterns);
        }

        @Override
        public boolean apply(Attribute input) {
            if (MiscUtil.endsWithIgnoreCase(input.getColumnConfig().getFieldName(), getPatterns())) {
                return true;
            }
            return false;
        }
    }

    public static class ColumnNameContainsPredicate extends PatternPredicate {
        public ColumnNameContainsPredicate(Iterable patterns) {
            super(patterns);
        }

        @Override
        public boolean apply(Attribute input) {
            if (MiscUtil.contains(input.getColumnName(), getPatterns())) {
                return true;
            }
            return false;
        }
    }

    public static class ColumnNameEqualsPredicate implements Predicate {
        private Iterable matches;

        public ColumnNameEqualsPredicate(Iterable matches) {
            this.matches = matches;
        }

        @Override
        public boolean apply(Attribute input) {
            for (String match : matches) {
                if (input.getVar().equalsIgnoreCase(match)) {
                    return true;
                }
            }
            return false;
        }
    }

    public static class NameEqualsPredicate extends PatternPredicate {
        public NameEqualsPredicate(Iterable patterns) {
            super(patterns);
        }

        @Override
        public boolean apply(Attribute input) {
            return MiscUtil.equalsIgnoreCase(input.getColumnConfig().getFieldName(), getPatterns());
        }
    }

    public static class HasPrefixPredicate extends PatternPredicate {
        public HasPrefixPredicate(Iterable patterns) {
            super(patterns);
        }

        @Override
        public boolean apply(Attribute input) {
            return MiscUtil.startsWithIgnoreCase(input.getColumnConfig().getFieldName(), getPatterns());
        }
    }

    public static class HasFileAttributesPredicate implements Predicate {
        @Override
        public boolean apply(Attribute input) {
            return input.getEntity().hasFileAttributes();
        }
    }

    public static class AttributeShareSameSuffix implements Predicate {
        private Attribute attribute;

        public AttributeShareSameSuffix(Attribute attribute) {
            this.attribute = attribute;
        }

        @Override
        public boolean apply(Attribute input) {
            if (!input.isInFileDefinition()) {
                return false;
            }
            String currentFieldName = input.getColumnConfig().getFieldName().toUpperCase();
            for (String suffixPattern : getSuffixes(input)) {
                String patternUpper = suffixPattern.toUpperCase();
                if (currentFieldName.equals(patternUpper) || currentFieldName.endsWith(patternUpper)) {
                    String currentFieldNameWithoutPrefix = currentFieldName.substring(0, currentFieldName.length() - patternUpper.length());
                    if (attribute.getColumnConfig().getFieldName().toUpperCase().startsWith(currentFieldNameWithoutPrefix)) {
                        return true;
                    }
                }
            }

            return false;
        }

        private Iterable getSuffixes(Attribute input) {
            if (input.isFileSize()) {
                return FILE_SIZE_SUFFIX;
            } else if (input.isFile()) {
                return BINARY_SUFFIX;
            } else if (input.isFilename()) {
                return FILE_NAME_SUFFIX;
            } else if (input.isContentType()) {
                return CONTENT_TYPE_SUFFIX;
            } else {
                return newArrayList();
            }
        }
    }

    // conventions
    public static List BOOLEAN_PREFIX = newArrayList("is", "has", "use", "can", "est");
    public static List VERSION_SUFFIX = newArrayList("version");
    public static List LOCALE_SUFFIX = newArrayList("locale");
    public static List LANGUAGE_SUFFIX = buildLanguagesSuffixes();
    public static List EMAIL_SUFFIX = newArrayList("email", "emailaddress", "mail");
    public static List PASSWORD_SUFFIX = newArrayList("password", "pwd", "passwd", "motdepasse");
    public static List BINARY_SUFFIX = newArrayList("binary", "content", "file", "picture");
    public static List CONTENT_TYPE_SUFFIX = newArrayList("contenttype");
    public static List FILE_NAME_SUFFIX = newArrayList("filename");
    public static List FILE_SIZE_SUFFIX = newArrayList("size", "length", "contentlength");
    public static List ROLE_SUFFIX = newArrayList("authority", "nameLocale", "roleName", "role");
    public static List URL_SUFFIX = newArrayList("url");
    public static List HTML_FRAGMENT = newArrayList("_html");
    public static List LABELS = newArrayList("libelle", "label", "title", "titre", "firstname", "prenom", "lastname", "name", "nom");

    // prefix
    public static Predicate IS_BOOLEAN_PREFIX = new HasPrefixPredicate(BOOLEAN_PREFIX);

    // suffix
    public static Predicate IS_VERSION_SUFFIX = new HasSuffixPredicate(VERSION_SUFFIX);
    public static Predicate IS_LOCALE_SUFFIX = new HasSuffixPredicate(LOCALE_SUFFIX);
    public static Predicate IS_LANGUAGE_SUFFIX = new HasSuffixPredicate(LANGUAGE_SUFFIX);
    public static Predicate IS_EMAIL_SUFFIX = new HasSuffixPredicate(EMAIL_SUFFIX);
    public static Predicate IS_PASSWORD_SUFFIX = new HasSuffixPredicate(PASSWORD_SUFFIX);
    public static Predicate IS_BINARY_SUFFIX = new HasSuffixPredicate(BINARY_SUFFIX);
    public static Predicate IS_CONTENT_TYPE_SUFFIX = new HasSuffixPredicate(CONTENT_TYPE_SUFFIX);
    public static Predicate IS_FILE_NAME_SUFFIX = new HasSuffixPredicate(FILE_NAME_SUFFIX);
    public static Predicate IS_FILE_SIZE_SUFFIX = new HasSuffixPredicate(FILE_SIZE_SUFFIX);
    public static Predicate IS_ROLE_SUFFIX = new HasSuffixPredicate(ROLE_SUFFIX);
    public static Predicate IS_URL_SUFFIX = new HasSuffixPredicate(URL_SUFFIX);
    public static Predicate HAS_FILE_ATTRIBUTES = new HasFileAttributesPredicate();

    // contains
    public static Predicate CONTAINS_HTML = new ColumnNameContainsPredicate(HTML_FRAGMENT);

    // equals
    public static Predicate IS_LABEL = and(STRING, new ColumnNameEqualsPredicate(LABELS));

    // derived
    public static Predicate IS_FILE_SIZE = and(NUMERIC, and(IS_FILE_SIZE_SUFFIX, HAS_FILE_ATTRIBUTES));
    public static Predicate IS_FILE_NAME = and(STRING, and(IS_FILE_NAME_SUFFIX, HAS_FILE_ATTRIBUTES));
    public static Predicate IS_FILE_BINARY = and(BLOB, and(IS_BINARY_SUFFIX, HAS_FILE_ATTRIBUTES));
    public static Predicate IS_FILE_CONTENT_TYPE = and(STRING, and(IS_CONTENT_TYPE_SUFFIX, HAS_FILE_ATTRIBUTES));
    public static Predicate IS_EMAIL = and(STRING, IS_EMAIL_SUFFIX);
    public static Predicate IS_PASSWORD = and(STRING, IS_PASSWORD_SUFFIX);

    private static List buildLanguagesSuffixes() {
        List ret = newArrayList();
        for (Locale locale : Locale.getAvailableLocales()) {
            ret.add("_" + locale.getLanguage());
        }
        return ret;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy