%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.ProjectManagement" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Regularization.RegularizationRule" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Regularization" %>
<%@ page import="org.crosswire.sword.keys.VerseKey" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.time.LocalDateTime" %>
<%@ page import="java.time.ZoneOffset" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="org.crosswire.xml.XMLBlock" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%!
@Description(value = "Submit a new or modified regularization rule.", name = "regularization/put")
public static class MyParameters extends Parameters {
private static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd[[ ]['T']HH:mm[:ss][XXX]]");
protected ProjectManagement.Project project = null;
protected LocalDateTime createDateDate = null;
@Description(value = "regularization rule id to modify. if not provided, a new regularization rule will be created.", example = "27")
public Integer regID = null;
@Description(value = "owner of the regularization rule", example = "psmith")
public String userName = null;
@Description(value = "internal")
public String groupName = null;
@Description(value = "project name to assign rule to", example = "ECM Matthew")
public String projectName = null;
@Description(value = "project ID to assign rule to", example = "12")
public Integer projectID = null;
@Description(value = "internal")
public String verse = null;
@Description(value = "index context for this rule", example = "Jn.2.2")
public String indexContent = null;
@Description(value = "scope of rule (Verse, Global, Once)")
public String scope = null;
@Description(value = "visibility of rule (Public, Private)")
public String visibility = null;
@Description(value = "source word to change in the text")
public String sourceWord = null;
@Description(value = "target word to change the source word to")
public String targetWord = null;
@Description(value = "context immediately before the source word")
public String contextPre = null;
@Description(value = "context immediately after the source word")
public String contextPost = null;
@Description(value = "user data")
public String userData = null;
@Description(value = "create date yyyy-mm-dd HH:mm:ss", defaultValue="now", example="2024-05-21 16:15:27")
public String createDate = null;
@Description(value = "type of rule (Spelling, Iticism)")
public String type = null;
@Description(value = "pipe-separated list of options for the rule or an int value, e.g., IGNORE_SUPPLIED|IGNORE_UNCLEAR|ONLY_NOMSAC")
public String options = null;
@Description(value = "any user notes about this rule")
public String comment = null;
@Override
protected void afterLoad() {
// DEPRECATED support
if (projectName == null) projectName = groupName;
if (indexContent == null) indexContent = verse;
sourceWord = Transcription.assureUnicode(sourceWord);
targetWord = Transcription.assureUnicode(targetWord);
contextPre = Transcription.assureUnicode(contextPre);
contextPost = Transcription.assureUnicode(contextPost);
comment = Transcription.assureUnicode(comment);
projectName = Transcription.assureUnicode(projectName);
}
@Override
protected void customValidation() {
if (createDate != null) {
try { createDateDate = LocalDateTime.parse(createDate, df); } catch (Exception e) {
addError(-4, "Error parsing {createDate}");
}
}
getLogger().info("createDate params value: " + createDateDate);
if (getUser() == null) {
addError(-5, "Must be logged in to edit regularization rules.");
return;
}
if (projectID != null || projectName != null) {
project = projectID != null ? ProjectManagement.getProject(projectID) : ProjectManagement.getProject(projectName);
if (project == null) {
addError(-6, "Project not found.");
return;
}
}
if (regID == null) {
if (sourceWord == null || targetWord == null) {
addError(-7, "Must supply both {sourceWord} and {targetWord} when creating a new rule.");
}
}
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
// who's rules we want to modify
String targetUserName = params.userName;
if (targetUserName == null) targetUserName = params.getUser().getUserName();
int options = -1; try { options = Integer.parseInt(params.options); } catch(Exception e) {}
if (params.options != null && options == -1) {
options = 0;
for (String o : params.options.split("\\|")) {
if ("IGNORE_SUPPLIED".equalsIgnoreCase(o)) {
options |= RegularizationRule.OPTION_IGNORE_SUPPLIED;
}
else if ("IGNORE_UNCLEAR".equalsIgnoreCase(o)) {
options |= RegularizationRule.OPTION_IGNORE_UNCLEAR;
}
else if ("ONLY_NOMSAC".equalsIgnoreCase(o)) {
options |= RegularizationRule.OPTION_ONLY_NOMSAC;
}
}
}
do {
RegularizationRule orig = null;
RegularizationRule reg = null;
params.getLogger().info("params.regID: " + params.regID);
if (params.regID != null) {
orig = RegularizationRule.getRegularizationRule(params.regID);
reg = (RegularizationRule)orig.clone();
if (orig == null) {
params.addError(-8, "regID: " + params.regID + " not found.");
break;
}
}
else reg = new RegularizationRule();
// we need these first, to check if we have a parallel segmentation rule we're modifying
if (params.sourceWord != null) reg.setSourceWord(params.sourceWord);
if (params.indexContent != null) reg.setContextVerse(new VerseKey(params.indexContent).getHashNumber());
if ("Parallel Segmentation".equals(params.type)) {
if (orig == null) {
RegularizationRule regs[] = Regularization.getRulesByVerse(targetUserName, false, reg.getContextVerse());
if (regs != null) {
for (RegularizationRule r : regs) {
if ( r.getType() == RegularizationRule.TYPE_PARALLEL_SEGMENTATION
&& ( reg.getSourceWord().startsWith(r.getSourceWord())
|| reg.getSourceWord().endsWith(r.getSourceWord()))
) {
orig = r;
reg = (RegularizationRule)orig.clone();
reg.setSourceWord(params.sourceWord);
break;
}
}
}
}
reg.setType(RegularizationRule.TYPE_PARALLEL_SEGMENTATION);
}
else if (params.type != null) {
String typeTexts[] = RegularizationRule.getTypeTexts();
int i = 0;
for (; i < typeTexts.length; ++i) {
if (typeTexts[i].equalsIgnoreCase(params.type)) break;
if (typeTexts[i].replaceAll(" ", "_").equalsIgnoreCase(params.type)) break;
}
if (i == typeTexts.length) {
params.addError(-9, "type must be one of: " + String.join(", ", Arrays.asList(typeTexts)));
break;
}
reg.setType(i);
}
if (targetUserName != null) reg.setUserID(targetUserName);
if (params.project != null) reg.setGroupName(params.project.getProjectName());
if (options != -1) reg.setOptions(options);
if ("Verse".equals(params.scope)) {
reg.setScope(RegularizationRule.SCOPE_VERSE);
}
else if ("Global".equals(params.scope)) {
reg.setScope(RegularizationRule.SCOPE_GLOBAL);
}
else if ("Once".equals(params.scope)) {
reg.setScope(RegularizationRule.SCOPE_ONCE);
}
else if (params.scope != null) {
out.print("");
return;
}
else {
if (orig == null) reg.setScope(params.indexContent != null ? RegularizationRule.SCOPE_VERSE : RegularizationRule.SCOPE_GLOBAL);
}
if ("Private".equals(params.visibility)) {
reg.setVisibility(RegularizationRule.VISIBILITY_PRIVATE);
}
else if ("Public".equals(params.visibility)) {
reg.setVisibility(RegularizationRule.VISIBILITY_PUBLIC);
}
else if (params.visibility != null) {
out.print("");
return;
}
if (params.targetWord != null) reg.setTargetWord(params.targetWord);
if (params.contextPre != null) reg.setContextPre(params.contextPre);
if (params.contextPost != null) reg.setContextPost(params.contextPost);
if (params.userData != null) reg.setUserData(params.userData);
if (params.createDateDate != null) reg.setCreateDate(Date.from(params.createDateDate.toInstant(ZoneOffset.UTC)));
if (orig != null) {
reg.save(orig);
}
else {
reg = reg.saveNew();
}
Serializer.output(response, out, params, XMLBlock.createXMLBlock(""));
return;
} while (false);
}
else params.format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>