Browse Source

more commands!

Olyol95 9 years ago
parent
commit
82aaa1a3b8

+ 9 - 0
.idea/libraries/android.xml

@@ -0,0 +1,9 @@
+<component name="libraryTable">
+  <library name="android">
+    <CLASSES>
+      <root url="jar://$PROJECT_DIR$/android.jar!/" />
+    </CLASSES>
+    <JAVADOC />
+    <SOURCES />
+  </library>
+</component>

+ 1 - 0
IPServer.iml

@@ -8,5 +8,6 @@
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
     <orderEntry type="library" name="mysql-connector-java" level="project" />
+    <orderEntry type="library" name="android" level="project" />
   </component>
 </module>

+ 98 - 0
src/ip/e_biz/Card.java

@@ -0,0 +1,98 @@
+package ip.e_biz;
+
+import android.graphics.Bitmap;
+
+import java.io.Serializable;
+
+public class Card implements Serializable {
+
+    private int id;
+    private Bitmap frontImage;
+    private Bitmap backImage;
+    private String email;
+    private String telephone;
+    private String website;
+
+    public Card(Bitmap frontImage, Bitmap backImage, String email, String telephone, String website) {
+
+        this.frontImage = frontImage;
+        this.backImage = backImage;
+        this.email = email;
+        this.telephone = telephone;
+        this.website = website;
+
+    }
+
+    public void setBackImage(Bitmap backImage) {
+
+        this.backImage = backImage;
+
+    }
+
+    public void setEmail(String email) {
+
+        this.email = email;
+
+    }
+
+    public void setFrontImage(Bitmap frontImage) {
+
+        this.frontImage = frontImage;
+
+    }
+
+    public void setID(int ID) {
+
+        id = ID;
+
+    }
+
+    public void setTelephone(String telephone) {
+
+        this.telephone = telephone;
+
+    }
+
+    public void setWebsite(String website) {
+
+        this.website = website;
+
+    }
+
+    public Bitmap getBackImage() {
+
+        return backImage;
+
+    }
+
+    public Bitmap getFrontImage() {
+
+        return frontImage;
+
+    }
+
+    public String getEmail() {
+
+        return email;
+
+    }
+
+    public int getID() {
+
+        return id;
+
+    }
+
+    public String getTelephone() {
+
+        return telephone;
+
+    }
+
+    public String getWebsite() {
+
+        return website;
+
+    }
+
+}

+ 0 - 9
src/uk/ac/bath/oy213/ip/entities/Card.java

@@ -1,9 +0,0 @@
-package uk.ac.bath.oy213.ip.entities;
-
-import java.io.Serializable;
-
-/**
- * Created by Ollie on 16/04/15.
- */
-public class Card implements Serializable {
-}

+ 2 - 2
src/uk/ac/bath/oy213/ip/server/commands/net/GetLocalCardsCommand.java

@@ -1,7 +1,7 @@
 package uk.ac.bath.oy213.ip.server.commands.net;
 
-import uk.ac.bath.oy213.ip.entities.Card;
-import uk.ac.bath.oy213.ip.entities.UserLocation;
+import ip.e_biz.Card;
+import uk.ac.bath.oy213.ip.server.location.UserLocation;
 import uk.ac.bath.oy213.ip.server.Server;
 import uk.ac.bath.oy213.ip.server.net.ConnectionThread;
 import uk.ac.bath.oy213.ip.server.net.Packet;

+ 4 - 2
src/uk/ac/bath/oy213/ip/server/commands/net/RequestCardCommand.java

@@ -36,7 +36,9 @@ public class RequestCardCommand extends NetCommand {
 
         } else {
 
-            ResultSet resultSet = Server.instance().getDatabaseConnection().callStoredProcedure("getCard",args[0]);
+            int cardID = Integer.parseInt(args[0]);
+
+            ResultSet resultSet = Server.instance().getDatabaseConnection().callStoredProcedure("getCard",cardID);
 
             try {
 
@@ -50,7 +52,7 @@ public class RequestCardCommand extends NetCommand {
 
                 }
 
-            } catch (SQLException e) {
+            } catch (SQLException | NumberFormatException e) {
 
                 Server.instance().log(LoggingLevel.SEVERE, e.getMessage());
 

+ 1 - 1
src/uk/ac/bath/oy213/ip/server/commands/net/RequestSavedCardsCommand.java

@@ -1,6 +1,6 @@
 package uk.ac.bath.oy213.ip.server.commands.net;
 
-import uk.ac.bath.oy213.ip.entities.Card;
+import ip.e_biz.Card;
 import uk.ac.bath.oy213.ip.server.Server;
 import uk.ac.bath.oy213.ip.server.net.ConnectionThread;
 import uk.ac.bath.oy213.ip.server.net.Packet;

+ 60 - 0
src/uk/ac/bath/oy213/ip/server/commands/net/ShareCardCommand.java

@@ -1,6 +1,12 @@
 package uk.ac.bath.oy213.ip.server.commands.net;
 
+import uk.ac.bath.oy213.ip.server.Server;
 import uk.ac.bath.oy213.ip.server.net.ConnectionThread;
+import uk.ac.bath.oy213.ip.server.net.Packet;
+import uk.ac.bath.oy213.ip.server.util.LoggingLevel;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
 
 /**
  * Created by Ollie on 14/04/15.
@@ -16,10 +22,64 @@ public class ShareCardCommand extends NetCommand {
     @Override
     public void dispatch(ConnectionThread connection, String[] args, Object data) {
 
+        dispatch(connection,args);
+
     }
 
     @Override
     public void dispatch(ConnectionThread connection, String[] args) {
 
+        if (args.length != 1) {
+
+            connection.sendPacket(new Packet(null,"error",null,null));
+
+        } else {
+
+            ResultSet resultSet = Server.instance().getDatabaseConnection().callStoredProcedure("getUsersSavedCards", connection.getUsername());
+
+            try {
+
+                if (!resultSet.next()) {
+
+                    connection.sendPacket(new Packet(null, "error", null, null));
+
+                } else {
+
+                    boolean isUsersCard = false;
+                    int cardID = Integer.parseInt(args[0]);
+
+                    do {
+
+                        if (cardID == resultSet.getInt("CardID")) {
+
+                            isUsersCard = true;
+                            break;
+
+                        }
+
+                    } while (resultSet.next());
+
+                    if (isUsersCard) {
+
+                        Server.instance().getDatabaseConnection().callStoredProcedure("setShared",cardID);
+
+                    } else {
+
+                        connection.sendPacket(new Packet(null, "error", null, null));
+
+                    }
+
+                }
+
+            } catch (SQLException | NumberFormatException e) {
+
+                Server.instance().log(LoggingLevel.SEVERE,e.getMessage());
+                connection.sendPacket(new Packet(null, "error", null, null));
+
+            }
+
+        }
+
     }
+
 }

+ 60 - 0
src/uk/ac/bath/oy213/ip/server/commands/net/StopSharingCardCommand.java

@@ -1,6 +1,12 @@
 package uk.ac.bath.oy213.ip.server.commands.net;
 
+import uk.ac.bath.oy213.ip.server.Server;
 import uk.ac.bath.oy213.ip.server.net.ConnectionThread;
+import uk.ac.bath.oy213.ip.server.net.Packet;
+import uk.ac.bath.oy213.ip.server.util.LoggingLevel;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
 
 /**
  * Created by Ollie on 16/04/15.
@@ -16,10 +22,64 @@ public class StopSharingCardCommand extends NetCommand {
     @Override
     public void dispatch(ConnectionThread connection, String[] args, Object data) {
 
+        dispatch(connection, args);
+
     }
 
     @Override
     public void dispatch(ConnectionThread connection, String[] args) {
 
+        if (args.length != 1) {
+
+            connection.sendPacket(new Packet(null,"error",null,null));
+
+        } else {
+
+            ResultSet resultSet = Server.instance().getDatabaseConnection().callStoredProcedure("getUsersSavedCards", connection.getUsername());
+
+            try {
+
+                if (!resultSet.next()) {
+
+                    connection.sendPacket(new Packet(null, "error", null, null));
+
+                } else {
+
+                    boolean isUsersCard = false;
+                    int cardID = Integer.parseInt(args[0]);
+
+                    do {
+
+                        if (cardID == resultSet.getInt("CardID")) {
+
+                            isUsersCard = true;
+                            break;
+
+                        }
+
+                    } while (resultSet.next());
+
+                    if (isUsersCard) {
+
+                        Server.instance().getDatabaseConnection().callStoredProcedure("setUnShared",cardID);
+
+                    } else {
+
+                        connection.sendPacket(new Packet(null, "error", null, null));
+
+                    }
+
+                }
+
+            } catch (SQLException | NumberFormatException e) {
+
+                Server.instance().log(LoggingLevel.SEVERE,e.getMessage());
+                connection.sendPacket(new Packet(null, "error", null, null));
+
+            }
+
+        }
+
     }
+
 }

+ 1 - 1
src/uk/ac/bath/oy213/ip/server/commands/net/UpdateLocationCommand.java

@@ -1,6 +1,6 @@
 package uk.ac.bath.oy213.ip.server.commands.net;
 
-import uk.ac.bath.oy213.ip.entities.UserLocation;
+import uk.ac.bath.oy213.ip.server.location.UserLocation;
 import uk.ac.bath.oy213.ip.server.Server;
 import uk.ac.bath.oy213.ip.server.net.ConnectionThread;
 import uk.ac.bath.oy213.ip.server.net.Packet;

+ 0 - 2
src/uk/ac/bath/oy213/ip/server/location/LocationTable.java

@@ -1,7 +1,5 @@
 package uk.ac.bath.oy213.ip.server.location;
 
-import uk.ac.bath.oy213.ip.entities.UserLocation;
-
 import java.util.ArrayList;
 
 /**

+ 1 - 1
src/uk/ac/bath/oy213/ip/entities/UserLocation.java → src/uk/ac/bath/oy213/ip/server/location/UserLocation.java

@@ -1,4 +1,4 @@
-package uk.ac.bath.oy213.ip.entities;
+package uk.ac.bath.oy213.ip.server.location;
 
 /**
  * Created by Ollie on 17/04/15.

+ 23 - 3
src/uk/ac/bath/oy213/ip/server/util/CardBuilder.java

@@ -1,6 +1,8 @@
 package uk.ac.bath.oy213.ip.server.util;
 
-import uk.ac.bath.oy213.ip.entities.Card;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import ip.e_biz.Card;
 import uk.ac.bath.oy213.ip.server.Server;
 
 import java.sql.ResultSet;
@@ -14,9 +16,27 @@ public class CardBuilder {
 
     public static Card buildCard(ResultSet rs) {
 
-        //todo build card
+        try {
+
+            BitmapFactory.Options options = new BitmapFactory.Options();
+            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
+            Bitmap frontImage = BitmapFactory.decodeFile(rs.getString("FrontImageLocation"), options);
+            Bitmap backImage = BitmapFactory.decodeFile(rs.getString("BackImageLocation"), options);
+            String email = rs.getString("Email");
+            String telephone = rs.getString("Telephone");
+            String website = rs.getString("Website");
+
+            Card card = new Card(frontImage, backImage, email, telephone, website);
+            card.setID(rs.getInt("CardID"));
 
-        return new Card();
+            return card;
+
+        } catch (SQLException e) {
+
+            Server.instance().log(LoggingLevel.SEVERE, e.getMessage());
+            return null;
+
+        }
 
     }
 

+ 22 - 0
src/uk/ac/bath/oy213/ip/server/util/DatabaseConnection.java

@@ -61,6 +61,28 @@ public class DatabaseConnection {
 
     }
 
+    public ResultSet callStoredProcedure(String procedureName, int arg1) {
+
+        try {
+
+            PreparedStatement preparedStatement = connection.prepareStatement("{call SWIFTCARD." + procedureName + "(?)}");
+            preparedStatement.setInt(1, arg1);
+            preparedStatement.execute();
+            ResultSet resultSet = preparedStatement.getResultSet();
+            preparedStatement.close();
+
+            return resultSet;
+
+        } catch (SQLException e) {
+
+            Server.instance().log(LoggingLevel.SEVERE, e.getMessage());
+
+        }
+
+        return null;
+
+    }
+
     public ResultSet callStoredProcedure(String procedureName, String arg1, String arg2) {
 
         try {