<commit_before>/*
 * Copyright (c) 2011 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */

package com.haulmont.cuba.gui.data.impl;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.cuba.core.entity.*;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.core.sys.SetValueEntity;
import com.haulmont.cuba.gui.data.*;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;

import javax.annotation.Nullable;
import java.util.*;

/**
 * Specific datasource for runtime properties.
 * It will be initialized only when main datasource will be valid.
 *
 * @author devyatkin
 * @version $Id$
 */
public class RuntimePropsDatasourceImpl
        extends AbstractDatasource<RuntimePropertiesEntity>
        implements RuntimePropsDatasource<RuntimePropertiesEntity> {

    private DsContext dsContext;
    private DataSupplier dataSupplier;
    private MetaClass metaClass;
    private View view;
    private Datasource mainDs;
    private boolean inittedBefore = false;
    private boolean categoryChanged = false;

    protected State state = State.NOT_INITIALIZED;

    private RuntimePropertiesEntity item;

    public RuntimePropsDatasourceImpl(DsContext dsContext, DataSupplier dataSupplier, String id, String mainDsId) {
        this.id = id;
        this.dsContext = dsContext;
        this.dataSupplier = dataSupplier;
        this.metaClass = new RuntimePropertiesMetaClass();
        this.setMainDs(mainDsId);

        this.setCommitMode(CommitMode.DATASTORE);

        mainDs.addListener(new DsListenerAdapter() {
            public void valueChanged(Entity source, String property, Object prevValue, Object value) {
                if (property.equals("category")) {
                    categoryChanged=true;
                    initMetaClass();
                }
            }
        });
    }

    @Override
    public void setup(DsContext dsContext, DataSupplier dataSupplier, String id,
                      MetaClass metaClass, @Nullable View view) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    protected void initMetaClass() {
        for (MetaProperty property : metaClass.getProperties()) {
            itemToCreate.clear();
            itemToUpdate.clear();
            itemToDelete.add(item.getCategoryValue(property.getName()));
        }
        metaClass.getProperties().clear();

        CategorizedEntity entity = (CategorizedEntity) mainDs.getItem();

        if (!inittedBefore && PersistenceHelper.isNew(entity) && (entity.getCategory() == null)) {
            entity.setCategory(getDefaultCategory(entity));
        }

        LoadContext valuesContext = new LoadContext(CategoryAttributeValue.class);
        LoadContext.Query query = valuesContext.setQueryString("select a from sys$CategoryAttributeValue a" +
                ",sys$CategoryAttribute atr where a.entityId =:e and a.categoryAttribute=atr and atr.category.id=:cat ");
        query.addParameter("e", entity.getUuid());
        query.addParameter("cat", entity.getCategory());
        valuesContext.setView("categoryAttributeValue");
        List<CategoryAttributeValue> entityValues = dataSupplier.loadList(valuesContext);

        LoadContext attributesContext = new LoadContext(CategoryAttribute.class);
        LoadContext.Query attributeQuery = attributesContext.setQueryString("select a from sys$CategoryAttribute a " +
                "where a.category.id=:cat order by a.orderNo");
        attributeQuery.addParameter("cat", entity.getCategory());
        valuesContext.setView("_local");
        List<CategoryAttribute> attributes = dataSupplier.loadList(attributesContext);<commit_msg>Runtime properties doesn't work -  fix view #PL-2009<commit_after>/*
 * Copyright (c) 2011 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */

package com.haulmont.cuba.gui.data.impl;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.cuba.core.entity.*;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.core.sys.SetValueEntity;
import com.haulmont.cuba.gui.data.*;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;

import javax.annotation.Nullable;
import java.util.*;

/**
 * Specific datasource for runtime properties.
 * It will be initialized only when main datasource will be valid.
 *
 * @author devyatkin
 * @version $Id$
 */
public class RuntimePropsDatasourceImpl
        extends AbstractDatasource<RuntimePropertiesEntity>
        implements RuntimePropsDatasource<RuntimePropertiesEntity> {

    private DsContext dsContext;
    private DataSupplier dataSupplier;
    private MetaClass metaClass;
    private View view;
    private Datasource mainDs;
    private boolean inittedBefore = false;
    private boolean categoryChanged = false;

    protected State state = State.NOT_INITIALIZED;

    private RuntimePropertiesEntity item;

    public RuntimePropsDatasourceImpl(DsContext dsContext, DataSupplier dataSupplier, String id, String mainDsId) {
        this.id = id;
        this.dsContext = dsContext;
        this.dataSupplier = dataSupplier;
        this.metaClass = new RuntimePropertiesMetaClass();
        this.setMainDs(mainDsId);

        this.setCommitMode(CommitMode.DATASTORE);

        mainDs.addListener(new DsListenerAdapter() {
            public void valueChanged(Entity source, String property, Object prevValue, Object value) {
                if (property.equals("category")) {
                    categoryChanged=true;
                    initMetaClass();
                }
            }
        });
    }

    @Override
    public void setup(DsContext dsContext, DataSupplier dataSupplier, String id,
                      MetaClass metaClass, @Nullable View view) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    protected void initMetaClass() {
        for (MetaProperty property : metaClass.getProperties()) {
            itemToCreate.clear();
            itemToUpdate.clear();
            itemToDelete.add(item.getCategoryValue(property.getName()));
        }
        metaClass.getProperties().clear();

        CategorizedEntity entity = (CategorizedEntity) mainDs.getItem();

        if (!inittedBefore && PersistenceHelper.isNew(entity) && (entity.getCategory() == null)) {
            entity.setCategory(getDefaultCategory(entity));
        }

        LoadContext valuesContext = new LoadContext(CategoryAttributeValue.class);
        LoadContext.Query query = valuesContext.setQueryString("select a from sys$CategoryAttributeValue a" +
                ",sys$CategoryAttribute atr where a.entityId =:e and a.categoryAttribute=atr and atr.category.id=:cat ");
        query.addParameter("e", entity.getUuid());
        query.addParameter("cat", entity.getCategory());
        valuesContext.setView("categoryAttributeValue");
        List<CategoryAttributeValue> entityValues = dataSupplier.loadList(valuesContext);

        LoadContext attributesContext = new LoadContext(CategoryAttribute.class);
        LoadContext.Query attributeQuery = attributesContext.setQueryString("select a from sys$CategoryAttribute a " +
                "where a.category.id=:cat order by a.orderNo");
        attributeQuery.addParameter("cat", entity.getCategory());
        attributesContext.setView(View.LOCAL);
        List<CategoryAttribute> attributes = dataSupplier.loadList(attributesContext);
{'fraction_non_alphanumeric': 0.09293007625694713, 'fraction_numerical': 0.0015509887553315238, 'mean_word_length': 3.347191011235955, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5834765', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1975, 'n_words': 457}