Search in sources :

Example 76 with MetaClass

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

the class StandardCacheLoader method updateData.

@Override
public void updateData(CacheSet cacheSet, Map<String, Object> params) throws CacheException {
    if (configuration.getConfig(GlobalConfig.class).getPerformanceTestMode())
        return;
    Collection<Object> items = cacheSet.getItems();
    List updateItems = (List) params.get("items");
    if ((updateItems != null) && (updateItems.size() > 0)) {
        MetaClass metaClass = metadata.getSession().getClass(metaClassName);
        View view = metadata.getViewRepository().getView(metaClass, viewName);
        Transaction tx = persistence.createTransaction();
        try {
            EntityManager em = persistence.getEntityManager();
            for (Object item : updateItems) {
                Entity entity = (Entity) item;
                entity = em.find(entity.getClass(), entity.getId(), view);
                items.remove(item);
                if (entity != null)
                    items.add(entity);
            }
            tx.commit();
        } catch (Exception e) {
            throw new CacheException(e);
        } finally {
            tx.end();
        }
    } else {
        log.debug("Nothing to update");
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) Transaction(com.haulmont.cuba.core.Transaction) List(java.util.List)

Example 77 with MetaClass

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

the class DomainDescriptionServiceBean method getDomainDescription.

@Override
public String getDomainDescription() {
    List<View> views = ((AbstractViewRepository) viewRepository).getAll();
    List<MetaClassRepresentation> classes = new ArrayList<>();
    List<TemplateHashModel> enums = new ArrayList<>();
    Set<String> addedEnums = new HashSet<>();
    Set<MetaClass> metas = new HashSet<>(metadataTools.getAllPersistentMetaClasses());
    metas.addAll(metadataTools.getAllEmbeddableMetaClasses());
    for (MetaClass meta : metas) {
        if (metadata.getExtendedEntities().getExtendedClass(meta) != null)
            continue;
        if (!readPermitted(meta))
            continue;
        List<View> metaClassViews = new ArrayList<>();
        for (View view : views) {
            if (view.getEntityClass().equals(meta.getJavaClass())) {
                metaClassViews.add(view);
            }
        }
        MetaClassRepresentation rep = new MetaClassRepresentation(meta, metaClassViews);
        classes.add(rep);
        for (MetaClassRepresentation.MetaClassRepProperty metaProperty : rep.getProperties()) {
            TemplateHashModel enumValues = metaProperty.getEnumValues();
            if (enumValues != null) {
                if (!addedEnums.contains(metaProperty.getJavaType())) {
                    addedEnums.add(metaProperty.getJavaType());
                    enums.add(enumValues);
                }
            }
        }
    }
    Collections.sort(classes, new Comparator<MetaClassRepresentation>() {

        @Override
        public int compare(MetaClassRepresentation o1, MetaClassRepresentation o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    Collections.sort(enums, new Comparator<TemplateHashModel>() {

        @Override
        public int compare(TemplateHashModel o1, TemplateHashModel o2) {
            try {
                return o1.get("name").toString().compareTo(o2.get("name").toString());
            } catch (TemplateModelException e) {
                return 0;
            }
        }
    });
    Map<String, Object> values = new HashMap<>();
    values.put("knownEntities", classes);
    String[] availableTypes = getAvailableBasicTypes();
    values.put("availableTypes", availableTypes);
    values.put("enums", enums);
    String template = resources.getResourceAsString("/com/haulmont/cuba/core/app/domain/DomainDescription.ftl");
    return TemplateHelper.processTemplate(template, values);
}
Also used : AbstractViewRepository(com.haulmont.cuba.core.sys.AbstractViewRepository) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 78 with MetaClass

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

the class MetaClassRepresentation method viewAccessPermitted.

private static boolean viewAccessPermitted(View view) {
    Class clazz = view.getEntityClass();
    MetaClass meta = getMetaClass(clazz);
    return MetaClassRepresentation.readPermitted(meta);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 79 with MetaClass

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

the class DynamicAttributesManager method loadEntityValues.

/**
 * Method loads entity values for CategoryAttributeValues of entity type and sets entity values to the corresponding
 * property of the {@code CategoryAttributeValue} entity.
 */
@SuppressWarnings("unchecked")
protected void loadEntityValues(List<CategoryAttributeValue> cavsOfEntityType) {
    HashMultimap<MetaClass, Object> entitiesIdsToBeLoaded = HashMultimap.create();
    HashMultimap<MetaClass, CategoryAttributeValue> cavByType = HashMultimap.create();
    cavsOfEntityType.forEach(cav -> {
        String className = cav.getCategoryAttribute().getEntityClass();
        try {
            Class<?> aClass = Class.forName(className);
            MetaClass metaClass = metadata.getClass(aClass);
            entitiesIdsToBeLoaded.put(metaClass, cav.getObjectEntityValueId());
            cavByType.put(metaClass, cav);
        } catch (ClassNotFoundException e) {
            log.error("Class {} not found", className);
        }
    });
    EntityManager em = persistence.getEntityManager();
    for (Map.Entry<MetaClass, Collection<Object>> entry : entitiesIdsToBeLoaded.asMap().entrySet()) {
        Map<Object, BaseGenericIdEntity> idToEntityMap = new HashMap<>();
        MetaClass metaClass = entry.getKey();
        Collection<Object> ids = entry.getValue();
        if (!ids.isEmpty()) {
            String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(metaClass);
            List<BaseGenericIdEntity> entitiesValues = em.createQuery(format("select e from %s e where e.%s in :ids", metaClass.getName(), pkName)).setParameter("ids", ids).setView(metaClass.getJavaClass(), View.MINIMAL).getResultList();
            for (BaseGenericIdEntity entity : entitiesValues) {
                idToEntityMap.put(entity.getId(), entity);
            }
        }
        for (CategoryAttributeValue cav : cavByType.get(metaClass)) {
            cav.setTransientEntityValue(idToEntityMap.get(cav.getObjectEntityValueId()));
        }
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 80 with MetaClass

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

the class CategoryEditor method initEntityTypeField.

protected void initEntityTypeField() {
    final ExtendedEntities extendedEntities = metadata.getExtendedEntities();
    // the map sorts meta classes by the string key
    Map<String, Object> options = new TreeMap<>();
    for (MetaClass metaClass : metadata.getTools().getAllPersistentMetaClasses()) {
        if (metadata.getTools().hasCompositePrimaryKey(metaClass) && !HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) {
            continue;
        }
        options.put(messageTools.getDetailedEntityCaption(metaClass), metaClass);
    }
    entityType.setOptionsMap(options);
    if (category.getEntityType() != null) {
        entityType.setValue(extendedEntities.getEffectiveMetaClass(extendedEntities.getEffectiveClass(category.getEntityType())));
    }
    entityType.addValueChangeListener(e -> {
        MetaClass metaClass = (MetaClass) e.getValue();
        MetaClass originalClass = extendedEntities.getOriginalMetaClass(metaClass);
        category.setEntityType(originalClass == null ? metaClass.getName() : originalClass.getName());
    });
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) TreeMap(java.util.TreeMap)

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