Skip to content

Commit

Permalink
Merge pull request #311 from consiglionazionaledellericerche/231-inse…
Browse files Browse the repository at this point in the history
…rire-metodi-rest-per-importareesportare-inizializzazioni

231 inserire metodi rest per importareesportare inizializzazioni
  • Loading branch information
darietto1983 authored Dec 17, 2024
2 parents 0e3424f + def1985 commit 3bf6ce9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
43 changes: 29 additions & 14 deletions app/controllers/Instances.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import cnr.sync.dto.v3.PersonConfigurationList;
import cnr.sync.dto.v3.PersonConfigurationShowDto;
import cnr.sync.dto.v3.PersonResidualDto;
import dao.GroupDao;
import dao.OfficeDao;
import dao.PersonDao;
import dao.QualificationDao;
Expand Down Expand Up @@ -124,6 +125,8 @@ public class Instances extends Controller {
static QualificationDao qualificationDao;
@Inject
static ImportManager importManager;
@Inject
static GroupDao groupDao;

public static void importInstance() {
render();
Expand Down Expand Up @@ -233,7 +236,7 @@ public static void importOfficeConfiguration(String instance, String codeId) {
for (ConfigurationOfficeDto dto : list) {
importManager.importConfig(dto, office.getId());
}

flash.success("Importata configurazione per la sede {}", office.getName());
render("@importInfo", instance, codeId, office);
}

Expand Down Expand Up @@ -387,30 +390,42 @@ public static void importGroups(String instance, String codeId) {
log.error("Errore di connessione: {}", httpResponse.getStatusText());
throw new ApiRequestException("Unauthorized");
}
Office office = officeDao.byCodeId(codeId).get();
List<GroupShowDto> list = (List<GroupShowDto>) new Gson()
.fromJson(httpResponse.getJson(), new TypeToken<List<GroupShowDto>>() {}.getType());
Group group = null;
for (GroupShowDto dto : list) {
group = new Group();
group.setDescription(dto.getDescription());
group.setEndDate(Strings.isNullOrEmpty(dto.getEndDate()) ? null : java.time.LocalDate.parse(dto.getEndDate()));
group.setName(dto.getName());
group.setOffice(officeDao.byCodeId(codeId).get());
group.setManager(personDao.getPersonByNumber(dto.getManager()));
group.save();
for (PersonAffiliationShowDto pasDto : dto.getList()) {
log.debug("Inizio a costruire le affiliazioni");
if (groupDao.checkGroupByOfficeAndName(office, dto.getName()).isPresent()) {
log.debug("Gruppo {} già esistente, procedo solo con le affiliazioni", dto.getName());
group = groupDao.checkGroupByOfficeAndName(office, dto.getName()).get();
} else {
group = new Group();
group.setDescription(dto.getDescription());
group.setEndDate(Strings.isNullOrEmpty(dto.getEndDate()) ? null : java.time.LocalDate.parse(dto.getEndDate()));
group.setName(dto.getName());
group.setOffice(officeDao.byCodeId(codeId).get());
group.setManager(personDao.getPersonByNumber(dto.getManager()));
group.save();
}
log.debug("Inizio a costruire le affiliazioni");
for (PersonAffiliationShowDto pasDto : dto.getList()) {
Person person = personDao.getPersonByNumber(pasDto.getNumber());
if (person == null) {
continue;
}
log.debug("Creo affiliazione per {}", person.getFullname());
Affiliation affiliation = new Affiliation();
affiliation.setPerson(personDao.getPersonByNumber(pasDto.getNumber()));
affiliation.setBeginDate(java.time.LocalDate.parse(pasDto.getBeginDate()));
affiliation.setEndDate(pasDto.getBeginDate() != null ?
java.time.LocalDate.parse(pasDto.getBeginDate()) : null);
affiliation.setEndDate(!Strings.isNullOrEmpty(pasDto.getEndDate()) ?
java.time.LocalDate.parse(pasDto.getEndDate()) : null);
affiliation.setGroup(group);
affiliation.save();
log.debug("Salvata affiliazione della matricola {} al gruppo {}", pasDto.getNumber(), group.getName());
log.debug("Salvata affiliazione di {} al gruppo {}", person.getFullname(), group.getName());
}
log.debug("Inserito gruppo {} con manager {}", group.getName(), group.getManager().getFullname());
}
Office office = officeDao.byCodeId(codeId).get();

flash.success("Importati %s gruppi", list.size());
render("@importInfo", instance, codeId, office);
}
Expand Down
6 changes: 6 additions & 0 deletions app/dao/GroupDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,10 @@ public Optional<Group> checkManagerPerson(Person manager, Person person) {
.fetchFirst();
return Optional.fromNullable(result);
}

public Optional<Group> checkGroupByOfficeAndName(Office office, String name) {
final QGroup group = QGroup.group;
return Optional.fromNullable(getQueryFactory().selectFrom(group)
.where(group.office.eq(office).and(group.name.equalsIgnoreCase(name))).fetchFirst());
}
}

0 comments on commit 3bf6ce9

Please sign in to comment.