Merge pull request #144 from ClaytonWWilson/lambda-robustness

Update list sharing
This commit is contained in:
Nathan Merz 2020-11-30 12:22:19 -05:00 committed by GitHub
commit 98596389ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 99 additions and 57 deletions

View File

@ -12,7 +12,7 @@ public class ListGetter implements CallHandler{
private final String cognitoID;
private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;";
private final String GET_LISTS = "SELECT listID FROM ListSharee WHERE userID = ? ORDER BY uiPosition;";
private final String GET_LISTS = "SELECT listID, permissionLevel FROM ListSharee WHERE userID = ? ORDER BY uiPosition;";
private final String SHARE_CHECK = "SELECT * FROM ListSharee WHERE listID = ?;";
private final String GET_ENTRIES = "SELECT * FROM ListProduct WHERE listID = ?;";
@ -32,7 +32,10 @@ public class ListGetter implements CallHandler{
System.out.println(getListsResults);
ArrayList<Integer> listIds = new ArrayList<>();
while (getListsResults.next()) {
listIds.add(getListsResults.getInt(1));
Integer permissionLevel = getListsResults.getInt("permissionLevel");
if (ListPermissions.hasPermission(permissionLevel, "Read")) {
listIds.add(getListsResults.getInt("listID"));
}
}
return listIds;
}

View File

@ -24,7 +24,7 @@ public class ListPermissions {
}
public static boolean hasPermission(Integer level, String permission) {
return level % getKeyForPermission(permission) == 0;
return (level % getKeyForPermission(permission) == 0 && level != 0);
}
public static Integer getKeyForPermission(String permissionRaw) {

View File

@ -9,9 +9,9 @@ public class ListShare {
Integer uiPosition;
ArrayList<ListShare> other;
public ListShare(ResultSet listRow) throws SQLException {
public ListShare(ResultSet listRow, String shareWithEmail) throws SQLException {
this.listID = listRow.getInt("listID");
this.shareWithEmail = listRow.getString("userID");
this.shareWithEmail = shareWithEmail;
this.permissionLevel = listRow.getInt("permissionLevel");
this.uiPosition = listRow.getInt("uiPosition");
other = new ArrayList<>();

View File

@ -1,8 +1,14 @@
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;
import com.google.gson.Gson;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
public class ListShareGetter implements CallHandler{
@ -24,14 +30,63 @@ public class ListShareGetter implements CallHandler{
getList.setInt(1, listID);
ResultSet getListResults = getList.executeQuery();
getListResults.first();
System.out.println(getListResults);
ListShare first = null;
while (getListResults.next() && first == null) {
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName("UserGET");
invokeRequest.setPayload("{" +
" \"body\": {" +
" }," +
" \"params\": {" +
" \"querystring\": {" +
" \"id\": \"" + getListResults.getString("userID") + "\"" +
" }" +
" }," +
" \"context\": {" +
" \"sub\": \"not used\"" +
" }" +
"}");
InvokeResult invokeResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
if (invokeResult.getStatusCode() != 200) {
throw new InputMismatchException("Could not find specified user to share with");
}
String shareWithEmail = new Gson().fromJson(new String(invokeResult.getPayload().array()), User.class).email;
first = new ListShare(getListResults, shareWithEmail);
if (first.permissionLevel == 0 || first.permissionLevel == 1) {
first = null;
}
}
//ListShare object to hold the data values of the first row retrived
ListShare first = new ListShare(getListResults);
//Insert the ListShare objects to hold the data of the remaining rows into first's ListShare list
while (getListResults.next()) {
first.addtoList(new ListShare(getListResults));
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName("UserGET");
invokeRequest.setPayload("{" +
" \"body\": {" +
" }," +
" \"params\": {" +
" \"querystring\": {" +
" \"id\": \"" + getListResults.getString("userID") + "\"" +
" }" +
" }," +
" \"context\": {" +
" \"sub\": \"not used\"" +
" }" +
"}");
InvokeResult invokeResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
if (invokeResult.getStatusCode() != 200) {
throw new InputMismatchException("Could not find specified user to share with");
}
String shareWithEmail = new Gson().fromJson(new String(invokeResult.getPayload().array()), User.class).email;
ListShare newShare = new ListShare(getListResults, shareWithEmail);
System.out.println(newShare);
if (newShare.permissionLevel != 0 && newShare.permissionLevel != 1) {
first.addtoList(newShare);
}
}
return first;

View File

@ -51,7 +51,7 @@ public class UserGetter implements CallHandler {
if (foundUsers.size() != 1) {
System.out.println(foundUsers);
if (foundUsers.size() == 0) {
throw new InputMismatchException("Not user with given attribute (" + attributeToGet + ")");
throw new InputMismatchException("No user with given attribute when searching for (" + attributeToGet + ")");
}
throw new InputMismatchException("Found more than one user with supposedly unique attribute (" + attributeToGet + ")");
}

View File

@ -1,42 +1,20 @@
package com.example.listify;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.listify.adapter.ShareeSwipeableAdapter;
import com.example.listify.adapter.ShoppingListsSwipeableAdapter;
import com.example.listify.data.Chain;
import com.example.listify.data.Item;
import com.example.listify.data.List;
import com.example.listify.data.ListEntry;
import com.example.listify.data.ListShare;
import org.json.JSONException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.listify.data.ListShare;
import org.json.JSONException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import static com.example.listify.MainActivity.am;
@ -48,6 +26,7 @@ public class ListSharees extends AppCompatActivity implements Requestor.Receiver
Button shareList;
Button removeSharee;
ArrayList<ListShare> lShareeEntries = new ArrayList<>();
ArrayList<String> lShareeEmails = new ArrayList<>();
@Override
@ -97,6 +76,7 @@ public class ListSharees extends AppCompatActivity implements Requestor.Receiver
if(sharee.getEntries() != null) {
for(ListShare ls : sharee.getEntries()) {
lShareeEntries.add(ls);
lShareeEmails.add(ls.getShareWithEmail());
}
}
@ -135,8 +115,16 @@ public class ListSharees extends AppCompatActivity implements Requestor.Receiver
removeSharee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lShareeEmails.remove(position);
myAdapter.notifyDataSetChanged();
try {
lShareeEntries.get(position).setPermissionLevel(0);
ListShare toRemove = lShareeEntries.remove(position);
System.out.println(toRemove);
requestor.putObject(toRemove);
myAdapter.notifyDataSetChanged();
}
catch(Exception e) {
e.printStackTrace();
}
}
});

View File

@ -61,14 +61,16 @@ public class ListShare {
" [Permissions: ");
int permissionLevelCopy = permissionLevel;
for (Integer permissionObject : keysToPerms.keySet()) {
Integer permissionInteger = permissionObject;
if (permissionLevelCopy % permissionInteger == 0) {
permissionLevelCopy /= permissionInteger;
toReturn.append(keysToPerms.get(permissionInteger)).append(",");
if (permissionLevel > 0) {
for (Integer permissionObject : keysToPerms.keySet()) {
Integer permissionInteger = permissionObject;
if (permissionLevelCopy % permissionInteger == 0) {
permissionLevelCopy /= permissionInteger;
toReturn.append(keysToPerms.get(permissionInteger)).append(",");
}
}
}
if (BuildConfig.DEBUG && permissionLevelCopy != 1) {
if (BuildConfig.DEBUG && (permissionLevelCopy != 1 && permissionLevelCopy != 0)) {
throw new AssertionError("Assertion failed");
}
toReturn.append("]}");

View File

@ -6,24 +6,15 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.example.listify.AuthManager;
import com.example.listify.CreateListDialogFragment;
import com.example.listify.LoadingCircleDialog;
import com.example.listify.R;
import com.example.listify.Requestor;
import com.example.listify.SynchronousReceiver;
import com.example.listify.*;
import com.example.listify.adapter.ShoppingListsSwipeableAdapter;
import com.example.listify.data.List;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.json.JSONException;
import java.io.IOException;
@ -157,6 +148,9 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
Integer[] listIds = (Integer[]) delivered;
// Create threads and add them to a list
if (listIds == null) {
return;
}
Thread[] threads = new Thread[listIds.length];
List[] results = new List[listIds.length];
for (int i = 0; i < listIds.length; i++) {