/*
 * 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.gui.data.impl;

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.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.MetadataTools;
import com.haulmont.cuba.core.global.View;
import com.haulmont.cuba.core.global.ViewProperty;
import com.haulmont.cuba.gui.WindowParams;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.DsContext;
import com.haulmont.cuba.gui.data.PropertyDatasource;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author krivopustov
 * @version $Id$
 */
public class CollectionDsHelper {

    public static List<MetaPropertyPath> createProperties(View view, MetaClass metaClass) {
        List<MetaPropertyPath> properties = new ArrayList<>();
        MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);

        if (view != null && metadataTools.isPersistent(metaClass)) {
            for (ViewProperty property : view.getProperties()) {
                final String name = property.getName();

                final MetaProperty metaProperty = metaClass.getProperty(name);
                final Range range = metaProperty.getRange();
                if (range == null) continue;

                final Range.Cardinality cardinality = range.getCardinality();
                if (!cardinality.isMany()) {
                    properties.add(new MetaPropertyPath(metaProperty.getDomain(), metaProperty));
                }
            }
        } else {
            if (view != null) {
                String message = String.format("Specified view %s for datasource with not persistent entity %s",
                        view.getName(), metaClass.getName());
                LogFactory.getLog(CollectionDsHelper.class).warn(message);
            }

            for (MetaProperty metaProperty : metaClass.getProperties()) {
                final Range range = metaProperty.getRange();
                if (range == null) continue;

                final Range.Cardinality cardinality = range.getCardinality();
                if (!cardinality.isMany()) {
                    properties.add(new MetaPropertyPath(metaProperty.getDomain(), metaProperty));
                }
            }
        }
        return properties;
    }

    public static void autoRefreshInvalid(CollectionDatasource datasource, boolean autoRefresh) {
        if (datasource instanceof PropertyDatasource) {
            return;
        }
        if (autoRefresh && Datasource.State.INVALID.equals(datasource.getState())) {
            DsContext dsContext = datasource.getDsContext();
            if (dsContext == null || !WindowParams.DISABLE_AUTO_REFRESH.getBool(dsContext.getWindowContext())) {
                if (datasource instanceof CollectionDatasource.Suspendable)
                    ((CollectionDatasource.Suspendable) datasource).refreshIfNotSuspended();
                else
                    datasource.refresh();
            }
        }
    }
}
@@ -441,165 +441,9 @@
 bal.
-AppBeans;%0D%0Aimport com.haulmont.cuba.core.global.MetadataTools;%0D%0Aimport com.haulmont.cuba.core.global.View;%0D%0Aimport com.haulmont.cuba.core.global.ViewProperty
+*
 ;%0D%0Ai
@@ -1386,24 +1386,699 @@
 rty(name);%0D%0A
+                if (metaProperty == null) %7B%0D%0A                    String message = String.format(%22Unable to find property %25s for entity %25s%22, name, metaClass.getName());%0D%0A                    throw new DevelopmentException(message);%0D%0A                %7D%0D%0A%0D%0A                if (!metadataTools.isPersistent(metaProperty)) %7B%0D%0A                    String message = String.format(%0D%0A                            %22Specified transient property %25s in view for datasource with persistent entity %25s%22,%0D%0A                            name, metaClass.getName());%0D%0A%0D%0A                    LogFactory.getLog(CollectionDsHelper.class).warn(message);%0D%0A                    continue;%0D%0A                %7D%0D%0A%0D%0A
             
@@ -2158,33 +2158,75 @@
 ge == null) 
-continue;
+%7B%0D%0A                    continue;%0D%0A                %7D
 %0D%0A%0D%0A        
@@ -2466,32 +2466,356 @@
 %0D%0A            %7D%0D
+%0A%0D%0A            // add all non-persistent properties%0D%0A            for (MetaProperty metaProperty : metaClass.getProperties()) %7B%0D%0A                if (metadataTools.isTransient(metaProperty)) %7B%0D%0A                    properties.add(new MetaPropertyPath(metaProperty.getDomain(), metaProperty));%0D%0A                %7D%0D%0A            %7D%0D
 %0A        %7D else 
@@ -3566,32 +3566,34 @@
   %7D%0D%0A        %7D%0D%0A
+%0D%0A
         return p

Add all non-persistent properties to container properties #PL-3102