/*
 * Copyright (c) 2008-2017 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.core.app.events;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.ExtendedEntities;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;

/**
 * A Spring application event that is sent by the framework to set up access to attributes of an entity instance.
 * Subscribers should write appropriate attribute names into the event object using {@link #addHidden(String)},
 * {@link #addReadOnly(String)} and {@link #addRequired(String)} methods.

 * @param <T> type of entity
 */
public class SetupAttributeAccessEvent<T extends Entity> extends ApplicationEvent
        implements ResolvableTypeProvider {

    private static final long serialVersionUID = -8775623806210166422L;

    protected Set<String> readonlyAttributes;
    protected Set<String> hiddenAttributes;
    protected Set<String> requiredAttributes;

    public SetupAttributeAccessEvent(T entity) {
        super(entity);
    }

    /**
     * Entity instance in managed state.
     */
    @SuppressWarnings("unchecked")
    public T getEntity() {
        return (T) getSource();
    }

    @Nullable
    public Set<String> getReadonlyAttributes() {
        return readonlyAttributes;
    }

    @Nullable
    public Set<String> getHiddenAttributes() {
        return hiddenAttributes;
    }

    @Nullable
    public Set<String> getRequiredAttributes() {
        return requiredAttributes;
    }

    public SetupAttributeAccessEvent addReadOnly(String attributeName) {
        if (readonlyAttributes == null) {
            readonlyAttributes = new HashSet<>();
        }
        readonlyAttributes.add(attributeName);
        return this;
    }

    public SetupAttributeAccessEvent addRequired(String attributeName) {
        if (requiredAttributes == null) {
            requiredAttributes = new HashSet<>();
        }
        requiredAttributes.add(attributeName);
        return this;
    }

    public SetupAttributeAccessEvent addHidden(String attributeName) {
        if (hiddenAttributes == null) {
            hiddenAttributes = new HashSet<>();
        }
        hiddenAttributes.add(attributeName);
        return this;
    }

    @Override
    public ResolvableType getResolvableType() {
        ExtendedEntities extendedEntities = AppBeans.get(ExtendedEntities.NAME);
        MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(getEntity().getMetaClass());
        return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forClass(metaClass.getJavaClass()));
    }
}

@@ -688,16 +688,79 @@
 aClass;%0A
+import com.haulmont.cuba.core.app.SetupAttributeAccessHandler;%0A
 import c
@@ -1151,26 +1151,8 @@
  * A
- Spring applicatio
 n ev
@@ -1167,167 +1167,144 @@
  is 
+u
 se
-nt
+d
  by 
-the framework to s
+%7B@link S
 et
- 
 up
- access to attributes of an entity instance.%0A * Subscribers should write appropriate attribute names into the event object using
+AttributeAccessHandler%7Ds to receive the entity instance and to return%0A * resulting access information.%0A * %3Cp%3E%0A * Use
  %7B@l
@@ -1327,19 +1327,16 @@
 tring)%7D,
-%0A *
  %7B@link 
@@ -1397,16 +1397,56 @@
  methods
+ to write%0A * appropriate attribute names
 .%0A%0A * @p
@@ -1529,16 +1529,24 @@
  Entity%3E
+%0A       
  extends
@@ -1562,24 +1562,16 @@
 ionEvent
-%0A       
  impleme
@@ -3096,24 +3096,139 @@
 his;%0A    %7D%0A%0A
+    /**%0A     * Kept for compatibility with the previous implementation based on Spring application events.%0A     */%0A
     @Overrid

Fix Javadoc