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

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

The 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.model.Entity;

import static com.jaxio.celerio.model.support.SuffixPrefixPredicates.*;

public class EntityPredicates {
    public static Predicate VIEW = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isView();
        }
    };

    public static Predicate VIRTUAL = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isVirtual();
        }
    };

    public static Predicate TABLE = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isTable();
        }
    };

    public static Predicate ROOT = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isRoot();
        }
    };

    public static Predicate ACCOUNT = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isAccount();
        }
    };

    public static Predicate ROLE = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isRole();
        }
    };

    public static Predicate AUDIT_LOG = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isAuditLog();
        }
    };

    public static Predicate SAVED_SEARCH = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isSavedSearch();
        }
    };

    public static Predicate NOT_MANY_TO_MANY_JOIN = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return !entity.isManyToManyJoinEntity();
        }
    };

    public static Predicate SEARCH = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isIndexed() && entity.getHibernateSearchAttributes().isNotEmpty();
        }
    };

    public static Predicate IS_INDEXED = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.isIndexed();
        }
    };

    public static Predicate HAS_FILE = new Predicate() {
        @Override
        public boolean apply(Entity entity) {
            return entity.hasFileAttributes();
        }
    };

    public static class ExcludeEntity implements Predicate {
        private Entity entity;

        public ExcludeEntity(Entity entity) {
            this.entity = entity;
        }

        @Override
        public boolean apply(Entity entity) {
            return this.entity != entity;
        }
    }

    public static class HasFileAttributesPredicate implements Predicate {
        @Override
        public boolean apply(Entity input) {
            boolean contentTypeFound = false;
            boolean nameFound = false;
            boolean sizeFound = false;
            boolean binaryFound = false;

            for (Attribute attribute : input.getCurrentAttributes()) {
                if (attribute.isInPk()) {
                    continue; // We do not support it. Would it make sense anyway?
                }
                if (attribute.isString() && IS_CONTENT_TYPE_SUFFIX.apply(attribute)) {
                    contentTypeFound = true;
                } else if (attribute.isString() && IS_FILE_NAME_SUFFIX.apply(attribute)) {
                    nameFound = true;
                } else if (attribute.isNumeric() && IS_FILE_SIZE_SUFFIX.apply(attribute)) {
                    sizeFound = true;
                } else if (attribute.isBlob() && IS_BINARY_SUFFIX.apply(attribute)) {
                    binaryFound = true;
                } else if (attribute.isBlob()) {
                    // simple isolated blob column found
                    return true;
                }
            }
            return contentTypeFound && nameFound && sizeFound && binaryFound;
        }
    }

    public static Predicate HAS_FILE_ATTRIBUTES = new HasFileAttributesPredicate();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy