Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
daaexample
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Martín Vázquez Torres
daaexample
Commits
f2341cbc
Commit
f2341cbc
authored
Feb 15, 2017
by
cyanide4all
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pet page not working yet, but progress is made
parent
971471c3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
429 additions
and
22 deletions
+429
-22
PetDAO.java
src/main/java/es/uvigo/esei/daa/dao/PetDAO.java
+200
-0
Pet.java
src/main/java/es/uvigo/esei/daa/entities/Pet.java
+16
-16
PetResource.java
src/main/java/es/uvigo/esei/daa/rest/PetResource.java
+207
-0
pet.js
src/main/webapp/js/dao/pet.js
+1
-1
pet.html
src/main/webapp/pet.html
+5
-5
No files found.
src/main/java/es/uvigo/esei/daa/dao/PetDAO.java
0 → 100644
View file @
f2341cbc
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
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
es.uvigo.esei.daa.entities.Pet
;
/**
* DAO class for the {@link Pet} entities.
*
* @author Cya
*
*/
public
class
PetDAO
extends
DAO
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetDAO
.
class
.
getName
());
//TODO junto con lo de justo debajo
private
int
ownerID
=
1
;
public
int
getOwnerID
(){
return
ownerID
;
}
/*//TODO ESTO IGUAL NO FUNCIONA LO TESTEO LUEGO
public PetDAO(int ownerID){
this.ownerID = ownerID;
}
*/
/**
* Returns a pet stored persisted in the system.
*
* @param id identifier of the pet.
* @return a pet with the provided identifier.
* @throws DAOException if an error happens while retrieving the pet.
* @throws IllegalArgumentException if the provided id does not corresponds
* with any persisted pet.
*/
public
Pet
get
(
int
id
)
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pet WHERE id=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
if
(
result
.
next
())
{
return
rowToEntity
(
result
);
}
else
{
throw
new
IllegalArgumentException
(
"Invalid id"
);
}
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error getting a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
/**
* Returns a list with all the pets persisted in the system.
*
* @return a list with all the pet persisted in the system.
* @throws DAOException if an error happens while retrieving the pets.
*/
public
List
<
Pet
>
list
()
throws
DAOException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pet"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
final
List
<
Pet
>
petList
=
new
LinkedList
<>();
while
(
result
.
next
())
{
petList
.
add
(
rowToEntity
(
result
));
}
return
petList
;
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pets"
,
e
);
throw
new
DAOException
(
e
);
}
}
/**
* Persists a new pet in the system. An identifier will be assigned
* automatically to the new pet.
*
* @param name name of the new pet. Can't be {@code null}.
* @return a {@link Pet} entity representing the persisted pet.
* @throws DAOException if an error happens while persisting the new pet.
* @throws IllegalArgumentException if the name is {@code null}.
*/
public
Pet
add
(
String
name
)
throws
DAOException
,
IllegalArgumentException
{
if
(
name
==
null
)
{
throw
new
IllegalArgumentException
(
"name can't be null"
);
}
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"INSERT INTO pet VALUES(null, ?)"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
,
Statement
.
RETURN_GENERATED_KEYS
))
{
statement
.
setString
(
1
,
name
);
if
(
statement
.
executeUpdate
()
==
1
)
{
try
(
ResultSet
resultKeys
=
statement
.
getGeneratedKeys
())
{
if
(
resultKeys
.
next
())
{
return
new
Pet
(
resultKeys
.
getInt
(
1
),
name
,
ownerID
);
}
else
{
LOG
.
log
(
Level
.
SEVERE
,
"Error retrieving inserted id"
);
throw
new
SQLException
(
"Error retrieving inserted id"
);
}
}
}
else
{
LOG
.
log
(
Level
.
SEVERE
,
"Error inserting value"
);
throw
new
SQLException
(
"Error inserting value"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error adding a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
/**
* Modifies a pet previously persisted in the system. The pet will be
* retrieved by the provided id and its current name will be
* replaced with the provided.
*
* @param pet a {@link Pet} entity with the new data.
* @throws DAOException if an error happens while modifying the pet.
* @throws IllegalArgumentException if the pet is {@code null}.
*/
public
void
modify
(
Pet
pet
)
throws
DAOException
,
IllegalArgumentException
{
if
(
pet
==
null
)
{
throw
new
IllegalArgumentException
(
"pet can't be null"
);
}
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"UPDATE pet SET name=? WHERE id=?"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setString
(
1
,
pet
.
getName
());
statement
.
setInt
(
2
,
pet
.
getId
());
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"name and surname can't be null"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a pet"
,
e
);
throw
new
DAOException
();
}
}
/**
* Removes a persisted pet from the system.
*
* @param id identifier of the pet to be deleted.
* @throws DAOException if an error happens while deleting the pet.
* @throws IllegalArgumentException if the provided id does not corresponds
* with any persisted pet.
*/
public
void
delete
(
int
id
)
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"DELETE FROM pet WHERE id=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"Invalid id"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
private
Pet
rowToEntity
(
ResultSet
row
)
throws
SQLException
{
return
new
Pet
(
row
.
getInt
(
"id"
),
row
.
getString
(
"name"
),
//TODO EXTREMO ESTO ES PROVISIONAL
ownerID
//TODO REPITO PROVISIONAL MECA
);
}
}
src/main/java/es/uvigo/esei/daa/entities/Pet.java
View file @
f2341cbc
...
@@ -3,14 +3,14 @@ package es.uvigo.esei.daa.entities;
...
@@ -3,14 +3,14 @@ package es.uvigo.esei.daa.entities;
import
static
java
.
util
.
Objects
.
requireNonNull
;
import
static
java
.
util
.
Objects
.
requireNonNull
;
/**
/**
* Entity that represents a pet from a
person
* Entity that represents a pet from a
int
*
*
* Created by cya on 2/9/17.
* Created by cya on 2/9/17.
*/
*/
public
class
Pet
{
public
class
Pet
{
private
int
id
;
private
int
id
;
private
String
name
;
private
String
name
;
private
Person
owner
;
private
int
ownerID
;
// Constructor needed for the JSON conversion
// Constructor needed for the JSON conversion
Pet
()
{}
Pet
()
{}
...
@@ -20,12 +20,12 @@ public class Pet {
...
@@ -20,12 +20,12 @@ public class Pet {
*
*
* @param id identifier of the pet.
* @param id identifier of the pet.
* @param name name of the pet.
* @param name name of the pet.
* @param owner
person
who owns the pet.
* @param owner
ID int
who owns the pet.
*/
*/
public
Pet
(
int
id
,
String
name
,
Person
owner
)
{
public
Pet
(
int
id
,
String
name
,
int
ownerID
)
{
this
.
id
=
id
;
this
.
id
=
id
;
this
.
setName
(
name
);
this
.
setName
(
name
);
this
.
set
Owner
(
owner
);
this
.
set
ownerID
(
ownerID
);
}
}
/**
/**
...
@@ -47,30 +47,30 @@ public class Pet {
...
@@ -47,30 +47,30 @@ public class Pet {
}
}
/**
/**
* Set the name of this
person
.
* Set the name of this
int
.
*
*
* @param name the new name of the
person
.
* @param name the new name of the
int
.
* @throws NullPointerException if the {@code name} is {@code null}.
* @throws NullPointerException if the {@code name} is {@code null}.
*/
*/
public
void
setName
(
String
name
)
{
public
void
setName
(
String
name
)
{
this
.
name
=
requireNonNull
(
name
,
"Name can't be null"
);
this
.
name
=
requireNonNull
(
name
,
"Name can't be null"
);
}
}
/**
/**
* Returns the owner of the pet.
* Returns the owner
ID
of the pet.
*
*
* @return the owner of the pet.
* @return the owner
ID
of the pet.
*/
*/
public
Person
getOwner
()
{
public
int
getownerID
()
{
return
owner
;
return
owner
ID
;
}
}
/**
/**
* Set the owner of this pet.
* Set the owner
ID
of this pet.
*
*
* @param owner
the owner
of the pet.
* @param owner
ID the ownerID
of the pet.
* @throws NullPointerException if the {@code owner} is {@code null}.
* @throws NullPointerException if the {@code owner
ID
} is {@code null}.
*/
*/
public
void
set
Owner
(
Person
owner
)
{
public
void
set
ownerID
(
int
ownerID
)
{
this
.
owner
=
requireNonNull
(
owner
,
"Owner
can't be a null reference"
);
this
.
owner
ID
=
requireNonNull
(
ownerID
,
"ownerID
can't be a null reference"
);
}
}
}
}
src/main/java/es/uvigo/esei/daa/rest/PetResource.java
0 → 100644
View file @
f2341cbc
package
es
.
uvigo
.
esei
.
daa
.
rest
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javax.ws.rs.DELETE
;
import
javax.ws.rs.FormParam
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.PUT
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
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.PetDAO
;
import
es.uvigo.esei.daa.entities.Pet
;
/**
* REST resource for managing pets.
*
* @author Martín Vázquez Torres
*/
@Path
(
"/pet"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
PetResource
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetResource
.
class
.
getName
());
private
final
PetDAO
dao
;
/**
* Constructs a new instance of {@link PetResource}.
*/
public
PetResource
()
{
this
(
new
PetDAO
());
}
// Needed for testing purposes (parece ser)
PetResource
(
PetDAO
dao
)
{
this
.
dao
=
dao
;
}
/**
* Returns a pet with the provided identifier.
*
* @param id the identifier of the pet to retrieve.
* @return a 200 OK response with a pet that has the provided identifier.
* If the identifier does not corresponds with any user, a 400 Bad Request
* response with an error message will be returned. If an error happens
* while retrieving the list, a 500 Internal Server Error response with an
* error message will be returned.
*/
@GET
@Path
(
"/{id}"
)
public
Response
get
(
@PathParam
(
"id"
)
int
id
)
{
try
{
final
Pet
pet
=
this
.
dao
.
get
(
id
);
return
Response
.
ok
(
pet
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in get method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error getting a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Returns the complete list of the pets stored in the system.
*
* @return a 200 OK response with the complete list of pets stored in the
* system. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@GET
public
Response
list
()
{
try
{
return
Response
.
ok
(
this
.
dao
.
list
()).
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pets"
,
e
);
return
Response
.
serverError
().
entity
(
e
.
getMessage
()).
build
();
}
}
/**
* Creates a new pet in the system.
*
* @param name the name of the new pet.
* @return a 200 OK response with a pet that has been created. If the
* name is not provided, a 400 Bad Request response with an
* error message will be returned. If an error happens while retrieving the
* list, a 500 Internal Server Error response with an error message will be
* returned.
*/
@POST
public
Response
add
(
@FormParam
(
"name"
)
String
name
)
{
try
{
final
Pet
newPet
=
this
.
dao
.
add
(
name
);
return
Response
.
ok
(
newPet
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in add method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error adding a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Modifies the data of a pet.
*
* @param id identifier of the pet to modify.
* @param name the new name of the pet.
* @return a 200 OK response with a pet that has been modified. If the
* identifier does not corresponds with any user or the name is
* not provided, a 400 Bad Request response with an error message will be
* returned. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@PUT
@Path
(
"/{id}"
)
public
Response
modify
(
@PathParam
(
"id"
)
int
id
,
@FormParam
(
"name"
)
String
name
)
{
try
{
final
Pet
modifiedPet
=
new
Pet
(
id
,
name
,
dao
.
getOwnerID
());
this
.
dao
.
modify
(
modifiedPet
);
return
Response
.
ok
(
modifiedPet
).
build
();
}
catch
(
NullPointerException
npe
)
{
final
String
message
=
String
.
format
(
"Invalid data for pet (name: %s)"
,
name
);
LOG
.
log
(
Level
.
FINE
,
message
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
message
)
.
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in modify method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Deletes a pet from the system.
*
* @param id the identifier of the pet to be deleted.
* @return a 200 OK response with the identifier of the pet that has
* been deleted. If the identifier does not corresponds with any user, a 400
* Bad Request response with an error message will be returned. If an error
* happens while retrieving the list, a 500 Internal Server Error response
* with an error message will be returned.
*/
@DELETE
@Path
(
"/{id}"
)
public
Response
delete
(
@PathParam
(
"id"
)
int
id
)
{
try
{
this
.
dao
.
delete
(
id
);
return
Response
.
ok
(
id
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in delete method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
}
src/main/webapp/js/dao/pet.js
View file @
f2341cbc
...
@@ -12,7 +12,7 @@ var PetDAO = (function() {
...
@@ -12,7 +12,7 @@ var PetDAO = (function() {
};
};
function
PetDAO
()
{
function
PetDAO
()
{
this
.
listPet
=
function
(
done
,
fail
,
always
)
{
this
.
listPet
s
=
function
(
done
,
fail
,
always
)
{
requestByAjax
({
requestByAjax
({
url
:
resourcePath
,
url
:
resourcePath
,
type
:
'GET'
type
:
'GET'
...
...
src/main/webapp/pet.html
View file @
f2341cbc
...
@@ -14,11 +14,11 @@
...
@@ -14,11 +14,11 @@
<script
type=
"text/javascript"
src=
"js/dao/pet.js"
></script>
<script
type=
"text/javascript"
src=
"js/dao/pet.js"
></script>
<script
type=
"text/javascript"
src=
"js/view/pet.js"
></script>
<script
type=
"text/javascript"
src=
"js/view/pet.js"
></script>
<script
type=
"text/javascript"
>
<script
type=
"text/javascript"
>
$
(
document
).
ready
(
function
()
{
$
(
document
).
ready
(
function
()
{
var
view
=
new
PetView
(
new
PetDAO
(),
'pet-container'
,
'pet-container'
);
var
view
=
new
PetView
(
new
PetDAO
(),
'pet-container'
,
'pet-container'
);
view
.
init
();
view
.
init
();
});
});
</script>
</script>
</body>
</body>
</html>
</html>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment