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

 * Author: Konstantin Krivopustov
 * Created: 19.12.2008 13:01:56
 *
 * $Id$
 */
package com.haulmont.cuba.core.sys;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.core.entity.BaseEntity;
import com.haulmont.cuba.core.entity.DeleteDeferred;
import com.haulmont.cuba.core.entity.Updatable;
import com.haulmont.cuba.core.global.MetadataProvider;
import com.haulmont.cuba.core.global.View;
import com.haulmont.cuba.core.global.ViewProperty;
import org.apache.openjpa.persistence.FetchPlan;

public class ViewHelper
{
    public static void setView(FetchPlan fetchPlan, View view) {
        if (fetchPlan == null)
            throw new IllegalArgumentException("FetchPlan is null");
        if (view == null)
            throw new IllegalArgumentException("View is null");

        fetchPlan.clearFetchGroups();
        fetchPlan.removeFetchGroup(FetchPlan.GROUP_DEFAULT);
        processView(view, fetchPlan);
    }

    private static void processView(View view, FetchPlan fetchPlan) {
        if (view.isIncludeSystemProperties()) {
            includeSystemProperties(view, fetchPlan);
        }
        for (ViewProperty property : view.getProperties()) {
            fetchPlan.addField(view.getEntityClass(), property.getName());
            if (property.getView() != null) {
                processView(property.getView(), fetchPlan);
            }
        }
    }

    private static void includeSystemProperties(View view, FetchPlan fetchPlan) {
        Class<? extends BaseEntity> entityClass = view.getEntityClass();
        MetaClass metaClass = MetadataProvider.getSession().getClass(entityClass);

        Class<?> declaringClass = metaClass.getProperty("createTs").getJavaField().getDeclaringClass();
        fetchPlan.addField(declaringClass, "createTs");
        fetchPlan.addField(declaringClass, "createdBy");
        if (Updatable.class.isAssignableFrom(entityClass)) {
            declaringClass = metaClass.getProperty("updateTs").getJavaField().getDeclaringClass();
            fetchPlan.addField(declaringClass, "updateTs");
            fetchPlan.addField(declaringClass, "updatedBy");
        }
        if (DeleteDeferred.class.isAssignableFrom(entityClass)) {
            declaringClass = metaClass.getProperty("deleteTs").getJavaField().getDeclaringClass();
            fetchPlan.addField(declaringClass, "deleteTs");
            fetchPlan.addField(declaringClass, "deletedBy");
        }
    }
}

@@ -333,16 +333,68 @@
 Class;%0D%0A
+import com.haulmont.chile.core.model.MetaProperty;%0D%0A
 import c
@@ -742,16 +742,51 @@
 lan;%0D%0A%0D%0A
+import java.lang.reflect.Field;%0D%0A%0D%0A
 public c
@@ -1406,70 +1406,346 @@
  %7D%0D%0A
-        for (ViewProperty property : view.getProperties()) %7B%0D%0A
+%0D%0A        MetaClass metaClass = MetadataProvider.getSession().getClass(view.getEntityClass());%0D%0A%0D%0A        for (ViewProperty property : view.getProperties()) %7B%0D%0A            MetaProperty metaProperty = metaClass.getProperty(property.getName());%0D%0A            Field field = metaProperty.getJavaField();%0D%0A            if (field != null) %7B%0D%0A    
     
@@ -1771,30 +1771,34 @@
 ddField(
-view
+field
 .get
-Entity
+Declaring
 Class(),
@@ -1812,32 +1812,36 @@
 ty.getName());%0D%0A
+    
             if (
@@ -1879,32 +1879,36 @@
                 
+    
 processView(prop
@@ -1928,32 +1928,51 @@
 ), fetchPlan);%0D%0A
+                %7D%0D%0A
             %7D%0D%0A 

fix ViewHelper