<commit_before>/*
 * author: marco
 * debugging, refactoring: prajwol, marco
 */

package org.nebula.client.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;
import org.nebula.models.Group;

public class RESTGroupManager extends Resource {

	public RESTGroupManager() {
		super("RESTGroups");
	}

	public List<Group> retrieveAllGroupsMembers() throws JSONException,
			ClientProtocolException, IOException {
		Response r = this.get("retrieveAllGroupsMembers");
		List<Group> myGroups = new ArrayList<Group>();

		for (Iterator iterator = r.getResult().keys(); iterator.hasNext();) {
			String groupName = "" + iterator.next();
			JSONObject groupObj = r.getResult().getJSONObject(groupName);

			myGroups.add(new Group(groupObj, groupName));
		}

		return myGroups;
	}

}<commit_msg>Refactoring the class<commit_after>/*
 * author: marco
 * debugging, refactoring: prajwol, marco
 */

package org.nebula.client.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;
import org.nebula.models.Group;
import org.nebula.models.Profile;

public class RESTGroupManager extends Resource {

	public RESTGroupManager() {
		super("RESTGroups");
	}

	public List<Group> retrieveAllGroupsMembers() throws JSONException,
			ClientProtocolException, IOException {
		Response r = this.get("retrieveAllGroupsMembers");
		List<Group> myGroups = new ArrayList<Group>();

		for (Iterator iterator = r.getResult().keys(); iterator.hasNext();) {
			String groupName = "" + iterator.next();
			JSONObject groupObj = r.getResult().getJSONObject(groupName);

			myGroups.add(new Group(groupObj, groupName));
		}

		return myGroups;
	}
	
	/**
	*
	* @param group group information for the one to add + (contacts to add inside the group)
	* @return the response from the server (HTTPstatus + message)
	 * @throws JSONException 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	*/
	public Status addNewGroup(Group group) throws ClientProtocolException, IOException, JSONException {
		//group insertion
		HashMap<String, Object> hM = new HashMap<String, Object>();
		hM.put("groupName", group.getGroupName());
		hM.put("status", group.getGroupStatus());
		
		Response r = this.post("insertGroup", hM);
		//201 = HTTP status code for user inserted
		boolean validOP = r.getStatus() == 201;
		Status status = new Status(validOP, "" + r.getResult());
		//store the id of the brand new group for utilize it in inserting the user
		group.setId(Integer.parseInt(r.getResult().getString("id")));
		return status;
	}
	
	public Status retrieveGroup(Group g) throws JSONException, ClientProtocolException, IOException {
		HashMap<String, String> hM = new HashMap<String, String>();
		hM.put("id", "" + g.getId());
		Response r = this.get("retrieveGroup", hM);
		g.setGroupName("" + r.getResult().get("groupName"));
		g.setGroupStatus("" + r.getResult().get("status"));
		boolean validOP = (r.getStatus() >= 200) && (r.getStatus() < 300);
		Status status = new Status(validOP, "" + r.getResult());
		return status;
	} 
	
	public List<Group> retrieveGroups() throws JSONException, ClientProtocolException, IOException {
		Response r = this.get("retrieveGroups");
		List<Group> myGroups = new ArrayList<Group>();
		
		for (int i=0; i < r.getResult().length();i++)
		{
			Group group = new Group();
			JSONObject jSonObj = r.getResult().getJSONObject("" + i);
			group.setGroupName(jSonObj.getString("groupName"));
			group.setId(jSonObj.getInt("id"));
			group.setGroupStatus(jSonObj.getString("status"));
			myGroups.add(group);
		}
		return myGroups;
	} 
	
	public Status insertUsersIntoGroup(Group group) throws ClientProtocolException, IOException, JSONException{
		//for every user, perform the insert operation
		Response r = null;
		List<Profile> user = group.getContacts();
		for (int i=0; i<user.size(); i++)
		{
			HashMap<String, Object> hMGroupUser = new HashMap<String, Object>();
			hMGroupUser.put("userID", user.get(i).getId());
			hMGroupUser.put("groupID", group.getId());
			r = this.post("insertUserIntoGroup", hMGroupUser);
		}
		boolean validOP = (r.getStatus() == 201);
		Status status = new Status(validOP, "" + r.getResult());
		return status;
	}
	
	public Status insertUserIntoGroup(Group group, String username) throws ClientProtocolException, IOException, JSONException {
		HashMap<String, Object> hMGroupUser = new HashMap<String, Object>();
		hMGroupUser.put("groupID", group.getId());
		hMGroupUser.put("username", username);
		Response r = this.post("insertUserIntoGroup", hMGroupUser);
		boolean validOP = (r.getStatus() == 201);
		Status status = new Status(validOP, "" + r.getResult());
		return status;
	}
	

	public Status addContact(String contactUsername) throws ClientProtocolException, IOException, JSONException {
		HashMap<String, Object> hMGroupUser = new HashMap<String, Object>();
		hMGroupUser.put("username", contactUsername);
		hMGroupUser.put("nickname", contactUsername);
		Response r = this.post("addContact", hMGroupUser);
		boolean validOP = (r.getStatus() == 201);
		Status status = new Status(validOP, "" + r.getResult());
		return status;
	}

	public Status addContact(String contactUsername, String contactNickName) throws ClientProtocolException, IOException, JSONException {
		HashMap<String, Object> hMGroupUser = new HashMap<String, Object>();
		hMGroupUser.put("username", contactUsername);
		hMGroupUser.put("nickname", contactNickName);
		Response r = this.post("addContact", hMGroupUser);
		boolean validOP = (r.getStatus() == 201);
		Status status = new Status(validOP, "" + r.getResult());
		return status;
	}
	
}
{'fraction_non_alphanumeric': 0.13608768971332208, 'fraction_numerical': 0.004384485666104553, 'mean_word_length': 5.0893223819301845, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 26, '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': '2134040', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1766, 'n_words': 442}