public class SaveCandidacyDocumentFiles {
@Atomic
public static void run(List<CandidacyDocumentUploadBean> candidacyDocuments) {
Group masterDegreeAdministrativeOfficeGroup = RoleType.MASTER_DEGREE_ADMINISTRATIVE_OFFICE.actualGroup();
Group coordinatorsGroup = RoleType.COORDINATOR.actualGroup();
Group permittedGroup = masterDegreeAdministrativeOfficeGroup.or(coordinatorsGroup);
for (CandidacyDocumentUploadBean candidacyDocumentUploadBean : candidacyDocuments) {
if (candidacyDocumentUploadBean.getTemporaryFile() != null) {
String filename = candidacyDocumentUploadBean.getFilename();
CandidacyDocument candidacyDocument = candidacyDocumentUploadBean.getCandidacyDocument();
Candidacy candidacy = candidacyDocument.getCandidacy();
Person person = candidacy.getPerson();
final byte[] content = read(candidacyDocumentUploadBean.getTemporaryFile());
if (candidacyDocument.getFile() != null) {
candidacyDocument.getFile().delete();
}
final CandidacyDocumentFile candidacyDocumentFile =
new CandidacyDocumentFile(filename, filename, content, permittedGroup.or(UserGroup.of(person.getUser())));
candidacyDocument.setFile(candidacyDocumentFile);
}
}
}