use of org.hisp.dhis.dxf2.metadata.feedback.ImportReport in project dhis2-core by dhis2.
the class UserController method putJsonObject.
@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
public void putJsonObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
List<User> users = getEntity(pvUid, NO_WEB_OPTIONS);
if (users.isEmpty()) {
throw new WebMessageException(WebMessageUtils.conflict(getEntityName() + " does not exist: " + pvUid));
}
User currentUser = currentUserService.getCurrentUser();
if (!aclService.canUpdate(currentUser, users.get(0))) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this user.");
}
User parsed = renderService.fromJson(request.getInputStream(), getEntityClass());
parsed.setUid(pvUid);
if (!userService.canAddOrUpdateUser(IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser)) {
throw new WebMessageException(WebMessageUtils.conflict("You must have permissions to create user, or ability to manage at least one user group for the user."));
}
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
params.setImportReportMode(ImportReportMode.FULL);
params.setImportStrategy(ImportStrategy.UPDATE);
params.addObject(parsed);
ImportReport importReport = importService.importMetadata(params);
if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
User user = userService.getUser(pvUid);
userGroupService.updateUserGroups(user, IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser);
}
renderService.toJson(response.getOutputStream(), importReport);
}
use of org.hisp.dhis.dxf2.metadata.feedback.ImportReport in project dhis2-core by dhis2.
the class UserController method inviteUser.
/**
* Creates a user invitation and invites the user.
*
* @param user user object parsed from the POST request.
*/
private ObjectReport inviteUser(User user, User currentUser, HttpServletRequest request) throws Exception {
RestoreOptions restoreOptions = user.getUsername() == null || user.getUsername().isEmpty() ? RestoreOptions.INVITE_WITH_USERNAME_CHOICE : RestoreOptions.INVITE_WITH_DEFINED_USERNAME;
securityService.prepareUserForInvite(user);
ImportReport importReport = createUser(user, currentUser);
ObjectReport objectReport = getObjectReport(importReport);
if (importReport.getStatus() == Status.OK && importReport.getStats().getCreated() == 1) {
securityService.sendRestoreMessage(user.getUserCredentials(), ContextUtils.getContextPath(request), restoreOptions);
}
return objectReport;
}
use of org.hisp.dhis.dxf2.metadata.feedback.ImportReport in project dhis2-core by dhis2.
the class UserController method putXmlObject.
// -------------------------------------------------------------------------
// PUT
// -------------------------------------------------------------------------
@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = { "application/xml", "text/xml" })
public void putXmlObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
List<User> users = getEntity(pvUid, NO_WEB_OPTIONS);
if (users.isEmpty()) {
throw new WebMessageException(WebMessageUtils.conflict(getEntityName() + " does not exist: " + pvUid));
}
User currentUser = currentUserService.getCurrentUser();
if (!aclService.canUpdate(currentUser, users.get(0))) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this user.");
}
User parsed = renderService.fromXml(request.getInputStream(), getEntityClass());
parsed.setUid(pvUid);
if (!userService.canAddOrUpdateUser(IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser)) {
throw new WebMessageException(WebMessageUtils.conflict("You must have permissions to create user, or ability to manage at least one user group for the user."));
}
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
params.setImportReportMode(ImportReportMode.FULL);
params.setImportStrategy(ImportStrategy.UPDATE);
params.addObject(parsed);
ImportReport importReport = importService.importMetadata(params);
if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
User user = userService.getUser(pvUid);
userGroupService.updateUserGroups(user, IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser);
}
renderService.toXml(response.getOutputStream(), importReport);
}
use of org.hisp.dhis.dxf2.metadata.feedback.ImportReport in project dhis2-core by dhis2.
the class CsvMetadataImportTest method testOptionSetImport.
@Test
public void testOptionSetImport() throws Exception {
input = new ClassPathResource("metadata/optionSets.csv").getInputStream();
Metadata metadata = csvImportService.fromCsv(input, OptionSet.class);
assertEquals(4, metadata.getOptionSets().size());
assertEquals(3, metadata.getOptionSets().get(0).getOptions().size());
assertEquals(3, metadata.getOptionSets().get(1).getOptions().size());
assertEquals(3, metadata.getOptionSets().get(2).getOptions().size());
assertEquals(3, metadata.getOptionSets().get(3).getOptions().size());
MetadataImportParams params = new MetadataImportParams();
params.addMetadata(schemaService.getMetadataSchemas(), metadata);
ImportReport importReport = importService.importMetadata(params);
assertEquals(16, importReport.getStats().getCreated());
List<OptionSet> optionSets = new ArrayList<>(optionService.getAllOptionSets());
assertEquals(4, optionSets.size());
assertEquals(3, optionSets.get(0).getOptions().size());
assertEquals(3, optionSets.get(1).getOptions().size());
assertEquals(3, optionSets.get(2).getOptions().size());
assertEquals(3, optionSets.get(3).getOptions().size());
}
use of org.hisp.dhis.dxf2.metadata.feedback.ImportReport in project dhis2-core by dhis2.
the class CsvMetadataImportTest method testDataElementImport.
@Test
public void testDataElementImport() throws Exception {
input = new ClassPathResource("metadata/dataElements.csv").getInputStream();
Metadata metadata = csvImportService.fromCsv(input, DataElement.class);
assertEquals(2, metadata.getDataElements().size());
MetadataImportParams params = new MetadataImportParams();
params.addMetadata(schemaService.getMetadataSchemas(), metadata);
ImportReport importReport = importService.importMetadata(params);
assertEquals(2, importReport.getStats().getCreated());
Collection<DataElement> dataElements = dataElementService.getAllDataElements();
assertEquals(2, dataElements.size());
}
Aggregations