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;
import java.util.HashMap;
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) {
HashMap<String, String> params = new HashMap<>();
params.put("id", id);
ArrayList<String> ids = parseOutSpaces(songids);
ArrayList<String> spotifys = new ArrayList<>();
StringBuilder sbParams = new StringBuilder();
int i = 0;
for (String key : params.keySet()) {
try {
if (i != 0){
sbParams.append("&");
for (String indv: ids) {
String spotty = getSongByID(indv);
spotifys.add(spotty);
}
sbParams.append(key).append("=").append(URLEncoder.encode(params.get(key), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
return spotifys;
}
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";
URL urlObj = new URL(url);
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);
return getFromDatabase(url, params);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
/*
* three enum inputs based on the database fields
* 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) {
result.append(line);
String cuisine = c.name(), vibe = v.name(), time = t.name();
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) {
e.printStackTrace();
}
String url = "https://cs2s.yorkdc.net/~jade.woodward/mobile-app-development/getsongbyatributes.php";
String songIDs = getFromDatabase(url, params);
if (conn != null) {
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<String> ids = parseOutSpaces(songIDs);
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<>();
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();
int i = 0;
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