<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.components.actions;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.config.WindowConfig;
import com.haulmont.cuba.gui.data.*;
import com.haulmont.cuba.gui.theme.ThemeConstantsManager;
import com.haulmont.cuba.security.entity.EntityAttrAccess;
import com.haulmont.cuba.security.entity.EntityOp;
import org.springframework.context.annotation.Scope;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * Standard list action to create a new entity instance.
 * <p>
 * Action's behaviour can be customized by providing arguments to constructor, setting properties, or overriding
 * methods {@link #afterCommit(com.haulmont.cuba.core.entity.Entity)}, {@link #afterWindowClosed(com.haulmont.cuba.gui.components.Window)}
 * <p>
 * In order to provide your own implementation globally, create a subclass and register it in {@code web-spring.xml},
 * for example:
 * <pre>
 * &lt;bean id="cuba_CreateAction" class="com.company.sample.gui.MyCreateAction" scope="prototype"/&gt;
 * </pre>
 * Also, use {@code create()} static methods instead of constructors when creating the action programmatically.
 */
@org.springframework.stereotype.Component("cuba_CreateAction")
@Scope("prototype")
public class CreateAction extends BaseAction implements Action.HasOpenType, Action.HasBeforeActionPerformedHandler  {

    public static final String ACTION_ID = ListActionType.CREATE.getId();

    protected WindowManager.OpenType openType;

    protected String windowId;

    protected Map<String, Object> windowParams;
    protected Supplier<Map<String, Object>> windowParamsSupplier;

    protected Map<String, Object> initialValues;
    protected Supplier<Map<String, Object>> initialValuesSupplier;

    protected boolean addFirst = true;

    protected Metadata metadata = AppBeans.get(Metadata.NAME);
    protected Security security = AppBeans.get(Security.NAME);

    protected AfterCommitHandler afterCommitHandler;

    protected AfterWindowClosedHandler afterWindowClosedHandler;

    protected Window.CloseListener editorCloseListener;

    protected BeforeActionPerformedHandler beforeActionPerformedHandler;

    public interface AfterCommitHandler {
        /**
         * @param entity    new committed entity instance
         */
        void handle(Entity entity);
    }

    public interface AfterWindowClosedHandler {
        /**
         * @param window        the editor window
         * @param closeActionId ID of action caused the screen closing
         */
        void handle(Window window, String closeActionId);
    }

    /**
     * Creates an action with default id, opening the editor screen in THIS tab.
     * @param target    component containing this action
     */
    public static CreateAction create(ListComponent target) {
        return AppBeans.getPrototype("cuba_CreateAction", target);
    }

    /**
     * Creates an action with default id.
     * @param target    component containing this action
     * @param openType  how to open the editor screen
     */
    public static CreateAction create(ListComponent target, WindowManager.OpenType openType) {
        return AppBeans.getPrototype("cuba_CreateAction", target);
    }

    /**
     * Creates an action with the given id.
     * @param target    component containing this action
     * @param openType  how to open the editor screen
     * @param id        action name
     */
    public static CreateAction create(ListComponent target, WindowManager.OpenType openType, String id) {
        return AppBeans.getPrototype("cuba_CreateAction", target, id);
    }

    /**
     * The simplest constructor. The action has default name and opens the editor screen in THIS tab.
     * @param target    component containing this action
     */
    public CreateAction(ListComponent target) {
        this(target, WindowManager.OpenType.THIS_TAB, ACTION_ID);
    }

    /**
     * Constructor that allows to specify how the editor screen opens. The action has default name.
     * @param target    component containing this action
     * @param openType  how to open the editor screen<commit_msg>PL-8930 CreateAction#create unuses OpenType parameter<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.components.actions;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.config.WindowConfig;
import com.haulmont.cuba.gui.data.*;
import com.haulmont.cuba.gui.theme.ThemeConstantsManager;
import com.haulmont.cuba.security.entity.EntityAttrAccess;
import com.haulmont.cuba.security.entity.EntityOp;
import org.springframework.context.annotation.Scope;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * Standard list action to create a new entity instance.
 * <p>
 * Action's behaviour can be customized by providing arguments to constructor, setting properties, or overriding
 * methods {@link #afterCommit(com.haulmont.cuba.core.entity.Entity)}, {@link #afterWindowClosed(com.haulmont.cuba.gui.components.Window)}
 * <p>
 * In order to provide your own implementation globally, create a subclass and register it in {@code web-spring.xml},
 * for example:
 * <pre>
 * &lt;bean id="cuba_CreateAction" class="com.company.sample.gui.MyCreateAction" scope="prototype"/&gt;
 * </pre>
 * Also, use {@code create()} static methods instead of constructors when creating the action programmatically.
 */
@org.springframework.stereotype.Component("cuba_CreateAction")
@Scope("prototype")
public class CreateAction extends BaseAction implements Action.HasOpenType, Action.HasBeforeActionPerformedHandler  {

    public static final String ACTION_ID = ListActionType.CREATE.getId();

    protected WindowManager.OpenType openType;

    protected String windowId;

    protected Map<String, Object> windowParams;
    protected Supplier<Map<String, Object>> windowParamsSupplier;

    protected Map<String, Object> initialValues;
    protected Supplier<Map<String, Object>> initialValuesSupplier;

    protected boolean addFirst = true;

    protected Metadata metadata = AppBeans.get(Metadata.NAME);
    protected Security security = AppBeans.get(Security.NAME);

    protected AfterCommitHandler afterCommitHandler;

    protected AfterWindowClosedHandler afterWindowClosedHandler;

    protected Window.CloseListener editorCloseListener;

    protected BeforeActionPerformedHandler beforeActionPerformedHandler;

    public interface AfterCommitHandler {
        /**
         * @param entity    new committed entity instance
         */
        void handle(Entity entity);
    }

    public interface AfterWindowClosedHandler {
        /**
         * @param window        the editor window
         * @param closeActionId ID of action caused the screen closing
         */
        void handle(Window window, String closeActionId);
    }

    /**
     * Creates an action with default id, opening the editor screen in THIS tab.
     * @param target    component containing this action
     */
    public static CreateAction create(ListComponent target) {
        return AppBeans.getPrototype("cuba_CreateAction", target);
    }

    /**
     * Creates an action with default id.
     * @param target    component containing this action
     * @param openType  how to open the editor screen
     */
    public static CreateAction create(ListComponent target, WindowManager.OpenType openType) {
        return AppBeans.getPrototype("cuba_CreateAction", target, openType);
    }

    /**
     * Creates an action with the given id.
     * @param target    component containing this action
     * @param openType  how to open the editor screen
     * @param id        action name
     */
    public static CreateAction create(ListComponent target, WindowManager.OpenType openType, String id) {
        return AppBeans.getPrototype("cuba_CreateAction", target, openType, id);
    }

    /**
     * The simplest constructor. The action has default name and opens the editor screen in THIS tab.
     * @param target    component containing this action
     */
    public CreateAction(ListComponent target) {
        this(target, WindowManager.OpenType.THIS_TAB, ACTION_ID);
    }

    /**
     * Constructor that allows to specify how the editor screen opens. The action has default name.
     * @param target    component containing this action
     * @param openType  how to open the editor screen
{'fraction_non_alphanumeric': 0.08391608391608392, 'fraction_numerical': 0.0027195027195027195, 'mean_word_length': 3.7539242843951985, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '4443698', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2648, 'n_words': 881}