diff --git a/.gitignore b/.gitignore index 907bd99..39314e8 100755 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ release.properties dependency-reduced-pom.xml buildNumber.properties .mvn/timing.properties + +profiles.txt diff --git a/app.json b/app.json index 72c77de..5444631 100644 --- a/app.json +++ b/app.json @@ -5,5 +5,10 @@ "test": "mvn test -B" } } + }, + "env": { + "DB_ENDPOINT": { + "required": true + } } } diff --git a/profiles.txt b/profiles.txt deleted file mode 100644 index e15577a..0000000 Binary files a/profiles.txt and /dev/null differ diff --git a/src/main/java/com/gameder/app/handlers/profiles/ProfileRegister.java b/src/main/java/com/gameder/app/handlers/profiles/ProfileRegister.java index 1a1f0b1..bc15f72 100644 --- a/src/main/java/com/gameder/app/handlers/profiles/ProfileRegister.java +++ b/src/main/java/com/gameder/app/handlers/profiles/ProfileRegister.java @@ -9,11 +9,20 @@ import java.io.ObjectOutputStream; import java.util.TreeSet; - +/** + * Simple backup register + * + * Created by Vili + */ public class ProfileRegister { private TreeSet profileTreeSet; + /** + * Getting the profile treeset from a textfile. + * @return treeset with profiles + * @throws IOException + */ public TreeSet getProfileRegister() throws IOException { try { FileInputStream fi = new FileInputStream(new File("profiles.txt")); @@ -22,7 +31,6 @@ public TreeSet getProfileRegister() throws IOException { fi.close(); oi.close(); System.out.println("File loaded"); - return profileTreeSet; } catch (FileNotFoundException e) { System.out.println("File not found"); @@ -33,6 +41,11 @@ public TreeSet getProfileRegister() throws IOException { } } + /** + * Profile treeset updating/creating + * @param ts + * @throws IOException + */ public void saveProfileRegister(TreeSet ts) throws IOException { try { FileOutputStream f = new FileOutputStream(new File("profiles.txt")); @@ -45,6 +58,4 @@ public void saveProfileRegister(TreeSet ts) throws IOException { System.out.println("File not found"); } } - - } diff --git a/src/main/java/com/gameder/app/handlers/profiles/ProfilesHandler.java b/src/main/java/com/gameder/app/handlers/profiles/ProfilesHandler.java index c7866aa..0dabdbf 100755 --- a/src/main/java/com/gameder/app/handlers/profiles/ProfilesHandler.java +++ b/src/main/java/com/gameder/app/handlers/profiles/ProfilesHandler.java @@ -3,12 +3,17 @@ import com.gameder.app.preferences.Preferences; import org.springframework.web.bind.annotation.*; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; import java.util.ArrayList; import java.util.Iterator; import java.util.TreeSet; @RestController public class ProfilesHandler { + private static final String dbEndpoint = System.getenv("DB_ENDPOINT"); private TreeSet profileTreeSet = new TreeSet<>(); @@ -65,7 +70,6 @@ public ArrayList getGamerListTest() { * */ public Profile findRoot() { int root = Preferences.getRandomPreferences(); - generateProfiles(); Iterator iterator = profileTreeSet.iterator(); while(true) { @@ -92,14 +96,68 @@ public Profile findRoot() { } } + /** + * For database + */ + public void loadProfileTree() { + try { + URL url = new URL(dbEndpoint); + URLConnection con = url.openConnection(); + HttpURLConnection http = (HttpURLConnection) con; + http.setRequestMethod("GET"); + http.setRequestProperty("Content-Type", "text/plain;charset=UTF-8"); + + System.out.println(http.getResponseCode()); + + BufferedInputStream bis = new BufferedInputStream(http.getInputStream()); + ObjectInputStream ois = new ObjectInputStream(bis); + + profileTreeSet = (TreeSet) ois.readObject(); + System.out.printf("Size of the tree is %d\n", profileTreeSet.size()); + + System.out.println("load"); + } catch (Exception e) { + System.out.println("Could not make the request"); + } + } + + /** + * For database + */ + public void saveProfileTree(TreeSet profiles) { + try { + URL url = new URL(dbEndpoint); + URLConnection con = url.openConnection(); + HttpURLConnection http = (HttpURLConnection) con; + http.setRequestMethod("POST"); + http.setRequestProperty("Content-Type", "text/plain;charset=UTF-8"); + http.setDoOutput(true); + + BufferedOutputStream bos = new BufferedOutputStream(http.getOutputStream()); + ObjectOutputStream oos = new ObjectOutputStream(bos); + + oos.writeObject(profiles); + oos.close(); + + System.out.printf("save: %d\n", http.getResponseCode()); + } catch (Exception e) { + System.out.println("Could not make the request"); + } + } + /** * Generates 50 profiles and adds them to the tree */ public void generateProfiles() { + TreeSet profiles = new TreeSet(); + for(int i = 0; i < 50; i++) { Profile p = ProfileGenerator.getRandomProfile(); - profileTreeSet.add(p); + profiles.add(p); } + + this.saveProfileTree(profiles); + this.loadProfileTree(); } /** @@ -119,6 +177,17 @@ public ArrayList getFiveProfiles() { * If either end is reached, it will skip that side * The algorithm can be easily limited if the profile pool is huge * */ + + /** + * If both ends have been reached, the roots will be reset + */ + if(maxRoot == null && minRoot == null) { + minnull = false; + maxnull = false; + maxRoot = profileTreeSet.higher(rootPreference); + minRoot = profileTreeSet.lower(rootPreference); + } + if(!minnull && !maxnull) { /** * Minroot is closer @@ -154,22 +223,18 @@ public ArrayList getFiveProfiles() { minRoot = profileTreeSet.lower(minRoot); } - /** - * If both ends have been reached, the roots will be reset - */ - if(maxRoot == null && minRoot == null) { - minnull = false; - maxnull = false; - maxRoot = profileTreeSet.higher(rootPreference); - minRoot = profileTreeSet.lower(rootPreference); - } } return profiles; } else { + System.out.println("Reached else"); return null; } } + public Profile getRoot() { + return this.rootPreference; + } + public TreeSet getProfilesTreeset() { return this.profileTreeSet; } diff --git a/src/test/java/com/gameder/app/handlers/profiles/ProfileSetTest.java b/src/test/java/com/gameder/app/handlers/profiles/ProfileSetTest.java index 4d6d2e6..6d48956 100644 --- a/src/test/java/com/gameder/app/handlers/profiles/ProfileSetTest.java +++ b/src/test/java/com/gameder/app/handlers/profiles/ProfileSetTest.java @@ -12,7 +12,7 @@ public class ProfileSetTest { public void ProfileSetIsFilled() { ProfilesHandler p = new ProfilesHandler(); - p.generateProfiles(); + p.getGamerList(); TreeSet ts = p.getProfilesTreeset(); Iterator ite = ts.iterator(); @@ -29,7 +29,7 @@ public void ProfileSetIsFilled() { public void ProfileSetIsInOrder() { ProfilesHandler p = new ProfilesHandler(); - p.generateProfiles(); + p.getGamerList(); TreeSet ts = p.getProfilesTreeset(); @@ -67,7 +67,9 @@ public void ProfileListIsValid() { public void RootIsFound() { ProfilesHandler p = new ProfilesHandler(); - Assert.assertNotNull(p.findRoot()); + p.getGamerList(); + + Assert.assertNotNull(p.getRoot()); } @Test