Create user if not exist, SQL fixes
This commit is contained in:
parent
87e0edf947
commit
1e5bf24157
@ -25,14 +25,23 @@ import com.google.firebase.auth.AuthResult;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.auth.FirebaseUser;
|
||||
import com.google.firebase.auth.GoogleAuthProvider;
|
||||
import com.google.firebase.iid.FirebaseInstanceId;
|
||||
import com.google.firebase.iid.InstanceIdResult;
|
||||
import com.google.firebase.messaging.FirebaseMessaging;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import at.smartshopper.smartshopper.R;
|
||||
import at.smartshopper.smartshopper.db.Database;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
private static final String TAG = "SMASH";
|
||||
private static final int RC_SIGN_IN = 1;
|
||||
private FirebaseAuth mAuth;
|
||||
private Database db;
|
||||
|
||||
SignInButton button;
|
||||
GoogleSignInClient mGoogleSignInClient;
|
||||
@ -65,6 +74,34 @@ public class LoginActivity extends AppCompatActivity {
|
||||
* Wechselt zu der Dash Activity
|
||||
*/
|
||||
private void goDash() {
|
||||
if (!db.checkIfUserExists(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
|
||||
FirebaseInstanceId.getInstance().getInstanceId()
|
||||
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<InstanceIdResult> task) {
|
||||
if (!task.isSuccessful()) {
|
||||
Log.w(TAG, "getInstanceId failed", task.getException());
|
||||
return;
|
||||
}
|
||||
|
||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
String uid = user.getUid();
|
||||
String name = user.getDisplayName();
|
||||
String email = user.getEmail();
|
||||
String picture = user.getPhotoUrl().toString();
|
||||
|
||||
// Get new Instance ID token
|
||||
String token = task.getResult().getToken();
|
||||
try {
|
||||
db.createUser(uid, token, name, email, picture);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Intent intent = new Intent(this, Dash.class);
|
||||
finish();
|
||||
startActivity(intent);
|
||||
@ -72,7 +109,8 @@ public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
/**
|
||||
* Loggt den User per Email ein
|
||||
* @param email Email des Users
|
||||
*
|
||||
* @param email Email des Users
|
||||
* @param password Passwort des Users
|
||||
*/
|
||||
private void signInEmail(String email, String password) {
|
||||
@ -100,7 +138,8 @@ public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
/**
|
||||
* Erstellt einen Account mit Email und Passwort
|
||||
* @param email Email des neuen Users
|
||||
*
|
||||
* @param email Email des neuen Users
|
||||
* @param password Passwort des neuen Users
|
||||
*/
|
||||
private void createAccount(String email, String password) {
|
||||
@ -129,11 +168,11 @@ public class LoginActivity extends AppCompatActivity {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_login);
|
||||
if (getIntent().getBooleanExtra("EXIT", false))
|
||||
{
|
||||
if (getIntent().getBooleanExtra("EXIT", false)) {
|
||||
finish();
|
||||
}
|
||||
mAuth = FirebaseAuth.getInstance();
|
||||
db = new Database();
|
||||
|
||||
Button loginEmailBtn = (Button) findViewById(R.id.loginEmailBtn);
|
||||
final TextView email = (TextView) findViewById(R.id.email);
|
||||
@ -141,7 +180,6 @@ public class LoginActivity extends AppCompatActivity {
|
||||
button = (SignInButton) findViewById(R.id.loginGoogleBtn);
|
||||
|
||||
|
||||
|
||||
loginEmailBtn.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
@ -171,6 +209,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
/**
|
||||
* Loggt den User ein. Bei erfolg wird der Aufruf zur Dash Activity getätigt
|
||||
*
|
||||
* @param acct Der Google Account, welcher eingelogt werden soll
|
||||
*/
|
||||
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
|
||||
@ -207,7 +246,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||
super.onStart();
|
||||
|
||||
|
||||
if(null != mAuth.getCurrentUser()){
|
||||
if (null != mAuth.getCurrentUser()) {
|
||||
goDash();
|
||||
}
|
||||
|
||||
@ -235,7 +274,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
doubleBackToExitPressedOnce=false;
|
||||
doubleBackToExitPressedOnce = false;
|
||||
}
|
||||
}, 2000);
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ public class Database {
|
||||
* @throws JSONException
|
||||
*/
|
||||
public Member getAdmin(String sl_id) throws SQLException, JSONException {
|
||||
String SQL = "SELECT row_to_json(\"User\") as obj FROM \"User\" JOIN \"Shoppinglist_member\" USING (username) WHERE sl_id = ?";
|
||||
String SQL = "SELECT row_to_json(\"User\") as obj FROM \"User\" JOIN \"Shoppinglist_admin\" USING (username) WHERE sl_id = ?";
|
||||
JSONObject jsonObject = new JSONObject(executeQuery(SQL, sl_id));
|
||||
return new Member(jsonObject.getString("username"), jsonObject.getString("message_id"));
|
||||
}
|
||||
@ -296,7 +296,7 @@ public class Database {
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void addItem(String group_id, String sl_id, String name, int count) throws SQLException {
|
||||
sqlUpdate5Param("INSERT INTO \"Item\" VALUES (?,?,?,?,?)", generateItemId(), group_id, sl_id, name, count);
|
||||
sqlUpdate5ParamLastInt("INSERT INTO \"Item\" VALUES (?,?,?,?,?)", generateItemId(), group_id, sl_id, name, count);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -393,9 +393,6 @@ public class Database {
|
||||
*/
|
||||
public void addShoppinglist(String name, String description, String username, String color) throws SQLException, JSONException {
|
||||
String sl_id = generateSL_Id(sl_idLength);
|
||||
if (!checkIfUserExists(username)) {
|
||||
createUser(username);
|
||||
}
|
||||
createShoppinglist(sl_id, name, description, color);
|
||||
createAdmin(sl_id, username);
|
||||
|
||||
@ -433,9 +430,9 @@ public class Database {
|
||||
* @param username Der Username des neuen Users
|
||||
* @throws SQLException
|
||||
*/
|
||||
private void createUser(String username) throws SQLException {
|
||||
String SQL = "INSERT INTO \"User\" (username) VALUES (?)";
|
||||
sqlUpdate(SQL, username);
|
||||
public void createUser(String username, String message_id, String name, String picture, String email) throws SQLException {
|
||||
String SQL = "INSERT INTO \"User\" (username, message_id, name, picture, email) VALUES (?, ?, ?, ?, ?)";
|
||||
sqlUpdate5Param(SQL, username, message_id, name, picture, email);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -445,14 +442,18 @@ public class Database {
|
||||
* @return True wenn User existiert, False wenn nicht
|
||||
* @throws SQLException
|
||||
*/
|
||||
private boolean checkIfUserExists(String username) throws SQLException, JSONException {
|
||||
public boolean checkIfUserExists(String username) {
|
||||
String SQL = "SELECT username FROM \"User\"";
|
||||
|
||||
ArrayList<String> outUserList = new ArrayList<String>();
|
||||
List<JSONObject> jsonObjects = executeQueryJSONObject(SQL, username);
|
||||
for (int i = 0; i < jsonObjects.size(); i++) {
|
||||
JSONObject jsonObject = jsonObjects.get(i);
|
||||
outUserList.add(jsonObject.getString("username"));
|
||||
ArrayList<String> outUserList = null;
|
||||
try {
|
||||
outUserList = executeQueryString(SQL);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (outUserList.contains(username)) {
|
||||
@ -709,6 +710,26 @@ public class Database {
|
||||
return jsonObjects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt ein SQL Befehl aus und gibt die antwort in ein JSONObject List
|
||||
*
|
||||
* @param SQL Der SQL der auszuführen ist
|
||||
* @return Das ergebnis als JSONObject
|
||||
* @throws SQLException
|
||||
* @throws JSONException
|
||||
*/
|
||||
|
||||
public ArrayList<String> executeQueryString(String SQL) throws SQLException, JSONException {
|
||||
ArrayList<String> stringArrayList = new ArrayList<>();
|
||||
PreparedStatement pstmt = conect.prepareStatement(SQL);
|
||||
ResultSet rsgroups = pstmt.executeQuery();
|
||||
while (rsgroups.next()) {
|
||||
String groupString = rsgroups.getString(1);
|
||||
stringArrayList.add(groupString);
|
||||
}
|
||||
return stringArrayList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt ein SQL Befehl aus und gibt die antwort in ein JSONObject List
|
||||
*
|
||||
@ -847,7 +868,7 @@ public class Database {
|
||||
* @param param5 ein 5. Parameter
|
||||
* @throws SQLException
|
||||
*/
|
||||
private void sqlUpdate5Param(String SQL, String param, String param2, String param3, String param4, int param5) throws SQLException {
|
||||
private void sqlUpdate5ParamLastInt(String SQL, String param, String param2, String param3, String param4, int param5) throws SQLException {
|
||||
//connectDatabase();
|
||||
PreparedStatement pstmt = conect.prepareStatement(SQL);
|
||||
pstmt.setString(1, param);
|
||||
@ -858,6 +879,28 @@ public class Database {
|
||||
pstmt.executeUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt einen SQL Befehl durch der keine rückgabe hat.
|
||||
*
|
||||
* @param SQL Der SQL befehl
|
||||
* @param param ein Parameter
|
||||
* @param param2 ein 2. Parameter
|
||||
* @param param3 ein 3. parameter
|
||||
* @param param4 ein 4. Parameter
|
||||
* @param param5 ein 5. Parameter
|
||||
* @throws SQLException
|
||||
*/
|
||||
private void sqlUpdate5Param(String SQL, String param, String param2, String param3, String param4, String param5) throws SQLException {
|
||||
//connectDatabase();
|
||||
PreparedStatement pstmt = conect.prepareStatement(SQL);
|
||||
pstmt.setString(1, param);
|
||||
pstmt.setString(2, param2);
|
||||
pstmt.setString(3, param3);
|
||||
pstmt.setString(4, param4);
|
||||
pstmt.setString(5, param5);
|
||||
pstmt.executeUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt einen SQL Befehl durch der keine rückgabe hat.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user