Skip to content
Snippets Groups Projects

Add auditing for the Feedback class

Files

/*
* Queue - A Queueing system that can be used to handle labs in higher education
* Copyright (C) 2016-2024 Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package nl.tudelft.queue.audit;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import javax.persistence.EntityManagerFactory;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.RevisionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import nl.tudelft.queue.csv.CsvHelper;
import nl.tudelft.queue.model.QueueRevisionEntity;
import nl.tudelft.queue.service.FileService;
@Service
public class AuditService {
@Autowired
private EntityManagerFactory factory;
@Autowired
private FileService fs;
public <T> List<Object[]> getHistoryOf(Class<T> clazz) {
AuditReader reader = AuditReaderFactory.get(factory.createEntityManager());
return reader.createQuery().forRevisionsOfEntity(clazz, false, false).getResultList();
}
public Resource getHistoryAsResource(Class<?> clazz) throws IOException {
if (!Auditable.class.isAssignableFrom(clazz)) {
throw new UnsupportedOperationException(clazz.getName() + " does not extend " +
"Auditable!");
}
List<Object[]> history = getHistoryOf(clazz);
if (history.isEmpty()) {
return fs.writeTempResource(clazz.getName(), () -> CsvHelper.writeCsv(new String[0],
new ArrayList<>(),
';'));
}
List<String[]> rows = new ArrayList<>();
String[] fields = ((Auditable) history.get(0)[0]).getHeaders().toArray(String[]::new);
for (Object[] obj : history) {
Auditable entity = (Auditable) obj[0];
List<String> row = entity.getRow();
QueueRevisionEntity revision = (QueueRevisionEntity) obj[1];
row.add(String.valueOf(revision.getUsername()));
row.add(String.valueOf(revision.getRevisionDate()));
RevisionType type = (RevisionType) obj[2];
row.add(type.name());
rows.add(row.toArray(String[]::new));
}
String[] headers = Stream.concat(Arrays.stream(fields), Arrays.stream(new String[] {
"username", "timestamp", "type" })).toArray(String[]::new);
return fs.writeTempResource(clazz.getName() + "-" + LocalDateTime.now(),
() -> CsvHelper.writeCsv(headers, rows, ';'));
}
}
Loading