Skip to content

Commit

Permalink
Merge branch 'US02-CadastrarAlunoNaSala' into developer
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielsclimaco committed May 13, 2017
2 parents 27dd622 + d7ca240 commit 24cc928
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 63 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ android {
includeNoLocationClasses = true
}
}

unitTests.returnDefaultValues = true
}
lintOptions {
abortOnError false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@


import android.content.Context;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutionException;

import fga.mds.gpp.trezentos.DAO.getAllClassRequest;
import fga.mds.gpp.trezentos.DAO.AddStudentToClassRequest;
import fga.mds.gpp.trezentos.DAO.getClassRequest;
import fga.mds.gpp.trezentos.DAO.CreateClassPost;
import fga.mds.gpp.trezentos.Exception.UserClassException;
import fga.mds.gpp.trezentos.Exception.UserException;
import fga.mds.gpp.trezentos.Model.UserClass;
import fga.mds.gpp.trezentos.R;

public class UserClassControl {

private static UserClassControl instance;
final Context context;

public UserClassControl(final Context context){

private UserClassControl(final Context context) {
this.context = context;
}

public static UserClassControl getInstance(final Context context) {

public static UserClassControl getInstance(final Context context){

if(instance == null){
if (instance == null) {
instance = new UserClassControl(context);
}

Expand All @@ -37,15 +41,15 @@ public static UserClassControl getInstance(final Context context){

public void validateCreateClass(String className, String institution,
Float cutOff, String password, Float addition,
Integer sizeGroups, String email) throws UserException{
Integer sizeGroups, String email) throws UserException {

try{
try {
UserClass userClass = new UserClass(className, institution, cutOff, password, addition, sizeGroups);

CreateClassPost createClassPost = new CreateClassPost(userClass, email);
createClassPost.execute();

}catch (UserException userException){
} catch (UserException userException) {
userException.printStackTrace();
}

Expand All @@ -55,22 +59,22 @@ public void validateCreateClass(String className, String institution,

public String validateInformation(String className, String institution,
String cutOff, String password,
String addition, String sizeGroups) throws UserException{
String addition, String sizeGroups) throws UserException {

String erro;
try{
try {
UserClass userClass = new UserClass(className, institution,
Float.parseFloat(cutOff), password, Float.parseFloat(addition),
Integer.parseInt(sizeGroups));
//Just to pass on Sonar
System.out.println(userClass.getClassName());
erro = "Sucesso";
return erro;
}catch (UserException userException){
erro = userException.getMessage();
return erro;
} catch (UserException userException) {
erro = userException.getMessage();
return erro;
}
}
}


public ArrayList<UserClass> getClasses() {
Expand Down Expand Up @@ -109,45 +113,45 @@ public ArrayList<UserClass> getClassesFromUser(String email) {
try {
serverResponse = classRequest.execute().get();

}catch (InterruptedException e){
} catch (InterruptedException e) {
e.printStackTrace();
}catch (ExecutionException e){
} catch (ExecutionException e) {
e.printStackTrace();
}

ArrayList<UserClass> userClasses = new ArrayList<UserClass>();

try{
try {
userClasses = getArrayList(serverResponse);
}catch (JSONException e){
} catch (JSONException e) {
e.printStackTrace();
}

return userClasses;

}

private ArrayList<UserClass> getArrayList(String serverResponse) throws JSONException{
private ArrayList<UserClass> getArrayList(String serverResponse) throws JSONException {

JSONArray array = null;

try{
try {
array = new JSONArray(serverResponse);
}catch (JSONException e){
} catch (JSONException e) {
e.printStackTrace();
}

ArrayList<UserClass> userClasses = new ArrayList<>();

for(int i = 0; i < array.length(); i++){
for (int i = 0; i < array.length(); i++) {
UserClass userClass = getUserClassFromJson(array.getJSONObject(i));
userClasses.add(userClass);
}

return userClasses;
}

private UserClass getUserClassFromJson(JSONObject jsonObject){
private UserClass getUserClassFromJson(JSONObject jsonObject) {
UserClass userClass = new UserClass();

try {
Expand All @@ -158,15 +162,31 @@ private UserClass getUserClassFromJson(JSONObject jsonObject){
userClass.setPassword(jsonObject.getString("password"));
userClass.setSizeGroups(Integer.parseInt(jsonObject.getString("numberOfStudentsPerGroup")));
userClass.setOwnerEmail(jsonObject.getString("ownerEmail"));
userClass.setStudents(jsonObject.getJSONArray("students"));
userClass.setStudents(new ArrayList<>((Collection) jsonObject.getJSONArray("students")));

}catch (JSONException e){
} catch (JSONException e) {
e.printStackTrace();
}catch (UserException e){
} catch (UserException e) {
e.printStackTrace();
}

return userClass;
}

public String validateJoinClass(UserClass userClass, String password, String studentEmail) throws UserClassException, ExecutionException, InterruptedException {
String serverResponse;
if (!password.isEmpty()){
if(password.equals(userClass.getPassword())){
AddStudentToClassRequest addStudentToClassRequest = new AddStudentToClassRequest(userClass, studentEmail);
serverResponse = addStudentToClassRequest.execute().get();
} else {
throw new UserClassException(context.getString(R.string.join_class_wrong_password_error));
}
} else {
throw new UserClassException(context.getString(R.string.join_class_null_password_error));
}

return serverResponse;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package fga.mds.gpp.trezentos.DAO;

import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;

import fga.mds.gpp.trezentos.Model.UserAccount;
import fga.mds.gpp.trezentos.Model.UserClass;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddStudentToClassRequest extends AsyncTask<String, String, String>{
private final String student;
private UserClass userClass;

private String url = "https://trezentos-api.herokuapp.com/api/class/user/student";

public AddStudentToClassRequest (UserClass userClass, String student){
this.userClass = userClass;
this.student = student;
}

@Override
protected String doInBackground(String... params){
OkHttpClient client = new OkHttpClient();

String urlWithParameters = getUrlWithParameters();

RequestBody body = RequestBody.create(null, "");
Request request = new Request.Builder()
.url(urlWithParameters)
.put(body)
.build();

try{
Response response = client.newCall(request).execute();
return response.body().string();
}catch (IOException e){
e.printStackTrace();
Log.i("LOG", "IOException in doInBackground method");
}
return null;
}

private String getUrlWithParameters(){
HttpUrl.Builder builder = HttpUrl.parse(url).newBuilder();

builder.addQueryParameter("email", userClass.getOwnerEmail());
builder.addQueryParameter("name", userClass.getClassName());
builder.addQueryParameter("student", student);

return builder.build().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fga.mds.gpp.trezentos.Exception;


public class UserClassException extends Exception{

public UserClassException(String message){
super(message);
}

}
19 changes: 4 additions & 15 deletions app/src/main/java/fga/mds/gpp/trezentos/Model/UserClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.StringTokenizer;

import fga.mds.gpp.trezentos.Exception.UserException;

Expand All @@ -17,8 +18,8 @@ public class UserClass implements Serializable {
private String password;
private float addition;
private int sizeGroups;
private String ownerEmail;
private ArrayList<String> students;
private String ownerEmail;

public UserClass(){
//An empty constructor is needed to create a new instance of object,
Expand Down Expand Up @@ -136,20 +137,8 @@ public ArrayList<String> getStudents(){
return students;
}

public void setStudents(JSONArray jArray){

ArrayList<String> tempList = new ArrayList<String>();

if (jArray != null) {
for(int i = 0;i < jArray.length(); i++){
try {
tempList.add(jArray.getString(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
this.students = tempList;
public void setStudents(ArrayList<String> students){
this.students = students;
}

public String getOwnerEmail(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import fga.mds.gpp.trezentos.Controller.UserClassControl;
Expand All @@ -24,7 +26,6 @@ public class ClassFragment extends Fragment{
public ArrayList<UserClass> userClasses;
private static CustomAdapter adapter;
private FloatingActionButton floatingActionButton;
private FragmentTransaction fragmentTransaction;
public ListView listView;

@Override
Expand Down Expand Up @@ -93,7 +94,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
UserClass userClassCalled = (UserClass) listView.getItemAtPosition(position);
goClass.putExtra("Class", userClassCalled);

startActivity(goClass);
startActivity(goClass);
}
});

Expand Down
Loading

0 comments on commit 24cc928

Please sign in to comment.