Advertisement
papyhardcore

Role : je JAVA c'est le mal m'voyez

Sep 4th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. //Mon code ce matin fonctionnait si je ne prennait pas en compte l'ObjectId
  2. //Je le jure sur la tête du lapin de ma copine que ça fonctionnait !!!!
  3.  
  4. Ma classe role sans id :
  5. //---------------------------------------------------------------------------
  6. package com.aerolitec.entities;
  7.  
  8.  
  9. import com.aerolitec.database.Db;
  10. import com.google.gson.Gson;
  11. import com.mongodb.BasicDBObject;
  12. import com.mongodb.DBCollection;
  13. import com.mongodb.DBCursor;
  14. import com.mongodb.DBObject;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17.  
  18.  
  19. /**
  20.  * Created by Damien on 25/08/2014.
  21.  */
  22. //Admin,User,etc....
  23.  
  24. public class Role {
  25.  
  26.  
  27.     private String name;
  28.     private String description;
  29.  
  30.     public Role() {
  31.     }
  32.  
  33.     public boolean add(){
  34.         DBCollection coll = Db.databse.getCollection("Roles");
  35.         DBObject r = new BasicDBObject();
  36.         r.put("name",this.getName());
  37.         DBObject result =  coll.findOne(r);
  38.  
  39.         if(result != null){
  40.             System.out.print("Nop");
  41.             return false;
  42.         }else{
  43.             r.put("description",this.getDescription());
  44.             coll.save(r);
  45.             return true;
  46.         }
  47.     }
  48.  
  49.  
  50.  
  51.     public static List<Role> getAll(){
  52.         Gson gson = new Gson();
  53.         List<Role> toReturn = new ArrayList<Role>();
  54.         DBCollection coll = Db.databse.getCollection("Roles");
  55.         DBCursor cursor = coll.find();
  56.         try {
  57.             while(cursor.hasNext()) {
  58.                 System.out.print(cursor.next());
  59.                 Role r = gson.fromJson(cursor.next().toString(),Role.class);
  60.  
  61.                 toReturn.add(r);
  62.             }
  63.         } finally {
  64.             cursor.close();
  65.         }
  66.         return toReturn;
  67.  
  68.  
  69.     }
  70.  
  71.  
  72.  
  73.  
  74.     public String getName() {
  75.         return name;
  76.     }
  77.  
  78.     public void setName(String name) {
  79.         this.name = name;
  80.     }
  81.  
  82.     public String getDescription() {
  83.         return description;
  84.     }
  85.  
  86.     public void setDescription(String description) {
  87.         this.description = description;
  88.     }
  89.  
  90.  
  91. }
  92.  
  93. //----------------------------------------------------------
  94.  
  95. Mon servlet pour gérer les roles :
  96.  
  97. //----------------------------------------------------------
  98. package com.aerolitec.servlets;
  99.  
  100. import com.aerolitec.entities.Role;
  101. import com.google.gson.Gson;
  102. import javax.ws.rs.*;
  103. import javax.ws.rs.core.Response;
  104. import java.util.ArrayList;
  105. import java.util.List;
  106.  
  107. /**
  108.  * Created by Damien on 29/08/2014.
  109.  */
  110. @Path("/role")
  111. public class RoleServlet {
  112.  
  113.     @POST
  114.     @Path("/add")
  115.     public Response add(@FormParam("name") String name, @FormParam("description") String description){
  116.         if(name != null){
  117.             Role r_to_rec = new Role();
  118.             r_to_rec.setName(name);
  119.             if(description != null){
  120.                r_to_rec.setDescription(description);
  121.             }
  122.             if(r_to_rec.add()){
  123.                 return Response.status(201).build();
  124.             }else{
  125.                 return Response.status(418).build();
  126.             }
  127.         }else{
  128.             return Response.status(404).build();
  129.         }
  130.  
  131.     }
  132.  
  133.     @GET
  134.     @Path("/")
  135.     public Response getAll(){
  136.         List<Role> roleList = new ArrayList<Role>();
  137.         roleList = Role.getAll();
  138.         String json = new Gson().toJson(roleList);
  139.         return Response.status(200).entity(json).build();
  140.     }
  141.  
  142.  
  143.  
  144.  
  145.  
  146. }
  147.  
  148. //-------------------------------------------------------------------
  149.  
  150. Le /add fonctionne trés bien.
  151. Le / en get ne fonctionne plus.
  152. Je pense que c'est coté GSON.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement