<commit_before>/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */
package com.haulmont.cuba.core.global;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.core.entity.Entity;

import java.io.Serializable;
import java.util.*;

/**
 * Class that defines parameters for loading entities from the database via {@link DataManager}.
 * <p>Typical usage:
 * <pre>
    LoadContext&lt;User&gt; context = LoadContext.create(User.class).setQuery(
            LoadContext.createQuery("select u from sec$User u where u.login like :login")
                    .setParameter("login", "a%")
                    .setMaxResults(10))
            .setView("user.browse");
    List&lt;User&gt; users = dataManager.loadList(context);
 * </pre>
 *
 * @author krivopustov
 * @version $Id$
 */
public class LoadContext<E extends Entity> implements Serializable {

    private static final long serialVersionUID = -8808320502197308698L;

    protected String metaClass;
    protected Query query;
    protected View view;
    protected Object id;
    protected boolean softDeletion = true;
    protected List<Query> prevQueries = new ArrayList<>();
    protected int queryKey;
    protected Map<String, Object> dbHints = new HashMap<>();
    protected boolean loadDynamicAttributes;

    /**
     * Factory method to create a LoadContext instance.
     *
     * @param entityClass   class of the loaded entities
     */
    public static <E extends Entity> LoadContext<E> create(Class<E> entityClass) {
        return new LoadContext<E>(entityClass);
    }

    /**
     * Factory method to create a LoadContext.Query instance for passing into {@link #setQuery(Query)} method.
     *
     * @param queryString   JPQL query string. Only named parameters are supported.
     */
    public static LoadContext.Query createQuery(String queryString) {
        return new LoadContext.Query(queryString);
    }

    /**
     * @param metaClass metaclass of the loaded entities
     */
    public LoadContext(MetaClass metaClass) {
        Objects.requireNonNull(metaClass, "metaClass is null");
        this.metaClass = metaClass.getName();
    }

    /**
     * @param javaClass class of the loaded entities
     */
    public LoadContext(Class<E> javaClass) {
        Metadata metadata = AppBeans.get(Metadata.NAME);
        this.metaClass = metadata.getSession().getClassNN(javaClass).getName();
    }

    /**
     * @return name of metaclass of the loaded entities
     */
    public String getMetaClass() {
        return metaClass;
    }

    /**
     * @return query definition
     */
    public Query getQuery() {
        return query;
    }

    /**
     * @param query query definition
     * @return this instance for chaining
     */
    public LoadContext<E> setQuery(Query query) {
        this.query = query;
        return this;
    }

    /**
     * @param queryString JPQL query string. Only named parameters are supported.
     * @return  query definition object
     */
    public Query setQueryString(String queryString) {
        final Query query = new Query(queryString);
        setQuery(query);
        return query;
    }

    /**
     * @return view that is used for loading entities
     */
    public View getView() {
        return view;
    }

    /**
     * @param view view that is used for loading entities
     * @return this instance for chaining
     */
    public LoadContext<E> setView(View view) {
        this.view = view;
        return this;
    }

    /**
     * @param viewName view that is used for loading entities
     * @return this instance for chaining
     */
    public LoadContext<E> setView(String viewName) {
        Metadata metadata = AppBeans.get(Metadata.NAME);
        this.view = metadata.getViewRepository().getView(metadata.getSession().getClass(metaClass), viewName);
        return this;
    }

    /**
     * @return id of an entity to be loaded
     */
    public Object getId() {
        return id;
    }

    /**
     * @param id id of an entity to be loaded
     * @return this instance for chaining
     */
    public LoadContext<E> setId(Object id) {
        this.id = id;
        return this;
    }

    /**
     * @return whether to use soft deletion when loading entities
     */
    public boolean isSoftDeletion() {
        return softDeletion;
    }

    /**
     * @param softDeletion whether to use soft deletion when loading entities
     */
    public void setSoftDeletion(boolean softDeletion) {
        this.softDeletion = softDeletion;
    }

    /**
     * Allows to execute query on a previous query result.
     * @return editable list of previous queries
     */
    public List<Query> getPrevQueries() {
        return prevQueries;
    }

    /**
     * @return key of the current stack of sequential queries, which is unique for the current user session
     */
    public int getQueryKey() {
        return queryKey;
    }

    /**
     * @param queryKey key of the current stack of sequential queries, which is unique for the current user session
     */
    public void setQueryKey(int queryKey) {
        this.queryKey = queryKey;
    }

    /**
     * @return custom hints which can be used later during query construction
     */
    public Map<String, Object> getDbHints() {
        return dbHints;
    }

    /**
     * @return whether to load dynamic attributes
     */
    public boolean getLoadDynamicAttributes() {
        return loadDynamicAttributes;
    }

    /**
     * @param loadDynamicAttributes whether to load dynamic attributes
     */
    public void setLoadDynamicAttributes(boolean loadDynamicAttributes) {
        this.loadDynamicAttributes = loadDynamicAttributes;
    }

    @Override
    public String toString() {
        return "LoadContext{" +
                "metaClass='" + metaClass + '\'' +
                ", query=" + query +
                ", view=" + view +
                ", id=" + id +
                ", softDeletion=" + softDeletion +
                '}';
    }

    /**
     * Class that defines a query to be executed for data loading.
     */
    public static class Query implements Serializable {

        private static final long serialVersionUID = 3819951144050635838L;

        private Map<String, Object> parameters = new HashMap<>();
        private String queryString;
        private int firstResult;
        private int maxResults;

        /**
         * @param queryString JPQL query string. Only named parameters are supported.
         */
        public Query(String queryString) {
            this.queryString = queryString;
        }<commit_msg>PL-5825 CategoryAttribute's field is not displaying  deleted entity values #PL-5825<commit_after>/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */
package com.haulmont.cuba.core.global;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.core.entity.Entity;

import java.io.Serializable;
import java.util.*;

/**
 * Class that defines parameters for loading entities from the database via {@link DataManager}.
 * <p>Typical usage:
 * <pre>
    LoadContext&lt;User&gt; context = LoadContext.create(User.class).setQuery(
            LoadContext.createQuery("select u from sec$User u where u.login like :login")
                    .setParameter("login", "a%")
                    .setMaxResults(10))
            .setView("user.browse");
    List&lt;User&gt; users = dataManager.loadList(context);
 * </pre>
 *
 * @author krivopustov
 * @version $Id$
 */
public class LoadContext<E extends Entity> implements Serializable {

    private static final long serialVersionUID = -8808320502197308698L;

    protected String metaClass;
    protected Query query;
    protected View view;
    protected Object id;
    protected boolean softDeletion = true;
    protected List<Query> prevQueries = new ArrayList<>();
    protected int queryKey;
    protected Map<String, Object> dbHints = new HashMap<>();
    protected boolean loadDynamicAttributes;

    /**
     * Factory method to create a LoadContext instance.
     *
     * @param entityClass   class of the loaded entities
     */
    public static <E extends Entity> LoadContext<E> create(Class<E> entityClass) {
        return new LoadContext<E>(entityClass);
    }

    /**
     * Factory method to create a LoadContext.Query instance for passing into {@link #setQuery(Query)} method.
     *
     * @param queryString   JPQL query string. Only named parameters are supported.
     */
    public static LoadContext.Query createQuery(String queryString) {
        return new LoadContext.Query(queryString);
    }

    /**
     * @param metaClass metaclass of the loaded entities
     */
    public LoadContext(MetaClass metaClass) {
        Objects.requireNonNull(metaClass, "metaClass is null");
        this.metaClass = metaClass.getName();
    }

    /**
     * @param javaClass class of the loaded entities
     */
    public LoadContext(Class<E> javaClass) {
        Metadata metadata = AppBeans.get(Metadata.NAME);
        this.metaClass = metadata.getSession().getClassNN(javaClass).getName();
    }

    /**
     * @return name of metaclass of the loaded entities
     */
    public String getMetaClass() {
        return metaClass;
    }

    /**
     * @return query definition
     */
    public Query getQuery() {
        return query;
    }

    /**
     * @param query query definition
     * @return this instance for chaining
     */
    public LoadContext<E> setQuery(Query query) {
        this.query = query;
        return this;
    }

    /**
     * @param queryString JPQL query string. Only named parameters are supported.
     * @return  query definition object
     */
    public Query setQueryString(String queryString) {
        final Query query = new Query(queryString);
        setQuery(query);
        return query;
    }

    /**
     * @return view that is used for loading entities
     */
    public View getView() {
        return view;
    }

    /**
     * @param view view that is used for loading entities
     * @return this instance for chaining
     */
    public LoadContext<E> setView(View view) {
        this.view = view;
        return this;
    }

    /**
     * @param viewName view that is used for loading entities
     * @return this instance for chaining
     */
    public LoadContext<E> setView(String viewName) {
        Metadata metadata = AppBeans.get(Metadata.NAME);
        this.view = metadata.getViewRepository().getView(metadata.getSession().getClass(metaClass), viewName);
        return this;
    }

    /**
     * @return id of an entity to be loaded
     */
    public Object getId() {
        return id;
    }

    /**
     * @param id id of an entity to be loaded
     * @return this instance for chaining
     */
    public LoadContext<E> setId(Object id) {
        this.id = id;
        return this;
    }

    /**
     * @return whether to use soft deletion when loading entities
     */
    public boolean isSoftDeletion() {
        return softDeletion;
    }

    /**
     * @param softDeletion whether to use soft deletion when loading entities
     */
    public LoadContext setSoftDeletion(boolean softDeletion) {
        this.softDeletion = softDeletion;
        return this;
    }

    /**
     * Allows to execute query on a previous query result.
     * @return editable list of previous queries
     */
    public List<Query> getPrevQueries() {
        return prevQueries;
    }

    /**
     * @return key of the current stack of sequential queries, which is unique for the current user session
     */
    public int getQueryKey() {
        return queryKey;
    }

    /**
     * @param queryKey key of the current stack of sequential queries, which is unique for the current user session
     */
    public LoadContext setQueryKey(int queryKey) {
        this.queryKey = queryKey;
        return this;
    }

    /**
     * @return custom hints which can be used later during query construction
     */
    public Map<String, Object> getDbHints() {
        return dbHints;
    }

    /**
     * @return whether to load dynamic attributes
     */
    public boolean getLoadDynamicAttributes() {
        return loadDynamicAttributes;
    }

    /**
     * @param loadDynamicAttributes whether to load dynamic attributes
     */
    public LoadContext setLoadDynamicAttributes(boolean loadDynamicAttributes) {
        this.loadDynamicAttributes = loadDynamicAttributes;
        return this;
    }

    @Override
    public String toString() {
        return "LoadContext{" +
                "metaClass='" + metaClass + '\'' +
                ", query=" + query +
                ", view=" + view +
                ", id=" + id +
                ", softDeletion=" + softDeletion +
                '}';
    }

    /**
     * Class that defines a query to be executed for data loading.
     */
    public static class Query implements Serializable {

        private static final long serialVersionUID = 3819951144050635838L;

        private Map<String, Object> parameters = new HashMap<>();
        private String queryString;
        private int firstResult;
        private int maxResults;

        /**
         * @param queryString JPQL query string. Only named parameters are supported.
         */
        public Query(String queryString) {
            this.queryString = queryString;
        }
{'fraction_non_alphanumeric': 0.09272646798465624, 'fraction_numerical': 0.0076718796105045735, 'mean_word_length': 2.3350553505535054, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, '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': '3315543', 'n_tokens_mistral': 3730, 'n_tokens_neox': 3543, 'n_words': 1149}