<commit_before>package org.sample.patron.demo.tasks;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBElement;

import org.sample.patron.demo.entity.ObjectFactory;
import org.sample.patron.demo.entity.User;
import org.sample.patron.demo.entity.UserIdentifier;
import org.sample.patron.demo.entity.UserIdentifier.IdType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Task to correct the Identifiers of a User record.
 * 
 * @author nishen
 */
public class TaskFixUserIdentifiers implements Task
{
	private static final Logger log = LoggerFactory.getLogger(TaskFixUserIdentifiers.class);

	private Properties config = null;

	private String userListFilename = null;

	private String identifiersFilename = null;

	private String cnFilename = null;

	private ObjectFactory of = new ObjectFactory();

	// Disable the default empty constructor. We just want to use the
	// parameterized one.
	@SuppressWarnings("unused")
	private TaskFixUserIdentifiers()
	{}

	/**
	 * @param args command line args passed in.
	 * @param config the properties loaded in at run time.
	 */
	public TaskFixUserIdentifiers(String[] args, Properties config)
	{
		this.config = config;

		// extract relevant command line arguments
		if (args != null && args.length > 0)
			for (int x = 0; x < args.length; x++)
			{
				if (args[x].equals("-ids"))
				{
					if (args.length > (x + 1))
						identifiersFilename = args[++x];
				}
				else if (args[x].equals("-users"))
				{
					if (args.length > (x + 1))
						userListFilename = args[++x];
				}
				else if (args[x].equals("-cns"))
				{
					if (args.length > (x + 1))
						cnFilename = args[++x];
				}
			}

		log.debug("initialised {}", this.getClass().getCanonicalName());
	}

	@Override
	public void run()
	{
		log.info("executing task: {}", this.getClass().getSimpleName());

		Map<String, String> identifiers = null;
		Map<String, String> cns = null;
		List<String> userList = null;

		if (userListFilename == null)
		{
			log.error("user list filename required.");
			return;
		}

		if (identifiersFilename == null)
		{
			log.error("identifiers filename required.");
			return;
		}

		if (cnFilename == null)
		{
			log.error("cn filename required.");
			return;
		}

		try
		{
			File userListFile = new File(userListFilename);
			userList = getUserList(userListFile);

			File idFile = new File(identifiersFilename);
			identifiers = getIdentifiers(idFile);

			File cnFile = new File(cnFilename);
			cns = getCnCaseMapping(cnFile);
		}
		catch (FileNotFoundException nfe)
		{
			log.error("cannot find or open file: {}", nfe.getMessage(), nfe);
			return;
		}

		String url = config.getProperty("ws.url.alma");
		String key = config.getProperty("ws.url.alma.key");

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url);
		target = target.path("users").queryParam("apikey", key);

		for (String oldPrimaryId : userList)
		{
			log.info("processing user: {}", oldPrimaryId);

			String newPrimaryId = identifiers.get(oldPrimaryId.toLowerCase());
			String oneId = cns.get(oldPrimaryId.toLowerCase());
			if (oneId == null)
				oneId = oldPrimaryId;

			WebTarget t = target.path(oldPrimaryId);
			User user = t.request(MediaType.APPLICATION_XML_TYPE).get(User.class);
			log.info("fetched user: {}", user.getPrimaryId());

			// remove PARTY_ID, ONE_ID, INSTITUTION_ID
			List<UserIdentifier> idsToRemove = new ArrayList<UserIdentifier>(5);
			for (UserIdentifier i : user.getUserIdentifiers().getUserIdentifier())
			{
				if ("PARTY_ID".equals(i.getIdType().getValue()))
					idsToRemove.add(i);
				if ("ONE_ID".equals(i.getIdType().getValue()))
					idsToRemove.add(i);
				if ("INST_ID".equals(i.getIdType().getValue()))
					idsToRemove.add(i);

				log.info("identifiers [{}]: {}", i.getIdType().getValue(), i.getValue());
			}

			for (UserIdentifier i : idsToRemove)
				user.getUserIdentifiers().getUserIdentifier().remove(i);

			// save with oldPrimaryId
			JAXBElement<User> jaxbUser = of.createUser(user);<commit_msg>Added partyId existence check.<commit_after>package org.sample.patron.demo.tasks;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBElement;

import org.sample.patron.demo.entity.ObjectFactory;
import org.sample.patron.demo.entity.User;
import org.sample.patron.demo.entity.UserIdentifier;
import org.sample.patron.demo.entity.UserIdentifier.IdType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Task to correct the Identifiers of a User record.
 * 
 * @author nishen
 */
public class TaskFixUserIdentifiers implements Task
{
	private static final Logger log = LoggerFactory.getLogger(TaskFixUserIdentifiers.class);

	private Properties config = null;

	private String userListFilename = null;

	private String identifiersFilename = null;

	private String cnFilename = null;

	private ObjectFactory of = new ObjectFactory();

	// Disable the default empty constructor. We just want to use the
	// parameterized one.
	@SuppressWarnings("unused")
	private TaskFixUserIdentifiers()
	{}

	/**
	 * @param args command line args passed in.
	 * @param config the properties loaded in at run time.
	 */
	public TaskFixUserIdentifiers(String[] args, Properties config)
	{
		this.config = config;

		// extract relevant command line arguments
		if (args != null && args.length > 0)
			for (int x = 0; x < args.length; x++)
			{
				if (args[x].equals("-ids"))
				{
					if (args.length > (x + 1))
						identifiersFilename = args[++x];
				}
				else if (args[x].equals("-users"))
				{
					if (args.length > (x + 1))
						userListFilename = args[++x];
				}
				else if (args[x].equals("-cns"))
				{
					if (args.length > (x + 1))
						cnFilename = args[++x];
				}
			}

		log.debug("initialised {}", this.getClass().getCanonicalName());
	}

	@Override
	public void run()
	{
		log.info("executing task: {}", this.getClass().getSimpleName());

		Map<String, String> identifiers = null;
		Map<String, String> cns = null;
		List<String> userList = null;

		if (userListFilename == null)
		{
			log.error("user list filename required.");
			return;
		}

		if (identifiersFilename == null)
		{
			log.error("identifiers filename required.");
			return;
		}

		if (cnFilename == null)
		{
			log.error("cn filename required.");
			return;
		}

		try
		{
			File userListFile = new File(userListFilename);
			userList = getUserList(userListFile);

			File idFile = new File(identifiersFilename);
			identifiers = getIdentifiers(idFile);

			File cnFile = new File(cnFilename);
			cns = getCnCaseMapping(cnFile);
		}
		catch (FileNotFoundException nfe)
		{
			log.error("cannot find or open file: {}", nfe.getMessage(), nfe);
			return;
		}

		String url = config.getProperty("ws.url.alma");
		String key = config.getProperty("ws.url.alma.key");

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url);
		target = target.path("users").queryParam("apikey", key);

		for (String oldPrimaryId : userList)
		{
			log.info("processing user: {}", oldPrimaryId);

			String newPrimaryId = identifiers.get(oldPrimaryId.toLowerCase());
			if (newPrimaryId == null)
			{
				log.warn("user partyId not found: {}", oldPrimaryId);
				continue;
			}

			String oneId = cns.get(oldPrimaryId.toLowerCase());
			if (oneId == null)
				oneId = oldPrimaryId;

			WebTarget t = target.path(oldPrimaryId);
			User user = t.request(MediaType.APPLICATION_XML_TYPE).get(User.class);
			log.info("fetched user: {}", user.getPrimaryId());

			// remove PARTY_ID, ONE_ID, INSTITUTION_ID
			List<UserIdentifier> idsToRemove = new ArrayList<UserIdentifier>(5);
			for (UserIdentifier i : user.getUserIdentifiers().getUserIdentifier())
			{
				if ("PARTY_ID".equals(i.getIdType().getValue()))
					idsToRemove.add(i);
				if ("ONE_ID".equals(i.getIdType().getValue()))
					idsToRemove.add(i);
				if ("INST_ID".equals(i.getIdType().getValue()))
					idsToRemove.add(i);

				log.info("identifiers [{}]: {}", i.getIdType().getValue(), i.getValue());
			}

			for (UserIdentifier i : idsToRemove)
				user.getUserIdentifiers().getUserIdentifier().remove(i);

			// save with oldPrimaryId
			JAXBElement<User> jaxbUser = of.createUser(user);
{'fraction_non_alphanumeric': 0.13995738477066277, 'fraction_numerical': 0.001794325445777728, 'mean_word_length': 3.8865753424657536, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5218655', 'n_tokens_mistral': 3261, 'n_tokens_neox': 2858, 'n_words': 643}