Skip to content

Commit

Permalink
validation enhancements
Browse files Browse the repository at this point in the history
* trigger type production start
* betty modifiers
* csv entry count
  • Loading branch information
nittka committed Dec 14, 2024
1 parent 3ead2fc commit 59d3287
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
12 changes: 12 additions & 0 deletions org.tvtower.db/src/org/tvtower/db/constants/ProgrammeModifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class ProgrammeModifier extends TVTEnum implements ModifierValueValidator
public static final String NOT_LIVE = "topicality::notLive";
public static final String TIMES_BROADCASTED = "topicality::timesBroadcasted";
public static final String PER_VIEWER_REVENUE = "callin::perViewerRevenue";
public static final String BETTY_ABSOLUTE = "betty::pointsabsolute";
public static final String BETTY_QUALITY = "betty::rawquality";
public static final String BETTY_MOD = "betty::pointsmod";

private static final BigDecimal TWO = new BigDecimal(2);
private static final BigDecimal FIVE = new BigDecimal(5);
Expand All @@ -32,6 +35,9 @@ public class ProgrammeModifier extends TVTEnum implements ModifierValueValidator
add(NOT_LIVE, "topicality loss if not live");
add(TIMES_BROADCASTED, "topicality loss due to number of broadcasts");
add(PER_VIEWER_REVENUE, "income per viewer for call-in");
add(BETTY_ABSOLUTE, "absolute Betty points added/removed");
add(BETTY_QUALITY, "programme quality for Betty");
add(BETTY_MOD, "factor for Betty point calculation");
}

@Override
Expand All @@ -41,6 +47,12 @@ public Optional<String> getValueError(Modifier m) {
BigDecimal max = TWO;
if (AGE.equals(m.getModName())) {
max = FIVE;
} else if (BETTY_ABSOLUTE.equals(m.getModName())) {
return CommonValidation.getIntRangeError(m.getValue(), "value", -500, 500, true);
} else if (BETTY_QUALITY.equals(m.getModName())) {
max = BigDecimal.ONE;
}else if (BETTY_MOD.equals(m.getModName())) {
min=max.negate();
}
return CommonValidation.getDecimalRangeError(m.getValue(), "value", min, max, true);
}
Expand Down
22 changes: 22 additions & 0 deletions org.tvtower.db/src/org/tvtower/db/constants/TriggerType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.tvtower.db.constants;

import org.eclipse.xtext.EcoreUtil2;
import org.tvtower.db.database.Effect;
import org.tvtower.db.database.NewsItem;
import org.tvtower.db.database.ScriptTemplate;

public class TriggerType extends TVTEnum {

TriggerType() {
Expand All @@ -8,6 +13,23 @@ public class TriggerType extends TVTEnum {
add("broadcastDone", "after every broadcast");
add("broadcastFirstTime", "on the first broadcast");
add("broadcastFirstTimeDone", "when the first broadcast is done");
add("productionStart", "on starting the production in the studio");
}

/**
* check if trigger type is allowed in the effect's context
* */
public boolean isSupported(Effect e) {
if("happen".equals(e.getTrigger())){
if(EcoreUtil2.getContainerOfType(e, NewsItem.class)==null) {
return false;
}
}else if("productionStart".equals(e.getTrigger())) {
if(EcoreUtil2.getContainerOfType(e, ScriptTemplate.class)==null) {
return false;
}
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.tvtower.db.validation.expressionhelper.ScriptExpression;
import org.tvtower.db.validation.expressionhelper.SimpleExpression;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
Expand Down Expand Up @@ -153,6 +154,7 @@ public void checkLanguageStringsContainer(ContainsLanguageStrings c) {
//prepare duplicate entry checks
Set<String> languages = new HashSet<>();
AtomicInteger optionsCount = new AtomicInteger(-1);
AtomicInteger csvCount = new AtomicInteger(-1);
if (checkLocalizationDuplicates) {
int languageCount = c.getLstrings().size();
if (languageCount == 0) {
Expand All @@ -178,7 +180,7 @@ public void checkLanguageStringsContainer(ContainsLanguageStrings c) {
} else {
languages.add(language);
}
validateOptionsCount(l, optionsCount);
validateOptionsCount(l, optionsCount, csvCount);
if("de".equals(language)) {
String content=l.getText();
if(germanContainsIllegalQuote(content)) {
Expand Down Expand Up @@ -224,18 +226,41 @@ private boolean germanContainsIllegalQuote(String s) {
return false;
}

private void validateOptionsCount(LanguageString s, AtomicInteger optionsCount) {
int count = -1;
private void validateOptionsCount(LanguageString s, AtomicInteger optionsCount, AtomicInteger csvCount) {
int optCount = -1;
int dataCount = -1;
List<String> options = new ArrayList<>();
if (!Strings.isNullOrEmpty(s.getText())) {
//TODO what about options within an expression
count = s.getText().split("\\|").length;
// TODO what about options within an expression
options = Splitter.on('|').splitToList(s.getText());
optCount = options.size();
}
for (String option : options) {
// remove html entities
option = option.replaceAll("&\\w+;", "");
int singleDataCount = option.split(";", -1).length;
if (dataCount < 0) {
dataCount = singleDataCount;
if (csvCount.get() < 0) {
csvCount.set(dataCount);
}
} else if (singleDataCount != dataCount) {
int index = options.indexOf(option) + 1;
error("alternative " + index + " has " + singleDataCount + " csv entries, " + dataCount
+ " in others for this language", s, $.getLanguageString_Text());
}
}
if (optionsCount.get() < 0) {
if (count >= 0) {
optionsCount.set(count);
if (optCount >= 0) {
optionsCount.set(optCount);
}
} else if (count >= 0 && count != optionsCount.get()) {
error("options count mismatch with other language", s, $.getLanguageString_Text());
} else if (optCount >= 0 && optCount != optionsCount.get()) {
error("options count mismatch wrt other language", s, $.getLanguageString_Text());
}
// mark mismatch only if one semicolon was found at all
// semicolon might be part of regular text
if (dataCount > 1 && csvCount.get() > 1 && dataCount != csvCount.get()) {
error("csv data entry count mismatch wrt other language", s, $.getLanguageString_Text());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public void checkNewsProbability(NewsProbability e) {
$.getNewsProbability_News());
}

//TODO extend check effects to programme and scripts
@Check
public void checkEffect(Effect e) {
Constants.triggerType.isValidValue(e.getTrigger(), "trigger", true)
Expand All @@ -190,6 +189,10 @@ public void checkEffect(Effect e) {
Constants._boolean.isValidValue(e.getEnable(), "enable", false)
.ifPresent(err -> error(err, $.getEffect_Enable()));

if(!Constants.triggerType.isSupported(e)) {
error("trigger type not supported for this element", $.getEffect_Trigger());
}

if (e.getType() != null) {
Boolean checkMinMax = Boolean.FALSE;
Boolean genreExpected = Boolean.FALSE;
Expand Down

0 comments on commit 59d3287

Please sign in to comment.