Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.21 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package beans;
  7.  
  8. import entities.*;
  9. import enums.RolesEnum;
  10. import java.sql.Date;
  11. import javax.ejb.Stateless;
  12. import javax.persistence.EntityManager;
  13. import javax.persistence.PersistenceContext;
  14.  
  15. /**
  16.  *
  17.  * @author Jons
  18.  */
  19. @Stateless
  20. public class InitializeDatabaseBean implements InitializeDatabaseBeanRemote {
  21.  
  22.     @PersistenceContext(unitName = "SLIT_v02-ejbPU")
  23.     private EntityManager em;
  24.  
  25.     public void createModule(String name, String description) {
  26.         Module myModule = new Module();
  27.         myModule.setDescription(description);
  28.         myModule.setName(name);
  29.         em.persist(myModule);
  30.     }
  31.  
  32.     public void createRole(int id, String name, String description) {
  33.         AvailableRoles myRole = new AvailableRoles(id);
  34.         myRole.setDescription(description);
  35.         myRole.setName(name);
  36.         em.persist(myRole);
  37.     }
  38.  
  39.     public void createUser(String firstName, String lastName, String email) {
  40.         Users myUser = new Users();
  41.         myUser.setEmail(email);
  42.         myUser.setFirstname(firstName);
  43.         myUser.setLastname(lastName);
  44.         em.persist(myUser);
  45.     }
  46.  
  47.     public void createRessource(String name, byte[] blob) {
  48.         Ressource myRessource = new Ressource();
  49.         myRessource.setFile(blob);
  50.         myRessource.setName(name);
  51.         em.persist(myRessource);
  52.     }
  53.  
  54.     public void persist(Object object) {
  55.         em.persist(object);
  56.     }
  57.  
  58.     /**
  59.      * initialize the database with some random values
  60.      */
  61.     @Override
  62.     public void createDatabase() {
  63.         createUsers();
  64.         createModules();
  65.         createRessources();
  66.         em.flush();
  67.         createRoles();
  68.         em.flush();
  69.         addRoleToUser();
  70.         addProgress();
  71.  
  72.     }
  73.  
  74.     private void addRoleToUser() {
  75.         Users testUser = em.find(Users.class, 7);
  76.         AvailableRoles role = em.find(AvailableRoles.class, RolesEnum.Teacher.ordinal());
  77.         AvailableRoles role2 = em.find(AvailableRoles.class, RolesEnum.Admin.ordinal());
  78.        
  79.         testUser.getAvailableRolesCollection().add(role2);
  80.         testUser.getAvailableRolesCollection().add(role);
  81.        
  82.         em.persist(testUser);
  83.     }
  84.  
  85.     private void createModules() {
  86.         System.out.println("Creating modules.....");
  87.         for (int i = 1; i <= 14; i++) {
  88. //            if(em.find(AvailableRoles.class, i) == null)
  89.             createModule("Module " + i, "Description of Module " + i + " here.");
  90.         }
  91.     }
  92.  
  93.     private void createUsers() {
  94.         System.out.println("Creating users.....");
  95.         createUser("Jonas", "Hinrichs", "jh@mail.de");
  96.         createUser("Peter", "G", "PG@mail.de");
  97.         createUser("Simen", "F", "SF@mail.de");
  98.         createUser("Jørgen", "W", "JW@mail.de");
  99.         createUser("Gabi", "H", "GH@mail.de");
  100.         createUser("Helle", "D", "HD@mail.de");
  101.         createUser("Helle", "S", "HS@mail.de");
  102.         createUser("Lena", "D", "LD@mail.de");
  103.         createUser("Olaf", "V", "OV@mail.de");
  104.         createUser("Niels", "S", "NS@mail.de");
  105.         createUser("Even", "A", "EA@mail.de");
  106.     }
  107.  
  108.     private void createRoles() {
  109.         System.out.println("Creating roles.....");
  110.         for (RolesEnum e : RolesEnum.values()){
  111.             createRole(e.ordinal(), e.name(), e.getDescription() );
  112.         }
  113.     }
  114.  
  115.     private void createRessources() {
  116.         System.out.println("Creating resources.....");
  117.         createRessource("The Story of everything", "www.wikipedia.de".getBytes());
  118.         createRessource("little Ressource text", "This ressource was written by Jonas H. and is probably the best ressource in the world wide web.".getBytes());
  119.         createRessource("Grammas Apple Pie", ("Put a layer of paper towels on a large baking sheet. Quarter, core, peel and slice the apples about 5mm thick and lay evenly on the baking sheet. Put paper towels on top and set aside while you make and chill the pastry.\n"
  120.                 + "    For the pastry, beat the butter and sugar in a large bowl until just mixed. Break in a whole egg and a yolk (keep the white for glazing later). Beat together for just under 1 min – it will look a bit like scrambled egg. Now work in the flour with a wooden spoon, a third at a time, until it’s beginning to clump up, then finish gathering it together with your hands. Gently work the dough into a ball, wrap in cling film, and chill for 45 mins. Now mix the 140g/5oz sugar, the cinnamon and flour for the filling in a bowl that is large enough to take the apples later.\n"
  121.                 + "    After the pastry has chilled, heat the oven to 190C/fan 170C/gas 5. Lightly beat the egg white with a fork. Cut off a third of the pastry and keep it wrapped while you roll out the rest, and use this to line a pie tin – 20-22cm round and 4cm deep – leaving a slight overhang. Roll the remaining third to a circle about 28cm in diameter. Pat the apples dry with kitchen paper, and tip them into the bowl with the cinnamon-sugar mix. Give a quick mix with your hands and immediately pile high into the pastry-lined tin.\n"
  122.                 + "    Brush a little water around the pastry rim and lay the pastry lid over the apples pressing the edges together to seal. Trim the edge with a sharp knife and make 5 little slashes on top of the lid for the steam to escape. (Can be frozen at this stage.) Brush it all with the egg white and sprinkle with caster sugar. Bake for 40-45 mins, until golden, then remove and let it sit for 5-10 mins. Sprinkle with more sugar and serve while still warm from the oven with softly whipped cream.").getBytes());
  123.  
  124.     }
  125.  
  126.     private void addProgress() {
  127.        
  128.         File myFile = new File();
  129.         myFile.setName("File for progress");
  130.         Date uploadDate = new Date(System.currentTimeMillis());
  131.         myFile.setUploadDate(uploadDate);
  132.         em.persist(myFile);
  133.        
  134.        
  135.         Progress myProgress = new Progress();
  136.         myProgress.setModule(em.find(Module.class, 1));
  137.         myProgress.setUser(em.find(Users.class, 7));
  138.         em.persist(myProgress);
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement