<commit_before>package com.xsmp.ochat.functions;

import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;

import org.apache.olingo.odata2.api.annotation.edm.EdmFacets;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport.ReturnType;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport.HttpMethod;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport.ReturnType.Type;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImportParameter;
import org.apache.olingo.odata2.api.exception.ODataException;
import org.apache.olingo.odata2.api.processor.ODataContext;
import org.apache.olingo.odata2.jpa.processor.core.ODataJPAContextImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xsmp.id.IdentityInteraction;
import com.xsmp.ochat.model.Conversation;
import com.xsmp.ochat.model.MessageUser;
import com.xsmp.ochat.util.Utility;

public class ConversationProcessor {
	
	private static Logger logger = LoggerFactory.getLogger(ConversationProcessor.class);

	private EntityManager em;

	public ConversationProcessor() {
		em = Utility.getEntityManagerFactory().createEntityManager();
	}

	/**
	 * Return a Conversation entity representing a two-party connection between the authenticated user and the
	 * specified target user id.  If such a two-way Conversation already exists, return that, otherwise return a new Conversation.
	 * 
	 * @param targetMessageUserId the OChat user entity to connect with
	 * @return the two-way Conversation entity
	 * @throws ODataException
	 */
	@EdmFunctionImport(
			returnType = @ReturnType(type = Type.ENTITY, isCollection = false), 
			entitySet = "Conversations", 
			httpMethod = HttpMethod.POST)
	public Conversation createConversation(
			@EdmFunctionImportParameter(name = "TargetUserId", facets = @EdmFacets(nullable = false)) final Integer targetMessageUserId)
					throws ODataException {
		
		Conversation result = null;

		if (targetMessageUserId <= 0) {
			throw new ODataException("Invalid TargetUserId");
		}
		
		/*
		 * Get the authenticated username
		 */
		ODataContext ctx = ODataJPAContextImpl.getContextInThreadLocal();
		HttpServletRequest r = (HttpServletRequest) ctx.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT);
		String user = r.getRemoteUser();
		MessageUser u1 = null;
		if (user != null) {
			u1 = IdentityInteraction.verifyUser(r, em);
			logger.debug( "username from HttpServletRequest '"+u1.getUsername()+"'" );
		}
		else {
			// This error will have the side effect of returning an empty result set
			logger.error("Assertion error: There is no authenticated user defined in the HttpServletRequest -- " +
					     "check your web.xml application configuration");
			throw new ODataException("There is no authenticated user defined in the HttpServletRequest -- " +
				     "check your web.xml application configuration");
		}<commit_msg>Changed function import to use HTTP GET to allow for easier interaction with SAPUI5<commit_after>package com.xsmp.ochat.functions;

import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;

import org.apache.olingo.odata2.api.annotation.edm.EdmFacets;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport.ReturnType;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport.HttpMethod;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport.ReturnType.Type;
import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImportParameter;
import org.apache.olingo.odata2.api.exception.ODataException;
import org.apache.olingo.odata2.api.processor.ODataContext;
import org.apache.olingo.odata2.jpa.processor.core.ODataJPAContextImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xsmp.id.IdentityInteraction;
import com.xsmp.ochat.model.Conversation;
import com.xsmp.ochat.model.MessageUser;
import com.xsmp.ochat.util.Utility;

public class ConversationProcessor {
	
	private static Logger logger = LoggerFactory.getLogger(ConversationProcessor.class);

	private EntityManager em;

	public ConversationProcessor() {
		em = Utility.getEntityManagerFactory().createEntityManager();
	}

	/**
	 * Return a Conversation entity representing a two-party connection between the authenticated user and the
	 * specified target user id.  If such a two-way Conversation already exists, return that, otherwise return a new Conversation.
	 * 
	 * @param targetMessageUserId the OChat user entity to connect with
	 * @return the two-way Conversation entity
	 * @throws ODataException
	 */
	@EdmFunctionImport(
			returnType = @ReturnType(type = Type.ENTITY, isCollection = false), 
			entitySet = "Conversations", 
			httpMethod = HttpMethod.GET)
	public Conversation createConversation(
			@EdmFunctionImportParameter(name = "TargetUserId", facets = @EdmFacets(nullable = false)) final Integer targetMessageUserId)
					throws ODataException {
		
		Conversation result = null;

		if (targetMessageUserId <= 0) {
			throw new ODataException("Invalid TargetUserId");
		}
		
		/*
		 * Get the authenticated username
		 */
		ODataContext ctx = ODataJPAContextImpl.getContextInThreadLocal();
		HttpServletRequest r = (HttpServletRequest) ctx.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT);
		String user = r.getRemoteUser();
		MessageUser u1 = null;
		if (user != null) {
			u1 = IdentityInteraction.verifyUser(r, em);
			logger.debug( "username from HttpServletRequest '"+u1.getUsername()+"'" );
		}
		else {
			// This error will have the side effect of returning an empty result set
			logger.error("Assertion error: There is no authenticated user defined in the HttpServletRequest -- " +
					     "check your web.xml application configuration");
			throw new ODataException("There is no authenticated user defined in the HttpServletRequest -- " +
				     "check your web.xml application configuration");
		}
{'fraction_non_alphanumeric': 0.0923921821999677, 'fraction_numerical': 0.005007268615732515, 'mean_word_length': 4.988394584139265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4740321', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1794, 'n_words': 433}