<commit_before>package edu.wustl.catissuecore.bizlogic;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.xml.bind.JAXBElement;

import org.xml.sax.SAXException;

import edu.common.dynamicextensions.domain.userinterface.Container;
import edu.common.dynamicextensions.domaininterface.AbstractEntityInterface;
import edu.common.dynamicextensions.domaininterface.EntityGroupInterface;
import edu.common.dynamicextensions.domaininterface.EntityInterface;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.common.dynamicextensions.util.DynamicExtensionsUtility;
import edu.wustl.catissuecore.domain.processingprocedure.Action;
import edu.wustl.catissuecore.processingprocedure.EventType;
import edu.wustl.catissuecore.processingprocedure.SPPType;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.metadata.XMLUtility;
import edu.wustl.common.exception.ApplicationException;
import edu.wustl.common.exception.BizLogicException;

public final class SPPXMLParser
{
	private static SPPXMLParser parser;

	private SPPXMLParser()
	{

	}

	/**
	 * get the only instance of this class
	 * @return the only instance of this class
	 */
	public static SPPXMLParser getInstance()
	{
		if (parser==null)
		{
			parser= new SPPXMLParser();
		}
		return parser;
	}

	/**
	 * Parses the XML file and returns the list of action objects.
	 * @param xmlFile xmlFile
	 * @param inputStream inputStream
	 * @return actionList
	 * @throws DynamicExtensionsSystemException DynamicExtensionsSystemException
	 * @throws SAXException SAXException
	 * @throws BizLogicException
	 */
	public Set<Action> parseXML(String xmlFile, InputStream inputStream) throws DynamicExtensionsSystemException, SAXException, BizLogicException
	{
		Set<Action> actionList = new HashSet<Action>();
		final String packageName = SPPType.class.getPackage().getName();

		final JAXBElement jAXBElement = (JAXBElement) XMLUtility.getJavaObjectForXML(xmlFile,
				inputStream, packageName, Constants.SPP_XSD_FILENAME);

		SPPType spp = (SPPType)jAXBElement.getValue();
		List<EventType> events = spp.getEvent();
		for(EventType event : events)
		{
			populateActionList(actionList, event);
		}
		handleActionErrors(actionList);
		return actionList;
	}

	/**
	 * Populates the list of actions belonging to an SPP.
	 * @param actionList actionList
	 * @param event event
	 * @throws DynamicExtensionsSystemException
	 */
	private void populateActionList(Set<Action> actionList, EventType event) throws DynamicExtensionsSystemException
	{
			String name = event.getName();
			String barcode = event.getBarcode();
			Long order = event.getOrder();
			String entityGrp = event.getEntityGroupName();
			String activity = event.getActivity();
			String uniqueId = event.getUniqueId();

			if(activity == null)
			{
				activity = Constants.ACTIVE;
			}
			if(barcode != null && barcode.length() == 0)
			{
				barcode = null;
			}
			Action actionObj = new Action();
			EntityGroupInterface entityGroup = DynamicExtensionsUtility.getEntityGroupByName(entityGrp);
			if(entityGroup == null)
			{
				throw new DynamicExtensionsSystemException(Constants.SPP_ENTITY_GRP_ERROR+": '"+entityGrp+"'");
			}
			EntityInterface entity = entityGroup.getEntityByName(name);
			if(entity == null)
			{
				throw new DynamicExtensionsSystemException(Constants.SPP_EVENT_ERROR+": '"+name+"'");
			}
			Collection<AbstractEntityInterface> containers = entity.getContainerCollection();
			Iterator<AbstractEntityInterface> itr = containers.iterator();
			if(itr.hasNext())
			{
				Container container = (Container)itr.next();
				actionObj.setContainerId(container.getId());
			}
			actionObj.setBarcode(barcode);
			actionObj.setActionOrder(order);
			actionObj.setUniqueId(uniqueId);
			actionObj.setActivityStatus(activity);
			actionList.add(actionObj);
	}

	/**
	 * Check the XML for validations and throw appropriate exception depending upon the error.
	 * @param actionList actionList
	 * @throws BizLogicException BizLogicException
	 */
	private void handleActionErrors(Set<Action> actionList) throws BizLogicException
	{
		for (Action action : actionList)
		{<commit_msg>* Task/Bug Id: <Development task name or bug id> * Code Reviewed By: <Reviewed By> On : <Review date> * Technical Details:  #21824, changed the error message * Unit tested:   <Names of the test cases> * Developer Notes: <Free form comments, typically will involve names of the methods changed etc> * Current Build++ Counts for this file : <B++ counts before the file is checked-in> * PMD - X     Checkstyle - X     Findbugs - X     CPD – X<commit_after>package edu.wustl.catissuecore.bizlogic;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.xml.bind.JAXBElement;

import org.xml.sax.SAXException;

import edu.common.dynamicextensions.domain.userinterface.Container;
import edu.common.dynamicextensions.domaininterface.AbstractEntityInterface;
import edu.common.dynamicextensions.domaininterface.EntityGroupInterface;
import edu.common.dynamicextensions.domaininterface.EntityInterface;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.common.dynamicextensions.util.DynamicExtensionsUtility;
import edu.wustl.catissuecore.domain.processingprocedure.Action;
import edu.wustl.catissuecore.processingprocedure.EventType;
import edu.wustl.catissuecore.processingprocedure.SPPType;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.metadata.XMLUtility;
import edu.wustl.common.exception.ApplicationException;
import edu.wustl.common.exception.BizLogicException;

public final class SPPXMLParser
{
	private static SPPXMLParser parser;

	private SPPXMLParser()
	{

	}

	/**
	 * get the only instance of this class
	 * @return the only instance of this class
	 */
	public static SPPXMLParser getInstance()
	{
		if (parser==null)
		{
			parser= new SPPXMLParser();
		}
		return parser;
	}

	/**
	 * Parses the XML file and returns the list of action objects.
	 * @param xmlFile xmlFile
	 * @param inputStream inputStream
	 * @return actionList
	 * @throws DynamicExtensionsSystemException DynamicExtensionsSystemException
	 * @throws SAXException SAXException
	 * @throws BizLogicException
	 */
	public Set<Action> parseXML(String xmlFile, InputStream inputStream) throws DynamicExtensionsSystemException, SAXException, BizLogicException
	{
		Set<Action> actionList = new HashSet<Action>();
		final String packageName = SPPType.class.getPackage().getName();

		final JAXBElement jAXBElement = (JAXBElement) XMLUtility.getJavaObjectForXML(xmlFile,
				inputStream, packageName, Constants.SPP_XSD_FILENAME);

		SPPType spp = (SPPType)jAXBElement.getValue();
		List<EventType> events = spp.getEvent();
		for(EventType event : events)
		{
			populateActionList(actionList, event);
		}
		handleActionErrors(actionList);
		return actionList;
	}

	/**
	 * Populates the list of actions belonging to an SPP.
	 * @param actionList actionList
	 * @param event event
	 * @throws DynamicExtensionsSystemException
	 */
	private void populateActionList(Set<Action> actionList, EventType event) throws DynamicExtensionsSystemException
	{
			String name = event.getName();
			String barcode = event.getBarcode();
			Long order = event.getOrder();
			String entityGrp = event.getEntityGroupName();
			String activity = event.getActivity();
			String uniqueId = event.getUniqueId();

			if(activity == null)
			{
				activity = Constants.ACTIVE;
			}
			if(barcode != null && barcode.length() == 0)
			{
				barcode = null;
			}
			Action actionObj = new Action();
			EntityGroupInterface entityGroup = DynamicExtensionsUtility.getEntityGroupByName(entityGrp);
			if(entityGroup == null)
			{
				throw new DynamicExtensionsSystemException("The group name '"+entityGrp+"' specified is not present in the system");
			}
			EntityInterface entity = entityGroup.getEntityByName(name);
			if(entity == null)
			{
				throw new DynamicExtensionsSystemException("The event name '"+name+"' specified is not present in the system");
			}
			Collection<AbstractEntityInterface> containers = entity.getContainerCollection();
			Iterator<AbstractEntityInterface> itr = containers.iterator();
			if(itr.hasNext())
			{
				Container container = (Container)itr.next();
				actionObj.setContainerId(container.getId());
			}
			actionObj.setBarcode(barcode);
			actionObj.setActionOrder(order);
			actionObj.setUniqueId(uniqueId);
			actionObj.setActivityStatus(activity);
			actionList.add(actionObj);
	}

	/**
	 * Check the XML for validations and throw appropriate exception depending upon the error.
	 * @param actionList actionList
	 * @throws BizLogicException BizLogicException
	 */
	private void handleActionErrors(Set<Action> actionList) throws BizLogicException
	{
		for (Action action : actionList)
		{
{'fraction_non_alphanumeric': 0.09555824975201146, 'fraction_numerical': 0.0007715198941915574, 'mean_word_length': 5.065508021390374, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7325922', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2650, 'n_words': 616}