argos/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/LanguageV2Controller.java

152 lines
7.4 KiB
Java

package eu.eudat.controllers.v2;
import com.fasterxml.jackson.core.JsonProcessingException;
import eu.eudat.audit.AuditableAction;
import eu.eudat.authorization.AuthorizationFlags;
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.commons.web.authz.service.AuthorizationService;
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.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 gr.cite.tools.validation.MyValidate;
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.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@RestController
@RequestMapping(path = {"api/v2/language"})
public class LanguageV2Controller {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(LanguageV2Controller.class));
private final BuilderFactory builderFactory;
private final AuditService auditService;
private final CensorFactory censorFactory;
private final QueryFactory queryFactory;
private final MessageSource messageSource;
private final AuthorizationService authorizationService;
private final LanguageService languageService;
@Autowired
public LanguageV2Controller(
BuilderFactory builderFactory,
AuditService auditService,
CensorFactory censorFactory,
QueryFactory queryFactory,
MessageSource messageSource, AuthorizationService authorizationService,
LanguageService languageService) {
this.builderFactory = builderFactory;
this.auditService = auditService;
this.censorFactory = censorFactory;
this.queryFactory = queryFactory;
this.messageSource = messageSource;
this.authorizationService = authorizationService;
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.OwnerOrDmpAssociatedOrPermissionOrPublic);
List<LanguageEntity> data = query.collectAs(lookup.getProject());
List<Language> models = this.builderFactory.builder(LanguageBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).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.OwnerOrDmpAssociatedOrPermissionOrPublic).ids(id);
Language model = this.builderFactory.builder(LanguageBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).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("code/{code}")
public Language get(@PathVariable("code") String code, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
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(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).codes(code);
Language model = this.builderFactory.builder(LanguageBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(fieldSet, query.firstAs(fieldSet));
if (model == null)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{code, Language.class.getSimpleName()}, LocaleContextHolder.getLocale()));
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("persist")
@Transactional
public Language persist(@MyValidate @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);
}
}