use of org.hisp.dhis.schema.MergeParams in project dhis2-core by dhis2.
the class DefaultPreheatService method collectObjectReferences.
@SuppressWarnings("unchecked")
private Map<Class<?>, Map<String, Map<String, Object>>> collectObjectReferences(Map<Class<?>, List<?>> objects) {
Map<Class<?>, Map<String, Map<String, Object>>> map = new HashMap<>();
if (objects.isEmpty()) {
return map;
}
Map<Class<?>, List<?>> targets = new HashMap<>();
// clone objects list, we don't want to modify it
targets.putAll(objects);
collectScanTargets(targets);
for (Class<?> objectClass : targets.keySet()) {
Schema schema = schemaService.getDynamicSchema(objectClass);
if (!schema.isIdentifiableObject()) {
continue;
}
List<Property> properties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).collect(Collectors.toList());
List<IdentifiableObject> identifiableObjects = (List<IdentifiableObject>) targets.get(objectClass);
Map<String, Map<String, Object>> refMap = new HashMap<>();
map.put(objectClass, refMap);
for (IdentifiableObject object : identifiableObjects) {
refMap.put(object.getUid(), new HashMap<>());
properties.forEach(p -> {
if (!p.isCollection()) {
IdentifiableObject reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
if (reference != null) {
try {
IdentifiableObject identifiableObject = (IdentifiableObject) p.getKlass().newInstance();
mergeService.merge(new MergeParams<>(reference, identifiableObject));
refMap.get(object.getUid()).put(p.getName(), identifiableObject);
} catch (InstantiationException | IllegalAccessException ignored) {
}
}
} else {
Collection<IdentifiableObject> refObjects = ReflectionUtils.newCollectionInstance(p.getKlass());
Collection<IdentifiableObject> references = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
if (references != null) {
for (IdentifiableObject reference : references) {
try {
IdentifiableObject identifiableObject = (IdentifiableObject) p.getItemKlass().newInstance();
mergeService.merge(new MergeParams<>(reference, identifiableObject));
refObjects.add(identifiableObject);
} catch (InstantiationException | IllegalAccessException ignored) {
}
}
}
refMap.get(object.getUid()).put(p.getCollectionName(), refObjects);
}
});
}
}
return map;
}
use of org.hisp.dhis.schema.MergeParams in project dhis2-core by dhis2.
the class MergeServiceTest method mergeOrgUnitGroupSet.
@Test
public void mergeOrgUnitGroupSet() {
OrganisationUnit organisationUnitA = createOrganisationUnit('A');
OrganisationUnit organisationUnitB = createOrganisationUnit('B');
OrganisationUnit organisationUnitC = createOrganisationUnit('C');
OrganisationUnit organisationUnitD = createOrganisationUnit('D');
OrganisationUnitGroup organisationUnitGroupA = createOrganisationUnitGroup('A');
organisationUnitGroupA.getMembers().add(organisationUnitA);
organisationUnitGroupA.getMembers().add(organisationUnitB);
organisationUnitGroupA.getMembers().add(organisationUnitC);
organisationUnitGroupA.getMembers().add(organisationUnitD);
OrganisationUnitGroupSet organisationUnitGroupSetA = createOrganisationUnitGroupSet('A');
OrganisationUnitGroupSet organisationUnitGroupSetB = createOrganisationUnitGroupSet('B');
organisationUnitGroupSetA.addOrganisationUnitGroup(organisationUnitGroupA);
mergeService.merge(new MergeParams<>(organisationUnitGroupSetA, organisationUnitGroupSetB).setMergeMode(MergeMode.REPLACE));
assertFalse(organisationUnitGroupSetB.getOrganisationUnitGroups().isEmpty());
assertEquals(organisationUnitGroupSetA.getName(), organisationUnitGroupSetB.getName());
assertEquals(organisationUnitGroupSetA.getDescription(), organisationUnitGroupSetB.getDescription());
assertEquals(organisationUnitGroupSetA.isCompulsory(), organisationUnitGroupSetB.isCompulsory());
assertEquals(organisationUnitGroupSetA.isIncludeSubhierarchyInAnalytics(), organisationUnitGroupSetB.isIncludeSubhierarchyInAnalytics());
assertEquals(1, organisationUnitGroupSetB.getOrganisationUnitGroups().size());
}
use of org.hisp.dhis.schema.MergeParams in project dhis2-core by dhis2.
the class MergeServiceTest method simpleCollection.
@Test
public void simpleCollection() {
Date date = new Date();
SimpleCollection source = new SimpleCollection("name");
source.getSimples().add(new Simple("simple", 10, date, false, 123, 2.5f));
source.getSimples().add(new Simple("simple", 20, date, false, 123, 2.5f));
source.getSimples().add(new Simple("simple", 30, date, false, 123, 2.5f));
SimpleCollection target = new SimpleCollection("target");
mergeService.merge(new MergeParams<>(source, target).setMergeMode(MergeMode.MERGE));
assertEquals("name", target.getName());
assertEquals(3, target.getSimples().size());
assertTrue(target.getSimples().contains(source.getSimples().get(0)));
assertTrue(target.getSimples().contains(source.getSimples().get(1)));
assertTrue(target.getSimples().contains(source.getSimples().get(2)));
}
use of org.hisp.dhis.schema.MergeParams in project dhis2-core by dhis2.
the class UserController method replicateUser.
@SuppressWarnings("unchecked")
@PreAuthorize("hasRole('ALL') or hasRole('F_REPLICATE_USER')")
@RequestMapping(value = "/{uid}/replica", method = RequestMethod.POST)
public void replicateUser(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
User existingUser = userService.getUser(uid);
if (existingUser == null || existingUser.getUserCredentials() == null) {
throw new WebMessageException(WebMessageUtils.conflict("User not found: " + uid));
}
User currentUser = currentUserService.getCurrentUser();
if (!validateCreateUser(existingUser, currentUser)) {
return;
}
Map<String, String> auth = renderService.fromJson(request.getInputStream(), Map.class);
String username = StringUtils.trimToNull(auth != null ? auth.get(KEY_USERNAME) : null);
String password = StringUtils.trimToNull(auth != null ? auth.get(KEY_PASSWORD) : null);
if (auth == null || username == null) {
throw new WebMessageException(WebMessageUtils.conflict("Username must be specified"));
}
if (userService.getUserCredentialsByUsername(username) != null) {
throw new WebMessageException(WebMessageUtils.conflict("Username already taken: " + username));
}
if (password == null) {
throw new WebMessageException(WebMessageUtils.conflict("Password must be specified"));
}
if (!ValidationUtils.passwordIsValid(password)) {
throw new WebMessageException(WebMessageUtils.conflict("Password must have at least 8 characters, one digit, one uppercase"));
}
User userReplica = new User();
mergeService.merge(new MergeParams<>(existingUser, userReplica).setMergeMode(MergeMode.MERGE));
userReplica.setUid(CodeGenerator.generateUid());
userReplica.setCode(null);
userReplica.setCreated(new Date());
UserCredentials credentialsReplica = new UserCredentials();
mergeService.merge(new MergeParams<>(existingUser.getUserCredentials(), credentialsReplica).setMergeMode(MergeMode.MERGE));
credentialsReplica.setUid(CodeGenerator.generateUid());
credentialsReplica.setCode(null);
credentialsReplica.setCreated(new Date());
credentialsReplica.setLdapId(null);
credentialsReplica.setOpenId(null);
credentialsReplica.setUsername(username);
userService.encodeAndSetPassword(credentialsReplica, password);
userReplica.setUserCredentials(credentialsReplica);
credentialsReplica.setUserInfo(userReplica);
userService.addUser(userReplica);
userService.addUserCredentials(credentialsReplica);
userGroupService.addUserToGroups(userReplica, IdentifiableObjectUtils.getUids(existingUser.getGroups()), currentUser);
// ---------------------------------------------------------------------
// Replicate user settings
// ---------------------------------------------------------------------
List<UserSetting> settings = userSettingService.getUserSettings(existingUser);
for (UserSetting setting : settings) {
Optional<UserSettingKey> key = UserSettingKey.getByName(setting.getName());
key.ifPresent(userSettingKey -> userSettingService.saveUserSetting(userSettingKey, setting.getValue(), userReplica));
}
response.addHeader("Location", UserSchemaDescriptor.API_ENDPOINT + "/" + userReplica.getUid());
webMessageService.send(WebMessageUtils.created("User replica created"), response, request);
}
use of org.hisp.dhis.schema.MergeParams in project dhis2-core by dhis2.
the class MapController method putJsonObject.
@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void putJsonObject(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws Exception {
Map map = mappingService.getMap(uid);
if (map == null) {
throw new WebMessageException(WebMessageUtils.notFound("Map does not exist: " + uid));
}
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
Map newMap = deserializeJsonEntity(request, response);
newMap.setUid(uid);
mergeService.merge(new MergeParams<>(newMap, map).setMergeMode(params.getMergeMode()));
mappingService.updateMap(map);
}
Aggregations