jorgediazest.missingrefchecker.ref.Reference.java Source code

Java tutorial

Introduction

Here is the source code for jorgediazest.missingrefchecker.ref.Reference.java

Source

/**
 * Copyright (c) 2015-present Jorge Daz All rights reserved.
 *
 * This library 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 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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.
 */

package jorgediazest.missingrefchecker.ref;

import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jorgediazest.missingrefchecker.chk.DLStoreReferenceChecker;
import jorgediazest.missingrefchecker.chk.DefaultLanguageReferenceChecker;
import jorgediazest.missingrefchecker.chk.DirectAttributeReferenceChecker;
import jorgediazest.missingrefchecker.chk.EntityNameReferenceChecker;
import jorgediazest.missingrefchecker.chk.NullReferenceChecker;
import jorgediazest.missingrefchecker.chk.ReferenceChecker;

import jorgediazest.util.model.Model;
import jorgediazest.util.model.TableInfo;
import jorgediazest.util.modelquery.DatabaseUtil;
import jorgediazest.util.modelquery.ModelQuery;
import jorgediazest.util.modelquery.ModelQueryFactory;

/**
 * @author Jorge Daz
 */
public class Reference implements Comparable<Reference>, Cloneable {

    public static Map<String, ReferenceChecker> referenceCheckerMap = new HashMap<String, ReferenceChecker>();

    static {
        referenceCheckerMap.put("DefaultLanguage", new DefaultLanguageReferenceChecker());
        referenceCheckerMap.put("DirectAttribute", new DirectAttributeReferenceChecker());
        referenceCheckerMap.put("DLStore", new DLStoreReferenceChecker());
        referenceCheckerMap.put("EntityName", new EntityNameReferenceChecker());
        referenceCheckerMap.put(StringPool.BLANK, new NullReferenceChecker());
    }

    public static String getAttributesWithTypes(Model model, String attributes) {

        String allAttrs = StringPool.BLANK;

        for (String attr : attributes.split(",")) {
            int type = model.getAttributeType(attr);

            if (type == 0) {
                TableInfo tableInfo = model.getTableInfo(attr);

                if (tableInfo != null) {
                    type = tableInfo.getAttributeType(attr);
                    attr = tableInfo.getName() + "." + attr;
                }
            }

            String typeStr = DatabaseUtil.getJdbcTypeNames().get(type);

            if (Validator.isNotNull(allAttrs)) {
                allAttrs += StringPool.COMMA;
            }

            allAttrs = allAttrs + attr + "(" + typeStr + ")";
        }

        return allAttrs;
    }

    public Reference(ModelQueryFactory queryFactory, ModelQuery originModel, String originAttrs,
            ReferenceChecker referenceChecker, ModelQuery destinationModel, String destinationAttrs) {

        this.queryFactory = queryFactory;
        this.originAttributes = originAttrs;
        this.originModel = originModel;
        this.destinationAttributes = destinationAttrs;
        this.destinationModel = destinationModel;
        this.referenceChecker = referenceChecker;

        if (Validator.isNull(this.destinationAttributes)) {
            this.destinationAttributes = StringPool.BLANK;
        }
    }

    public Reference clone() {
        return new Reference(this.queryFactory, this.originModel, this.originAttributes, this.referenceChecker,
                this.destinationModel, this.destinationAttributes);
    }

    public int compareTo(Reference ref) {
        return getOrigin().compareTo(ref.getOrigin());
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof Reference)) {
            return false;
        }

        Reference ref = (Reference) obj;
        return getOrigin().equals(ref.getOrigin());
    }

    public String gerOriginAttributesWithTypes() {
        return Reference.getAttributesWithTypes(originModel.getModel(), originAttributes);
    }

    public String getDestination() {

        if (destinationModel == null) {
            return destinationAttributes;
        }

        if (destination == null) {
            destination = destinationModel.getModel().getName() + "#" + destinationAttributes;
        }

        return destination;
    }

    public String getDestinationAttributes() {
        return destinationAttributes;
    }

    public ModelQuery getDestinationModelQuery() {
        return destinationModel;
    }

    public String getDestinationWithTypes() {

        if (destinationModel == null) {
            return destinationAttributes;
        }

        return destinationModel.getModel().getName() + "#"
                + Reference.getAttributesWithTypes(destinationModel.getModel(), destinationAttributes);
    }

    public Collection<String> getMissingReferences(ModelQueryFactory queryFactory, boolean ignoreNullValues)
            throws Exception {

        return referenceChecker.getMissingReferences(this, ignoreNullValues);
    }

    public ModelQueryFactory getModelQueryFactory() {
        return this.queryFactory;
    }

    public String getOrigin() {

        if (origin == null) {
            origin = originModel.getModel().getName() + "#" + originAttributes;
        }

        return origin;
    }

    public String getOriginAttributes() {
        return originAttributes;
    }

    public List<String> getOriginAttributesList() {
        return Arrays.asList(originAttributes.split(","));
    }

    public ModelQuery getOriginModelQuery() {
        return originModel;
    }

    public String getReferenceInfoWithTypes() {

        String allAttrsOrigin = gerOriginAttributesWithTypes();

        if (Validator.isNotNull(destinationAttributes) || (destinationModel != null)) {

            return allAttrsOrigin + " => " + getType() + ": " + getDestinationWithTypes();
        }

        return getOrigin() + " => " + getType();
    }

    public String getType() {
        if (Validator.isNull(referenceChecker)) {
            return StringPool.BLANK;
        }

        String simpleName = referenceChecker.getClass().getSimpleName();
        return simpleName.replace("ReferenceChecker", StringPool.BLANK);
    }

    public int hashCode() {
        return getOrigin().hashCode();
    }

    public String toString() {

        if (Validator.isNotNull(destinationAttributes)) {
            return getOrigin() + " => " + getType() + ": " + getDestination();
        }

        return getOrigin() + " => " + getType();
    }

    protected String destination = null;
    protected String destinationAttributes = null;
    protected ModelQuery destinationModel = null;
    protected String origin = null;
    protected String originAttributes = null;
    protected ModelQuery originModel = null;
    protected ModelQueryFactory queryFactory = null;
    protected ReferenceChecker referenceChecker = null;

}