178 lines
8.3 KiB
Java
178 lines
8.3 KiB
Java
package eu.eudat.controllers;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import eu.eudat.audit.AuditableAction;
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import gr.cite.tools.validation.ValidationFilterAnnotation;
|
|
import eu.eudat.data.LanguageEntity;
|
|
import eu.eudat.model.Language;
|
|
import eu.eudat.model.builder.LanguageBuilder;
|
|
import eu.eudat.model.censorship.LanguageCensor;
|
|
import eu.eudat.model.persist.LanguagePersist;
|
|
import eu.eudat.model.result.QueryResult;
|
|
import eu.eudat.query.LanguageQuery;
|
|
import eu.eudat.query.lookup.LanguageLookup;
|
|
import eu.eudat.service.language.LanguageService;
|
|
import gr.cite.tools.auditing.AuditService;
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
import gr.cite.tools.data.censor.CensorFactory;
|
|
import gr.cite.tools.data.query.Ordering;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import gr.cite.tools.exception.MyApplicationException;
|
|
import gr.cite.tools.exception.MyForbiddenException;
|
|
import gr.cite.tools.exception.MyNotFoundException;
|
|
import gr.cite.tools.fieldset.FieldSet;
|
|
import gr.cite.tools.logging.LoggerService;
|
|
import gr.cite.tools.logging.MapLogEntry;
|
|
import jakarta.transaction.Transactional;
|
|
import jakarta.xml.bind.JAXBException;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.MessageSource;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
import static eu.eudat.authorization.AuthorizationFlags.Public;
|
|
|
|
@RestController
|
|
@RequestMapping(path = {"api/language"})
|
|
public class LanguageController {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(LanguageController.class));
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final AuditService auditService;
|
|
|
|
private final CensorFactory censorFactory;
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
private final LanguageService languageService;
|
|
|
|
@Autowired
|
|
public LanguageController(
|
|
BuilderFactory builderFactory,
|
|
AuditService auditService,
|
|
CensorFactory censorFactory,
|
|
QueryFactory queryFactory,
|
|
MessageSource messageSource,
|
|
LanguageService languageService) {
|
|
this.builderFactory = builderFactory;
|
|
this.auditService = auditService;
|
|
this.censorFactory = censorFactory;
|
|
this.queryFactory = queryFactory;
|
|
this.messageSource = messageSource;
|
|
this.languageService = languageService;
|
|
}
|
|
|
|
@PostMapping("query")
|
|
public QueryResult<Language> query(@RequestBody LanguageLookup lookup) throws MyApplicationException, MyForbiddenException {
|
|
logger.debug("querying {}", Language.class.getSimpleName());
|
|
|
|
this.censorFactory.censor(LanguageCensor.class).censor(lookup.getProject(), null);
|
|
|
|
LanguageQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
|
|
List<LanguageEntity> data = query.collectAs(lookup.getProject());
|
|
List<Language> models = this.builderFactory.builder(LanguageBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data);
|
|
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
|
|
|
|
this.auditService.track(AuditableAction.Language_Query, "lookup", lookup);
|
|
|
|
return new QueryResult<>(models, count);
|
|
}
|
|
|
|
@GetMapping("{id}")
|
|
public Language get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
|
|
logger.debug(new MapLogEntry("retrieving" + Language.class.getSimpleName()).And("id", id).And("fields", fieldSet));
|
|
|
|
this.censorFactory.censor(LanguageCensor.class).censor(fieldSet, null);
|
|
|
|
LanguageQuery query = this.queryFactory.query(LanguageQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
|
|
Language model = this.builderFactory.builder(LanguageBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
|
|
if (model == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Language.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
this.auditService.track(AuditableAction.Language_Lookup, Map.ofEntries(
|
|
new AbstractMap.SimpleEntry<String, Object>("id", id),
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
));
|
|
|
|
return model;
|
|
}
|
|
|
|
@GetMapping("public/code/{code}")
|
|
public Language get(@PathVariable("code") String code, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, IOException {
|
|
logger.debug(new MapLogEntry("retrieving" + Language.class.getSimpleName()).And("code", code).And("fields", fieldSet));
|
|
|
|
this.censorFactory.censor(LanguageCensor.class).censor(fieldSet, null);
|
|
|
|
LanguageQuery query = this.queryFactory.query(LanguageQuery.class).authorize(EnumSet.of(Public)).codes(code);
|
|
Language model = this.builderFactory.builder(LanguageBuilder.class).authorize(EnumSet.of(Public)).build(fieldSet, query.firstAs(fieldSet));
|
|
|
|
if (model == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{code, Language.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
if (model.getPayload() == null) {
|
|
model.setPayload(this.languageService.getPayload(code));
|
|
}
|
|
this.auditService.track(AuditableAction.Language_Lookup, Map.ofEntries(
|
|
new AbstractMap.SimpleEntry<String, Object>("code", code),
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
));
|
|
|
|
return model;
|
|
}
|
|
|
|
@PostMapping("public/available-languages")
|
|
public QueryResult<String> queryLanguageCodes(@RequestBody LanguageLookup lookup) {
|
|
logger.debug("querying {}", Language.class.getSimpleName());
|
|
|
|
this.censorFactory.censor(LanguageCensor.class).censor(lookup.getProject(), null);
|
|
|
|
LanguageQuery query = lookup.enrich(this.queryFactory).authorize(EnumSet.of(Public));
|
|
query.setOrder(new Ordering().addAscending(Language._ordinal));
|
|
List<LanguageEntity> data = query.collectAs(lookup.getProject());
|
|
List<Language> models = this.builderFactory.builder(LanguageBuilder.class).authorize(EnumSet.of(Public)).build(lookup.getProject(), data);
|
|
|
|
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
|
|
|
|
return new QueryResult<>(models.stream().map(Language::getCode).collect(Collectors.toList()), count);
|
|
}
|
|
|
|
@PostMapping("persist")
|
|
@Transactional
|
|
@ValidationFilterAnnotation(validator = LanguagePersist.LanguagePersistValidator.ValidatorName, argumentName = "model")
|
|
public Language persist(@RequestBody LanguagePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, JsonProcessingException, InvalidApplicationException {
|
|
logger.debug(new MapLogEntry("persisting" + Language.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
|
|
this.censorFactory.censor(LanguageCensor.class).censor(fieldSet, null);
|
|
|
|
Language persisted = this.languageService.persist(model, fieldSet);
|
|
|
|
this.auditService.track(AuditableAction.Language_Persist, Map.ofEntries(
|
|
new AbstractMap.SimpleEntry<String, Object>("model", model),
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
));
|
|
|
|
return persisted;
|
|
}
|
|
|
|
@DeleteMapping("{id}")
|
|
@Transactional
|
|
public void delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
logger.debug(new MapLogEntry("retrieving" + Language.class.getSimpleName()).And("id", id));
|
|
|
|
this.languageService.deleteAndSave(id);
|
|
|
|
this.auditService.track(AuditableAction.Language_Delete, "id", id);
|
|
}
|
|
|
|
}
|