<commit_before>package graphene.services;

import graphene.dao.GroupDAO;
import graphene.dao.PermissionDAO;
import graphene.dao.RoleDAO;
import graphene.dao.UserDAO;
import graphene.dao.UserGroupDAO;
import graphene.dao.UserWorkspaceDAO;
import graphene.dao.WorkspaceDAO;
import graphene.model.idl.AuthenticationException;
import graphene.model.idl.G_Group;
import graphene.model.idl.G_Permission;
import graphene.model.idl.G_Role;
import graphene.model.idl.G_User;
import graphene.model.idl.G_UserDataAccess;
import graphene.model.idl.G_UserSpaceRelationshipType;
import graphene.model.idl.G_Workspace;
import graphene.model.idl.UnauthorizedActionException;
import graphene.util.crypto.PasswordHash;
import graphene.util.validator.ValidationUtils;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import org.apache.avro.AvroRemoteException;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.joda.time.DateTime;
import org.slf4j.Logger;

/**
 * 
 * @author djue
 * 
 */
public class UserServiceImpl implements G_UserDataAccess {
	@Inject
	private GroupDAO gDao;
	@Inject
	private Logger logger;
	PasswordHash passwordHasher = new PasswordHash();
	@Inject
	private PermissionDAO pDao;
	@Inject
	private RoleDAO rDao;
	@Inject
	private UserDAO uDao;
	@Inject
	private UserGroupDAO ugDao;
	@Inject
	private UserWorkspaceDAO uwDao;

	@Inject
	private WorkspaceDAO wDao;

	@Override
	public G_Workspace addNewWorkspaceForUser(final String userId,
			final G_Workspace workspace) {
		// save the workspace
		try {
			final G_Workspace w = wDao.save(workspace);
			if (w == null) {
				logger.error("Could not create new workspace");
			} else {
				final boolean success = uwDao.addRelationToWorkspace(userId,
						G_UserSpaceRelationshipType.EDITOR_OF,
						workspace.getId());
				if (!success) {
					logger.error("Could not create editor relationship for workspace");
				}
			}

		} catch (final Exception e) {
			e.printStackTrace();
			logger.error("Could not create editor relationship for workspace "
					+ e.getMessage());
		}
		return workspace;
	}

	@Override
	public int countUsers(final String partialName) {
		// TODO: Put business logic here to let admins see all users.
		final long lcount = uDao.countUsers(partialName);
		if (lcount > Integer.MAX_VALUE) {
			// I know this won't ever happen, but I want to make the static code
			// checkers happy.
			logger.error("Too many workspaces");
			return Integer.MAX_VALUE;
		} else {
			return (int) lcount;
		}
	}

	@Override
	public int countWorkspaces(final String userId,
			final String partialWorkspaceName) {
		// TODO: Put business logic here to let admins see all workspaces.
		final long lcount = wDao.countWorkspaces(userId, partialWorkspaceName);
		if (lcount > Integer.MAX_VALUE) {
			// I know this won't ever happen, but I want to make the static code
			// checkers happy.
			logger.error("Too many workspaces");
			return Integer.MAX_VALUE;
		} else {
			return (int) lcount;
		}
	}

	@Override
	public G_Workspace createFirstWorkspaceForUser(final String userId) {
		G_Workspace w = createTempWorkspaceForUser(userId);

		if (ValidationUtils.isValid(w)) {
			w.setTitle("First Workspace");
			w = wDao.save(w);
			if (ValidationUtils.isValid(w)) {
				uwDao.addRelationToWorkspace(userId,
						G_UserSpaceRelationshipType.CREATOR_OF, w.getId());
				uwDao.addRelationToWorkspace(userId,
						G_UserSpaceRelationshipType.EDITOR_OF, w.getId());
			}
		}
		return w;
	}

	@Override
	public G_Workspace createTempWorkspaceForUser(final String userId) {

		final G_User user = getUser(userId);
		G_Workspace w = null;
		if (ValidationUtils.isValid(user)) {
			// we need a valid user in order to create the workspace.
			final DateTime time = DateTime.now();
			w = new G_Workspace();
			w.setActive(true);
			w.setCreated(time.getMillis());
			w.setModified(time.getMillis());
			w.setDescription("New Workspace");
			w.setTitle(user.getUsername() + "-Workspace"
					+ time.toString("YYYYmmDD-HHMMSS"));
			w.setDatamap("");
			w.setJson("");
			w.setQueries("");
		}
		return w;
	}

	@Override
	public boolean deleteUser(final String userId) {
		return uDao.delete(userId);
	}

	@Override
	public boolean deleteWorkspace(final String userId, final String workspaceId)
			throws UnauthorizedActionException {
		if (uwDao.hasRelationship(userId, workspaceId,
				G_UserSpaceRelationshipType.CREATOR_OF)) {
			return wDao.delete(workspaceId);<commit_msg>Fixed some errors due to model change.<commit_after>package graphene.services;

import graphene.dao.GroupDAO;
import graphene.dao.PermissionDAO;
import graphene.dao.RoleDAO;
import graphene.dao.UserDAO;
import graphene.dao.UserGroupDAO;
import graphene.dao.UserWorkspaceDAO;
import graphene.dao.WorkspaceDAO;
import graphene.model.idl.AuthenticationException;
import graphene.model.idl.G_Group;
import graphene.model.idl.G_Permission;
import graphene.model.idl.G_Role;
import graphene.model.idl.G_User;
import graphene.model.idl.G_UserDataAccess;
import graphene.model.idl.G_UserSpaceRelationshipType;
import graphene.model.idl.G_Workspace;
import graphene.model.idl.UnauthorizedActionException;
import graphene.util.crypto.PasswordHash;
import graphene.util.validator.ValidationUtils;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;

import org.apache.avro.AvroRemoteException;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.joda.time.DateTime;
import org.slf4j.Logger;

/**
 * 
 * @author djue
 * 
 */
public class UserServiceImpl implements G_UserDataAccess {
	@Inject
	private GroupDAO gDao;
	@Inject
	private Logger logger;
	PasswordHash passwordHasher = new PasswordHash();
	@Inject
	private PermissionDAO pDao;
	@Inject
	private RoleDAO rDao;
	@Inject
	private UserDAO uDao;
	@Inject
	private UserGroupDAO ugDao;
	@Inject
	private UserWorkspaceDAO uwDao;

	@Inject
	private WorkspaceDAO wDao;

	@Override
	public G_Workspace addNewWorkspaceForUser(final String userId,
			final G_Workspace workspace) {
		// save the workspace
		try {
			final G_Workspace w = wDao.save(workspace);
			if (w == null) {
				logger.error("Could not create new workspace");
			} else {
				final boolean success = uwDao.addRelationToWorkspace(userId,
						G_UserSpaceRelationshipType.EDITOR_OF,
						workspace.getId());
				if (!success) {
					logger.error("Could not create editor relationship for workspace");
				}
			}

		} catch (final Exception e) {
			e.printStackTrace();
			logger.error("Could not create editor relationship for workspace "
					+ e.getMessage());
		}
		return workspace;
	}

	@Override
	public int countUsers(final String partialName) {
		// TODO: Put business logic here to let admins see all users.
		final long lcount = uDao.countUsers(partialName);
		if (lcount > Integer.MAX_VALUE) {
			// I know this won't ever happen, but I want to make the static code
			// checkers happy.
			logger.error("Too many workspaces");
			return Integer.MAX_VALUE;
		} else {
			return (int) lcount;
		}
	}

	@Override
	public int countWorkspaces(final String userId,
			final String partialWorkspaceName) {
		// TODO: Put business logic here to let admins see all workspaces.
		final long lcount = wDao.countWorkspaces(userId, partialWorkspaceName);
		if (lcount > Integer.MAX_VALUE) {
			// I know this won't ever happen, but I want to make the static code
			// checkers happy.
			logger.error("Too many workspaces");
			return Integer.MAX_VALUE;
		} else {
			return (int) lcount;
		}
	}

	@Override
	public G_Workspace createFirstWorkspaceForUser(final String userId) {
		G_Workspace w = createTempWorkspaceForUser(userId);

		if (ValidationUtils.isValid(w)) {
			w.setTitle("First Workspace");
			w = wDao.save(w);
			if (ValidationUtils.isValid(w)) {
				uwDao.addRelationToWorkspace(userId,
						G_UserSpaceRelationshipType.CREATOR_OF, w.getId());
				uwDao.addRelationToWorkspace(userId,
						G_UserSpaceRelationshipType.EDITOR_OF, w.getId());
			}
		}
		return w;
	}

	@Override
	public G_Workspace createTempWorkspaceForUser(final String userId) {

		final G_User user = getUser(userId);
		G_Workspace w = null;
		if (ValidationUtils.isValid(user)) {
			// we need a valid user in order to create the workspace.
			final DateTime time = DateTime.now();
			w = new G_Workspace();
			w.setActive(true);
			w.setCreated(time.getMillis());
			w.setModified(time.getMillis());
			w.setDescription("New Workspace");
			w.setTitle(user.getUsername() + "-Workspace"
					+ time.toString("YYYYmmDD-HHMMSS"));
			w.setQueries(new ArrayList<String>());
		}
		return w;
	}

	@Override
	public boolean deleteUser(final String userId) {
		return uDao.delete(userId);
	}

	@Override
	public boolean deleteWorkspace(final String userId, final String workspaceId)
			throws UnauthorizedActionException {
		if (uwDao.hasRelationship(userId, workspaceId,
				G_UserSpaceRelationshipType.CREATOR_OF)) {
			return wDao.delete(workspaceId);
{'fraction_non_alphanumeric': 0.09899680299856686, 'fraction_numerical': 0.0004409657149156653, 'mean_word_length': 4.403216200119118, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, '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': '422448', 'n_tokens_mistral': 3205, 'n_tokens_neox': 2957, 'n_words': 704}