Search in sources :

Example 41 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class SchemaToDataFetcher method mapUniqueFields.

@SuppressWarnings("unchecked")
private List<? extends IdentifiableObject> mapUniqueFields(Schema schema) {
    List<Property> uniqueProperties = schema.getUniqueProperties();
    List objects = new ArrayList();
    if (!uniqueProperties.isEmpty()) {
        final String fields = extractUniqueFields(uniqueProperties);
        objects = sessionFactory.getCurrentSession().createQuery("SELECT " + fields + " from " + schema.getKlass().getSimpleName()).setReadOnly(true).getResultList();
    }
    // or a "simple" List if only one columns is used in the query
    return uniqueProperties.size() == 1 ? handleSingleColumn(objects, uniqueProperties, schema) : handleMultipleColumn(objects, uniqueProperties, schema);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.hisp.dhis.schema.Property)

Example 42 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class OrderTest method setUp.

@BeforeEach
void setUp() throws Exception {
    object1 = new TestObject();
    object2 = new TestObject();
    PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(object1, "value");
    valueProperty = new Property(String.class, propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod());
    valueProperty.setName("value");
    orderAsc = new Order(valueProperty, Direction.ASCENDING);
    orderDesc = new Order(valueProperty, Direction.DESCENDING);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Property(org.hisp.dhis.schema.Property) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 43 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultQueryPlannerTest method verifyPlanQueryReturnsPersistedAndNotPersistedQueries.

@Test
void verifyPlanQueryReturnsPersistedAndNotPersistedQueries() throws Exception {
    // Create schema with attributes
    final Attribute attribute = new Attribute();
    final Map<String, Property> propertyMap = new HashMap<>();
    addProperty(propertyMap, attribute, "id", true);
    addProperty(propertyMap, attribute, "uid", true);
    Schema schema = new OrganisationUnitSchemaDescriptor().getSchema();
    schema.setPropertyMap(propertyMap);
    // Add restriction
    Query query = Query.from(schema, Junction.Type.OR);
    query.add(Restrictions.eq("id", 100L));
    // method under test
    QueryPlan queryPlan = subject.planQuery(query, true);
    Query persistedQuery = queryPlan.getPersistedQuery();
    assertTrue(persistedQuery.isPlannedQuery());
    assertEquals(persistedQuery.getCriterions().size(), 1);
    assertEquals(persistedQuery.getFirstResult().intValue(), 0);
    assertEquals(persistedQuery.getMaxResults().intValue(), Integer.MAX_VALUE);
    assertEquals(persistedQuery.getRootJunctionType(), Junction.Type.OR);
    Query nonPersistedQuery = queryPlan.getNonPersistedQuery();
    assertEquals(nonPersistedQuery.getCriterions().size(), 0);
    assertTrue(nonPersistedQuery.isPlannedQuery());
}
Also used : OrganisationUnitSchemaDescriptor(org.hisp.dhis.schema.descriptors.OrganisationUnitSchemaDescriptor) Query(org.hisp.dhis.query.Query) Attribute(org.hisp.dhis.attribute.Attribute) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property) Test(org.junit.jupiter.api.Test)

Example 44 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultQueryPlannerTest method verifyPlanQueryReturnsNonPersistedQueryWithCriterion2.

@Test
void verifyPlanQueryReturnsNonPersistedQueryWithCriterion2() throws Exception {
    // Create schema with attributes
    final Attribute attribute = new Attribute();
    final Map<String, Property> propertyMap = new HashMap<>();
    addProperty(propertyMap, attribute, "id", true);
    addProperty(propertyMap, attribute, "uid", true);
    addProperty(propertyMap, attribute, "name", true);
    Schema schema = new OrganisationUnitSchemaDescriptor().getSchema();
    schema.setPropertyMap(propertyMap);
    // Add restrictions on a non persisted field
    Query query = Query.from(schema, Junction.Type.AND);
    query.setMaxResults(10);
    query.setFirstResult(500);
    query.add(Restrictions.eq("name", "test"));
    query.add(Restrictions.eq("id", 100));
    // method under test
    QueryPlan queryPlan = subject.planQuery(query, false);
    Query persistedQuery = queryPlan.getPersistedQuery();
    assertTrue(persistedQuery.isPlannedQuery());
    assertEquals(persistedQuery.getCriterions().size(), 2);
    assertEquals(persistedQuery.getFirstResult().intValue(), 500);
    assertEquals(persistedQuery.getMaxResults().intValue(), 10);
    assertEquals(persistedQuery.getRootJunctionType(), Junction.Type.AND);
    Query nonPersistedQuery = queryPlan.getNonPersistedQuery();
    assertEquals(nonPersistedQuery.getCriterions().size(), 0);
    assertTrue(nonPersistedQuery.isPlannedQuery());
    assertEquals(nonPersistedQuery.getRootJunctionType(), Junction.Type.AND);
}
Also used : OrganisationUnitSchemaDescriptor(org.hisp.dhis.schema.descriptors.OrganisationUnitSchemaDescriptor) Query(org.hisp.dhis.query.Query) Attribute(org.hisp.dhis.attribute.Attribute) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property) Test(org.junit.jupiter.api.Test)

Example 45 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultQueryPlannerTest method addProperty.

private void addProperty(Map<String, Property> propertyMap, Object bean, String property, boolean persisted) throws Exception {
    PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(bean, property);
    Property p = new Property(pd.getPropertyType(), pd.getReadMethod(), pd.getWriteMethod());
    p.setName(pd.getName());
    p.setReadable(true);
    p.setPersisted(persisted);
    propertyMap.put(pd.getName(), p);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Property(org.hisp.dhis.schema.Property)

Aggregations

Property (org.hisp.dhis.schema.Property)126 Schema (org.hisp.dhis.schema.Schema)69 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)26 Collection (java.util.Collection)21 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)21 List (java.util.List)20 Map (java.util.Map)16 Test (org.junit.jupiter.api.Test)16 Attribute (org.hisp.dhis.attribute.Attribute)14 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)14 Collectors (java.util.stream.Collectors)13 User (org.hisp.dhis.user.User)13 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 SimpleNode (org.hisp.dhis.node.types.SimpleNode)12 Query (org.hisp.dhis.query.Query)12 SchemaService (org.hisp.dhis.schema.SchemaService)12 Transactional (org.springframework.transaction.annotation.Transactional)12