PollService.java
/*
* Copyright © 2026 The CTAN Team and individual authors
*
* This file is distributed under the 3-clause BSD license.
* See file LICENSE for details.
*/
package org.ctan.site.services.poll;
import java.time.LocalDate;
import java.util.Date;
import java.util.Map;
import org.ctan.site.domain.poll.Poll;
import org.ctan.site.stores.PollStore;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.validation.ValidationException;
import lombok.NonNull;
/**
* The class <code>PollService</code> contains...TODO
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
public class PollService {
/**
* The field <code>MAPPER</code> contains a static mapper.
*/
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* The field <code>store</code> contains the underlying store.
*/
private PollStore store;
/**
* This is the constructor for the class <code>PollService</code>.
*
* @param store the underlying store
*/
public PollService(PollStore store) {
this.store = store;
}
/**
* The method <code>save</code> provides means to validate a poll and save
* it.
*
* @param poll the poll
* @return the saved poll
* @throws ValidationException if the validation fails
*/
public Poll save(@NonNull Map<String, String> map) {
Poll poll = validate(map);
return store.save(poll);
}
/**
* The method <code>checkArea</code> provides means to TODO gene
*
* @param poll the poll to check
* @return <code>true</code> iff the area is 'none'
* @throws ValidationException if the validation fails
*/
private boolean checkArea(Poll poll) {
if (poll.isAreaNone()) {
if (poll.isAreaAcademic()
|| poll.isAreaBusiness()
|| poll.isAreaPrivate()
|| poll.isAreaOther()) {
throw new ValidationException("conflicting area none");
}
return true;
} else if (poll.isAreaAcademic()
|| poll.isAreaBusiness()
|| poll.isAreaPrivate()
|| poll.isAreaOther()) {
} else {
throw new ValidationException("area is not defined");
}
return false;
}
/**
* The method <code>checkDistribution</code> provides means to verify that
* at least one distribution is selected. The distributions not matching to
* the OS are unflagged.
*
* @param poll the poll to check
* @throws ValidationException if the validation fails
*/
private void checkDistribution(Poll poll) {
if (poll.isDistributionAmiweb2c()
|| poll.isDistributionApltex()
|| poll.isDistributionBakoma()
|| poll.isDistributionChitex()
|| poll.isDistributionEmtex()
|| poll.isDistributionFptex()
|| poll.isDistributionMactex()
|| poll.isDistributionMiktex()
|| poll.isDistributionOztex()
|| poll.isDistributionProtext()
|| poll.isDistributionTetex()
|| poll.isDistributionTexlive()
|| poll.isDistributionOther()) {
} else {
throw new ValidationException("distribution is not defined");
}
clearDistribution(poll);
}
/**
* The method <code>checkEngine</code> provides means to verify that at
* least one engine is selected.
*
* @param poll the poll to check
* @throws ValidationException if the validation fails
*/
private void checkEngine(Poll poll) {
if (poll.isEngineLuatex()
|| poll.isEnginePdftex()
|| poll.isEngineXetex()
|| poll.isEngineOther()) {
} else {
throw new ValidationException("engine is not defined");
}
}
/**
* The method <code>checkFormat</code> provides means to verify that at
* least one format is selected.
*
* @param poll the poll to check
* @throws ValidationException if the validation fails
*/
private void checkFormat(Poll poll) {
if (poll.isFormatContext()
|| poll.isFormatLatex()
|| poll.isFormatPlainTex()
|| poll.isFormatOther()) {
} else {
throw new ValidationException("format is not defined");
}
}
/**
* The method <code>checkOs</code> provides means to verify that at least
* one OS is selected.
*
* @param poll the poll to check
* @throws ValidationException if the validation fails
*/
private void checkOs(Poll poll) {
if (poll.isOsWindows()
|| poll.isOsMacos()
|| poll.isOsLinux()
|| poll.isOsOther()) {
} else {
throw new ValidationException("os is not defined");
}
}
/**
* The method <code>clearDistribution</code> provides means to unflag
* distributions which do not fit to the selected operating systems.
*
* @param poll the poll to clear
*/
private void clearDistribution(Poll poll) {
boolean linux = poll.isOsLinux();
boolean macos = poll.isOsMacos();
boolean other = poll.isOsOther();
boolean windows = poll.isOsWindows();
poll.setDistributionAmiweb2c(other);
poll.setDistributionApltex(macos || linux);
poll.setDistributionBakoma(windows);
poll.setDistributionChitex(windows || linux);
poll.setDistributionEmtex(windows);
poll.setDistributionFptex(windows);
poll.setDistributionMactex(macos);
poll.setDistributionMiktex(windows || linux);
poll.setDistributionOztex(macos);
poll.setDistributionProtext(windows);
poll.setDistributionTetex(linux || other);
poll.setDistributionTexlive(windows || macos || linux || other);
poll.setDistributionOther(windows || macos || linux || other);
}
/**
* The method <code>clearEngine</code> provides means to unflag all engines.
*
* @param poll the poll to clear
*/
private void clearEngine(Poll poll) {
poll.setEngineLuatex(false);
poll.setEnginePdftex(false);
poll.setEngineXetex(false);
poll.setEngineOther(false);
}
/**
* The method <code>clearFormat</code> provides means to unflag all formats.
*
* @param poll the poll to clear
*/
private void clearFormat(Poll poll) {
poll.setFormatContext(false);
poll.setFormatLatex(false);
poll.setFormatPlainTex(false);
poll.setFormatOther(false);
}
/**
* The method <code>clearOs</code> provides means to unflag all operating
* systems.
*
* @param poll the poll to clear
*/
private void clearOs(Poll poll) {
poll.setOsWindows(false);
poll.setOsMacos(false);
poll.setOsLinux(false);
poll.setOsOther(false);
}
/**
* The method <code>validate</code> provides means to translate the Map into
* a Poll and validate the values.
*
* @param poll the poll to check
* @throws ValidationException if the validation fails
*/
private Poll validate(Map<String, String> pMap) {
Poll poll = MAPPER.convertValue(pMap, Poll.class);
poll.setDate(new Date());
Integer since = poll.getSince();
if (since == null) {
throw new ValidationException("since should not be null");
}
if (since < 1978 || since > LocalDate.now().getYear()) {
throw new ValidationException("since is not valid: " + since);
}
if (checkArea(poll)) {
clearOs(poll);
clearEngine(poll);
clearFormat(poll);
clearDistribution(poll);
return poll;
}
checkOs(poll);
checkEngine(poll);
checkFormat(poll);
checkDistribution(poll);
return poll;
}
}