Commit 9794eeb3 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Card management

Prepare to add contributions
Prepare to add accounts
parent b38dc7ba
......@@ -39,7 +39,7 @@
<activity android:name=".BuyerInfoActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".ReadCardInfoActivity"
<activity android:name=".CardManagementActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
</application>
......
package fr.utc.simde.payutc;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import fr.utc.simde.payutc.tools.CASConnexion;
import fr.utc.simde.payutc.tools.Config;
......@@ -395,11 +387,11 @@ public abstract class BaseActivity extends NFCActivity {
}.start();
}
protected void startReadCardInfoActivity(final Activity activity) {
protected void startCardManagementActivity(final Activity activity) {
hasRights(getString(R.string.user_rights_list_collecting), new String[]{"STAFF", "POSS3", "GESUSERS"}, new Runnable() {
@Override
public void run() {dialog.startLoading(activity, activity.getResources().getString(R.string.information_collection), activity.getResources().getString(R.string.buyer_info_collecting));
activity.startActivity(new Intent(activity, ReadCardInfoActivity.class));
activity.startActivity(new Intent(activity, CardManagementActivity.class));
}
});
}
......
package fr.utc.simde.payutc;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import fr.utc.simde.payutc.adapters.ListAdapater;
import fr.utc.simde.payutc.tools.HTTPRequest;
/**
* Created by Samy on 29/10/2017.
*/
public class ReadCardInfoActivity extends BaseActivity {
private static final String LOG_TAG = "_ReadCardInfoActivity";
public class CardManagementActivity extends BaseActivity {
private static final String LOG_TAG = "CardManagementActivity";
TextView textUserId;
TextView textUsername;
......@@ -39,6 +27,7 @@ public class ReadCardInfoActivity extends BaseActivity {
TextView textTagId;
TextView textTag;
TextView textShortTag;
TextView textSolde;
TextView textUsernameGinger;
TextView textFirstnameGinger;
......@@ -47,14 +36,17 @@ public class ReadCardInfoActivity extends BaseActivity {
TextView textEmailGinger;
TextView textAdultGinger;
TextView textCotisantGinger;
TextView textTagGinger;
String username;
Boolean canRead;
String badgeId;
Runnable toRun;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_card_info);
setContentView(R.layout.activity_card_management);
this.textUserId = findViewById(R.id.text_user_id);
this.textUsername = findViewById(R.id.text_username);
......@@ -66,6 +58,7 @@ public class ReadCardInfoActivity extends BaseActivity {
this.textTagId = findViewById(R.id.text_tag_id);
this.textTag = findViewById(R.id.text_tag);
this.textShortTag = findViewById(R.id.text_short_tag);
this.textSolde = findViewById(R.id.text_solde);
this.textUsernameGinger = findViewById(R.id.text_username_ginger);
this.textFirstnameGinger = findViewById(R.id.text_firstname_ginger);
......@@ -74,167 +67,53 @@ public class ReadCardInfoActivity extends BaseActivity {
this.textTypeGinger = findViewById(R.id.text_type_ginger);
this.textAdultGinger = findViewById(R.id.text_adult_ginger);
this.textCotisantGinger = findViewById(R.id.text_cotisant_ginger);
this.textTagGinger = findViewById(R.id.text_tag_ginger);
findViewById(R.id.read_new_card).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readNewCard();
}
});
readNewCard();
}
@Override
protected void onIdentification(final String badgeId) {
if (!canRead)
return;
this.canRead = false;
dialog.startLoading(ReadCardInfoActivity.this, getResources().getString(R.string.information_collection), getResources().getString(R.string.buyer_info_collecting));
new Thread() {
@Override
public void run() {
try {
nemopaySession.getBuyerInfo(badgeId);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode buyerInfo = nemopaySession.getRequest().getJSONResponse();
if (!buyerInfo.has("lastname") || !buyerInfo.has("username") || !buyerInfo.has("firstname") || !buyerInfo.has("solde") || !buyerInfo.has("last_purchases") || !buyerInfo.get("last_purchases").isArray())
throw new Exception("Unexpected JSON");
username = nemopaySession.getRequest().getJSONResponse().get("username").textValue();
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(ReadCardInfoActivity.this, getString(R.string.information_collection), getString(R.string.badge_error_not_recognized));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(ReadCardInfoActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
dialog.infoDialog(CardManagementActivity.this, getString(R.string.badge_read), getString(R.string.badge_waiting), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
toRun = null;
}
});
return;
}
runOnUiThread(new Runnable() {
toRun = new Thread(){
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_searching));
readCard();
}
});
};
}
});
try {
nemopaySession.foundUser(username);
Thread.sleep(100);
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(ReadCardInfoActivity.this, getString(R.string.information_collection), getString(R.string.user_not_recognized));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(ReadCardInfoActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
findViewById(R.id.contribute).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.infoDialog(CardManagementActivity.this, getString(R.string.contribute), getString(R.string.badge_waiting), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
toRun = null;
}
});
return;
}
runOnUiThread(new Runnable() {
toRun = new Thread(){
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_info_collecting));
}
});
try {
final JsonNode usersFounded = nemopaySession.getRequest().getJSONResponse();
if (!usersFounded.has(0) || !usersFounded.get(0).has("id"))
throw new Exception(getString(R.string.user_not_recognized));
nemopaySession.getUser(usersFounded.get(0).get("id").intValue());
Thread.sleep(100);
ginger.getInfo(username);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode userInfo = nemopaySession.getRequest().getJSONResponse();
final JsonNode gingerInfo = new ObjectMapper().readTree(ginger.getRequest().getResponse()); // La réponse n'est pas considérée comme une réponse json.. (l'en-tête ne renvoie pas une application/json)
if (!userInfo.has("user") || !userInfo.has("tag"))
throw new Exception("Unexpected Nemopay JSON");
if (!gingerInfo.has("login") || !gingerInfo.has("nom") || !gingerInfo.has("prenom") || !gingerInfo.has("mail") || !gingerInfo.has("type") || !gingerInfo.has("is_adulte") || !gingerInfo.has("is_cotisant"))
throw new Exception("Unexpected Ginger JSON");
runOnUiThread(new Runnable() {
@Override
public void run() {
setUserInfo(userInfo.get("user"), userInfo.get("tag"), gingerInfo);
dialog.stopLoading();
}
});
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(ReadCardInfoActivity.this, getString(R.string.information_collection), getString(R.string.user_error_collecting));
}
});
else if (ginger.getRequest().getResponseCode() == 404)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(ReadCardInfoActivity.this, getString(R.string.information_collection), getString(R.string.user_not_recognized));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(ReadCardInfoActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
contributeCard();
readCard();
}
return;
}
};
}
}.start();
});
}
@Override
protected void onIdentification(final String badgeId) {
if (this.toRun != null) {
this.badgeId = badgeId;
this.toRun.run();
this.toRun = null;
}
}
void setUserInfo(JsonNode userInfo, JsonNode tagInfo, JsonNode gingerInfo) {
......@@ -262,6 +141,7 @@ public class ReadCardInfoActivity extends BaseActivity {
this.textAdultGinger.setTextColor(gingerInfo.get("is_adulte").booleanValue() ? Color.BLUE : Color.RED);
this.textCotisantGinger.setText(gingerInfo.get("is_cotisant").booleanValue() ? getString(R.string.yes) : getString(R.string.no));
this.textCotisantGinger.setTextColor(gingerInfo.get("is_cotisant").booleanValue() ? Color.BLUE : Color.RED);
this.textTagGinger.setText(gingerInfo.get("badge_uid").textValue());
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(this, getResources().getString(R.string.information_collection), getResources().getString(R.string.error_view), new DialogInterface.OnClickListener() {
......@@ -273,13 +153,253 @@ public class ReadCardInfoActivity extends BaseActivity {
}
}
protected void readNewCard() {
dialog.infoDialog(ReadCardInfoActivity.this, getString(R.string.information_collection), getString(R.string.badge_waiting), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
canRead = false;
protected void contributeCard() {
try {
nemopaySession.getBuyerInfo(badgeId);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode buyerInfo = nemopaySession.getRequest().getJSONResponse();
if (!buyerInfo.has("lastname") || !buyerInfo.has("username") || !buyerInfo.has("firstname") || !buyerInfo.has("solde") || !buyerInfo.has("last_purchases") || !buyerInfo.get("last_purchases").isArray())
throw new Exception("Unexpected JSON");
username = nemopaySession.getRequest().getJSONResponse().get("username").textValue();
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.badge_error_not_recognized));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(CardManagementActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
}
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_ginger_info_collecting));
}
});
this.canRead = true;
try {
ginger.addCotisation(username, "20");
Thread.sleep(100);
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (ginger.getRequest().getResponseCode() == 404)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.user_not_recognized));
}
});
if (ginger.getRequest().getResponseCode() == 409)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.cotisant), getString(R.string.contribute_already));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(CardManagementActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
}
return;
}
}
protected void readCard() {
try {
nemopaySession.getBuyerInfo(badgeId);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode buyerInfo = nemopaySession.getRequest().getJSONResponse();
if (!buyerInfo.has("lastname") || !buyerInfo.has("username") || !buyerInfo.has("firstname") || !buyerInfo.has("solde") || !buyerInfo.has("last_purchases") || !buyerInfo.get("last_purchases").isArray())
throw new Exception("Unexpected JSON");
username = nemopaySession.getRequest().getJSONResponse().get("username").textValue();
textSolde.setText(String.format("%.2f", new Float(buyerInfo.get("solde").intValue()) / 100.00f) + "€");
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.badge_error_not_recognized));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(CardManagementActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
}
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_searching));
}
});
try {
nemopaySession.foundUser(username);
Thread.sleep(100);
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.user_not_recognized));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(CardManagementActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
}
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_nemopay_info_collecting));
}
});
try {
final JsonNode usersFounded = nemopaySession.getRequest().getJSONResponse();
if (!usersFounded.has(0) || !usersFounded.get(0).has("id"))
throw new Exception(getString(R.string.user_not_recognized));
nemopaySession.getUser(usersFounded.get(0).get("id").intValue());
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode userInfo = nemopaySession.getRequest().getJSONResponse();
if (!userInfo.has("user") || !userInfo.has("tag"))
throw new Exception("Unexpected JSON");
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.user_error_collecting));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(CardManagementActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
}
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_ginger_info_collecting));
}
});
try {
ginger.getInfoFromBadge(badgeId);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode userInfo = nemopaySession.getRequest().getJSONResponse();
final JsonNode gingerInfo = new ObjectMapper().readTree(ginger.getRequest().getResponse()); // La réponse n'est pas considérée comme une réponse json.. (l'en-tête ne renvoie pas une application/json)
if (!gingerInfo.has("login") || !gingerInfo.has("nom") || !gingerInfo.has("prenom") || !gingerInfo.has("mail") || !gingerInfo.has("type") || !gingerInfo.has("is_adulte") || !gingerInfo.has("is_cotisant"))
throw new Exception("Unexpected JSON");
runOnUiThread(new Runnable() {
@Override
public void run() {
setUserInfo(userInfo.get("user"), userInfo.get("tag"), gingerInfo);
dialog.stopLoading();
}
});
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (ginger.getRequest().getResponseCode() == 404)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.user_not_recognized));
}
});
else
throw</