From 47675f364c3bb37edb7084dfbf3f525e068208bc Mon Sep 17 00:00:00 2001 From: Diego Mauricio Date: Sun, 19 May 2013 13:33:31 +0200 Subject: [PATCH] @diego added example for the management of a complex bean, called Railway Station --- pom.xml | 57 ++++++------ .../beans/RailwayStation.java | 38 ++++++++ .../controller/RailwayStationController.java | 53 +++++++++++ .../service/RailwayStationService.java | 21 +++++ .../service/RailwayStationServiceImpl.java | 65 +++++++++++++ src/main/webapp/WEB-INF/html/index.html | 2 + .../WEB-INF/html/railwaystations/layout.html | 86 +++++++++++++++++ .../webapp/WEB-INF/html/trains/layout.html | 3 +- src/main/webapp/WEB-INF/web.xml | 2 +- src/main/webapp/resources/js/app.js | 5 + .../controllers/RailwayStationController.js | 93 +++++++++++++++++++ .../js/controllers/TrainController.js | 2 +- 12 files changed, 396 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/xvitcoder/angualrspringapp/beans/RailwayStation.java create mode 100644 src/main/java/com/xvitcoder/angualrspringapp/controller/RailwayStationController.java create mode 100644 src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationService.java create mode 100644 src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationServiceImpl.java create mode 100644 src/main/webapp/WEB-INF/html/railwaystations/layout.html create mode 100644 src/main/webapp/resources/js/controllers/RailwayStationController.js diff --git a/pom.xml b/pom.xml index e82ad2e..6202668 100644 --- a/pom.xml +++ b/pom.xml @@ -1,34 +1,35 @@ - 4.0.0 - com.xvitcoder.angularspringapp - AngularSpringApp - war - 1.0-SNAPSHOT - AngularSpringApp Maven Webapp - http://maven.apache.org - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 + com.xvitcoder.angularspringapp + AngularSpringApp + war + 1.0-SNAPSHOT + AngularSpringApp Maven Webapp + http://maven.apache.org + - - org.springframework - spring-webmvc - 3.1.2.RELEASE - + + org.springframework + spring-webmvc + 3.1.2.RELEASE + - - org.apache.velocity - velocity - 1.7 - + + org.apache.velocity + velocity + 1.7 + - - org.codehaus.jackson - jackson-mapper-asl - 1.9.4 - + + org.codehaus.jackson + jackson-mapper-asl + 1.9.12 + - - - AngularSpringApp - + + + + AngularSpringApp + diff --git a/src/main/java/com/xvitcoder/angualrspringapp/beans/RailwayStation.java b/src/main/java/com/xvitcoder/angualrspringapp/beans/RailwayStation.java new file mode 100644 index 0000000..9ba142b --- /dev/null +++ b/src/main/java/com/xvitcoder/angualrspringapp/beans/RailwayStation.java @@ -0,0 +1,38 @@ +package com.xvitcoder.angualrspringapp.beans; + +public class RailwayStation { + + + private Long id; + + private String name; + + private Train train; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Train getTrain() { + return train; + } + + public void setTrain(Train train) { + this.train = train; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + + +} diff --git a/src/main/java/com/xvitcoder/angualrspringapp/controller/RailwayStationController.java b/src/main/java/com/xvitcoder/angualrspringapp/controller/RailwayStationController.java new file mode 100644 index 0000000..5dd2129 --- /dev/null +++ b/src/main/java/com/xvitcoder/angualrspringapp/controller/RailwayStationController.java @@ -0,0 +1,53 @@ +package com.xvitcoder.angualrspringapp.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.xvitcoder.angualrspringapp.beans.RailwayStation; +import com.xvitcoder.angualrspringapp.service.RailwayStationServiceImpl; + +@Controller +@RequestMapping("/railwaystations") +public class RailwayStationController { + + @Autowired + private RailwayStationServiceImpl railwayStationsService; + + @RequestMapping("railwaystationlist.json") + public @ResponseBody List getRailwayStationList() { + return railwayStationsService.getAllRailwayStations(); + } + + @RequestMapping(value = "/add", method = RequestMethod.POST) + public @ResponseBody void addRailwayStation(@RequestBody RailwayStation railwayStation) { + railwayStationsService.addRailwayStation(railwayStation); + } + + @RequestMapping(value = "/update", method = RequestMethod.PUT) + public @ResponseBody void updateRailwayStation(@RequestBody RailwayStation railwayStation) { + railwayStationsService.updateRailwayStation(railwayStation); + } + + @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE) + public @ResponseBody void removeRailwayStation(@PathVariable("id") Long id) { + railwayStationsService.deleteRailwayStationById(id); + } + + @RequestMapping(value = "/removeAll", method = RequestMethod.DELETE) + public @ResponseBody void removeAllRailwayStations() { + railwayStationsService.deleteAll(); + } + + @RequestMapping("/layout") + public String getRailwayStationPartialPage(ModelMap modelMap) { + return "railwaystations/layout"; + } +} diff --git a/src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationService.java b/src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationService.java new file mode 100644 index 0000000..c295cae --- /dev/null +++ b/src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationService.java @@ -0,0 +1,21 @@ +package com.xvitcoder.angualrspringapp.service; + +import com.xvitcoder.angualrspringapp.beans.RailwayStation; + +import java.util.List; + + +public interface RailwayStationService { + + public List getAllRailwayStations(); + + public RailwayStation getRailwayStationById(Long id); + + public void addRailwayStation(RailwayStation RailwayStation); + + public void deleteRailwayStationById(Long id); + + public void deleteAll(); + + public void updateRailwayStation(RailwayStation RailwayStation); +} diff --git a/src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationServiceImpl.java b/src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationServiceImpl.java new file mode 100644 index 0000000..e1a8430 --- /dev/null +++ b/src/main/java/com/xvitcoder/angualrspringapp/service/RailwayStationServiceImpl.java @@ -0,0 +1,65 @@ +package com.xvitcoder.angualrspringapp.service; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.xvitcoder.angualrspringapp.beans.RailwayStation; + + +@Service("RailwayStationService") +public class RailwayStationServiceImpl implements RailwayStationService { + + private static List rsList = new ArrayList(); + private static Long id = 0L; + + public RailwayStation getRailwayStationById(Long id) { + + return findRailwayStationById(id); + } + + private RailwayStation findRailwayStationById(Long id) { + for (RailwayStation rs : rsList) { + if (rs.getId() == id) { + return rs; + } + } + + return null; + } + + public List getAllRailwayStations() { + return rsList; + } + + + public void addRailwayStation(RailwayStation railwayStation) { + railwayStation.setId(++ id); + rsList.add(railwayStation); + + } + + public void deleteRailwayStationById(Long id) { + RailwayStation found = findRailwayStationById(id); + if (found != null) { + rsList.remove(found); + id--; + } + + } + + public void updateRailwayStation(RailwayStation railwayStation) { + RailwayStation found = findRailwayStationById(railwayStation.getId()); + if (found != null) { + rsList.remove(found); + rsList.add(railwayStation); + } + } + + @Override + public void deleteAll() { + rsList.clear(); + id = 0L; + } +} diff --git a/src/main/webapp/WEB-INF/html/index.html b/src/main/webapp/WEB-INF/html/index.html index 6904463..00a3c39 100755 --- a/src/main/webapp/WEB-INF/html/index.html +++ b/src/main/webapp/WEB-INF/html/index.html @@ -12,6 +12,7 @@
@@ -21,6 +22,7 @@ + diff --git a/src/main/webapp/WEB-INF/html/railwaystations/layout.html b/src/main/webapp/WEB-INF/html/railwaystations/layout.html new file mode 100644 index 0000000..4a30422 --- /dev/null +++ b/src/main/webapp/WEB-INF/html/railwaystations/layout.html @@ -0,0 +1,86 @@ +
{{errorMessage}}
+
+ +
+ + +
+ +
+
+ + +
+ + +
+ +
+
+
+ + +
+ + + + km/h +
+
+
+
+ +
+
+
+ + + + +
+
+
+
+

Railway Stations List

+
No Railway Stations found
+ + + + + + + + + + + + + + + + + + + + +
IdNameAction
{{item.id}}{{item.name}} + + +
+ \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/html/trains/layout.html b/src/main/webapp/WEB-INF/html/trains/layout.html index e10622f..719b154 100755 --- a/src/main/webapp/WEB-INF/html/trains/layout.html +++ b/src/main/webapp/WEB-INF/html/trains/layout.html @@ -14,6 +14,7 @@
+ km/h
@@ -60,4 +61,4 @@

Trains List

- \ No newline at end of file + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 047fee5..7d7e406 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -17,11 +17,11 @@ DispatcherServlet org.springframework.web.servlet.DispatcherServlet - 1 contextConfigLocation /WEB-INF/spring/webapp-config.xml + 1 diff --git a/src/main/webapp/resources/js/app.js b/src/main/webapp/resources/js/app.js index 718144f..888a43b 100755 --- a/src/main/webapp/resources/js/app.js +++ b/src/main/webapp/resources/js/app.js @@ -15,6 +15,11 @@ App.config(['$routeProvider', function ($routeProvider) { templateUrl: 'trains/layout', controller: TrainController }); + + $routeProvider.when('/railwaystations', { + templateUrl: 'railwaystations/layout', + controller: RailwayStationController + }); $routeProvider.otherwise({redirectTo: '/cars'}); }]); diff --git a/src/main/webapp/resources/js/controllers/RailwayStationController.js b/src/main/webapp/resources/js/controllers/RailwayStationController.js new file mode 100644 index 0000000..325d180 --- /dev/null +++ b/src/main/webapp/resources/js/controllers/RailwayStationController.js @@ -0,0 +1,93 @@ +'use strict'; + +/** + * RailwayStationController + * @constructor + */ +var RailwayStationController = function($scope, $http) { + $scope.rs = {}; + $scope.editMode = false; + + $scope.fetchRailwayStationsList = function() { + $http.get('railwaystations/railwaystationlist.json').success(function(rsList){ + $scope.railwaystations = rsList; + }); + } + + $scope.addNewRailwayStation = function(rs) { + + $scope.resetError(); + + $http.post('railwaystations/add', rs).success(function() { + $scope.fetchRailwayStationsList(); + $scope.rs.name = ''; + $scope.rs.train.name = ''; + $scope.rs.train.speed = ''; + $scope.rs.train.diesel = false; + }).error(function() { + $scope.setError('Could not add a new station'); + }); + } + + $scope.updateRailwayStation = function(rs) { + $scope.resetError(); + + $http.put('railwaystations/update', rs).success(function() { + $scope.fetchRailwayStationsList(); + $scope.rs.name = ''; + $scope.rs.train.name = ''; + $scope.rs.train.speed = ''; + $scope.rs.train.diesel = false; + $scope.editMode = false; + }).error(function() { + $scope.setError('Could not update the train'); + }); + } + + $scope.editRailwayStation = function(rs) { + $scope.resetError(); + $scope.rs = rs; + $scope.editMode = true; + } + + $scope.removeRailwayStation = function(id) { + $scope.resetError(); + + $http.delete('railwaystations/remove/' + id).success(function() { + $scope.fetchRailwayStationsList(); + }).error(function() { + $scope.setError('Could not remove train'); + }); + } + + $scope.removeAllRailwayStations = function() { + $scope.resetError(); + + $http.delete('railwaystations/removeAll').success(function() { + $scope.fetchRailwayStationsList(); + }).error(function() { + $scope.setError('Could not remove all RailwayStations'); + }); + + }; + + $scope.resetRailwayStationForm = function() { + $scope.resetError(); + $scope.rs = {}; + $scope.editMode = false; + } + + $scope.resetError = function() { + $scope.error = false; + $scope.errorMessage = ''; + } + + $scope.setError = function(message) { + $scope.error = true; + $scope.errorMessage = message; + } + + $scope.fetchRailwayStationsList(); + + $scope.predicate = 'id'; +} \ No newline at end of file diff --git a/src/main/webapp/resources/js/controllers/TrainController.js b/src/main/webapp/resources/js/controllers/TrainController.js index 04ce59e..7080ce4 100755 --- a/src/main/webapp/resources/js/controllers/TrainController.js +++ b/src/main/webapp/resources/js/controllers/TrainController.js @@ -86,5 +86,5 @@ var TrainController = function($scope, $http) { $scope.fetchTrainsList(); - $scope.predicate = 'id' + $scope.predicate = 'id'; } \ No newline at end of file