<commit_before>package uk.ac.cam.cl.ticking.ui.api;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import uk.ac.cam.cl.signups.api.exceptions.DuplicateNameException;
import uk.ac.cam.cl.ticking.signups.TickSignups;
import uk.ac.cam.cl.ticking.ui.actors.Group;
import uk.ac.cam.cl.ticking.ui.actors.Grouping;
import uk.ac.cam.cl.ticking.ui.actors.Role;
import uk.ac.cam.cl.ticking.ui.actors.User;
import uk.ac.cam.cl.ticking.ui.api.public_interfaces.IGroupApiFacade;
import uk.ac.cam.cl.ticking.ui.api.public_interfaces.beans.GroupBean;
import uk.ac.cam.cl.ticking.ui.configuration.Configuration;
import uk.ac.cam.cl.ticking.ui.configuration.ConfigurationLoader;
import uk.ac.cam.cl.ticking.ui.dao.IDataManager;
import uk.ac.cam.cl.ticking.ui.exceptions.DuplicateDataEntryException;
import uk.ac.cam.cl.ticking.ui.ticks.Tick;
import uk.ac.cam.cl.ticking.ui.util.PermissionsManager;
import uk.ac.cam.cl.ticking.ui.util.Strings;

import com.google.inject.Inject;

public class GroupApiFacade implements IGroupApiFacade {

	private static final Logger log = LoggerFactory
			.getLogger(GroupApiFacade.class.getName());

	private IDataManager db;
	@SuppressWarnings("unused")
	// not currently used but could quite possibly be needed in the future, will
	// remove if not
	private ConfigurationLoader<Configuration> config;

	private TickSignups tickSignupService;
	
	private PermissionsManager permissions;

	/**
	 * @param db
	 * @param config
	 */
	@Inject
	public GroupApiFacade(IDataManager db,
			ConfigurationLoader<Configuration> config,
			TickSignups tickSignupService, PermissionsManager permissions) {
		this.db = db;
		this.config = config;
		this.tickSignupService = tickSignupService;
		this.permissions = permissions;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Response getGroup(String groupId) {
		Group group = db.getGroup(groupId);

		/* Get the group object, returning if not found */
		if (group == null) {
			log.error("Requested group " + groupId
					+ " but it couldn't be found");
			return Response.status(Status.NOT_FOUND).entity(Strings.MISSING)
					.build();
		}

		return Response.ok(group).build();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public Response exportGroup(String groupId) {
		Group group = db.getGroup(groupId);

		/* Get the group object, returning if not found */
		if (group == null) {
			log.error("Requested group " + groupId
					+ " but it couldn't be found");
			return Response.status(Status.NOT_FOUND).entity(Strings.MISSING)
					.build();
		}
		
		File temp;
		PrintWriter writer;
		
		try {
			temp = File.createTempFile(group.getGroupId(), ".txt");
			writer = new PrintWriter(temp);
			
		} catch (IOException e) {
			log.error("Tried exporting group "
					+ groupId, e);
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity(Strings.IDEMPOTENTRETRY).build();
		} 
		
		for (Role role : Role.values()) {
			writer.println(role.name());
			writer.println();
			for (User user : db.getUsers(groupId, role)) {
				writer.print(user.getCrsid()+", ");
			}
		}
		
		writer.println("The first line");
		writer.println("The second line");
		writer.close();

		ResponseBuilder response = Response.ok((Object) temp);
        response.header("Content-Disposition", "attachment; filename='"+group.getName()+"'");
        return response.build();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Response deleteGroup(HttpServletRequest request, String groupId) {
		String crsid = (String) request.getSession().getAttribute(
				"RavenRemoteUser");

		/* Get the group object, returning if not found */
		Group group = db.getGroup(groupId);<commit_msg>fixing group exporting<commit_after>package uk.ac.cam.cl.ticking.ui.api;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import uk.ac.cam.cl.signups.api.exceptions.DuplicateNameException;
import uk.ac.cam.cl.ticking.signups.TickSignups;
import uk.ac.cam.cl.ticking.ui.actors.Group;
import uk.ac.cam.cl.ticking.ui.actors.Grouping;
import uk.ac.cam.cl.ticking.ui.actors.Role;
import uk.ac.cam.cl.ticking.ui.actors.User;
import uk.ac.cam.cl.ticking.ui.api.public_interfaces.IGroupApiFacade;
import uk.ac.cam.cl.ticking.ui.api.public_interfaces.beans.GroupBean;
import uk.ac.cam.cl.ticking.ui.configuration.Configuration;
import uk.ac.cam.cl.ticking.ui.configuration.ConfigurationLoader;
import uk.ac.cam.cl.ticking.ui.dao.IDataManager;
import uk.ac.cam.cl.ticking.ui.exceptions.DuplicateDataEntryException;
import uk.ac.cam.cl.ticking.ui.ticks.Tick;
import uk.ac.cam.cl.ticking.ui.util.PermissionsManager;
import uk.ac.cam.cl.ticking.ui.util.Strings;

import com.google.inject.Inject;

public class GroupApiFacade implements IGroupApiFacade {

	private static final Logger log = LoggerFactory
			.getLogger(GroupApiFacade.class.getName());

	private IDataManager db;
	@SuppressWarnings("unused")
	// not currently used but could quite possibly be needed in the future, will
	// remove if not
	private ConfigurationLoader<Configuration> config;

	private TickSignups tickSignupService;
	
	private PermissionsManager permissions;

	/**
	 * @param db
	 * @param config
	 */
	@Inject
	public GroupApiFacade(IDataManager db,
			ConfigurationLoader<Configuration> config,
			TickSignups tickSignupService, PermissionsManager permissions) {
		this.db = db;
		this.config = config;
		this.tickSignupService = tickSignupService;
		this.permissions = permissions;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Response getGroup(String groupId) {
		Group group = db.getGroup(groupId);

		/* Get the group object, returning if not found */
		if (group == null) {
			log.error("Requested group " + groupId
					+ " but it couldn't be found");
			return Response.status(Status.NOT_FOUND).entity(Strings.MISSING)
					.build();
		}

		return Response.ok(group).build();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public Response exportGroup(String groupId) {
		Group group = db.getGroup(groupId);

		/* Get the group object, returning if not found */
		if (group == null) {
			log.error("Requested group " + groupId
					+ " but it couldn't be found");
			return Response.status(Status.NOT_FOUND).entity(Strings.MISSING)
					.build();
		}
		
		File temp;
		PrintWriter writer;
		
		try {
			temp = File.createTempFile(group.getGroupId(), ".txt");
			writer = new PrintWriter(temp);
			
		} catch (IOException e) {
			log.error("Tried exporting group "
					+ groupId, e);
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity(Strings.IDEMPOTENTRETRY).build();
		} 
		
		for (Role role : Role.values()) {
			writer.println(role.name());
			for (User user : db.getUsers(groupId, role)) {
				writer.print(user.getCrsid()+", ");
			}
			writer.println();
		}
		
		writer.close();

		ResponseBuilder response = Response.ok((Object) temp);
        response.header("Content-Disposition", "attachment; filename="+group.getName());
        return response.build();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Response deleteGroup(HttpServletRequest request, String groupId) {
		String crsid = (String) request.getSession().getAttribute(
				"RavenRemoteUser");

		/* Get the group object, returning if not found */
		Group group = db.getGroup(groupId);
{'fraction_non_alphanumeric': 0.12824260138838145, 'fraction_numerical': 0.00048715138229204726, 'mean_word_length': 4.181072555205048, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '938161', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2631, 'n_words': 533}