<commit_before>/*
 * Copyright (c) 2008-2016 Haulmont.
 *
 * 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.haulmont.cuba.gui.app.core.entityinspector;

import com.haulmont.bali.util.ParamsMap;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.chile.core.model.Range;
import com.haulmont.chile.core.model.utils.InstanceUtils;
import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.entity.Categorized;
import com.haulmont.cuba.core.entity.Category;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.UiComponents;
import com.haulmont.cuba.gui.WindowManager.OpenType;
import com.haulmont.cuba.gui.WindowParam;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.actions.AddAction;
import com.haulmont.cuba.gui.components.actions.BaseAction;
import com.haulmont.cuba.gui.components.actions.ItemTrackingAction;
import com.haulmont.cuba.gui.components.actions.RemoveAction;
import com.haulmont.cuba.gui.data.*;
import com.haulmont.cuba.gui.data.impl.*;
import com.haulmont.cuba.gui.theme.ThemeConstants;
import com.haulmont.cuba.security.entity.EntityAttrAccess;
import com.haulmont.cuba.security.entity.EntityOp;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.inject.Inject;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import java.util.*;

public class EntityInspectorEditor extends AbstractWindow {

    public static final int CAPTION_MAX_LENGTH = 100;
    public static final int MAX_TEXT_LENGTH = 50;

    public static final OpenType OPEN_TYPE = OpenType.THIS_TAB;
    public static final int MAX_TEXTFIELD_STRING_LENGTH = 255;

    @Inject
    protected Metadata metadata;
    @Inject
    protected MessageTools messageTools;
    @Inject
    protected ViewRepository viewRepository;
    @Inject
    protected Security security;
    @Inject
    protected DataSupplier dataSupplier;

    @Inject
    protected BoxLayout buttonsBox;
    @Inject
    protected BoxLayout contentPane;
    @Inject
    protected BoxLayout runtimePane;
    @Inject
    protected TabSheet tablesTabSheet;

    @Inject
    protected UiComponents uiComponents;
    @Inject
    protected Configuration configuration;
    @Inject
    protected ThemeConstants themeConstants;

    @Inject
    protected EntityStates entityStates;

    @WindowParam(name = "item")
    protected Entity item;

    @WindowParam(name = "parent")
    protected Entity parent;

    @WindowParam(name = "parentProperty")
    protected String parentProperty;

    @WindowParam(name = "parentDs")
    protected Datasource parentDs;

    @WindowParam(name = "datasource")
    protected Datasource datasource;

    protected MetaClass meta;
    protected DsContextImpl dsContext;
    protected Map<String, Datasource> datasources;

    protected Boolean isNew;
    protected Boolean autocommit;
    protected Boolean showSystemFields;
    protected Collection<Table> tables;

    protected Collection<Field> reserveLineSeparatorFields;
    protected RuntimePropsDatasource rDS;
    protected CollectionDatasource categoriesDs;

    protected ButtonsPanel buttonsPanel;
    protected Button commitButton;
    protected Button cancelButton;
    protected FieldGroup focusFieldGroup;
    protected String focusFieldId;

    public EntityInspectorEditor() {
        datasources = new HashMap<>();
        tables = new LinkedList<>();
        isNew = true;
        autocommit = true;
        showSystemFields = false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void init(Map<String, Object> params) {
        isNew = item == null || PersistenceHelper.isNew(item);
        meta = item != null ? item.getMetaClass() : metadata.getSession().getClass((String) params.get("metaClass"));
        autocommit = params.get("autocommit") != null ? (Boolean) params.get("autocommit") : true;
        showSystemFields = params.get("showSystemFields") != null ? (Boolean) params.get("showSystemFields") : false;

        if (meta == null) {
            throw new IllegalStateException("Entity or entity's MetaClass must be specified");
        }

        setCaption(meta.getName());
        initShortcuts();

        View view = createView(meta);

        dsContext = new DsContextImpl(dataSupplier);
        dsContext.setFrameContext(getDsContext().getFrameContext());
        setDsContext(dsContext);

        boolean createRequest = item == null || item.getId() == null;
        if (createRequest) {
            item = metadata.create(meta);
            setParentField(item, parentProperty, parent);
        } else {
            //edit request
            Object itemId = item.getId();
            if (!isNew) {
                item = loadSingleItem(meta, itemId, view);
            }
            if (item == null) {
                throw new EntityAccessException(meta, itemId);
            }
        }
        createEmbeddedFields(meta, item);

        boolean categorizedEntity = item instanceof Categorized;

        if (datasource == null) {
            datasource = new DatasourceImpl<>();
            datasource.setup(dsContext, dataSupplier, meta.getName() + "Ds", item.getMetaClass(), view);
            ((DatasourceImpl) datasource).setParent(parentDs);
            ((DatasourceImpl) datasource).valid();
        }

        dsContext.register(datasource);
        createPropertyDatasources(datasource);<commit_msg>Dynamic attributes issue in the EntityInspector #2112<commit_after>/*
 * Copyright (c) 2008-2016 Haulmont.
 *
 * 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.haulmont.cuba.gui.app.core.entityinspector;

import com.haulmont.bali.util.ParamsMap;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.chile.core.model.Range;
import com.haulmont.chile.core.model.utils.InstanceUtils;
import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.entity.BaseGenericIdEntity;
import com.haulmont.cuba.core.entity.Categorized;
import com.haulmont.cuba.core.entity.Category;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.UiComponents;
import com.haulmont.cuba.gui.WindowManager.OpenType;
import com.haulmont.cuba.gui.WindowParam;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.actions.AddAction;
import com.haulmont.cuba.gui.components.actions.BaseAction;
import com.haulmont.cuba.gui.components.actions.ItemTrackingAction;
import com.haulmont.cuba.gui.components.actions.RemoveAction;
import com.haulmont.cuba.gui.data.*;
import com.haulmont.cuba.gui.data.impl.*;
import com.haulmont.cuba.gui.theme.ThemeConstants;
import com.haulmont.cuba.security.entity.EntityAttrAccess;
import com.haulmont.cuba.security.entity.EntityOp;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.inject.Inject;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import java.util.*;

public class EntityInspectorEditor extends AbstractWindow {

    public static final int CAPTION_MAX_LENGTH = 100;
    public static final int MAX_TEXT_LENGTH = 50;

    public static final OpenType OPEN_TYPE = OpenType.THIS_TAB;
    public static final int MAX_TEXTFIELD_STRING_LENGTH = 255;

    @Inject
    protected Metadata metadata;
    @Inject
    protected MessageTools messageTools;
    @Inject
    protected ViewRepository viewRepository;
    @Inject
    protected Security security;
    @Inject
    protected DataSupplier dataSupplier;

    @Inject
    protected BoxLayout buttonsBox;
    @Inject
    protected BoxLayout contentPane;
    @Inject
    protected BoxLayout runtimePane;
    @Inject
    protected TabSheet tablesTabSheet;

    @Inject
    protected UiComponents uiComponents;
    @Inject
    protected Configuration configuration;
    @Inject
    protected ThemeConstants themeConstants;

    @Inject
    protected EntityStates entityStates;

    @WindowParam(name = "item")
    protected Entity item;

    @WindowParam(name = "parent")
    protected Entity parent;

    @WindowParam(name = "parentProperty")
    protected String parentProperty;

    @WindowParam(name = "parentDs")
    protected Datasource parentDs;

    @WindowParam(name = "datasource")
    protected Datasource datasource;

    protected MetaClass meta;
    protected DsContextImpl dsContext;
    protected Map<String, Datasource> datasources;

    protected Boolean isNew;
    protected Boolean autocommit;
    protected Boolean showSystemFields;
    protected Collection<Table> tables;

    protected Collection<Field> reserveLineSeparatorFields;
    protected RuntimePropsDatasource rDS;
    protected CollectionDatasource categoriesDs;

    protected ButtonsPanel buttonsPanel;
    protected Button commitButton;
    protected Button cancelButton;
    protected FieldGroup focusFieldGroup;
    protected String focusFieldId;

    public EntityInspectorEditor() {
        datasources = new HashMap<>();
        tables = new LinkedList<>();
        isNew = true;
        autocommit = true;
        showSystemFields = false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void init(Map<String, Object> params) {
        isNew = item == null || PersistenceHelper.isNew(item);
        meta = item != null ? item.getMetaClass() : metadata.getSession().getClass((String) params.get("metaClass"));
        autocommit = params.get("autocommit") != null ? (Boolean) params.get("autocommit") : true;
        showSystemFields = params.get("showSystemFields") != null ? (Boolean) params.get("showSystemFields") : false;

        if (meta == null) {
            throw new IllegalStateException("Entity or entity's MetaClass must be specified");
        }

        setCaption(meta.getName());
        initShortcuts();

        View view = createView(meta);

        dsContext = new DsContextImpl(dataSupplier);
        dsContext.setFrameContext(getDsContext().getFrameContext());
        setDsContext(dsContext);

        boolean createRequest = item == null || item.getId() == null;
        if (createRequest) {
            item = metadata.create(meta);
            setParentField(item, parentProperty, parent);
            if (item instanceof BaseGenericIdEntity) {
                ((BaseGenericIdEntity) item).setDynamicAttributes(new HashMap<>());
            }
        } else {
            //edit request
            Object itemId = item.getId();
            if (!isNew) {
                item = loadSingleItem(meta, itemId, view);
            }
            if (item == null) {
                throw new EntityAccessException(meta, itemId);
            }
        }
        createEmbeddedFields(meta, item);

        boolean categorizedEntity = item instanceof Categorized;

        if (datasource == null) {
            datasource = new DatasourceImpl<>();
            datasource.setup(dsContext, dataSupplier, meta.getName() + "Ds", item.getMetaClass(), view);
            ((DatasourceImpl) datasource).setParent(parentDs);
            ((DatasourceImpl) datasource).valid();
        }

        dsContext.register(datasource);
        createPropertyDatasources(datasource);
{'fraction_non_alphanumeric': 0.0939937283910911, 'fraction_numerical': 0.0038594516362466832, 'mean_word_length': 3.4516821760916248, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2409551', 'n_tokens_mistral': 3531, 'n_tokens_neox': 3386, 'n_words': 852}