Commit 575ff2e829e2f445933ceddadfaddba13ecb5637

Authored by msahmane
1 parent 24e5d678

Examen terminé.

src/main/java/fr/plil/sio/examen/api/Animal.java
1 1 package fr.plil.sio.examen.api;
2 2  
3 3 import java.io.Serializable;
  4 +import java.util.LinkedList;
  5 +import java.util.List;
4 6 import javax.persistence.Column;
5 7 import javax.persistence.Entity;
6 8 import javax.persistence.GeneratedValue;
7 9 import javax.persistence.GenerationType;
8 10 import javax.persistence.Id;
9 11 import javax.persistence.ManyToOne;
  12 +import javax.persistence.OneToMany;
10 13  
11 14 /**
12 15 * An animal must have a name one and only one owner
... ... @@ -24,6 +27,10 @@ public class Animal implements Serializable {
24 27 @ManyToOne(optional = false)
25 28 private Owner owner;
26 29  
  30 + @OneToMany(mappedBy = "reporter")
  31 + List<Comment> comments = new LinkedList<>();
  32 +
  33 +
27 34 public Long getId() {
28 35 return id;
29 36 }
... ... @@ -48,5 +55,13 @@ public class Animal implements Serializable {
48 55 this.owner = owner;
49 56 }
50 57  
  58 + public List<Comment> getComments(){
  59 + return this.comments;
  60 + }
  61 +
  62 + public void setComments(List<Comment> comments){
  63 + this.comments = comments;
  64 + }
  65 +
51 66  
52 67 }
... ...
src/main/java/fr/plil/sio/examen/api/Comment.java
1 1 package fr.plil.sio.examen.api;
2 2  
  3 +import javax.persistence.Column;
  4 +import javax.persistence.Entity;
  5 +import javax.persistence.GeneratedValue;
  6 +import javax.persistence.GenerationType;
  7 +import javax.persistence.Id;
  8 +import javax.persistence.JoinColumn;
  9 +import javax.persistence.ManyToOne;
  10 +
3 11 /**
4 12 * A comment is made by an reporter (i.e. an owner) on ANY animal
5 13 * (not only the ones he owns).
... ... @@ -11,11 +19,52 @@ package fr.plil.sio.examen.api;
11 19 * TODO: complete the classes in API to support comment serialization in the
12 20 * database.
13 21 */
  22 +@Entity
14 23 public class Comment {
15 24  
  25 + @Id
  26 + @GeneratedValue(strategy = GenerationType.AUTO)
  27 + private Long id;
  28 +
  29 + @ManyToOne(optional = false)
16 30 private Owner reporter;
17 31  
  32 + @ManyToOne(optional = false)
18 33 private Animal animal;
19 34  
  35 + @Column(nullable = false)
20 36 private String message;
  37 +
  38 + public Long getId() {
  39 + return id;
  40 + }
  41 +
  42 + public void setId(Long id) {
  43 + this.id = id;
  44 + }
  45 +
  46 + public Owner getReporter() {
  47 + return this.reporter;
  48 + }
  49 +
  50 + public void setReporter(Owner owner) {
  51 + this.reporter = owner;
  52 + }
  53 +
  54 + public Animal getAnimal() {
  55 + return animal;
  56 + }
  57 +
  58 + public void setAnimal(Animal animal) {
  59 + this.animal = animal;
  60 + }
  61 +
  62 + public String getMessage(){
  63 + return this.message;
  64 + }
  65 +
  66 + public void setMessage(String message){
  67 + this.message = message;
  68 + }
  69 +
21 70 }
... ...
src/main/java/fr/plil/sio/examen/api/Owner.java
... ... @@ -25,6 +25,9 @@ public class Owner {
25 25 @OneToMany(mappedBy = "owner")
26 26 private List<Animal> animals = new LinkedList<>();
27 27  
  28 + @OneToMany(mappedBy = "reporter")
  29 + private List<Comment> comments = new LinkedList<>();
  30 +
28 31 public Long getId() {
29 32 return id;
30 33 }
... ... @@ -48,4 +51,12 @@ public class Owner {
48 51 public void setAnimals(List<Animal> animals) {
49 52 this.animals = animals;
50 53 }
  54 +
  55 + public List<Comment> getComments(){
  56 + return this.comments;
  57 + }
  58 +
  59 + public void setComments(List<Comment> comments){
  60 + this.comments = comments;
  61 + }
51 62 }
... ...
src/main/java/fr/plil/sio/examen/repositories/CommentRepository.java
1 1 package fr.plil.sio.examen.repositories;
2 2  
  3 +import fr.plil.sio.examen.api.Animal;
3 4 import fr.plil.sio.examen.api.Comment;
  5 +import fr.plil.sio.examen.api.Owner;
  6 +import java.util.List;
4 7 import org.springframework.data.jpa.repository.JpaRepository;
5 8  
6 9 /**
7 10 * TODO: complete this interface for new find methods.
8 11 */
9 12 public interface CommentRepository extends JpaRepository<Comment, Long>{
  13 +
  14 + /**
  15 + * List all messages about an animal
  16 + */
  17 + List<Comment> findByAnimal(Animal animal);
  18 +
  19 + /**
  20 + * List all messages reported by one owner
  21 + */
  22 + List<Comment> findByReporter(Owner reporter);
  23 +
  24 +
  25 +
10 26 }
... ...
src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java
1 1 package fr.plil.sio.examen.services;
2 2  
3 3 import fr.plil.sio.examen.api.Animal;
  4 +import fr.plil.sio.examen.api.Comment;
4 5 import fr.plil.sio.examen.api.Owner;
5 6 import fr.plil.sio.examen.repositories.AnimalRepository;
  7 +import fr.plil.sio.examen.repositories.CommentRepository;
6 8 import org.springframework.beans.factory.annotation.Autowired;
7 9 import org.springframework.stereotype.Service;
8 10 import org.springframework.transaction.annotation.Transactional;
... ... @@ -12,6 +14,9 @@ public class AnimalServiceImpl implements AnimalService {
12 14  
13 15 @Autowired
14 16 private AnimalRepository animalRepository;
  17 +
  18 + @Autowired
  19 + private CommentRepository commentRepository;
15 20  
16 21 @Override
17 22 @Transactional
... ... @@ -33,7 +38,16 @@ public class AnimalServiceImpl implements AnimalService {
33 38 @Override
34 39 @Transactional
35 40 public void delete(Animal animal) {
36   - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  41 + if(animal == null)
  42 + throw new IllegalArgumentException("Animal must not be null");
  43 +
  44 + if(animal.getId() == null)
  45 + throw new IllegalArgumentException("Animal doesn't exist");
  46 +
  47 + for(Comment c : animal.getComments())
  48 + commentRepository.delete(c);
  49 +
  50 + animalRepository.delete(animal);
37 51 }
38 52  
39 53 }
... ...
src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java
... ... @@ -3,7 +3,10 @@ package fr.plil.sio.examen.services;
3 3 import fr.plil.sio.examen.api.Animal;
4 4 import fr.plil.sio.examen.api.Comment;
5 5 import fr.plil.sio.examen.api.Owner;
  6 +import fr.plil.sio.examen.repositories.CommentRepository;
  7 +import java.util.LinkedList;
6 8 import java.util.List;
  9 +import org.springframework.beans.factory.annotation.Autowired;
7 10 import org.springframework.stereotype.Service;
8 11 import org.springframework.transaction.annotation.Transactional;
9 12  
... ... @@ -13,34 +16,78 @@ import org.springframework.transaction.annotation.Transactional;
13 16 @Service
14 17 public class CommentServiceImpl implements CommentService {
15 18  
  19 + @Autowired
  20 + private CommentRepository commentRepository;
  21 +
16 22 @Override
17 23 @Transactional
18 24 public Comment add(Owner owner, Animal animal, String text) {
19   - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  25 + if(owner == null)
  26 + throw new IllegalArgumentException("Owner cannot be null");
  27 + if(animal == null)
  28 + throw new IllegalArgumentException("Animal Cannot be null");
  29 + if(text == null)
  30 + throw new IllegalArgumentException("Text cannot be null");
  31 +
  32 + Comment comment = new Comment();
  33 + comment.setReporter(owner);
  34 + comment.setAnimal(animal);
  35 + comment.setMessage(text);
  36 + commentRepository.save(comment);
  37 +
  38 + return comment;
20 39 }
21 40  
22 41 @Override
23 42 @Transactional
24 43 public void delete(Comment comment) {
25   - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  44 + if(comment == null)
  45 + throw new IllegalArgumentException("comment cannot be null");
  46 + if(comment.getId() == null)
  47 + throw new IllegalArgumentException("Comment doesn't exists");
  48 +
  49 + commentRepository.delete(comment);
26 50 }
27 51  
28 52 @Override
29 53 @Transactional(readOnly = true)
30 54 public List<Comment> findByAnimal(Animal animal) {
31   - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  55 + if(animal == null)
  56 + throw new IllegalArgumentException("Animal cannot be null");
  57 + if(animal.getId() == null)
  58 + throw new IllegalArgumentException("Animal doesn't exists");
  59 +
  60 + return commentRepository.findByAnimal(animal);
32 61 }
33 62  
34 63 @Override
35 64 @Transactional(readOnly = true)
36 65 public List<Comment> findByReporter(Owner reporter) {
37   - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  66 + if(reporter == null)
  67 + throw new IllegalArgumentException("reporter cannot be null");
  68 + if(reporter.getId() == null)
  69 + throw new IllegalArgumentException("reporter doesn't exists");
  70 +
  71 + return commentRepository.findByReporter(reporter);
38 72 }
39 73  
40 74 @Override
41 75 @Transactional(readOnly = true)
42 76 public List<Comment> findByOwner(Owner owner) {
43   - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  77 + if(owner == null)
  78 + throw new IllegalArgumentException("owner cannot be null");
  79 + if(owner.getId() == null)
  80 + throw new IllegalArgumentException("owner doesn't exists");
  81 +
  82 + List<Animal> animalsOfOwner = owner.getAnimals();
  83 + List<Comment> comments = new LinkedList<>();
  84 +
  85 + for(Animal a : animalsOfOwner){
  86 + comments.addAll(a.getComments());
  87 + }
  88 +
  89 + return comments;
  90 +
44 91 }
45 92  
46 93 }
... ...
src/main/java/fr/plil/sio/examen/services/OwnerServiceImpl.java
1 1 package fr.plil.sio.examen.services;
2 2  
  3 +import fr.plil.sio.examen.api.Comment;
3 4 import fr.plil.sio.examen.api.Owner;
  5 +import fr.plil.sio.examen.repositories.CommentRepository;
4 6 import fr.plil.sio.examen.repositories.OwnerRepository;
5 7 import org.springframework.beans.factory.annotation.Autowired;
6 8 import org.springframework.stereotype.Service;
... ... @@ -10,10 +12,22 @@ public class OwnerServiceImpl implements OwnerService {
10 12  
11 13 @Autowired
12 14 private OwnerRepository ownerRepository;
  15 +
  16 + @Autowired
  17 + private CommentRepository commentRepository;
13 18  
14 19 @Override
15 20 public void delete(Owner owner) {
16   - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  21 + if(owner == null)
  22 + throw new IllegalArgumentException("Owner must be not null");
  23 +
  24 + if(owner.getId() == null)
  25 + throw new IllegalArgumentException("Owner doesn't exist");
  26 +
  27 + for(Comment c : owner.getComments())
  28 + commentRepository.delete(c);
  29 +
  30 + ownerRepository.delete(owner);
17 31 }
18 32  
19 33 @Override
... ...