<commit_before>package net.ontopia.presto.jaxrs.process.impl;

import java.util.Collection;
import java.util.LinkedHashSet;

import net.ontopia.presto.jaxb.FieldData;
import net.ontopia.presto.jaxb.Link;
import net.ontopia.presto.jaxrs.Presto;
import net.ontopia.presto.jaxrs.links.Links;
import net.ontopia.presto.jaxrs.process.FieldDataProcessor;
import net.ontopia.presto.spi.PrestoField;
import net.ontopia.presto.spi.PrestoTopic;
import net.ontopia.presto.spi.PrestoType;
import net.ontopia.presto.spi.PrestoView;
import net.ontopia.presto.spi.utils.PrestoContext;
import net.ontopia.presto.spi.utils.PrestoContextRules;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

public class AddOnChangeLinkPostProcessor extends FieldDataProcessor {

    @Override
    public FieldData processFieldData(FieldData fieldData, PrestoContextRules rules, PrestoField field) {
        
        ObjectNode processorConfig = getConfig();
        if (processorConfig != null) {
            JsonNode validateNode = processorConfig.path("validateOnChange");
            if (validateNode.isBoolean() && validateNode.asBoolean()) {
                return addOnChangeLink(fieldData, rules, field);
            }
        }
        
        return fieldData;
    }

    private FieldData addOnChangeLink(FieldData fieldData, PrestoContextRules rules, PrestoField field) {
        // TODO: may want to support this for inline *new* topics as well
        PrestoContext context = rules.getContext();
        PrestoTopic topic = context.getTopic();
        if (topic != null) {
            Collection<Link> links = fieldData.getLinks();
            if (links == null) {
                links = new LinkedHashSet<Link>();
            }
            
            Presto presto = getPresto();
            
            String topicId = topic.getId();
            String typeId = topic.getTypeId();
            PrestoType type = getSchemaProvider().getTypeById(typeId);
            PrestoView view = field.getView();
            PrestoContext parentContext = context.getParentContext();
            PrestoField parentField = context.getParentField();
            
            Links lx = presto.getLinks();
            Link link = lx.fieldOnChangeLink(parentContext, parentField, topicId, type, view, field);
    
            if (!links.contains(link)) {
                links.add(link);
            }
            fieldData.setLinks(links);
        }
        return fieldData;
    }

}<commit_msg>Added support for new topics to AddOnChangeLinkPostProcessor.<commit_after>package net.ontopia.presto.jaxrs.process.impl;

import java.util.Collection;
import java.util.LinkedHashSet;

import net.ontopia.presto.jaxb.FieldData;
import net.ontopia.presto.jaxb.Link;
import net.ontopia.presto.jaxrs.Presto;
import net.ontopia.presto.jaxrs.links.Links;
import net.ontopia.presto.jaxrs.process.FieldDataProcessor;
import net.ontopia.presto.spi.PrestoField;
import net.ontopia.presto.spi.PrestoType;
import net.ontopia.presto.spi.PrestoView;
import net.ontopia.presto.spi.utils.PrestoContext;
import net.ontopia.presto.spi.utils.PrestoContextRules;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

public class AddOnChangeLinkPostProcessor extends FieldDataProcessor {

    @Override
    public FieldData processFieldData(FieldData fieldData, PrestoContextRules rules, PrestoField field) {
        
        ObjectNode processorConfig = getConfig();
        if (processorConfig != null) {
            JsonNode validateNode = processorConfig.path("validateOnChange");
            if (validateNode.isBoolean() && validateNode.asBoolean()) {
                return addOnChangeLink(fieldData, rules, field);
            }
        }
        
        return fieldData;
    }

    private FieldData addOnChangeLink(FieldData fieldData, PrestoContextRules rules, PrestoField field) {
        // TODO: may want to support this for inline *new* topics as well
        PrestoContext context = rules.getContext();
        Collection<Link> links = fieldData.getLinks();
        if (links == null) {
            links = new LinkedHashSet<Link>();
        }

        Presto presto = getPresto();

        String topicId = context.getTopicId();
        PrestoType type = context.getType();
        PrestoView view = field.getView();
        PrestoContext parentContext = context.getParentContext();
        PrestoField parentField = context.getParentField();

        Links lx = presto.getLinks();
        Link link = lx.fieldOnChangeLink(parentContext, parentField, topicId, type, view, field);

        if (!links.contains(link)) {
            links.add(link);
        }
        fieldData.setLinks(links);
        return fieldData;
    }

}
{'fraction_non_alphanumeric': 0.09667927700714586, 'fraction_numerical': 0.0, 'mean_word_length': 2.8754071661237783, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2410408', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1376, 'n_words': 275}