<commit_before>/*
 * Copyright 2003 - 2013 The eFaps Team
 *
 * 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.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

package org.efaps.update.schema.datamodel;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.efaps.admin.datamodel.AttributeSet;
import org.efaps.admin.datamodel.Classification;
import org.efaps.admin.datamodel.Type;
import org.efaps.admin.event.EventType;
import org.efaps.ci.CIAdminCommon;
import org.efaps.ci.CIAdminDataModel;
import org.efaps.ci.CIType;
import org.efaps.db.Delete;
import org.efaps.db.Insert;
import org.efaps.db.Instance;
import org.efaps.db.InstanceQuery;
import org.efaps.db.MultiPrintQuery;
import org.efaps.db.QueryBuilder;
import org.efaps.db.Update;
import org.efaps.update.AbstractUpdate;
import org.efaps.update.LinkInstance;
import org.efaps.update.UpdateLifecycle;
import org.efaps.update.event.Event;
import org.efaps.update.util.InstallationException;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handles the import / update of types for eFaps read from a XML configuration
 * item file.
 *
 * @author The eFaps Team
 * @version $Id$
 */
public class TypeUpdate
    extends AbstractUpdate
{

    /**
     * Logging instance used to give logging information of this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(TypeUpdate.class);

    /**
     * Link the data model type to allowed event types.
     */
    private static final Link LINK2ALLOWEDEVENT = new Link("Admin_DataModel_TypeEventIsAllowedFor", "To",
                                                           "Admin_DataModel_Type", "From");

    /**
     * Link the data model type to a store.
     */
    private static final Link LINK2STORE = new Link("Admin_DataModel_Type2Store", "From", "DB_Store", "To");

    /**
     * Link the data model type to a type that it classifies.
     */
    private static final Link LINK2CLASSIFIES = new Link("Admin_DataModel_TypeClassifies", "From",
                                                         "Admin_DataModel_Type", "To");

    /**
     * Link the data model type to a tyep used for classification.
     */
    private static final Link LINK2CLASSIFYREL = new Link("Admin_DataModel_TypeClassifyRelation", "From",
                                                          "Admin_DataModel_Type", "To");

    /**
     * Link the data model type to a tyep used for classification.
     */
    private static final Link LINK2CLASSIFYCOMPANY = new Link("Admin_DataModel_TypeClassifyCompany", "From",
                                                          "Admin_User_Company", "To");

    /**
     * List of all links for the type.
     */
    private static final Set<Link> ALLLINKS = new HashSet<Link>();
    static {
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2ALLOWEDEVENT);
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2STORE);
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2CLASSIFIES);
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2CLASSIFYREL);
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2CLASSIFYCOMPANY);
    }

    /**
     * Default constructor to initialize this type instance for given
     * <code>_url</code>.
     *
     * @param _url URL of the file
     */
    public TypeUpdate(final URL _url)
    {
        super(_url, "Admin_DataModel_Type", TypeUpdate.ALLLINKS);
    }

    /**
     * Creates new instance of class {@link TypeDefinition}.
     *
     * @return new definition instance
     * @see TypeDefinition
     */
    @Override
    protected AbstractDefinition newDefinition()
    {
        return new TypeDefinition();
    }

    /**
     * The class defines an attribute of a type.
     */
    public class AttributeDefinition
        extends AbstractDefinition
    {

        /** Name of the attribute. */
        private String name = null;

        /** Name of the Attribute Type of the attribute. */
        private String type = null;

        /** Name of the SQL Table of the attribute. */
        private String sqlTable = null;

        /** SQL Column of the attribute. */
        private String sqlColumn = null;

        /** Name of the Linked Type (used for links to another type). */
        private String typeLink = null;

        /** Events for this Attribute. */
        private final List<Event> events = new ArrayList<Event>();

        /** default value for this Attribute. */
        private String defaultValue = null;

        /**
         * UUID of the Dimension for this Attribute. (Used in conjunction with
         * e.g. IntegerWithUoM).
         */
        private String dimensionUUID;
        /**
         * ClassName for this Attribute. (Used in conjunction with
         * e.g. EnumType and BitEnumType).
         */
        private String className;

        /**
         * @see org.efaps.update.AbstractUpdate.AbstractDefinition#readXML(java.util.List,
         *      java.util.Map, java.lang.String)
         * @param _tags list of the tags
         * @param _attributes attributes
         * @param _text text
         * @throws EFapsException on error
         */
        @Override
        protected void readXML(final List<String> _tags,
                               final Map<String, String> _attributes,
                               final String _text)
            throws EFapsException
        {

            final String value = _tags.get(0);
            if ("className".equals(value)) {
                this.className = _text;
            } if ("defaultvalue".equals(value)) {
                this.defaultValue = _text;
            } else if ("dimensionUUID".equals(value)) {
                this.dimensionUUID = _text;
            } else if ("name".equals(value)) {
                this.name = _text;
            } else if ("sqlcolumn".equals(value)) {
                this.sqlColumn = _text;<commit_msg>kernel: bugFix, dupplicated if<commit_after>/*
 * Copyright 2003 - 2013 The eFaps Team
 *
 * 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.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

package org.efaps.update.schema.datamodel;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.efaps.admin.datamodel.AttributeSet;
import org.efaps.admin.datamodel.Classification;
import org.efaps.admin.datamodel.Type;
import org.efaps.admin.event.EventType;
import org.efaps.ci.CIAdminCommon;
import org.efaps.ci.CIAdminDataModel;
import org.efaps.ci.CIType;
import org.efaps.db.Delete;
import org.efaps.db.Insert;
import org.efaps.db.Instance;
import org.efaps.db.InstanceQuery;
import org.efaps.db.MultiPrintQuery;
import org.efaps.db.QueryBuilder;
import org.efaps.db.Update;
import org.efaps.update.AbstractUpdate;
import org.efaps.update.LinkInstance;
import org.efaps.update.UpdateLifecycle;
import org.efaps.update.event.Event;
import org.efaps.update.util.InstallationException;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handles the import / update of types for eFaps read from a XML configuration
 * item file.
 *
 * @author The eFaps Team
 * @version $Id$
 */
public class TypeUpdate
    extends AbstractUpdate
{

    /**
     * Logging instance used to give logging information of this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(TypeUpdate.class);

    /**
     * Link the data model type to allowed event types.
     */
    private static final Link LINK2ALLOWEDEVENT = new Link("Admin_DataModel_TypeEventIsAllowedFor", "To",
                                                           "Admin_DataModel_Type", "From");

    /**
     * Link the data model type to a store.
     */
    private static final Link LINK2STORE = new Link("Admin_DataModel_Type2Store", "From", "DB_Store", "To");

    /**
     * Link the data model type to a type that it classifies.
     */
    private static final Link LINK2CLASSIFIES = new Link("Admin_DataModel_TypeClassifies", "From",
                                                         "Admin_DataModel_Type", "To");

    /**
     * Link the data model type to a tyep used for classification.
     */
    private static final Link LINK2CLASSIFYREL = new Link("Admin_DataModel_TypeClassifyRelation", "From",
                                                          "Admin_DataModel_Type", "To");

    /**
     * Link the data model type to a tyep used for classification.
     */
    private static final Link LINK2CLASSIFYCOMPANY = new Link("Admin_DataModel_TypeClassifyCompany", "From",
                                                          "Admin_User_Company", "To");

    /**
     * List of all links for the type.
     */
    private static final Set<Link> ALLLINKS = new HashSet<Link>();
    static {
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2ALLOWEDEVENT);
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2STORE);
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2CLASSIFIES);
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2CLASSIFYREL);
        TypeUpdate.ALLLINKS.add(TypeUpdate.LINK2CLASSIFYCOMPANY);
    }

    /**
     * Default constructor to initialize this type instance for given
     * <code>_url</code>.
     *
     * @param _url URL of the file
     */
    public TypeUpdate(final URL _url)
    {
        super(_url, "Admin_DataModel_Type", TypeUpdate.ALLLINKS);
    }

    /**
     * Creates new instance of class {@link TypeDefinition}.
     *
     * @return new definition instance
     * @see TypeDefinition
     */
    @Override
    protected AbstractDefinition newDefinition()
    {
        return new TypeDefinition();
    }

    /**
     * The class defines an attribute of a type.
     */
    public class AttributeDefinition
        extends AbstractDefinition
    {

        /** Name of the attribute. */
        private String name = null;

        /** Name of the Attribute Type of the attribute. */
        private String type = null;

        /** Name of the SQL Table of the attribute. */
        private String sqlTable = null;

        /** SQL Column of the attribute. */
        private String sqlColumn = null;

        /** Name of the Linked Type (used for links to another type). */
        private String typeLink = null;

        /** Events for this Attribute. */
        private final List<Event> events = new ArrayList<Event>();

        /** default value for this Attribute. */
        private String defaultValue = null;

        /**
         * UUID of the Dimension for this Attribute. (Used in conjunction with
         * e.g. IntegerWithUoM).
         */
        private String dimensionUUID;
        /**
         * ClassName for this Attribute. (Used in conjunction with
         * e.g. EnumType and BitEnumType).
         */
        private String className;

        /**
         * @see org.efaps.update.AbstractUpdate.AbstractDefinition#readXML(java.util.List,
         *      java.util.Map, java.lang.String)
         * @param _tags list of the tags
         * @param _attributes attributes
         * @param _text text
         * @throws EFapsException on error
         */
        @Override
        protected void readXML(final List<String> _tags,
                               final Map<String, String> _attributes,
                               final String _text)
            throws EFapsException
        {

            final String value = _tags.get(0);
            if ("className".equals(value)) {
                this.className = _text;
            } else if ("defaultvalue".equals(value)) {
                this.defaultValue = _text;
            } else if ("dimensionUUID".equals(value)) {
                this.dimensionUUID = _text;
            } else if ("name".equals(value)) {
                this.name = _text;
            } else if ("sqlcolumn".equals(value)) {
                this.sqlColumn = _text;
{'fraction_non_alphanumeric': 0.09438814097715928, 'fraction_numerical': 0.004084102253819392, 'mean_word_length': 2.2803274621681964, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1371913', 'n_tokens_mistral': 3629, 'n_tokens_neox': 3561, 'n_words': 1083}