Commit 39593ebb authored by jade.woodward's avatar jade.woodward

Added method to get playlist songs by attributes

with enums and comments for the entire method and a better method structure
parent dc3d7039
...@@ -15,83 +15,107 @@ import java.util.ArrayList; ...@@ -15,83 +15,107 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
public class databaseInterface { public class databaseInterface {
public enum Cuisine {
chinese,
thai,
indian,
japanese,
italian,
mexican,
french,
spanish,
english,
american,
any
}
public enum Vibe {
chill,
happy,
energetic,
busy,
any
}
public enum Time {
breakfast,
lunch,
dinner,
any
}
/*
* one string input - should still be in 123 format though
* * returns array of spotify ids
*/
public ArrayList<String> getPlaylistByID(String id) {
String songids = getPlaylistSongIDsByID(id);
public String getSongByID(String id) { ArrayList<String> ids = parseOutSpaces(songids);
HashMap<String, String> params = new HashMap<>(); ArrayList<String> spotifys = new ArrayList<>();
params.put("id", id);
StringBuilder sbParams = new StringBuilder(); for (String indv: ids) {
int i = 0; String spotty = getSongByID(indv);
for (String key : params.keySet()) { spotifys.add(spotty);
try {
if (i != 0){
sbParams.append("&");
} }
sbParams.append(key).append("=").append(URLEncoder.encode(params.get(key), "UTF-8"));
} catch (UnsupportedEncodingException e) { return spotifys;
e.printStackTrace();
}
i++;
} }
String spotify = "failed to fetch from database"; /*
* one string input - should still be in 123 format though
* gets a Spotify id from the database
*/
public String getSongByID(String id) {
HashMap<String, String> params = new HashMap<>();
params.put("id", id);
try {
String url = "https://cs2s.yorkdc.net/~jade.woodward/mobile-app-development/getsongbyid.php"; String url = "https://cs2s.yorkdc.net/~jade.woodward/mobile-app-development/getsongbyid.php";
URL urlObj = new URL(url); return getFromDatabase(url, params);
}
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.connect();
String paramsString = sbParams.toString();
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close();
try {
InputStream is = conn.getInputStream();
BufferedInputStream in = new BufferedInputStream(is);
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); /*
StringBuilder result = new StringBuilder(); * three enum inputs based on the database fields
String line; * returns an array of song ids that meet the criteria
*/
public ArrayList<String> getPlaylistByAttributes(Cuisine c, Vibe v, Time t) {
HashMap<String, String> params = new HashMap<>();
while ((line = reader.readLine()) != null) { String cuisine = c.name(), vibe = v.name(), time = t.name();
result.append(line); if (cuisine.equals("any")) {
cuisine = "%";
} if (vibe.equals("any")) {
vibe = "%";
} if (time.equals("any")) {
time = "%";
} }
spotify = result.toString(); params.put("cuisine", cuisine);
params.put("vibe", vibe);
params.put("time", time);
} catch (IOException e) { String url = "https://cs2s.yorkdc.net/~jade.woodward/mobile-app-development/getsongbyatributes.php";
e.printStackTrace(); String songIDs = getFromDatabase(url, params);
}
if (conn != null) { ArrayList<String> ids = parseOutSpaces(songIDs);
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
return spotify; return ids;
} }
/*
public String getPlaylistSongIDsByID(String id) { * one string input - should still be in 123 format though
* gets a list of song ids from the database
*/
private String getPlaylistSongIDsByID(String id) {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
params.put("id", id); params.put("id", id);
String url = "https://cs2s.yorkdc.net/~jade.woodward/mobile-app-development/getplaylistbyid.php";
return getFromDatabase(url, params);
}
/*
* base to fetch from the database
*/
private String getFromDatabase(String url, HashMap <String, String> params) {
StringBuilder sbParams = new StringBuilder(); StringBuilder sbParams = new StringBuilder();
int i = 0; int i = 0;
for (String key : params.keySet()) { for (String key : params.keySet()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment