From 5311f6544590b37bed5bb6ef9bc28304a99aa06c Mon Sep 17 00:00:00 2001
From: Hallvard Traetteberg <hal@ntnu.no>
Date: Mon, 30 Sep 2019 15:28:13 +0200
Subject: [PATCH] =?UTF-8?q?Endret=20eksempel=20s=C3=A5=20app-en=20kj=C3=B8?=
 =?UTF-8?q?rer=20serveren,=20hvis=20baseUri=20gis=20inn=20som=20arg[0].?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 simpleexample2/fxui/build.gradle              | 11 +++++-
 .../fxui/src/main/java/simpleex/ui/FxApp.java | 38 +++++++++++++++++--
 .../simpleex/ui/FxAppUsingRestController.java |  2 +-
 simpleexample2/restserver/build.gradle        | 17 +++++----
 4 files changed, 55 insertions(+), 13 deletions(-)

diff --git a/simpleexample2/fxui/build.gradle b/simpleexample2/fxui/build.gradle
index 24d94bd..5123a2b 100644
--- a/simpleexample2/fxui/build.gradle
+++ b/simpleexample2/fxui/build.gradle
@@ -25,6 +25,10 @@ application {
 	mainClassName = 'simpleex.ui.FxApp' // application plugin
 }
 
+run {
+    args = ["http://localhost:8080/", "[[63.1, 11.2], [63.2, 11.0]]"]
+}
+
 // javafx specific way of specifying dependencies
 javafx {
 	version = '11'
@@ -61,13 +65,16 @@ dependencies {
 	implementation project(':core')
     // brukergrensesnitt
     implementation name: 'fx-map-control-1.0'
-    
+	// rest-server (siden den foreløpig må startes av app-en)
+	implementation project(':restserver')
+
     testImplementation 'junit:junit:4.12'
 
-    // brukergrensesnitt
+    // rest-server
 	testCompile project(':restapi')
 	testImplementation project(':restserver')
 	
+    // brukergrensesnitt
     testImplementation 'org.testfx:testfx-core:4.0.16-alpha'
     testImplementation 'org.testfx:testfx-junit:4.0.16-alpha'
 	testImplementation 'org.mockito:mockito-core:2.28.2'
diff --git a/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java b/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
index cd1bdc3..aa0b92a 100644
--- a/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
+++ b/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
@@ -1,25 +1,57 @@
 package simpleex.ui;
 
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import org.glassfish.grizzly.http.server.HttpServer;
 import javafx.application.Application;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Parent;
 import javafx.scene.Scene;
 import javafx.stage.Stage;
 import simpleex.core.LatLongs;
+import simpleex.restapi.LatLongsService;
+import simpleex.restserver.LatLongGrizzlyApp;
 
 public class FxApp extends Application {
 
+  private HttpServer restServer = null;
   @Override
   public void start(final Stage stage) throws Exception {
-    final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FxApp.fxml"));
+    URI baseUri = null;
+    final List<String> args = getParameters().getRaw();
+    if (args.size() >= 1) {
+      final List<String> serverArgs = new ArrayList<String>();
+      baseUri = URI.create(args.get(0));
+      serverArgs.add(baseUri.toString());
+      if (args.size() >= 2) {
+        // json of initial data
+        serverArgs.add(args.get(1));
+      }
+      restServer = LatLongGrizzlyApp.startServer(serverArgs.toArray(new String[serverArgs.size()]));
+    }
+    final String fxml = (baseUri != null ? "FxAppUsingRest.fxml" : "FxApp.fxml");
+    final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
     final Parent root = fxmlLoader.load();
-    final FxAppController controller = fxmlLoader.getController();
-    controller.setLatLongs(new LatLongs(63.1, 11.2, 63.2, 11.0));
+    if (baseUri == null) {
+      // set initial data manually
+      final FxAppController controller = fxmlLoader.getController();
+      controller.setLatLongs(new LatLongs(63.1, 11.2, 63.2, 11.0));
+    } else {
+      final FxAppUsingRestController controller = fxmlLoader.getController();
+      controller.setDataAccess(new RestLatLongsDataAccess(baseUri + LatLongsService.LAT_LONG_SERVICE_PATH, controller.getObjectMapper()));
+    }
     final Scene scene = new Scene(root);
     stage.setScene(scene);
     stage.show();
   }
 
+  @Override
+  public void stop() throws Exception {
+    restServer.shutdown();
+    super.stop();
+  }
+
   /**
    * Launches the app.
    * @param args the command line arguments
diff --git a/simpleexample2/fxui/src/main/java/simpleex/ui/FxAppUsingRestController.java b/simpleexample2/fxui/src/main/java/simpleex/ui/FxAppUsingRestController.java
index 905bac6..e424f3b 100644
--- a/simpleexample2/fxui/src/main/java/simpleex/ui/FxAppUsingRestController.java
+++ b/simpleexample2/fxui/src/main/java/simpleex/ui/FxAppUsingRestController.java
@@ -25,6 +25,6 @@ FxAppController --> ListView: "locationListView"
 public class FxAppUsingRestController extends AbstractFxAppController {
 
   public FxAppUsingRestController() {
-    setDataAccess(new RestLatLongsDataAccess("http://localhost:8080/dict", getObjectMapper()));
+//    setDataAccess(new RestLatLongsDataAccess("http://localhost:8080/latLong", getObjectMapper()));
   }
 }
diff --git a/simpleexample2/restserver/build.gradle b/simpleexample2/restserver/build.gradle
index c472e7c..ce20dad 100644
--- a/simpleexample2/restserver/build.gradle
+++ b/simpleexample2/restserver/build.gradle
@@ -1,5 +1,5 @@
 plugins {
-	id 'application'
+	id 'java-library'
 }
 
 ext {
@@ -7,15 +7,18 @@ ext {
     jerseyVersion = '2.28'
 }
 
-application {
-	sourceCompatibility = JavaVersion.VERSION_1_10
-	targetCompatibility = JavaVersion.VERSION_1_10
-	mainClassName = 'simpleex.restserver.LatLongGrizzlyApp'
-}
+sourceCompatibility = JavaVersion.VERSION_1_10
+targetCompatibility = JavaVersion.VERSION_1_10
+
+//application {
+//	sourceCompatibility = JavaVersion.VERSION_1_10
+//	targetCompatibility = JavaVersion.VERSION_1_10
+//	// mainClassName = 'simpleex.restserver.LatLongGrizzlyApp'
+//}
 
 dependencies {
 	implementation project(':core')
-	implementation project(':restapi')
+	compile project(':restapi')
 
     compile "org.slf4j:slf4j-api:$slf4jVersion"
     compile "org.slf4j:slf4j-simple:$slf4jVersion"
-- 
GitLab