<commit_before>package org.devilry.core.session;

import javax.ejb.*;
import javax.persistence.*;
import java.util.Date;
import org.devilry.core.entity.*;

@Stateless
public class TreeManagerImpl implements TreeManagerRemote {
	@PersistenceContext(unitName="DevilryCore")
	private EntityManager em;

	public long addNode(String name, String displayName) {
		return addNode(name, displayName, -1);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addNode(String name, String displayName, long parentId) {
		Node node = new Node();
		node.setName(name.toLowerCase());
		node.setDisplayName(displayName);
		node.setParentId(parentId);

		em.persist(node);
		em.flush();

		return node.getId();
	}

	public long addCourseNode(String name, String displayName, long parentId) {
		return addCourseNode(name, name, displayName, parentId);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addCourseNode(String name, String courseCode, String displayName, long parentId) {
		CourseNode node = new CourseNode();
		node.setName(name.toLowerCase());
		node.setDisplayName(displayName);
		node.setCourseCode(courseCode);
		node.setParentId(parentId);

		em.persist(node);
		em.flush();
		
		return node.getId();
	}

	public long addPeriodNode(String name, Date start, Date end, long parentId) {
		return addPeriodNode(name, name, start, end, parentId);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addPeriodNode(String name, String displayName, Date start, Date end, long parentId) {
		PeriodNode node = new PeriodNode();
		node.setName(name.toLowerCase());
		node.setDisplayName(displayName);
		node.setStartDate(start);
		node.setEndDate(end);
		node.setParentId(parentId);

		em.persist(node);
		em.flush();

		return node.getId();
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addAssignmentNode(String name, String displayName, long parentId) {
		AssignmentNode node = new AssignmentNode();
		node.setName(name.toLowerCase());
		node.setDisplayName(displayName);
		node.setParentId(parentId);

		em.persist(node);
		em.flush();

		return node.getId();
	}

	public long getNodeIdFromPath(String path) {
		String[] sp = path.split("\\.");

		if(sp.length == 1) {
			return getNodeId(sp[0], -1);
		} else if(sp.length > 1) {
			int length = 0;
			long id = 0;

			while(length < sp.length-1) {
				id = getNodeId(sp[length+1], sp[length]);
				length++;

				if(id == -1)
					return id;
			}

			return id;
		}

		return -1;
	}

	private Node getNode(long nodeId) {
		Query q = em.createQuery("SELECT n FROM Node n WHERE n.id=:nodeId");
		q.setParameter("nodeId", nodeId);

		Node node;

		try {
			node = (Node) q.getSingleResult();
		} catch(NoResultException e) {
			node = null;
		}

		return node;
	}

	private long getNodeId(String name) {
		return getNodeId(name, -1);
	}

	private long getNodeId(String name, long parentId) {
		Query q;<commit_msg>changed to JPQL => em.find in treeManager.getNode<commit_after>package org.devilry.core.session;

import javax.ejb.*;
import javax.persistence.*;
import java.util.Date;
import org.devilry.core.entity.*;

@Stateless
public class TreeManagerImpl implements TreeManagerRemote {
	@PersistenceContext(unitName="DevilryCore")
	private EntityManager em;

	public long addNode(String name, String displayName) {
		return addNode(name, displayName, -1);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addNode(String name, String displayName, long parentId) {
		Node node = new Node();
		node.setName(name.toLowerCase());
		node.setDisplayName(displayName);
		node.setParentId(parentId);

		em.persist(node);
		em.flush();

		return node.getId();
	}

	public long addCourseNode(String name, String displayName, long parentId) {
		return addCourseNode(name, name, displayName, parentId);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addCourseNode(String name, String courseCode, String displayName, long parentId) {
		CourseNode node = new CourseNode();
		node.setName(name.toLowerCase());
		node.setDisplayName(displayName);
		node.setCourseCode(courseCode);
		node.setParentId(parentId);

		em.persist(node);
		em.flush();
		
		return node.getId();
	}

	public long addPeriodNode(String name, Date start, Date end, long parentId) {
		return addPeriodNode(name, name, start, end, parentId);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addPeriodNode(String name, String displayName, Date start, Date end, long parentId) {
		PeriodNode node = new PeriodNode();
		node.setName(name.toLowerCase());
		node.setDisplayName(displayName);
		node.setStartDate(start);
		node.setEndDate(end);
		node.setParentId(parentId);

		em.persist(node);
		em.flush();

		return node.getId();
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addAssignmentNode(String name, String displayName, long parentId) {
		AssignmentNode node = new AssignmentNode();
		node.setName(name.toLowerCase());
		node.setDisplayName(displayName);
		node.setParentId(parentId);

		em.persist(node);
		em.flush();

		return node.getId();
	}

	public long getNodeIdFromPath(String path) {
		String[] sp = path.split("\\.");

		if(sp.length == 1) {
			return getNodeId(sp[0], -1);
		} else if(sp.length > 1) {
			int length = 0;
			long id = 0;

			while(length < sp.length-1) {
				id = getNodeId(sp[length+1], sp[length]);
				length++;

				if(id == -1)
					return id;
			}

			return id;
		}

		return -1;
	}

	private Node getNode(long nodeId) {
		return em.find(Node.class, nodeId);
	}

	private long getNodeId(String name) {
		return getNodeId(name, -1);
	}

	private long getNodeId(String name, long parentId) {
		Query q;
{'fraction_non_alphanumeric': 0.12389686797023707, 'fraction_numerical': 0.00415296764146046, 'mean_word_length': 4.6611165523996085, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '3522092', 'n_tokens_mistral': 2063, 'n_tokens_neox': 1890, 'n_words': 434}