2023-11-29 15:56:25 +01:00
|
|
|
package eu.old.eudat.migration;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import eu.eudat.commons.JsonHandlingService;
|
|
|
|
import eu.eudat.commons.enums.IsActive;
|
|
|
|
import eu.eudat.commons.enums.ReferenceSourceType;
|
|
|
|
import eu.eudat.commons.types.user.AdditionalInfoEntity;
|
|
|
|
import eu.eudat.data.ReferenceEntity;
|
|
|
|
import eu.eudat.data.UserEntity;
|
|
|
|
import eu.eudat.model.User;
|
|
|
|
import eu.eudat.query.ReferenceQuery;
|
|
|
|
import eu.eudat.query.UserQuery;
|
|
|
|
import eu.old.eudat.data.dao.entities.UserInfoDao;
|
|
|
|
import eu.old.eudat.data.entities.UserInfo;
|
|
|
|
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
|
|
|
import gr.cite.tools.data.query.Ordering;
|
|
|
|
import gr.cite.tools.data.query.Paging;
|
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
import jakarta.persistence.EntityManager;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2024-02-21 17:38:28 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
2023-11-29 15:56:25 +01:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class UserMigrationService {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserMigrationService.class));
|
|
|
|
|
|
|
|
private static final int PageSize = 500;
|
|
|
|
private static final boolean TestMode = false;
|
|
|
|
private final DatabaseRepository databaseRepository;
|
|
|
|
private final EntityManager entityManager;
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final JsonHandlingService jsonHandlingService;
|
|
|
|
public UserMigrationService(DatabaseRepository databaseRepository, EntityManager entityManager, QueryFactory queryFactory, JsonHandlingService jsonHandlingService) {
|
|
|
|
this.databaseRepository = databaseRepository;
|
|
|
|
this.entityManager = entityManager;
|
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
this.jsonHandlingService = jsonHandlingService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void migrate() throws JsonProcessingException {
|
|
|
|
UserInfoDao userInfoDao = databaseRepository.getUserInfoDao();
|
|
|
|
long total = userInfoDao.asQueryable().count();
|
|
|
|
logger.debug("Migrate Contacts Total : " + total);
|
|
|
|
int page = 0;
|
|
|
|
|
|
|
|
List<UserEntity> items;
|
2024-02-21 17:38:28 +01:00
|
|
|
List<ReferenceEntity> organizations = this.queryFactory.query(ReferenceQuery.class).typeIds(ReferenceTypeIds.Organizations).isActive(IsActive.Active).collect();
|
2023-11-29 15:56:25 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
UserQuery userQuery = this.queryFactory.query(UserQuery.class);
|
|
|
|
userQuery.setOrder(new Ordering().addDescending(User._createdAt).addDescending(User._id));
|
|
|
|
userQuery.setPage(new Paging(page * PageSize, PageSize));
|
|
|
|
items = userQuery.collect();
|
|
|
|
|
|
|
|
if (items != null && !items.isEmpty()) {
|
|
|
|
logger.debug("Migrate User " + page * PageSize + " of " + total);
|
|
|
|
List<UserEntity> finalItems = items;
|
|
|
|
Map<UUID, UserInfo> oldUsers = userInfoDao.asQueryable().where((builder, root) -> root.get("id").in(finalItems.stream().map(UserEntity::getId).toList())).toList().stream().collect(Collectors.toMap(UserInfo::getId, x-> x));
|
|
|
|
|
|
|
|
for (UserEntity item : items) {
|
|
|
|
UserInfo oldUser = oldUsers.getOrDefault(item.getId(), null);
|
|
|
|
String avatarUrl = "";
|
|
|
|
String culture = "en-001";
|
|
|
|
String language = "en";
|
2024-01-09 17:28:04 +01:00
|
|
|
String timezone = "UTC";
|
2023-11-29 15:56:25 +01:00
|
|
|
String roleOrganization = "";
|
|
|
|
UUID organizationId = null;
|
|
|
|
if (oldUser != null){
|
|
|
|
try {
|
|
|
|
avatarUrl = oldUser.getAdditionalinfo() != null ? new ObjectMapper().readTree(oldUser.getAdditionalinfo()).get("avatarUrl").asText() : "";
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
culture = oldUser.getAdditionalinfo() != null ? new ObjectMapper().readTree(oldUser.getAdditionalinfo()).get("culture").get("name").asText() : "";
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
language = oldUser.getAdditionalinfo() != null ? new ObjectMapper().readTree(oldUser.getAdditionalinfo()).get("language").get("value").asText() : "";
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
timezone = oldUser.getAdditionalinfo() != null ? new ObjectMapper().readTree(oldUser.getAdditionalinfo()).get("timezone").asText() : "";
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
roleOrganization = oldUser.getAdditionalinfo() != null ? new ObjectMapper().readTree(oldUser.getAdditionalinfo()).get("roleOrganization").asText() : "";
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
String organizationReference = oldUser.getAdditionalinfo() != null ? new ObjectMapper().readTree(oldUser.getAdditionalinfo()).get("organization").get("reference").asText() : "";
|
|
|
|
String[] referenceParts = organizationReference.split("::", 2);
|
|
|
|
if (referenceParts.length == 2){
|
|
|
|
organizationId = organizations.stream().filter(x-> x.getReference().equalsIgnoreCase(referenceParts[1]) && x.getSource().equalsIgnoreCase(referenceParts[0])).map(ReferenceEntity::getId).findFirst().orElse(null);
|
|
|
|
if (organizationId == null){
|
|
|
|
String organizationName = oldUser.getAdditionalinfo() != null ? new ObjectMapper().readTree(oldUser.getAdditionalinfo()).get("organization").get("name").asText() : "";
|
|
|
|
if (organizationName != null && !organizationName.isBlank()) {
|
|
|
|
ReferenceEntity data = new ReferenceEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setLabel(organizationName);
|
|
|
|
data.setAbbreviation(null);
|
|
|
|
data.setIsActive(IsActive.Active);
|
2024-02-21 17:38:28 +01:00
|
|
|
data.setTypeId(ReferenceTypeIds.Organizations);
|
2023-11-29 15:56:25 +01:00
|
|
|
data.setCreatedAt(item.getCreatedAt());
|
|
|
|
data.setUpdatedAt(item.getUpdatedAt());
|
|
|
|
data.setReference(referenceParts[1]);
|
|
|
|
data.setSource(referenceParts[0]);
|
|
|
|
data.setSourceType(ReferenceSourceType.External);
|
|
|
|
organizationId = data.getId();
|
|
|
|
this.entityManager.persist(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AdditionalInfoEntity data = new AdditionalInfoEntity();
|
|
|
|
data.setOrganizationId(organizationId);
|
|
|
|
data.setRoleOrganization(roleOrganization);
|
|
|
|
data.setCulture(culture);
|
|
|
|
data.setTimezone(timezone);
|
|
|
|
data.setLanguage(language);
|
|
|
|
data.setAvatarUrl(avatarUrl);
|
|
|
|
item.setAdditionalInfo(this.jsonHandlingService.toJson(data));
|
|
|
|
this.entityManager.persist(item);
|
|
|
|
}
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
page++;
|
|
|
|
}
|
|
|
|
} while (items != null && !items.isEmpty() && !TestMode);
|
|
|
|
}
|
|
|
|
}
|