Search in sources :

Example 51 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DsBuilder method buildDatasource.

public Datasource buildDatasource() {
    init();
    Datasource datasource;
    try {
        if (master == null && property == null) {
            if (dsClass == null) {
                datasource = createDatasource();
            } else {
                datasource = (Datasource) dsClass.newInstance();
            }
            datasource.setup(dsContext, dataSupplier, id, metaClass, view);
        } else {
            boolean isEmbedded = false;
            if (master != null) {
                MetaClass metaClass = master.getMetaClass();
                MetaProperty metaProperty = metaClass.getProperty(property);
                MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
                isEmbedded = metadataTools.isEmbedded(metaProperty);
            }
            if (dsClass == null) {
                datasource = isEmbedded ? createEmbeddedDatasource() : createPropertyDatasource();
            } else {
                datasource = (Datasource) dsClass.newInstance();
            }
            ((NestedDatasource) datasource).setup(id, master, property);
        }
    } catch (IllegalAccessException | InstantiationException e) {
        throw new RuntimeException(e);
    }
    datasource.setAllowCommit(allowCommit);
    registerDatasource(datasource);
    return datasource;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 52 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class AbstractComparator method compareAsc.

protected int compareAsc(Object o1, Object o2) {
    int c;
    if (o1 instanceof String && o2 instanceof String) {
        c = ((String) o1).compareToIgnoreCase((String) o2);
    } else if (o1 instanceof Comparable && o2 instanceof Comparable) {
        c = ((Comparable) o1).compareTo(o2);
    } else if (o1 instanceof Instance && o2 instanceof Instance) {
        MetaClass metaClass = metadata.getClassNN(o1.getClass());
        Collection<MetaProperty> namePatternProperties = metadata.getTools().getNamePatternProperties(metaClass, true);
        if (namePatternProperties.isEmpty()) {
            c = ((Instance) o1).getInstanceName().compareToIgnoreCase(((Instance) o2).getInstanceName());
        } else {
            c = 0;
            for (MetaProperty property : namePatternProperties) {
                Object v1 = ((Instance) o1).getValue(property.getName());
                Object v2 = ((Instance) o2).getValue(property.getName());
                c = compareAsc(v1, v2);
                if (c != 0)
                    break;
            }
        }
    } else if (Objects.equals(o1, o2)) {
        c = 0;
    } else if (o1 == null && o2 != null) {
        c = nullsLast;
    } else {
        c = -nullsLast;
    }
    return c;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Instance(com.haulmont.chile.core.model.Instance) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 53 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DynamicAttributesGuiTools method initEntityPickerField.

/**
 * Initializes the pickerField for selecting the dynamic attribute value. If the CategoryAttribute has "where" or
 * "join" clauses then the data in lookup screens will be filtered with these clauses
 *
 * @param pickerField       PickerField component whose lookup action must be initialized
 * @param categoryAttribute CategoryAttribute that is represented by the pickerField
 */
public void initEntityPickerField(PickerField pickerField, CategoryAttribute categoryAttribute) {
    Class javaClass = categoryAttribute.getJavaClassForEntity();
    if (javaClass == null) {
        throw new IllegalArgumentException("Entity type is not specified in category attribute");
    }
    MetaClass metaClass = metadata.getClassNN(javaClass);
    PickerField.LookupAction lookupAction = (PickerField.LookupAction) pickerField.getAction(PickerField.LookupAction.NAME);
    if (!Strings.isNullOrEmpty(categoryAttribute.getJoinClause()) || !Strings.isNullOrEmpty(categoryAttribute.getWhereClause())) {
        lookupAction = createLookupAction(pickerField, categoryAttribute.getJoinClause(), categoryAttribute.getWhereClause());
        pickerField.addAction(lookupAction);
    }
    if (lookupAction == null) {
        lookupAction = pickerField.addLookupAction();
    }
    String screen = categoryAttribute.getScreen();
    if (StringUtils.isNotBlank(screen)) {
        lookupAction.setLookupScreen(screen);
    } else {
        screen = windowConfig.getBrowseScreenId(metaClass);
        if (windowConfig.findWindowInfo(screen) != null) {
            lookupAction.setLookupScreen(screen);
            lookupAction.setLookupScreenOpenType(OpenType.THIS_TAB);
        } else {
            lookupAction.setLookupScreen(CommonLookupController.SCREEN_ID);
            lookupAction.setLookupScreenParams(ParamsMap.of(CommonLookupController.CLASS_PARAMETER, metaClass));
            lookupAction.setLookupScreenOpenType(OpenType.DIALOG);
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) PickerField(com.haulmont.cuba.gui.components.PickerField) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 54 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DynamicAttributesGuiTools method screenContainsDynamicAttributes.

/**
 * Method checks whether any class in the view hierarchy contains dynamic attributes that must be displayed on
 * the current screen
 */
public boolean screenContainsDynamicAttributes(View mainDatasourceView, String screenId) {
    DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
    Set<Class> classesWithDynamicAttributes = collectEntityClassesWithDynamicAttributes(mainDatasourceView);
    for (Class classWithDynamicAttributes : classesWithDynamicAttributes) {
        MetaClass metaClass = metadata.getClassNN(classWithDynamicAttributes);
        if (!dynamicAttributesGuiTools.getAttributesToShowOnTheScreen(metaClass, screenId, null).isEmpty()) {
            return true;
        }
    }
    return false;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 55 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class PersistenceTools method getReferenceId.

/**
 * Returns an ID of directly referenced entity without loading it from DB.
 *
 * If the view does not contain the reference and {@link View#loadPartialEntities()} is true,
 * the returned {@link RefId} will have {@link RefId#isLoaded()} = false.
 *
 * <p>Usage example:
 * <pre>
 *   PersistenceTools.RefId refId = persistenceTools.getReferenceId(doc, "currency");
 *   if (refId.isLoaded()) {
 *       String currencyCode = (String) refId.getValue();
 *   }
 * </pre>
 *
 * @param entity   entity instance in managed state
 * @param property name of reference property
 * @return {@link RefId} instance which contains the referenced entity ID
 * @throws IllegalArgumentException if the specified property is not a reference
 * @throws IllegalStateException if the entity is not in Managed state
 * @throws RuntimeException if anything goes wrong when retrieving the ID
 */
public RefId getReferenceId(BaseGenericIdEntity entity, String property) {
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    MetaProperty metaProperty = metaClass.getPropertyNN(property);
    if (!metaProperty.getRange().isClass() || metaProperty.getRange().getCardinality().isMany())
        throw new IllegalArgumentException("Property is not a reference");
    if (!PersistenceHelper.isManaged(entity))
        throw new IllegalStateException("Entity must be in managed state");
    String[] inaccessibleAttributes = BaseEntityInternalAccess.getInaccessibleAttributes(entity);
    if (inaccessibleAttributes != null) {
        for (String inaccessibleAttr : inaccessibleAttributes) {
            if (inaccessibleAttr.equals(property))
                return RefId.createNotLoaded(property);
        }
    }
    if (entity instanceof FetchGroupTracker) {
        FetchGroup fetchGroup = ((FetchGroupTracker) entity)._persistence_getFetchGroup();
        if (fetchGroup != null) {
            if (!fetchGroup.getAttributeNames().contains(property))
                return RefId.createNotLoaded(property);
            else {
                Entity refEntity = (Entity) entity.getValue(property);
                return RefId.create(property, refEntity == null ? null : refEntity.getId());
            }
        }
    }
    try {
        Class<?> declaringClass = metaProperty.getDeclaringClass();
        if (declaringClass == null) {
            throw new RuntimeException("Property does not belong to persistent class");
        }
        Method vhMethod = declaringClass.getDeclaredMethod(String.format("_persistence_get_%s_vh", property));
        vhMethod.setAccessible(true);
        ValueHolderInterface vh = (ValueHolderInterface) vhMethod.invoke(entity);
        if (vh instanceof DatabaseValueHolder) {
            AbstractRecord row = ((DatabaseValueHolder) vh).getRow();
            if (row != null) {
                Session session = persistence.getEntityManager().getDelegate().unwrap(Session.class);
                ClassDescriptor descriptor = session.getDescriptor(entity);
                DatabaseMapping mapping = descriptor.getMappingForAttributeName(property);
                Vector<DatabaseField> fields = mapping.getFields();
                if (fields.size() != 1) {
                    throw new IllegalStateException("Invalid number of columns in mapping: " + fields);
                }
                Object value = row.get(fields.get(0));
                if (value != null) {
                    ClassDescriptor refDescriptor = mapping.getReferenceDescriptor();
                    DatabaseMapping refMapping = refDescriptor.getMappingForAttributeName(metadata.getTools().getPrimaryKeyName(metaClass));
                    if (refMapping instanceof AbstractColumnMapping) {
                        Converter converter = ((AbstractColumnMapping) refMapping).getConverter();
                        if (converter != null) {
                            return RefId.create(property, converter.convertDataValueToObjectValue(value, session));
                        }
                    }
                }
                return RefId.create(property, value);
            } else {
                return RefId.create(property, null);
            }
        }
        return RefId.createNotLoaded(property);
    } catch (Exception e) {
        throw new RuntimeException(String.format("Error retrieving reference ID from %s.%s", entity.getClass().getSimpleName(), property), e);
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Method(java.lang.reflect.Method) AbstractColumnMapping(org.eclipse.persistence.mappings.foundation.AbstractColumnMapping) FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) DatabaseValueHolder(org.eclipse.persistence.internal.indirection.DatabaseValueHolder) MetaClass(com.haulmont.chile.core.model.MetaClass) ValueHolderInterface(org.eclipse.persistence.indirection.ValueHolderInterface) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) FetchGroup(org.eclipse.persistence.queries.FetchGroup) Converter(org.eclipse.persistence.mappings.converters.Converter) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Session(org.eclipse.persistence.sessions.Session)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)302 MetaProperty (com.haulmont.chile.core.model.MetaProperty)103 Entity (com.haulmont.cuba.core.entity.Entity)54 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)36 Nullable (javax.annotation.Nullable)25 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)24 Element (org.dom4j.Element)21 java.util (java.util)18 Inject (javax.inject.Inject)17 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)16 Test (org.junit.Test)15 EntityManager (com.haulmont.cuba.core.EntityManager)14 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)14 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)13 Metadata (com.haulmont.cuba.core.global.Metadata)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)12 Collectors (java.util.stream.Collectors)11 Range (com.haulmont.chile.core.model.Range)10