use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class DefaultCollectionService method clearCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void clearCollectionItems(IdentifiableObject object, String pvProperty) throws WebMessageException, InvocationTargetException, IllegalAccessException {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (!schema.haveProperty(pvProperty)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + pvProperty + " does not exist on " + object.getClass().getName()));
}
Property property = schema.getProperty(pvProperty);
if (!property.isCollection() || !property.isIdentifiableObject()) {
throw new WebMessageException(WebMessageUtils.conflict("Only identifiable collections are allowed to be cleared."));
}
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
manager.refresh(object);
if (property.isOwner()) {
collection.clear();
manager.update(object);
} else {
for (IdentifiableObject itemObject : collection) {
Schema itemSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property itemProperty = itemSchema.propertyByRole(property.getOwningRole());
Collection<IdentifiableObject> itemCollection = (Collection<IdentifiableObject>) itemProperty.getGetterMethod().invoke(itemObject);
itemCollection.remove(object);
manager.update(itemObject);
manager.refresh(itemObject);
}
}
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class DefaultCollectionService method addCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void addCollectionItems(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 added to."));
}
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.add(item);
}
manager.update(object);
} 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.add(object);
manager.update(item);
}
} catch (Exception ex) {
}
}
}
dbmsManager.clearSession();
cacheManager.clearCache();
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class SmsController method sendSMSMessage.
@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(value = "/outbound", method = RequestMethod.POST, consumes = "application/json")
public void sendSMSMessage(HttpServletResponse response, HttpServletRequest request) throws WebMessageException, IOException {
OutboundSms sms = renderService.fromJson(request.getInputStream(), OutboundSms.class);
OutboundMessageResponse status = smsSender.sendMessage(null, sms.getMessage(), sms.getRecipients());
if (status.isOk()) {
webMessageService.send(WebMessageUtils.ok("SMS sent"), response, request);
} else {
throw new WebMessageException(WebMessageUtils.error(status.getDescription()));
}
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class SmsGatewayController method removeGateway.
// -------------------------------------------------------------------------
// DELETE
// -------------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(value = "/{uid}", method = RequestMethod.DELETE)
public void removeGateway(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
SmsGatewayConfig gateway = gatewayAdminService.getGatewayConfigurationByUid(uid);
if (gateway == null) {
throw new WebMessageException(WebMessageUtils.notFound("No gateway found with id: " + uid));
}
gatewayAdminService.removeGatewayByUid(uid);
webMessageService.send(WebMessageUtils.ok("Gateway removed successfully"), response, request);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class SmsGatewayController method setDefault.
@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(value = "/default/{uid}", method = RequestMethod.PUT)
public void setDefault(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
SmsGatewayConfig gateway = gatewayAdminService.getGatewayConfigurationByUid(uid);
if (gateway == null) {
throw new WebMessageException(WebMessageUtils.notFound("No gateway found"));
}
gatewayAdminService.setDefaultGateway(uid);
webMessageService.send(WebMessageUtils.ok(gateway.getName() + " is set to default"), response, request);
}
Aggregations