Search in sources :

Example 26 with Schema

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

the class MetadataImportParams method addMetadata.

@SuppressWarnings("unchecked")
public MetadataImportParams addMetadata(List<Schema> schemas, Metadata metadata) {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objectMap = new HashMap<>();
    for (Schema schema : schemas) {
        Object value = ReflectionUtils.invokeGetterMethod(schema.getPlural(), metadata);
        if (value != null) {
            if (Collection.class.isAssignableFrom(value.getClass()) && schema.isIdentifiableObject()) {
                List<IdentifiableObject> objects = new ArrayList<>((Collection<IdentifiableObject>) value);
                if (!objects.isEmpty()) {
                    objectMap.put((Class<? extends IdentifiableObject>) schema.getKlass(), objects);
                }
            }
        }
    }
    setObjects(objectMap);
    return this;
}
Also used : HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 27 with Schema

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

the class DefaultCollectionService method delCollectionItems.

@Override
@SuppressWarnings("unchecked")
public void delCollectionItems(IdentifiableObject object, String propertyName, List<IdentifiableObject> objects) throws Exception {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    if (!aclService.canUpdate(currentUserService.getCurrentUser(), object)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    if (!schema.haveProperty(propertyName)) {
        throw new WebMessageException(WebMessageUtils.notFound("Property " + propertyName + " does not exist on " + object.getClass().getName()));
    }
    Property property = schema.getProperty(propertyName);
    if (!property.isCollection() || !property.isIdentifiableObject()) {
        throw new WebMessageException(WebMessageUtils.conflict("Only identifiable object collections can be removed from."));
    }
    Collection<String> itemCodes = objects.stream().map(IdentifiableObject::getUid).collect(Collectors.toList());
    if (itemCodes.isEmpty()) {
        return;
    }
    List<? extends IdentifiableObject> items = manager.get(((Class<? extends IdentifiableObject>) property.getItemKlass()), itemCodes);
    manager.refresh(object);
    if (property.isOwner()) {
        Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
        for (IdentifiableObject item : items) {
            if (collection.contains(item))
                collection.remove(item);
        }
    } else {
        Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
        Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
        for (IdentifiableObject item : items) {
            try {
                Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke(item);
                if (collection.contains(object)) {
                    collection.remove(object);
                    manager.update(item);
                }
            } catch (Exception ex) {
            }
        }
    }
    manager.update(object);
    dbmsManager.clearSession();
    cacheManager.clearCache();
}
Also used : UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 28 with Schema

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

the class DefaultMetadataExportService method getParamsFromMap.

@Override
@SuppressWarnings("unchecked")
public MetadataExportParams getParamsFromMap(Map<String, List<String>> parameters) {
    MetadataExportParams params = new MetadataExportParams();
    Map<Class<? extends IdentifiableObject>, Map<String, List<String>>> map = new HashMap<>();
    if (parameters.containsKey("fields")) {
        params.setDefaultFields(parameters.get("fields"));
        parameters.remove("fields");
    }
    if (parameters.containsKey("filter")) {
        params.setDefaultFilter(parameters.get("filter"));
        parameters.remove("filter");
    }
    if (parameters.containsKey("order")) {
        params.setDefaultOrder(parameters.get("order"));
        parameters.remove("order");
    }
    for (String parameterKey : parameters.keySet()) {
        String[] parameter = parameterKey.split(":");
        Schema schema = schemaService.getSchemaByPluralName(parameter[0]);
        if (schema == null || !schema.isIdentifiableObject()) {
            continue;
        }
        Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) schema.getKlass();
        // class is enabled if value = true, or fields/filter/order is present
        if ("true".equalsIgnoreCase(parameters.get(parameterKey).get(0)) || (parameter.length > 1 && ("fields".equalsIgnoreCase(parameter[1]) || "filter".equalsIgnoreCase(parameter[1]) || "order".equalsIgnoreCase(parameter[1])))) {
            if (!map.containsKey(klass))
                map.put(klass, new HashMap<>());
        } else {
            continue;
        }
        if (parameter.length > 1) {
            if ("fields".equalsIgnoreCase(parameter[1])) {
                if (!map.get(klass).containsKey("fields"))
                    map.get(klass).put("fields", new ArrayList<>());
                map.get(klass).get("fields").addAll(parameters.get(parameterKey));
            }
            if ("filter".equalsIgnoreCase(parameter[1])) {
                if (!map.get(klass).containsKey("filter"))
                    map.get(klass).put("filter", new ArrayList<>());
                map.get(klass).get("filter").addAll(parameters.get(parameterKey));
            }
            if ("order".equalsIgnoreCase(parameter[1])) {
                if (!map.get(klass).containsKey("order"))
                    map.get(klass).put("order", new ArrayList<>());
                map.get(klass).get("order").addAll(parameters.get(parameterKey));
            }
        }
    }
    map.keySet().forEach(params::addClass);
    for (Class<? extends IdentifiableObject> klass : map.keySet()) {
        Map<String, List<String>> classMap = map.get(klass);
        Schema schema = schemaService.getDynamicSchema(klass);
        if (classMap.containsKey("fields"))
            params.addFields(klass, classMap.get("fields"));
        if (classMap.containsKey("filter") && classMap.containsKey("order")) {
            OrderParams orderParams = new OrderParams(Sets.newHashSet(classMap.get("order")));
            Query query = queryService.getQueryFromUrl(klass, classMap.get("filter"), orderParams.getOrders(schema));
            query.setDefaultOrder();
            params.addQuery(query);
        } else if (classMap.containsKey("filter")) {
            Query query = queryService.getQueryFromUrl(klass, classMap.get("filter"), new ArrayList<>());
            query.setDefaultOrder();
            params.addQuery(query);
        } else if (classMap.containsKey("order")) {
            OrderParams orderParams = new OrderParams();
            orderParams.setOrder(Sets.newHashSet(classMap.get("order")));
            Query query = queryService.getQueryFromUrl(klass, new ArrayList<>(), orderParams.getOrders(schema));
            query.setDefaultOrder();
            params.addQuery(query);
        }
    }
    return params;
}
Also used : Query(org.hisp.dhis.query.Query) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) List(java.util.List) ArrayList(java.util.ArrayList) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) Map(java.util.Map) SetMap(org.hisp.dhis.common.SetMap) HashMap(java.util.HashMap)

Example 29 with Schema

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

the class DefaultLinkService method generateLink.

private <T> void generateLink(T object, String hrefBase, boolean deepScan) {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    if (schema == null) {
        log.warn("Could not find schema for object of type " + object.getClass().getName() + ".");
        return;
    }
    generateHref(object, hrefBase);
    if (!deepScan) {
        return;
    }
    for (Property property : schema.getProperties()) {
        try {
            // TODO should we support non-idObjects?
            if (property.isIdentifiableObject()) {
                Object propertyObject = property.getGetterMethod().invoke(object);
                if (propertyObject == null) {
                    continue;
                }
                // unwrap hibernate PersistentCollection
                if (PersistentCollection.class.isAssignableFrom(propertyObject.getClass())) {
                    PersistentCollection collection = (PersistentCollection) propertyObject;
                    propertyObject = collection.getValue();
                }
                if (!property.isCollection()) {
                    generateHref(propertyObject, hrefBase);
                } else {
                    Collection<?> collection = (Collection<?>) propertyObject;
                    for (Object collectionObject : collection) {
                        generateHref(collectionObject, hrefBase);
                    }
                }
            }
        } catch (InvocationTargetException | IllegalAccessException ignored) {
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Property(org.hisp.dhis.schema.Property) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 30 with Schema

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

the class OAuth2ClientSchemaDescriptor method getSchema.

@Override
public Schema getSchema() {
    Schema schema = new Schema(OAuth2Client.class, SINGULAR, PLURAL);
    schema.setRelativeApiEndpoint(API_ENDPOINT);
    schema.setOrder(1030);
    schema.getAuthorities().add(new Authority(AuthorityType.READ, Lists.newArrayList("F_OAUTH2_CLIENT_MANAGE")));
    schema.getAuthorities().add(new Authority(AuthorityType.CREATE, Lists.newArrayList("F_OAUTH2_CLIENT_MANAGE")));
    schema.getAuthorities().add(new Authority(AuthorityType.DELETE, Lists.newArrayList("F_OAUTH2_CLIENT_MANAGE")));
    return schema;
}
Also used : Authority(org.hisp.dhis.security.Authority) Schema(org.hisp.dhis.schema.Schema)

Aggregations

Schema (org.hisp.dhis.schema.Schema)149 Authority (org.hisp.dhis.security.Authority)65 Property (org.hisp.dhis.schema.Property)29 ArrayList (java.util.ArrayList)20 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)20 Test (org.junit.Test)16 Collection (java.util.Collection)14 List (java.util.List)13 HashMap (java.util.HashMap)12 DhisSpringTest (org.hisp.dhis.DhisSpringTest)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 Map (java.util.Map)10 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)10 User (org.hisp.dhis.user.User)10 AnalyticalObject (org.hisp.dhis.common.AnalyticalObject)9 BaseAnalyticalObject (org.hisp.dhis.common.BaseAnalyticalObject)9 UserCredentials (org.hisp.dhis.user.UserCredentials)9 HashSet (java.util.HashSet)8 Set (java.util.Set)8 Log (org.apache.commons.logging.Log)8