package ox.softeng.metadatacatalogue.api;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import ox.softeng.metadatacatalogue.db.ApiContext;
import ox.softeng.metadatacatalogue.db.EMCallable;
import ox.softeng.metadatacatalogue.domain.core.User;
import ox.softeng.metadatacatalogue.domain.core.User.UserRole;

public final class UserApi extends CatalogueApi {

 	protected UserApi() { } // Private constructor as it makes no sense to instantiate this!

	
	public static User createUser(ApiContext apiCtx, String firstName, String lastName, String emailAddress, String password, UserRole role ) throws Exception
	{
		return apiCtx.executeTransaction(new EMCallable<User>(){
            @Override
            public User call(EntityManager em) {
            	try{
            		User u = new User(firstName, lastName, emailAddress, password, role);
					em.persist(u);
					return u;
				}
				catch(Exception e)
				{
					e.printStackTrace();
					return null;
				}
            }
		});
	}

	
	public static User getByEmailAddress(ApiContext apiCtx, String emailAddress) throws Exception
	{
		User u = apiCtx.executeQuery(new EMCallable<User>(){
            @Override
            public User call(EntityManager em) {
				Query query = em.createNamedQuery("User.getUserByEmailAddress");
				query.setParameter("emailAddress", emailAddress.trim());
				User user;
				try{
					user = (User) query.getSingleResult();
				}catch(NoResultException nre)
				{
					return null;
				}
				return user;
            }
            
		});
		return u;
	}

	public static User getByEmailAddressAndPassword(ApiContext apiCtx, String emailAddress, String password) throws Exception
	{
		User u = apiCtx.executeQuery(new EMCallable<User>(){
            @Override
            public User call(EntityManager em) {

				User u;
				try {
					u = getByEmailAddress(apiCtx, emailAddress);
				} catch (Exception e1) {
					e1.printStackTrace();
					return null;
				}
				if(u==null)
				{
					return null;
				}
				try {
					if(u.validatePassword(password))
					{
						return u;
					}
				} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
					e.printStackTrace();
					return null;
				}
				return null;
			}
		});
		return u;
	}
}

@@ -1146,16 +1146,386 @@
 %7D);%0A%09%7D%0A%0A
+%09public static User findOrCreateUser(ApiContext apiCtx, String firstName, String lastName, String emailAddress, String password, UserRole role ) throws Exception%0A%09%7B%0A%09%09User existingUser = getByEmailAddress(apiCtx, emailAddress);%0A%09%09if(existingUser != null)%0A%09%09%7B%0A%09%09%09return existingUser;%0A%09%09%7D%0A%09%09return createUser(apiCtx, firstName, lastName, emailAddress, password, role);%0A%09%7D%0A
 %09%0A%09publi

Add method to API to find or create a user