use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class TestUtils method getFieldDescriptors.
public static Set<FieldDescriptor> getFieldDescriptors(Schema schema) {
Set<FieldDescriptor> fieldDescriptors = new HashSet<>();
Map<String, Property> persistedPropertyMap = schema.getPersistedProperties();
for (String s : persistedPropertyMap.keySet()) {
Property p = persistedPropertyMap.get(s);
FieldDescriptor f = fieldWithPath(p.key()).description(TestUtils.getFieldDescription(p));
if (!p.isRequired()) {
f.optional().type(p.getPropertyType());
}
fieldDescriptors.add(f);
}
Map<String, Property> nonPersistedPropertyMap = schema.getNonPersistedProperties();
for (String s : nonPersistedPropertyMap.keySet()) {
Property p = nonPersistedPropertyMap.get(s);
FieldDescriptor f = fieldWithPath(p.key()).description(TestUtils.getFieldDescription(p));
if (!p.isRequired()) {
f.optional().type(p.getPropertyType());
}
fieldDescriptors.add(f);
}
return fieldDescriptors;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class GeoJsonAttributesCheckTest method setUpTest.
@BeforeEach
public void setUpTest() {
organisationUnit = new OrganisationUnit();
organisationUnit.setName("A");
attribute = new Attribute();
attribute.setUid("geoJson");
attribute.setName("geoJson");
validationContext = Mockito.mock(ValidationContext.class);
Schema schema = new Schema(OrganisationUnit.class, "organisationUnit", "organisationUnits");
Property property = new Property();
property.setPersisted(true);
schema.getPropertyMap().put("attributeValues", property);
SchemaService schemaService = Mockito.mock(SchemaService.class);
when(schemaService.getDynamicSchema(OrganisationUnit.class)).thenReturn(schema);
when(validationContext.getSchemaService()).thenReturn(schemaService);
Preheat preheat = Mockito.mock(Preheat.class);
when(preheat.getAttributeIdsByValueType(OrganisationUnit.class, ValueType.GEOJSON)).thenReturn(Sets.newSet("geoJson"));
objectBundle = Mockito.mock(ObjectBundle.class);
when(objectBundle.getPreheat()).thenReturn(preheat);
}
use of org.hisp.dhis.schema.Property 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(HibernateProxyUtils.getRealClass(object));
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) {
}
}
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class AbstractCrudController method getCollectionItem.
//--------------------------------------------------------------------------
// Identifiable object collections add, delete
//--------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/{property}/{itemId}", method = RequestMethod.GET)
@ResponseBody
public RootNode getCollectionItem(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @PathVariable("itemId") String pvItemId, @RequestParam Map<String, String> parameters, TranslateParams translateParams, HttpServletRequest request, HttpServletResponse response) throws Exception {
User user = currentUserService.getCurrentUser();
setUserContext(user, translateParams);
if (!aclService.canRead(user, getEntityClass())) {
throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
}
RootNode rootNode = getObjectInternal(pvUid, parameters, Lists.newArrayList(), Lists.newArrayList(pvProperty + "[:all]"), user);
// TODO optimize this using field filter (collection filtering)
if (!rootNode.getChildren().isEmpty() && rootNode.getChildren().get(0).isCollection()) {
rootNode.getChildren().get(0).getChildren().stream().filter(Node::isComplex).forEach(node -> {
node.getChildren().stream().filter(child -> child.isSimple() && child.getName().equals("id") && !((SimpleNode) child).getValue().equals(pvItemId)).forEach(child -> rootNode.getChildren().get(0).removeChild(node));
});
}
if (rootNode.getChildren().isEmpty() || rootNode.getChildren().get(0).getChildren().isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(pvProperty + " with ID " + pvItemId + " could not be found."));
}
return rootNode;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class AbstractCrudController method updateObjectProperty.
@RequestMapping(value = "/{uid}/{property}", method = { RequestMethod.PUT, RequestMethod.PATCH })
public void updateObjectProperty(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
WebOptions options = new WebOptions(rpParameters);
List<T> entities = getEntity(pvUid, options);
if (entities.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
}
if (!getSchema().haveProperty(pvProperty)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + pvProperty + " does not exist on " + getEntityName()));
}
Property property = getSchema().getProperty(pvProperty);
T persistedObject = entities.get(0);
if (!aclService.canUpdate(currentUserService.getCurrentUser(), persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
if (!property.isWritable()) {
throw new UpdateAccessDeniedException("This property is read-only.");
}
T object = deserialize(request);
if (object == null) {
throw new WebMessageException(WebMessageUtils.badRequest("Unknown payload format."));
}
Object value = property.getGetterMethod().invoke(object);
property.getSetterMethod().invoke(persistedObject, value);
manager.update(persistedObject);
postPatchEntity(persistedObject);
}
Aggregations