From 616caa1e10c391e6e81d826d020ad90bd7d60fa5 Mon Sep 17 00:00:00 2001 From: NMerz Date: Sun, 15 Nov 2020 17:55:25 -0500 Subject: [PATCH] List renaming Added list reanming through a ListPUT lambda. Potentially could be used to alter other List aspects in the future. Also renamed itemID to listID for semantic unity --- Lambdas/Lists/List/src/List.java | 14 +++--- Lambdas/Lists/List/src/ListPUT.java | 11 +++++ Lambdas/Lists/List/src/ListPutter.java | 44 +++++++++++++++++++ .../java/com/example/listify/ItemDetails.java | 8 +--- .../com/example/listify/MainActivity.java | 1 + .../ShoppingListsSwipeableAdapter.java | 10 ++--- .../java/com/example/listify/data/List.java | 20 ++++----- .../example/listify/ui/home/HomeFragment.java | 2 +- 8 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 Lambdas/Lists/List/src/ListPUT.java create mode 100644 Lambdas/Lists/List/src/ListPutter.java diff --git a/Lambdas/Lists/List/src/List.java b/Lambdas/Lists/List/src/List.java index 02d9723..fe6420a 100644 --- a/Lambdas/Lists/List/src/List.java +++ b/Lambdas/Lists/List/src/List.java @@ -3,7 +3,7 @@ import java.sql.SQLException; import java.util.ArrayList; public class List { - Integer itemID; + Integer listID; String name; String owner; long lastUpdated; @@ -11,7 +11,7 @@ public class List { boolean shared; public List(ResultSet listRow, boolean shared) throws SQLException { - itemID = listRow.getInt("listID"); + listID = listRow.getInt("listID"); name = listRow.getString("name"); owner = listRow.getString("owner"); lastUpdated = listRow.getTimestamp("lastUpdated").toInstant().toEpochMilli(); @@ -26,7 +26,7 @@ public class List { @Override public String toString() { return "List{" + - "itemID=" + itemID + + "listID=" + listID + ", name='" + name + '\'' + ", owner='" + owner + '\'' + ", lastUpdated=" + lastUpdated + @@ -38,12 +38,12 @@ public class List { return entries.toArray(new ItemEntry[entries.size()]); } - public Integer getItemID() { - return itemID; + public Integer getListID() { + return listID; } - public void setItemID(Integer itemID) { - this.itemID = itemID; + public void setListID(Integer listID) { + this.listID = listID; } public String getName() { diff --git a/Lambdas/Lists/List/src/ListPUT.java b/Lambdas/Lists/List/src/ListPUT.java new file mode 100644 index 0000000..a1ecb06 --- /dev/null +++ b/Lambdas/Lists/List/src/ListPUT.java @@ -0,0 +1,11 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class ListPUT implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ListPutter.class); + } +} diff --git a/Lambdas/Lists/List/src/ListPutter.java b/Lambdas/Lists/List/src/ListPutter.java new file mode 100644 index 0000000..4909a7c --- /dev/null +++ b/Lambdas/Lists/List/src/ListPutter.java @@ -0,0 +1,44 @@ +import java.security.AccessControlException; +import java.sql.*; +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; + +public class ListPutter implements CallHandler { + private final Connection connection; + private final String cognitoID; + + private final String ACCESS_CHECK = "SELECT * from ListSharee WHERE userID = ? and listID = ?;"; + private final String LIST_RENAME = "UPDATE List SET name = ?, lastUpdated = ? WHERE listID = ?;"; + + public ListPutter(Connection connection, String cognitoID) { + this.connection = connection; + this.cognitoID = cognitoID; + } + + @Override + public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { + Integer listID = Integer.parseInt(bodyMap.get("listID").toString()); + + PreparedStatement accessCheck = connection.prepareStatement(ACCESS_CHECK); + accessCheck.setString(1, cognitoID); + accessCheck.setInt(2, listID); + System.out.println(accessCheck); + ResultSet userLists = accessCheck.executeQuery(); + if (!userLists.next()) { + throw new AccessControlException("User does not have access to list"); + } else { + if (!ListPermissions.hasPermission(userLists.getInt("permissionLevel"), "Delete")) { + throw new AccessControlException("User " + cognitoID + " does not have permission to edit list " + listID); + } + } + PreparedStatement renameList = connection.prepareStatement(LIST_RENAME); + renameList.setString(1, bodyMap.get("name").toString()); + renameList.setTimestamp(2, Timestamp.from(Instant.now())); + renameList.setInt(3, listID); + System.out.println(renameList); + renameList.executeUpdate(); + connection.commit(); + return null; + } +} diff --git a/Listify/app/src/main/java/com/example/listify/ItemDetails.java b/Listify/app/src/main/java/com/example/listify/ItemDetails.java index 46dd926..8295ec3 100644 --- a/Listify/app/src/main/java/com/example/listify/ItemDetails.java +++ b/Listify/app/src/main/java/com/example/listify/ItemDetails.java @@ -1,11 +1,7 @@ package com.example.listify; -import android.app.Dialog; -import android.graphics.Color; -import android.graphics.drawable.ColorDrawable; import android.os.Bundle; -import com.amplifyframework.auth.AuthException; import com.bumptech.glide.Glide; import com.example.listify.data.List; import com.example.listify.data.ListEntry; @@ -14,8 +10,6 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.view.View; -import android.view.Window; -import android.view.WindowManager; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; @@ -215,7 +209,7 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr try { - ListEntry entry = new ListEntry(shoppingLists.get(selectedListIndex).getItemID(), curProduct.getItemId(), quantity, Instant.now().toEpochMilli(),false); + ListEntry entry = new ListEntry(shoppingLists.get(selectedListIndex).getListID(), curProduct.getItemId(), quantity, Instant.now().toEpochMilli(),false); requestor.postObject(entry); Toast.makeText(this, String.format("%d of Item added to %s", quantity, shoppingLists.get(selectedListIndex).getName()), Toast.LENGTH_LONG).show(); } catch (Exception e) { diff --git a/Listify/app/src/main/java/com/example/listify/MainActivity.java b/Listify/app/src/main/java/com/example/listify/MainActivity.java index ec0c89b..bd76973 100644 --- a/Listify/app/src/main/java/com/example/listify/MainActivity.java +++ b/Listify/app/src/main/java/com/example/listify/MainActivity.java @@ -107,6 +107,7 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF SynchronousReceiver historyReceiver = new SynchronousReceiver<>(); requestor.getObject("N/A", SearchHistory.class, historyReceiver, historyReceiver); try { + requestor.putObject(new List(293, "Java.py", "me!", 1)); System.out.println(historyReceiver.await()); } catch (Exception e) { e.printStackTrace(); diff --git a/Listify/app/src/main/java/com/example/listify/adapter/ShoppingListsSwipeableAdapter.java b/Listify/app/src/main/java/com/example/listify/adapter/ShoppingListsSwipeableAdapter.java index 8fa0d6a..e523216 100644 --- a/Listify/app/src/main/java/com/example/listify/adapter/ShoppingListsSwipeableAdapter.java +++ b/Listify/app/src/main/java/com/example/listify/adapter/ShoppingListsSwipeableAdapter.java @@ -90,7 +90,7 @@ public class ShoppingListsSwipeableAdapter extends BaseAdapter { final List curList = lists.get(position); // Bind the view to the unique list ID - binderHelper.bind(holder.swipeLayout, Integer.toString(curList.getItemID())); + binderHelper.bind(holder.swipeLayout, Integer.toString(curList.getListID())); if(curList.isShared()) { holder.textView.setText(curList.getName() + " (shared)"); @@ -102,7 +102,7 @@ public class ShoppingListsSwipeableAdapter extends BaseAdapter { @Override public void onClick(View v) { try { - requestor.deleteObject(Integer.toString(curList.getItemID()), List.class); + requestor.deleteObject(Integer.toString(curList.getListID()), List.class); } catch(Exception e) { e.printStackTrace(); @@ -129,7 +129,7 @@ public class ShoppingListsSwipeableAdapter extends BaseAdapter { public void onClick(DialogInterface dialog, int which) { EditText sharedEmailText = (EditText) codeView.findViewById(R.id.editTextTextSharedEmail); String sharedEmail = sharedEmailText.getText().toString(); - ListShare listShare = new ListShare(curList.getItemID(), sharedEmail, "Read, Write, Delete, Share"); + ListShare listShare = new ListShare(curList.getListID(), sharedEmail, "Read, Write, Delete, Share"); try { requestor.putObject(listShare); } @@ -148,7 +148,7 @@ public class ShoppingListsSwipeableAdapter extends BaseAdapter { Toast.makeText(activity, String.format("Share %s", curList.getName()), Toast.LENGTH_SHORT).show(); // Close the layout - binderHelper.closeLayout(Integer.toString(curList.getItemID())); + binderHelper.closeLayout(Integer.toString(curList.getListID())); } }); @@ -158,7 +158,7 @@ public class ShoppingListsSwipeableAdapter extends BaseAdapter { Intent listPage = new Intent(activity, ListPage.class); // Send the list ID - listPage.putExtra("listID", curList.getItemID()); + listPage.putExtra("listID", curList.getListID()); activity.startActivity(listPage); } }); diff --git a/Listify/app/src/main/java/com/example/listify/data/List.java b/Listify/app/src/main/java/com/example/listify/data/List.java index e04211f..fbefab9 100644 --- a/Listify/app/src/main/java/com/example/listify/data/List.java +++ b/Listify/app/src/main/java/com/example/listify/data/List.java @@ -3,15 +3,15 @@ package com.example.listify.data; import java.util.Arrays; public class List { - Integer itemID; + Integer listID; String name; String owner; long lastUpdated; final ListEntry[] entries; boolean shared; - public List(Integer itemID, String name, String owner, long lastUpdated, ListEntry[] entries, boolean shared) { - this.itemID = itemID; + public List(Integer listID, String name, String owner, long lastUpdated, ListEntry[] entries, boolean shared) { + this.listID = listID; this.name = name; this.owner = owner; this.lastUpdated = lastUpdated; @@ -19,14 +19,14 @@ public class List { this.shared = false; } - public List(Integer itemID, String name, String owner, long lastUpdated) { - this(itemID, name, owner, lastUpdated, null, false); + public List(Integer listID, String name, String owner, long lastUpdated) { + this(listID, name, owner, lastUpdated, null, false); } @Override public String toString() { return "List{" + - "itemID=" + itemID + + "listID=" + listID + ", name='" + name + '\'' + ", owner='" + owner + '\'' + ", lastUpdated=" + lastUpdated + @@ -35,12 +35,12 @@ public class List { '}'; } - public Integer getItemID() { - return itemID; + public Integer getListID() { + return listID; } - public void setItemID(Integer itemID) { - this.itemID = itemID; + public void setListID(Integer listID) { + this.listID = listID; } public String getName() { diff --git a/Listify/app/src/main/java/com/example/listify/ui/home/HomeFragment.java b/Listify/app/src/main/java/com/example/listify/ui/home/HomeFragment.java index bd44ea3..c2d5f0f 100644 --- a/Listify/app/src/main/java/com/example/listify/ui/home/HomeFragment.java +++ b/Listify/app/src/main/java/com/example/listify/ui/home/HomeFragment.java @@ -101,7 +101,7 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O @Override public void run() { try { - newList.setItemID(idReceiver.await()); + newList.setListID(idReceiver.await()); } catch (Exception e) { getActivity().runOnUiThread(new Runnable() { @Override