<commit_before>package com.joelj.jenkins.eztemplates.utils;

import com.joelj.jenkins.eztemplates.TemplateImplementationProperty;
import com.joelj.jenkins.eztemplates.TemplateProperty;
import hudson.model.*;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import hudson.util.CopyOnWriteList;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This is where all the magic really happens.
 * The templates and implementations, when they change, call one of the two public handle* methods.
 * <p/>
 * User: <NAME>
 * Date: 2/25/13
 * Time: 10:55 PM
 */
public class TemplateUtils {
	private static final Logger LOG = Logger.getLogger("ez-templates");

	public static void handleTemplate(AbstractProject templateProject, TemplateProperty property) throws IOException {
		LOG.info("Template " + templateProject.getDisplayName() + " was saved. Syncing implementations. " + property);
		Set<String> implementations = property.getImplementations();

		Iterator<String> iterator = implementations.iterator();
		boolean changedTemplateProject = false;
		while (iterator.hasNext()) {
			String implementationName = iterator.next();
			AbstractProject project = ProjectUtils.findProject(implementationName);
			if (project == null) {
				LOG.warning(implementationName + " doesn't exist as a project. Cleaning it out of the template.");
				changedTemplateProject = true;
				iterator.remove();
				continue;
			}

			@SuppressWarnings("unchecked")
			TemplateImplementationProperty impProperty = (TemplateImplementationProperty) project.getProperty(TemplateImplementationProperty.class);
			handleImplementation(project, impProperty);
		}

		if(changedTemplateProject) {
			ProjectUtils.silentSave(templateProject);
		}
	}

	public static void handleImplementation(AbstractProject implementationProject, TemplateImplementationProperty property) throws IOException {
		LOG.info("Implementation " + implementationProject.getDisplayName() + " was saved. Syncing with " + property.getTemplateJobName());
		AbstractProject templateProject = property.findProject();

		//Capture values we want to keep
		@SuppressWarnings("unchecked")
		boolean implementationIsTemplate = implementationProject.getProperty(TemplateProperty.class) != null;
		List<ParameterDefinition> oldImplementationParameters = findParameters(implementationProject);
		@SuppressWarnings("unchecked")
		Map<TriggerDescriptor,Trigger> oldTriggers = implementationProject.getTriggers();
		boolean shouldBeDisabled = implementationProject.isDisabled();
		String description = implementationProject.getDescription();

		implementationProject = synchronizeConfigFiles(implementationProject, templateProject);

		//Set values that we wanted to keep via reflection to prevent infinite save recursion
		fixProperties(implementationProject, property, implementationIsTemplate);
		fixParameters(implementationProject, oldImplementationParameters);
		fixBuildTriggers(implementationProject, oldTriggers);
		ReflectionUtils.setFieldValue(AbstractProject.class, implementationProject, "disabled", shouldBeDisabled);
		ReflectionUtils.setFieldValue(AbstractItem.class, implementationProject, "description", description);

		ProjectUtils.silentSave(implementationProject);
	}

	private static void fixBuildTriggers(AbstractProject implementationProject, Map<TriggerDescriptor, Trigger> oldTriggers) {
		List<Trigger<?>> triggersToReplace = ProjectUtils.getTriggers(implementationProject);<commit_msg>sync the description if it's not set by the user.<commit_after>package com.joelj.jenkins.eztemplates.utils;

import com.joelj.jenkins.eztemplates.TemplateImplementationProperty;
import com.joelj.jenkins.eztemplates.TemplateProperty;
import hudson.model.*;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import hudson.util.CopyOnWriteList;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This is where all the magic really happens.
 * The templates and implementations, when they change, call one of the two public handle* methods.
 * <p/>
 * User: <NAME>
 * Date: 2/25/13
 * Time: 10:55 PM
 */
public class TemplateUtils {
	private static final Logger LOG = Logger.getLogger("ez-templates");

	public static void handleTemplate(AbstractProject templateProject, TemplateProperty property) throws IOException {
		LOG.info("Template " + templateProject.getDisplayName() + " was saved. Syncing implementations. " + property);
		Set<String> implementations = property.getImplementations();

		Iterator<String> iterator = implementations.iterator();
		boolean changedTemplateProject = false;
		while (iterator.hasNext()) {
			String implementationName = iterator.next();
			AbstractProject project = ProjectUtils.findProject(implementationName);
			if (project == null) {
				LOG.warning(implementationName + " doesn't exist as a project. Cleaning it out of the template.");
				changedTemplateProject = true;
				iterator.remove();
				continue;
			}

			@SuppressWarnings("unchecked")
			TemplateImplementationProperty impProperty = (TemplateImplementationProperty) project.getProperty(TemplateImplementationProperty.class);
			handleImplementation(project, impProperty);
		}

		if(changedTemplateProject) {
			ProjectUtils.silentSave(templateProject);
		}
	}

	public static void handleImplementation(AbstractProject implementationProject, TemplateImplementationProperty property) throws IOException {
		LOG.info("Implementation " + implementationProject.getDisplayName() + " was saved. Syncing with " + property.getTemplateJobName());
		AbstractProject templateProject = property.findProject();

		//Capture values we want to keep
		@SuppressWarnings("unchecked")
		boolean implementationIsTemplate = implementationProject.getProperty(TemplateProperty.class) != null;
		List<ParameterDefinition> oldImplementationParameters = findParameters(implementationProject);
		@SuppressWarnings("unchecked")
		Map<TriggerDescriptor,Trigger> oldTriggers = implementationProject.getTriggers();
		boolean shouldBeDisabled = implementationProject.isDisabled();
		String description = implementationProject.getDescription();

		implementationProject = synchronizeConfigFiles(implementationProject, templateProject);

		//Set values that we wanted to keep via reflection to prevent infinite save recursion
		fixProperties(implementationProject, property, implementationIsTemplate);
		fixParameters(implementationProject, oldImplementationParameters);
		fixBuildTriggers(implementationProject, oldTriggers);
		ReflectionUtils.setFieldValue(AbstractProject.class, implementationProject, "disabled", shouldBeDisabled);

		if(description != null) {
			ReflectionUtils.setFieldValue(AbstractItem.class, implementationProject, "description", description);
		}

		ProjectUtils.silentSave(implementationProject);
	}

	private static void fixBuildTriggers(AbstractProject implementationProject, Map<TriggerDescriptor, Trigger> oldTriggers) {
		List<Trigger<?>> triggersToReplace = ProjectUtils.getTriggers(implementationProject);
{'fraction_non_alphanumeric': 0.09088471849865952, 'fraction_numerical': 0.002412868632707775, 'mean_word_length': 6.613265306122449, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7321774', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1826, 'n_words': 450}