Search in sources :

Example 16 with KeyValueEntity

use of io.jmix.core.entity.KeyValueEntity in project jmix-docs by Haulmont.

the class OrderService method getCustomerPurchases.

// end::load-by-query[]
// tag::load-values[]
String getCustomerPurchases(LocalDate toDate) {
    List<KeyValueEntity> kvEntities = dataManager.loadValues("select o.customer, sum(o.amount) from sample_Order o " + "where o.date >= :date group by o.customer").store(// <1>
    "main").properties("customer", // <2>
    "sum").parameter("date", toDate).list();
    StringBuilder sb = new StringBuilder();
    for (KeyValueEntity kvEntity : kvEntities) {
        // <3>
        Customer customer = kvEntity.getValue("customer");
        // <3>
        BigDecimal sum = kvEntity.getValue("sum");
        sb.append(customer.getName()).append(" : ").append(sum);
    }
    return sb.toString();
}
Also used : Customer(dataaccess.ex1.entity.Customer) KeyValueEntity(io.jmix.core.entity.KeyValueEntity) BigDecimal(java.math.BigDecimal)

Example 17 with KeyValueEntity

use of io.jmix.core.entity.KeyValueEntity in project jmix-docs by Haulmont.

the class KeyValueTest method test.

@Test
void test() {
    // tag::load[]
    List<KeyValueEntity> entities = dataManager.loadValues("select e.customer, sum(e.amount) from sample_Order e group by e.customer").properties("customer", "total").list();
    // end::load[]
    assertEquals(2, entities.size());
    // tag::get-value[]
    for (KeyValueEntity entity : entities) {
        Customer customer = entity.getValue("customer");
        BigDecimal totalAmount = entity.getValue("total");
    // ...
    }
// end::get-value[]
}
Also used : Customer(datamodel.ex1.entity.Customer) KeyValueEntity(io.jmix.core.entity.KeyValueEntity) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with KeyValueEntity

use of io.jmix.core.entity.KeyValueEntity in project jmix by jmix-framework.

the class ValueDatasourceDelegate method afterLoadValues.

protected void afterLoadValues(Map<String, Object> params, ValueLoadContext context, List<KeyValueEntity> entities) {
    ds.detachListener(ds.data.values());
    ds.data.clear();
    boolean hasEnumerations = ds.metaClass.getOwnProperties().stream().anyMatch(p -> p.getRange().isEnum());
    if (!hasEnumerations) {
        for (KeyValueEntity entity : entities) {
            ds.data.put(entity.getId(), entity);
            ds.attachListener(entity);
            entity.setInstanceMetaClass(ds.metaClass);
        }
    } else {
        List<MetaProperty> enumProperties = getEnumProperties(ds.metaClass);
        for (KeyValueEntity entity : entities) {
            convertEnumValues(entity, enumProperties);
            ds.data.put(entity.getId(), entity);
            ds.attachListener(entity);
            entity.setInstanceMetaClass(ds.metaClass);
        }
    }
}
Also used : MetaProperty(io.jmix.core.metamodel.model.MetaProperty) KeyValueEntity(io.jmix.core.entity.KeyValueEntity)

Example 19 with KeyValueEntity

use of io.jmix.core.entity.KeyValueEntity in project jmix by jmix-framework.

the class EntityListValueFragment method editEntityValue.

@Subscribe("entitiesTable.edit")
public void editEntityValue(Action.ActionPerformedEvent event) {
    KeyValueEntity item = entitiesDc.getItemOrNull();
    if (item != null) {
        oldValue = item;
        openEntityValueScreen(tableValues.get(item));
    }
}
Also used : KeyValueEntity(io.jmix.core.entity.KeyValueEntity)

Example 20 with KeyValueEntity

use of io.jmix.core.entity.KeyValueEntity in project jmix by jmix-framework.

the class OracleEntityIdsLoader method loadValues.

protected List<KeyValueEntity> loadValues(ValueLoadContext valueLoadContext) {
    String storeName = valueLoadContext.getStoreName();
    valueLoadContext.setJoinTransaction(true);
    TransactionTemplate transactionTemplate = storeAwareLocator.getTransactionTemplate(storeName);
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    List<KeyValueEntity> loadedValues = transactionTemplate.execute(status -> {
        EntityManager entityManager = storeAwareLocator.getEntityManager(storeName);
        Query selectParametersQuery = entityManager.createNativeQuery("SELECT PARAMETER, value FROM nls_session_parameters WHERE parameter IN ('NLS_COMP', 'NLS_SORT')");
        List<?> resultList = selectParametersQuery.getResultList();
        Map<String, String> originalValues = new HashMap<>();
        resultList.forEach(item -> {
            Object[] row = (Object[]) item;
            String name = (String) row[0];
            String value = (String) row[1];
            if (!"BINARY".equalsIgnoreCase(value)) {
                originalValues.put(name, value);
            }
        });
        originalValues.keySet().forEach(name -> {
            Query alterSessionQuery = entityManager.createNativeQuery("ALTER SESSION SET " + name + " = BINARY");
            alterSessionQuery.executeUpdate();
        });
        List<KeyValueEntity> result = dataManager.loadValues(valueLoadContext);
        originalValues.forEach((name, value) -> {
            Query alterSessionQuery = entityManager.createNativeQuery("ALTER SESSION SET " + name + " = " + value);
            alterSessionQuery.executeUpdate();
        });
        return result;
    });
    return loadedValues == null ? Collections.emptyList() : loadedValues;
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) HashMap(java.util.HashMap) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) KeyValueEntity(io.jmix.core.entity.KeyValueEntity)

Aggregations

KeyValueEntity (io.jmix.core.entity.KeyValueEntity)23 ValueLoadContext (io.jmix.core.ValueLoadContext)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)3 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)3 EntityMap (io.jmix.reports.app.EntityMap)2 JmixTableData (io.jmix.reports.entity.JmixTableData)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 EntityManager (javax.persistence.EntityManager)2 Query (javax.persistence.Query)2 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)2 ValueLoadContext (com.haulmont.cuba.core.global.ValueLoadContext)1 BandData (com.haulmont.yarg.structure.BandData)1 Customer (dataaccess.ex1.entity.Customer)1 Customer (datamodel.ex1.entity.Customer)1 DataManager (io.jmix.core.DataManager)1 Entity (io.jmix.core.Entity)1 Metadata (io.jmix.core.Metadata)1 MetadataTools (io.jmix.core.MetadataTools)1 EntityListParameterValue (io.jmix.dashboards.model.parameter.type.EntityListParameterValue)1