diff --git a/.gitignore b/.gitignore
index 2f7896d1d1365eafb0da03d9fe456fac81408487..4cf3d323a22e0f4f23901c630207be13c83ef862 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,6 @@
-target/
+/bin
+/target
+/assembly
+.project
+.classpath
+.settings
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000000000000000000000000000000000000..e4fef9d4a3e1ad209bd9689107691de22468b410
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,64 @@
+
+ 4.0.0
+ es.uvigo.esei.daa
+ example
+ war
+ 0.0.1-SNAPSHOT
+ DAA Example
+
+
+
+ GNU GENERAL PUBLIC LICENSE, Version 3
+ http://www.gnu.org/licenses/gpl.html
+ repo
+
+
+
+
+ 1.7
+ 1.7
+
+
+
+
+ junit
+ junit
+ 4.11
+ test
+
+
+
+ org.glassfish.jersey.containers
+ jersey-container-servlet
+ 2.5.1
+
+
+
+ org.glassfish.jersey.media
+ jersey-media-json-jackson
+ 2.5.1
+
+
+
+ mysql
+ mysql-connector-java
+ 5.1.28
+
+
+
+
+ DAAExample
+
+
+
+ maven-war-plugin
+ org.apache.maven.plugins
+ 2.1.1
+
+ DAAExample
+
+
+
+
+
diff --git a/src/main/java/es/uvigo/esei/daa/dao/DAO.java b/src/main/java/es/uvigo/esei/daa/dao/DAO.java
new file mode 100644
index 0000000000000000000000000000000000000000..0b82b80182a2a29d12e4dd3fd3b6168a51acb4a9
--- /dev/null
+++ b/src/main/java/es/uvigo/esei/daa/dao/DAO.java
@@ -0,0 +1,29 @@
+package es.uvigo.esei.daa.dao;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+public abstract class DAO {
+ private final static String JNDI_NAME = "java:/comp/env/jdbc/daaexample";
+
+ private final DataSource dataSource;
+
+ public DAO() {
+ Context initContext;
+ try {
+ initContext = new InitialContext();
+ this.dataSource = (DataSource) initContext.lookup(JNDI_NAME);
+ } catch (NamingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected Connection getConnection() throws SQLException {
+ return this.dataSource.getConnection();
+ }
+}
diff --git a/src/main/java/es/uvigo/esei/daa/dao/DAOException.java b/src/main/java/es/uvigo/esei/daa/dao/DAOException.java
new file mode 100644
index 0000000000000000000000000000000000000000..fda528bfa123ec4ad6c272dc0e7235b205a64438
--- /dev/null
+++ b/src/main/java/es/uvigo/esei/daa/dao/DAOException.java
@@ -0,0 +1,25 @@
+package es.uvigo.esei.daa.dao;
+
+public class DAOException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public DAOException() {
+ }
+
+ public DAOException(String message) {
+ super(message);
+ }
+
+ public DAOException(Throwable cause) {
+ super(cause);
+ }
+
+ public DAOException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public DAOException(String message, Throwable cause,
+ boolean enableSuppression, boolean writableStackTrace) {
+ super(message, cause, enableSuppression, writableStackTrace);
+ }
+}
diff --git a/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java
new file mode 100644
index 0000000000000000000000000000000000000000..1c7ffe0cbfbf5b54fab59b65c099630fe51aa297
--- /dev/null
+++ b/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java
@@ -0,0 +1,96 @@
+package es.uvigo.esei.daa.dao;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.LinkedList;
+import java.util.List;
+
+import es.uvigo.esei.daa.entities.Person;
+
+public class PeopleDAO extends DAO {
+ public List list() throws DAOException {
+ try (final Connection conn = this.getConnection()) {
+ try (Statement statement = conn.createStatement()) {
+ try (ResultSet result = statement.executeQuery("SELECT * FROM people")) {
+ final List people = new LinkedList<>();
+
+ while (result.next()) {
+ people.add(new Person(
+ result.getInt("id"),
+ result.getString("name"),
+ result.getString("surname")
+ ));
+ }
+
+ return people;
+ }
+ }
+ } catch (SQLException e) {
+ throw new DAOException(e);
+ }
+ }
+
+ public void delete(int id) throws DAOException {
+ try (final Connection conn = this.getConnection()) {
+ final String query = "DELETE FROM people WHERE id=?";
+
+ try (PreparedStatement statement = conn.prepareStatement(query)) {
+ statement.setInt(1, id);
+
+ if (statement.executeUpdate() != 1) {
+ throw new SQLException("Error inserting value");
+ }
+ }
+ } catch (SQLException e) {
+ throw new DAOException(e);
+ }
+ }
+
+ public Person modify(int id, String name, String surname) throws DAOException {
+ try (final Connection conn = this.getConnection()) {
+ final String query = "UPDATE people SET name=?, surname=? WHERE id=?";
+
+ try (PreparedStatement statement = conn.prepareStatement(query)) {
+ statement.setString(1, name);
+ statement.setString(2, surname);
+ statement.setInt(3, id);
+
+ if (statement.executeUpdate() == 1) {
+ return new Person(id, name, surname);
+ } else {
+ throw new SQLException("Error inserting value");
+ }
+ }
+ } catch (SQLException e) {
+ throw new DAOException();
+ }
+ }
+
+ public Person add(String name, String surname) throws DAOException {
+ try (final Connection conn = this.getConnection()) {
+ final String query = "INSERT INTO people VALUES(null, ?, ?)";
+
+ try (PreparedStatement statement = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
+ statement.setString(1, name);
+ statement.setString(2, surname);
+
+ if (statement.executeUpdate() == 1) {
+ try (ResultSet resultKeys = statement.getGeneratedKeys()) {
+ if (resultKeys.next()) {
+ return new Person(resultKeys.getInt(1), name, surname);
+ } else {
+ throw new SQLException("Error retrieving inserted id");
+ }
+ }
+ } else {
+ throw new SQLException("Error inserting value");
+ }
+ }
+ } catch (SQLException e) {
+ throw new DAOException(e);
+ }
+ }
+}
diff --git a/src/main/java/es/uvigo/esei/daa/entities/Person.java b/src/main/java/es/uvigo/esei/daa/entities/Person.java
new file mode 100644
index 0000000000000000000000000000000000000000..d7926b91222444bb54547ae97dde4cb80278d272
--- /dev/null
+++ b/src/main/java/es/uvigo/esei/daa/entities/Person.java
@@ -0,0 +1,37 @@
+package es.uvigo.esei.daa.entities;
+
+public class Person {
+ private int id;
+ private String name;
+ private String surname;
+
+ public Person(int id, String name, String surname) {
+ this.id = id;
+ this.name = name;
+ this.surname = surname;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getSurname() {
+ return surname;
+ }
+
+ public void setSurname(String surname) {
+ this.surname = surname;
+ }
+}
diff --git a/src/main/java/es/uvigo/esei/daa/rest/People.java b/src/main/java/es/uvigo/esei/daa/rest/People.java
new file mode 100644
index 0000000000000000000000000000000000000000..9695cdf9acc8be7a972c8a1fec4dbfab668536bc
--- /dev/null
+++ b/src/main/java/es/uvigo/esei/daa/rest/People.java
@@ -0,0 +1,76 @@
+package es.uvigo.esei.daa.rest;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import es.uvigo.esei.daa.dao.DAOException;
+import es.uvigo.esei.daa.dao.PeopleDAO;
+
+@Path("/people")
+@Produces(MediaType.APPLICATION_JSON)
+public class People {
+ private final PeopleDAO dao;
+
+ public People() {
+ this.dao = new PeopleDAO();
+ }
+
+ @GET
+ @Path("/list")
+ public Response list() {
+ try {
+ return Response.ok(this.dao.list(), MediaType.APPLICATION_JSON).build();
+ } catch (DAOException e) {
+ e.printStackTrace();
+ return Response.serverError().entity(e.getMessage()).build();
+ }
+ }
+
+ @GET
+ @Path("/delete")
+ public Response delete(
+ @QueryParam("id") int id
+ ) {
+ try {
+ this.dao.delete(id);
+
+ return Response.ok(id).build();
+ } catch (DAOException e) {
+ e.printStackTrace();
+ return Response.serverError().entity(e.getMessage()).build();
+ }
+ }
+
+ @GET
+ @Path("/modify")
+ public Response modify(
+ @QueryParam("id") int id,
+ @QueryParam("name") String name,
+ @QueryParam("surname") String surname
+ ) {
+ try {
+ return Response.ok(this.dao.modify(id, name, surname)).build();
+ } catch (DAOException e) {
+ e.printStackTrace();
+ return Response.serverError().entity(e.getMessage()).build();
+ }
+ }
+
+ @GET
+ @Path("/add")
+ public Response add(
+ @QueryParam("name") String name,
+ @QueryParam("surname") String surname
+ ) {
+ try {
+ return Response.ok(this.dao.add(name, surname)).build();
+ } catch (DAOException e) {
+ e.printStackTrace();
+ return Response.serverError().entity(e.getMessage()).build();
+ }
+ }
+}
diff --git a/src/main/webapp/META-INF/MANIFEST.MF b/src/main/webapp/META-INF/MANIFEST.MF
new file mode 100644
index 0000000000000000000000000000000000000000..5e9495128c0376427420c4189993b3851770b702
--- /dev/null
+++ b/src/main/webapp/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path:
+
diff --git a/src/main/webapp/META-INF/context.xml b/src/main/webapp/META-INF/context.xml
new file mode 100644
index 0000000000000000000000000000000000000000..17a6b1d62d3ab7485f2dc0385f33bd5c0a9d55e2
--- /dev/null
+++ b/src/main/webapp/META-INF/context.xml
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000000000000000000000000000000000000..4a5a3c51e303666bb561a6bd5f2475ac72d57d7e
--- /dev/null
+++ b/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,25 @@
+
+
+ DAAExampleTMP
+
+ index.html
+ index.htm
+ index.jsp
+ default.html
+ default.htm
+ default.jsp
+
+
+ DAA Example DB Connection
+ jdbc/daaexample
+ javax.sql.DataSource
+ Container
+
+
+ javax.ws.rs.core.Application
+
+
+ javax.ws.rs.core.Application
+ /rest/*
+
+
\ No newline at end of file
diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html
new file mode 100644
index 0000000000000000000000000000000000000000..74acdfccfb257dfc41965a0fe151174a01e20b56
--- /dev/null
+++ b/src/main/webapp/index.html
@@ -0,0 +1,22 @@
+
+
+
+
+DAA Example
+
+
+
+
People
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/webapp/js/dao/people.js b/src/main/webapp/js/dao/people.js
new file mode 100644
index 0000000000000000000000000000000000000000..c75389dbedf6c6dd4cde76fa8318d5f2bf973f09
--- /dev/null
+++ b/src/main/webapp/js/dao/people.js
@@ -0,0 +1,43 @@
+function listPeople(done, fail, always) {
+ done = typeof done !== 'undefined' ? done : function() {};
+ fail = typeof fail !== 'undefined' ? fail : function() {};
+ always = typeof always !== 'undefined' ? always : function() {};
+
+ $.getJSON('rest/people/list')
+ .done(done)
+ .fail(fail)
+ .always(always);
+}
+
+function addPerson(person, done, fail, always) {
+ done = typeof done !== 'undefined' ? done : function() {};
+ fail = typeof fail !== 'undefined' ? fail : function() {};
+ always = typeof always !== 'undefined' ? always : function() {};
+
+ $.getJSON('rest/people/add', person)
+ .done(done)
+ .fail(fail)
+ .always(always);
+}
+
+function modifyPerson(person, done, fail, always) {
+ done = typeof done !== 'undefined' ? done : function() {};
+ fail = typeof fail !== 'undefined' ? fail : function() {};
+ always = typeof always !== 'undefined' ? always : function() {};
+
+ $.getJSON('rest/people/modify', person)
+ .done(done)
+ .fail(fail)
+ .always(always);
+}
+
+function deletePerson(id, done, fail, always) {
+ done = typeof done !== 'undefined' ? done : function() {};
+ fail = typeof fail !== 'undefined' ? fail : function() {};
+ always = typeof always !== 'undefined' ? always : function() {};
+
+ $.getJSON('rest/people/delete', { 'id': id })
+ .done(done)
+ .fail(fail)
+ .always(always);
+}
\ No newline at end of file
diff --git a/src/main/webapp/js/view/people.js b/src/main/webapp/js/view/people.js
new file mode 100644
index 0000000000000000000000000000000000000000..1885530f120dbb4eb61302a96c6234aa60d8e54e
--- /dev/null
+++ b/src/main/webapp/js/view/people.js
@@ -0,0 +1,152 @@
+var peopleFormId = 'people-form';
+var peopleListId = 'people-list';
+var peopleFormQuery = '#' + peopleFormId;
+var peopleListQuery = '#' + peopleListId;
+
+function insertPeopleList(parent) {
+ parent.append(
+ '\
+ \
+ Nombre | \
+ Apellido | \
+ | \
+ | \
+
\
+
'
+ );
+}
+
+function insertPeopleForm(parent) {
+ parent.append(
+ ''
+ );
+}
+
+function createPersonRow(person) {
+ return '\
+ ' + person.name + ' | \
+ ' + person.surname + ' | \
+ \
+ Edit\
+ | \
+ \
+ Delete\
+ | \
+
';
+}
+
+function formToPerson() {
+ var form = $(peopleFormQuery);
+ return {
+ 'id': form.find('input[name="id"]').val(),
+ 'name': form.find('input[name="name"]').val(),
+ 'surname': form.find('input[name="surname"]').val()
+ };
+}
+
+function rowToPerson(id) {
+ var row = $('#person-' + id);
+
+ return {
+ 'id': id,
+ 'name': row.find('td.name').text(),
+ 'surname': row.find('td.surname').text()
+ };
+}
+
+function personToForm(person) {
+ var form = $(peopleFormQuery);
+ form.find('input[name="id"]').val(person.id);
+ form.find('input[name="name"]').val(person.name);
+ form.find('input[name="surname"]').val(person.surname);
+}
+
+function isEditing() {
+ return $(peopleFormQuery + ' input[name="id"]').val() != "";
+}
+
+function disableForm() {
+ $(peopleFormQuery + ' input').prop('disabled', true);
+}
+
+function enableForm() {
+ $(peopleFormQuery + ' input').prop('disabled', false);
+}
+
+function showErrorMessage(jqxhr, textStatus, error) {
+ alert(textStatus + ": " + error);
+}
+
+function resetForm() {
+ $(peopleFormQuery)[0].reset();
+ $(peopleFormQuery + ' input[name="id"]').val('');
+ $('#btnSubmit').val('Crear');
+}
+
+function addRowListeners(person) {
+ $('#person-' + person.id + ' a.edit').click(function() {
+ personToForm(rowToPerson(person.id));
+ $('input#btnSubmit').val('Modificar');
+ });
+ $('#person-' + person.id + ' a.delete').click(function() {
+ if (confirm('Está a punto de eliminar a una persona. ¿Está seguro de que desea continuar?')) {
+ deletePerson(person.id,
+ function() {
+ $('tr#person-' + person.id).remove();
+ },
+ showErrorMessage
+ );
+ }
+ });
+}
+
+function appendToTable(person) {
+ $(peopleListQuery + ' > tbody:last')
+ .append(createPersonRow(person));
+ addRowListeners(person);
+}
+
+function initPeople() {
+ $.getScript('js/dao/people.js', function() {
+ listPeople(function(people) {
+ $.each(people, function(key, person) {
+ appendToTable(person);
+ });
+ });
+
+ $(peopleFormQuery).submit(function(event) {
+ var person = formToPerson();
+
+ if (isEditing()) {
+ modifyPerson(person,
+ function(person) {
+ $('#person-' + person.id + ' td.name').text(person.name);
+ $('#person-' + person.id + ' td.surname').text(person.surname);
+ resetForm();
+ },
+ showErrorMessage,
+ enableForm
+ );
+ } else {
+ addPerson(person,
+ function(person) {
+ appendToTable(person);
+ resetForm();
+ },
+ showErrorMessage,
+ enableForm
+ );
+ }
+
+ return false;
+ });
+
+ $('#btnClear').click(resetForm);
+ });
+};