Skip to content

Commit 5d0b21c

Browse files
committed
Added CRUD, form in web page and rest services.
Bootstrap css dependency in bower.
1 parent b24911c commit 5d0b21c

8 files changed

Lines changed: 223 additions & 34 deletions

File tree

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"angular-resource": "~1.2.20",
1313
"jquery": "~1.9.1",
1414
"angular-bootstrap": "~0.11.0",
15+
"bootstrap-css-only": "3.2.0",
1516
"angular-grid": "~2.0.11"
1617
}
1718
}

src/main/java/com/cortez/samples/javaee7angular/data/Person.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Person {
1717

1818
private String description;
1919

20+
private String link;
21+
2022
public Long getId() {
2123
return id;
2224
}
@@ -41,6 +43,14 @@ public void setDescription(String description) {
4143
this.description = description;
4244
}
4345

46+
public String getLink() {
47+
return link;
48+
}
49+
50+
public void setLink(String link) {
51+
this.link = link;
52+
}
53+
4454
@Override
4555
public boolean equals(Object o) {
4656
if (this == o) { return true; }

src/main/java/com/cortez/samples/javaee7angular/rest/PersonResource.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
@Stateless
2121
@ApplicationPath("/resources")
2222
@Path("persons")
23+
@Consumes(MediaType.APPLICATION_JSON)
24+
@Produces(MediaType.APPLICATION_JSON)
2325
public class PersonResource extends Application {
2426
@PersistenceContext
2527
private EntityManager entityManager;
@@ -37,7 +39,7 @@ private List<Person> findPersons(int startPosition, int maxResults, String sortF
3739
return query.getResultList();
3840
}
3941

40-
public PaginatedListWrapper<Person> findPersons(PaginatedListWrapper<Person> wrapper) {
42+
private PaginatedListWrapper<Person> findPersons(PaginatedListWrapper<Person> wrapper) {
4143
wrapper.setTotalResults(countPersons());
4244
int start = (wrapper.getCurrentPage() - 1) * wrapper.getPageSize();
4345
wrapper.setList(findPersons(start,
@@ -62,7 +64,35 @@ public PaginatedListWrapper<Person> listPersons(@DefaultValue("1")
6264
paginatedListWrapper.setCurrentPage(page);
6365
paginatedListWrapper.setSortFields(sortFields);
6466
paginatedListWrapper.setSortDirections(sortDirections);
65-
paginatedListWrapper.setPageSize(5);
67+
paginatedListWrapper.setPageSize(10);
6668
return findPersons(paginatedListWrapper);
6769
}
70+
71+
@GET
72+
@Path("{id}")
73+
public Person getPerson( @PathParam("id") Long id) {
74+
return entityManager.find(Person.class, id);
75+
}
76+
77+
@POST
78+
public Person savePerson(Person person) {
79+
if (person.getId() == null) {
80+
person.setId(countPersons().longValue() + 1);
81+
entityManager.persist(person);
82+
} else {
83+
Person personToSave = getPerson(person.getId());
84+
personToSave.setName(person.getName());
85+
personToSave.setDescription(person.getDescription());
86+
personToSave.setLink(person.getLink());
87+
person = entityManager.merge(personToSave);
88+
}
89+
90+
return person;
91+
}
92+
93+
@DELETE
94+
@Path("{id}")
95+
public void deletePerson(@PathParam("id") Long id) {
96+
entityManager.remove(getPerson(id));
97+
}
6898
}

src/main/resources/sql/create.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CREATE TABLE PERSON("ID" INTEGER not null primary key, "NAME" VARCHAR(50), "DESCRIPTION" VARCHAR(100))
1+
CREATE TABLE PERSON("ID" INTEGER not null primary key, "NAME" VARCHAR(50), "DESCRIPTION" VARCHAR(100), "LINK" VARCHAR(500))

src/main/resources/sql/load.sql

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (1, 'Uzumaki Naruto', 'Konoha')
2-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (2, 'Hatake Kakashi', 'Konoha')
3-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (3, 'Haruno Sakura', 'Konoha')
4-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (4, 'Uchiha Sasuke', 'Missing-nin')
5-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (5, 'Gaara', 'Sunagakure')
6-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (6, 'Killer Bee', 'Kumogakure')
7-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (7, 'Jiraya', 'Konoha')
8-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (8, 'Namikaze Minato', 'Konoha')
9-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (9, 'Uchiha Madara', 'Missing-nin')
10-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (10, 'Senju Hashirama', 'Konoha')
11-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (11, 'Might Guy', 'Konoha')
12-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (12, 'Hyuga Neji', 'Konoha')
13-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (13, 'Rock Lee', 'Konoha')
14-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (14, 'Uchiha Obito', 'Missing-nin')
15-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (15, 'Kurama', 'Tailed Beast')
16-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (16, 'Uzumaki Kushina', 'Konoha')
17-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (17, 'Nara Shikamaru', 'Konoha')
18-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (18, 'Sarutobi Hiruzen', 'Konoha')
19-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (19, 'Tsunade', 'Konoha')
20-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (20, 'Orochimaru', 'Missing-nin')
21-
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (21, 'Uchicha Itachi', 'Missing-nin')
1+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (1, 'Uzumaki Naruto', 'Konoha', 'http://img1.wikia.nocookie.net/__cb20140523045537/naruto/images/thumb/3/36/Naruto_Uzumaki.png/300px-Naruto_Uzumaki.png');
2+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (2, 'Hatake Kakashi', 'Konoha', 'http://img1.wikia.nocookie.net/__cb20140616090940/naruto/images/thumb/b/b3/KakashiHatake.png/300px-KakashiHatake.png');
3+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (3, 'Haruno Sakura', 'Konoha', 'http://img1.wikia.nocookie.net/__cb20130910111848/naruto/images/thumb/5/59/Sakura_Part_II.png/300px-Sakura_Part_II.png');
4+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (4, 'Uchiha Sasuke', 'Missing-nin', 'http://img2.wikia.nocookie.net/__cb20140124043530/naruto/images/thumb/c/ce/Sasuke_Shippuden.png/300px-Sasuke_Shippuden.png');
5+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (5, 'Gaara', 'Sunagakure', 'http://img3.wikia.nocookie.net/__cb20130910220958/naruto/images/thumb/0/0f/Gaara_Part_II.png/300px-Gaara_Part_II.png');
6+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (6, 'Killer Bee', 'Kumogakure', 'http://img1.wikia.nocookie.net/__cb20120215120020/naruto/images/thumb/3/36/Killer_Bee.png/300px-Killer_Bee.png');
7+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (7, 'Jiraya', 'Konoha', 'http://img2.wikia.nocookie.net/__cb20120925123905/naruto/images/thumb/2/21/Profile_Jiraiya.PNG/300px-Profile_Jiraiya.PNG');
8+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (8, 'Namikaze Minato', 'Konoha', 'http://img4.wikia.nocookie.net/__cb20140209115534/naruto/images/thumb/1/1f/Minato_Namikaze.PNG/300px-Minato_Namikaze.PNG');
9+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (9, 'Uchiha Madara', 'Missing-nin', 'http://img2.wikia.nocookie.net/__cb20140118022828/naruto/images/thumb/2/29/Uchiha_Madara.png/300px-Uchiha_Madara.png');
10+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (10, 'Senju Hashirama', 'Konoha', 'http://img2.wikia.nocookie.net/__cb20120915132454/naruto/images/thumb/7/7e/Hashirama_Senju.png/300px-Hashirama_Senju.png');
11+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (11, 'Might Guy', 'Konoha', 'http://img4.wikia.nocookie.net/__cb20140605202934/naruto/images/thumb/4/40/MightGuy.png/300px-MightGuy.png');
12+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (12, 'Hyuga Neji', 'Konoha', 'http://img1.wikia.nocookie.net/__cb20130913104919/naruto/images/thumb/a/a8/Neji_Part_II.png/300px-Neji_Part_II.png');
13+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (13, 'Rock Lee', 'Konoha', 'http://img1.wikia.nocookie.net/__cb20131029112352/naruto/images/thumb/7/7d/Lee_timeskip.png/300px-Lee_timeskip.png');
14+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (14, 'Uchiha Obito', 'Missing-nin', 'http://img1.wikia.nocookie.net/__cb20140616090247/naruto/images/thumb/a/a0/Obito_Uchiha2.png/300px-Obito_Uchiha2.png');
15+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (15, 'Kurama', 'Tailed Beast', 'http://img2.wikia.nocookie.net/__cb20100601024642/naruto/images/thumb/2/28/9_tails.PNG/300px-9_tails.PNG');
16+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (16, 'Uzumaki Kushina', 'Konoha', 'http://img4.wikia.nocookie.net/__cb20121006054451/naruto/images/thumb/4/4d/Kushina_2.png/300px-Kushina_2.png');
17+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (17, 'Nara Shikamaru', 'Konoha', 'http://img1.wikia.nocookie.net/__cb20130917013425/naruto/images/thumb/9/9a/Shikamaru_Nara.png/300px-Shikamaru_Nara.png');
18+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (18, 'Sarutobi Hiruzen', 'Konoha', 'http://img4.wikia.nocookie.net/__cb20120912121115/naruto/images/thumb/e/e4/Hiruzen_Sarutobi.png/300px-Hiruzen_Sarutobi.png');
19+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (19, 'Tsunade', 'Konoha', 'http://img2.wikia.nocookie.net/__cb20140417023105/naruto/images/thumb/1/14/Tsunade_newshot.png/300px-Tsunade_newshot.png');
20+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (20, 'Orochimaru', 'Missing-nin', 'http://img3.wikia.nocookie.net/__cb20100623204832/naruto/images/thumb/e/e8/Orochimaru2.jpg/300px-Orochimaru2.jpg');
21+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION", "LINK") VALUES (21, 'Uchicha Itachi', 'Missing-nin', 'http://img1.wikia.nocookie.net/__cb20110723152743/naruto/images/thumb/8/8d/UchihaItachi.png/300px-UchihaItachi.png');

src/main/webapp/css/style.css

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
body {
2-
width: 50%;
2+
width: 90%;
33
margin-left: auto;
44
margin-right: auto;
5+
text-align: center;
6+
}
7+
8+
.message {
9+
margin-left: 5%;
10+
margin-right: 5%;
511
}
612

713
.grid {
8-
text-align: center;
14+
width: 40%;
15+
float: left;
16+
margin-left: 5%;
17+
margin-right: 5%;
918
}
1019

1120
.gridStyle {
1221
border: 1px solid rgb(212, 212, 212);
13-
height: 185px;
22+
height: 335px;
23+
}
24+
25+
.form {
26+
width: 40%;
27+
float: right;
28+
margin-left: 5%;
29+
margin-right: 5%;
30+
text-align: left;
31+
}
32+
33+
.form label {
34+
width: 100px;
35+
}
36+
37+
.form input {
38+
width: 300px;
39+
float: right;
40+
}
41+
42+
.form .form-group span {
43+
float: right;
44+
width: 20px;
45+
height: 20px;
46+
margin-left: 5px;
47+
color: green;
48+
}
49+
50+
.form .avatar {
51+
height: 250px;
52+
float: right;
53+
}
54+
55+
.form .buttons {
56+
clear: both;
57+
float: right;
58+
margin-top: 10px;
1459
}

src/main/webapp/index.html

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
<head>
55
<title>javaee7-angular</title>
66

7-
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
8-
97
<!-- build:css css/third-party.css -->
108
<!-- bower:css -->
9+
<link rel="stylesheet" href="lib/bower/bootstrap-css-only/css/bootstrap.css" />
1110
<link rel="stylesheet" href="lib/bower/angular-grid/ng-grid.css" />
1211
<!-- endbower -->
1312
<!-- endbuild -->
@@ -33,11 +32,19 @@
3332

3433
<body>
3534

35+
<h1>Java EE - Angular Application</h1>
36+
37+
<br/>
38+
39+
<div class="message" ng-controller="alertMessagesController">
40+
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</alert>
41+
</div>
42+
3643
<br>
3744

3845
<div class="grid">
3946
<!-- Specify a JavaScript controller script that binds Javascript variables to the HTML.-->
40-
<div ng-controller="personsList">
47+
<div ng-controller="personsListController">
4148
<!-- Binds the grid component to be displayed. -->
4249
<div class="gridStyle" ng-grid="gridOptions"></div>
4350

@@ -49,5 +56,36 @@
4956
</div>
5057
</div>
5158

59+
<div class="form">
60+
<div>
61+
<form name="personForm" ng-controller="personsFormController" ng-submit="updatePerson()">
62+
<div class="form-group">
63+
<label for="name">Name:</label>
64+
<input id="name" name="name" type="text" class="form-control" ng-model="person.name" required/>
65+
</div>
66+
67+
<div class="form-group">
68+
<label for="description">Description:</label>
69+
<input id="description" name="description" type="text" class="form-control" ng-model="person.description"/>
70+
</div>
71+
72+
<div class="form-group">
73+
<label for="link">Link:</label>
74+
<input id="link" name="link" type="url" class="form-control" ng-model="person.link"/>
75+
</div>
76+
77+
<div class="avatar" ng-if="person.link">
78+
<img src="{{person.link}}"/>
79+
</div>
80+
81+
<div class="buttons">
82+
<button type="button" class="btn btn-primary" ng-click="clearForm()">Clear</button>
83+
<button type="button" class="btn btn-primary" ng-click="deletePerson()">Delete</button>
84+
<button type="submit" class="btn btn-primary">Save</button>
85+
</div>
86+
</form>
87+
</div>
88+
</div>
89+
5290
</body>
5391
</html>

src/main/webapp/script/person.js

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var app = angular.module('persons', ['ngResource', 'ngGrid', 'ui.bootstrap']);
22

33
// Create a controller with name personsList to bind to the html page.
4-
app.controller('personsList', function ($scope, personService) {
4+
app.controller('personsListController', function ($scope, $rootScope, personService) {
55
// Refresh the grid, calling the appropriate service method.
66
$scope.refreshGrid = function () {
77
var listPersonsArgs = {
@@ -10,7 +10,7 @@ app.controller('personsList', function ($scope, personService) {
1010
sortDirections: $scope.sortInfo.directions[0]
1111
};
1212

13-
personService.get(listPersonsArgs, function(data) {
13+
personService.get(listPersonsArgs, function (data) {
1414
$scope.persons = data;
1515
})
1616
};
@@ -31,14 +31,79 @@ app.controller('personsList', function ($scope, personService) {
3131
// Initialize required information: sorting, the first page to show and the grid options.
3232
$scope.sortInfo = {fields: ['id'], directions: ['asc']};
3333
$scope.persons = {currentPage: 1};
34+
3435
$scope.gridOptions = {
3536
data: 'persons.list',
3637
useExternalSorting: true,
37-
sortInfo: $scope.sortInfo
38+
sortInfo: $scope.sortInfo,
39+
40+
columnDefs: [
41+
{ field: 'id', displayName:'Id' },
42+
{ field: 'name', displayName: 'Name' },
43+
{ field: 'description', displayName: 'Description' }
44+
],
45+
46+
multiSelect: false,
47+
selectedItems: [],
48+
afterSelectionChange: function (rowItem) {
49+
if (rowItem.selected) {
50+
$rootScope.$broadcast('personSelected', $scope.gridOptions.selectedItems[0].id);
51+
}
52+
}
53+
};
54+
55+
$scope.$on('personsDirty', function () {
56+
$scope.refreshGrid();
57+
});
58+
});
59+
60+
app.controller('personsFormController', function ($scope, $rootScope, personService) {
61+
$scope.clearForm = function () {
62+
$scope.person = null;
63+
};
64+
65+
$scope.$on('personSelected', function (event, id) {
66+
$scope.person = personService.get({id: id});
67+
});
68+
69+
$scope.updatePerson = function () {
70+
personService.save($scope.person).$promise.then(
71+
function () {
72+
$rootScope.$broadcast('personsDirty');
73+
$rootScope.$broadcast('personSaved')
74+
$scope.clearForm();
75+
});
76+
};
77+
78+
$scope.deletePerson = function () {
79+
personService.delete({id: $scope.person.id}).$promise.then(
80+
function () {
81+
$rootScope.$broadcast('personsDirty');
82+
$rootScope.$broadcast('personDeleted');
83+
$scope.clearForm();
84+
});
85+
};
86+
});
87+
88+
app.controller('alertMessagesController', function ($scope) {
89+
$scope.$on('personSaved', function (event) {
90+
$scope.alerts = [
91+
{ type: 'success', msg: 'Record saved successfully!' }
92+
];
93+
});
94+
95+
$scope.$on('personDeleted', function (event) {
96+
$scope.alerts = [
97+
{ type: 'success', msg: 'Record deleted successfully!' }
98+
];
99+
});
100+
101+
$scope.closeAlert = function(index) {
102+
$scope.alerts.splice(index, 1);
38103
};
39104
});
40105

41106
// Service that provides persons operations
42107
app.factory('personService', function ($resource) {
43-
return $resource('resources/persons');
108+
return $resource('resources/persons/:id');
44109
});

0 commit comments

Comments
 (0)