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

 * Author: Dmitry Abramov
 * Created: 13.02.2009 16:35:29
 * $Id$
 */
package com.haulmont.cuba.core.global;

import com.haulmont.cuba.core.entity.Entity;
import org.apache.openjpa.enhance.PersistenceCapable;

public class PersistenceHelper {
    public static boolean isNew(Entity entity) {
        if (entity instanceof PersistenceCapable)
            return ((PersistenceCapable) entity).pcIsDetached() == null;
        else
            return entity.getId() != null;
    }
}

@@ -375,16 +375,242 @@
 apable;%0D
+%0Aimport org.apache.openjpa.enhance.StateManager;%0D%0Aimport org.apache.openjpa.kernel.OpenJPAStateManager;%0D%0Aimport org.apache.openjpa.meta.ClassMetaData;%0D%0Aimport org.apache.openjpa.meta.FieldMetaData;%0D%0A%0D%0Aimport java.util.BitSet;%0D
 %0A%0D%0Apubli
@@ -877,12 +877,793 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    public static boolean isLoaded(Entity entity, String property) %7B%0D%0A        if (entity instanceof PersistenceCapable) %7B%0D%0A            final PersistenceCapable persistenceCapable = (PersistenceCapable) entity;%0D%0A            final OpenJPAStateManager stateManager = (OpenJPAStateManager) persistenceCapable.pcGetStateManager();%0D%0A%0D%0A            final BitSet loaded = stateManager.getLoaded();%0D%0A            final ClassMetaData metaData = stateManager.getMetaData();%0D%0A%0D%0A            final FieldMetaData fieldMetaData = metaData.getField(property);%0D%0A            if (fieldMetaData == null) throw new IllegalStateException();%0D%0A%0D%0A            final int index = fieldMetaData.getIndex();%0D%0A%0D%0A            return loaded.get(index);%0D%0A        %7D else %7B%0D%0A            return true;%0D%0A        %7D%0D%0A    %7D%0D
 %0A%7D%0D%0A

add method isloaded