Ejemplo n.º 1
1
  @Override
  public HomeInvite findHomeInviteById(int id) {
    String q = "find homeInvite where id = :id";

    Query<HomeInvite> query = ebean.createQuery(HomeInvite.class, q);
    query.setParameter("id", id);

    return query.findUnique();
  }
  public void convertNewDatabaseSystem() {
    if (existTable("playerstats")) {
      databaseServer.beginTransaction();
      try {
        final SqlUpdate convert =
            databaseServer.createSqlUpdate(
                "INSERT INTO player_stats "
                    + "(playername, kills, deaths, mobkills, killstreak, last_online) "
                    + "SELECT playername, kills, deaths, mobkills, killstreak, ? "
                    + "FROM playerstats");

        // sql parameters begins with one
        convert.setParameter(1, System.currentTimeMillis());
        final int affectedRows = convert.execute();
        final SqlUpdate dropTable = databaseServer.createSqlUpdate("DROP TABLE playerstats");
        dropTable.execute();
        // a drop have to commited and should be rollbackable if process wasn't complete
        databaseServer.commitTransaction();

        Logger.getLogger("ScoreboardStats")
            .log(Level.INFO, "Successfully converted {0} into new table structure", affectedRows);
      } finally {
        databaseServer.endTransaction();
      }
    }
  }
Ejemplo n.º 3
0
  private void findAndRateBeer(User user) {
    System.out.print("beer to find: ");
    String n = scanner.nextLine();
    Beer foundBeer = server.find(Beer.class).where().like("name", n).findUnique();

    if (foundBeer == null) {
      System.out.println(n + " not found");
      return;
    }
    int quantity = 0;
    int ratingsCombined = 0;
    int average = 0;
    List<Rating> ratings = server.find(Rating.class).where().findList();
    for (Rating rating : ratings) {
      if (rating.getBeer().getName().equals(n)) {
        ratingsCombined += rating.getRating();
        quantity++;
      }
    }
    if (quantity > 0) {
      average = ratingsCombined / quantity;
    }

    System.out.println("found: " + foundBeer);
    System.out.println("  number of ratings: " + quantity + " average " + average);
    System.out.print("give rating (leave empty if not): ");
    int ratingValue = scanner.nextInt();

    Rating rating = new Rating(user, foundBeer, ratingValue);
    server.save(rating);
  }
  @Test
  public void testCreateGenerator() throws Exception {

    EbeanServer server = Ebean.getDefaultServer();

    StringWriter writer = new StringWriter();
    JsonContext json = server.json();
    JsonGenerator generator = json.createGenerator(writer);

    Customer customer = new Customer();
    customer.setId(1);
    customer.setName("Jim");

    // we can use the generator before and after our json.toJson() call
    // ... confirming we are not closing the generator
    generator.writeStartArray();
    json.toJson(customer, generator, PathProperties.parse("id,name"));
    generator.writeEndArray();
    generator.close();

    String jsonString = writer.toString();
    assertTrue(jsonString, jsonString.startsWith("["));
    assertTrue(jsonString, jsonString.endsWith("]"));
    assertTrue(jsonString, jsonString.contains("{\"id\":1,\"name\":\"Jim\"}"));
  }
  @Test
  public void test_toObject() throws Exception {

    EbeanServer server = Ebean.getDefaultServer();

    JsonContext json = server.json();

    Customer customer = new Customer();
    customer.setId(1);
    customer.setName("Jim");

    String asJson = json.toJson(customer);

    Object bean = json.toObject(Customer.class, asJson);

    assertTrue(bean instanceof Customer);
    assertEquals(Integer.valueOf(1), ((Customer) bean).getId());
    assertEquals("Jim", ((Customer) bean).getName());

    StringReader reader = new StringReader(asJson);
    bean = json.toObject(Customer.class, reader);
    assertTrue(bean instanceof Customer);
    assertEquals(Integer.valueOf(1), ((Customer) bean).getId());
    assertEquals("Jim", ((Customer) bean).getName());
  }
Ejemplo n.º 6
0
 @Override
 public void deleteHomeInvite(HomeInvite homeInvite) throws StorageException {
   Transaction tx = ebean.beginTransaction();
   tx.setPersistCascade(false);
   ebean.delete(homeInvite, tx);
   tx.commit();
 }
  @Transactional
  public void doSomething(EBasicVer v) {

    EbeanServer server = Ebean.getServer(null);

    inMethodTransaction = server.currentTransaction();

    Transaction t = server.createTransaction();
    SqlUpdate u =
        server.createSqlUpdate("update e_basicver set last_update = last_update+1 where id = ?");
    u.setParameter(1, v.getId());
    int count = server.execute(u, t);
    Assert.assertEquals(1, count);

    t.commit();

    v.setName("some change");
    try {
      Ebean.save(v);
      // never get here
      Assert.assertTrue(false);
    } catch (PersistenceException e) {
      throw e;
    }
  }
  @Test
  public void testIsSupportedType() throws Exception {

    EbeanServer server = Ebean.getDefaultServer();

    JsonContext json = server.json();
    assertTrue(json.isSupportedType(Customer.class));
    assertFalse(json.isSupportedType(System.class));
  }
Ejemplo n.º 9
0
  private void deleteBrewery() {
    System.out.print("brewery to delete: ");
    String name = scanner.nextLine();
    Brewery foundBrewery = server.find(Brewery.class).where().like("name", name).findUnique();

    if (foundBrewery == null) {
      System.out.println(name + " not found");
    } else {
      server.delete(foundBrewery);
      System.out.println("brewery: " + name + " has been deleted");
    }
  }
Ejemplo n.º 10
0
  private void deleteBeer() {
    System.out.print("beer to delete: ");
    String n = scanner.nextLine();
    Beer beerToDelete = server.find(Beer.class).where().like("name", n).findUnique();

    if (beerToDelete == null) {
      System.out.println(n + " not found");
      return;
    }

    server.delete(beerToDelete);
    System.out.println("deleted: " + beerToDelete);
  }
Ejemplo n.º 11
0
  private void addPub() {
    System.out.print("pub to add: ");

    String name = scanner.nextLine();

    Pub exists = server.find(Pub.class).where().like("name", name).findUnique();
    if (exists != null) {
      System.out.println(name + " exists already");
      return;
    }

    server.save(new Pub(name));
  }
Ejemplo n.º 12
0
  private void addBrewery() {
    System.out.println("brewery name: ");
    String name = scanner.nextLine();

    Brewery brewery = server.find(Brewery.class).where().like("name", name).findUnique();

    if (brewery != null) {
      System.out.println(name + " already exists");
    } else {
      brewery = new Brewery(name);
      server.save(brewery);
      System.out.println("new brewery: " + name + " created");
    }
  }
 @Test
 public void testList() throws Exception {
   query = mock(Query.class, RETURNS_DEEP_STUBS);
   when(database.createQuery(InetAddressRecord.class)).thenReturn(query);
   Assert.assertNotNull("Returned list is inconsistent!", manager.list());
   verify(query, times(1)).findList();
 }
 @Before
 public void setUp() throws Exception {
   database = mock(EbeanServer.class);
   query = mock(Query.class);
   when(database.createQuery(InetAddressRecord.class)).thenReturn(query);
   manager = new InetAddressRecordManager(database);
 }
Ejemplo n.º 15
0
  @Ignore
  @Test
  public void testBulkUpdate() throws IOException {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);
    JsonContext jsonContext = server.json();

    StringWriter writer = new StringWriter();
    JsonGenerator generator = jsonContext.createGenerator(writer);

    List<Customer> customers = Ebean.find(Customer.class).findList();

    for (Customer customer : customers) {

      customer.setName(customer.getName() + "esMod");
      PathProperties updateProps = PathProperties.parse("name");

      generator.writeStartObject();
      generator.writeFieldName("update");
      generator.writeStartObject();
      generator.writeStringField("_id", customer.getId().toString());
      generator.writeStringField("_type", "customer");
      generator.writeStringField("_index", "customer");
      generator.writeEndObject();
      generator.writeEndObject();
      generator.writeRaw("\n");

      generator.writeStartObject();
      generator.writeFieldName("doc");
      jsonContext.toJson(customer, generator, updateProps);
      generator.writeEndObject();
      generator.writeRaw("\n");
    }

    generator.close();
    String json = writer.toString();

    post("http://localhost:9200/_bulk", json);

    // curl -s -XPOST localhost:9200/_bulk

    //    { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }
    //    { "doc" : {"field2" : "value2"} }

  }
Ejemplo n.º 16
0
  @Override
  public Set<HomeInvite> findInvitesByHome(Home home) {
    String q = "find homeInvite where home = :home";
    Query<HomeInvite> query = ebean.createQuery(HomeInvite.class, q);
    query.setParameter("home", home.getId());

    return query.findSet();
  }
Ejemplo n.º 17
0
 void addSign(TeleportSign ts) {
   signs.add(ts);
   database.save(ts);
   if (debugmode)
     Bukkit.getLogger()
         .log(
             Level.INFO, "[TeleportSigns] Saved TeleportSign at {0}", ts.getLocation().toString());
 }
Ejemplo n.º 18
0
  private void createNewUser() {
    System.out.print("give username: "******"user created");
  }
Ejemplo n.º 19
0
  @Ignore
  @Test
  public void test() throws IOException {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);
    JsonContext jsonContext = server.json();

    List<Customer> customers = Ebean.find(Customer.class).findList();

    PathProperties paths = PathProperties.parse("name, status, anniversary");
    for (Customer customer : customers) {

      String json = jsonContext.toJson(customer, paths);
      put("http://localhost:9200/customer/customer/" + customer.getId(), json);
    }
  }
Ejemplo n.º 20
0
 public static Official findByNumber(String number, ProcessingType processingType) {
   Official official;
   Query<Official> query = null;
   if (processingType.equals(ProcessingType.batch)) query = ebeanServer.find(Official.class);
   else if (processingType.equals(ProcessingType.online)) query = Ebean.find(Official.class);
   query.where().eq("number", number);
   official = query.findUnique();
   return official;
 }
 private boolean existTable(String tableName) {
   try {
     final SqlQuery query =
         databaseServer.createSqlQuery(TEST_TABLE.replace("%table%", tableName));
     query.findUnique();
     return true;
   } catch (PersistenceException exception) {
     return false;
   }
 }
Ejemplo n.º 22
0
  private void listBeers() {
    List<Brewery> breweries = server.find(Brewery.class).findList();

    for (Brewery brewery : breweries) {
      List<Beer> beers = brewery.getBeers();
      for (Beer beer : beers) {
        System.out.println(beer.getName());
      }
    }
  }
Ejemplo n.º 23
0
 void removeSign(TeleportSign ts) {
   signs.remove(ts);
   database.delete(ts);
   if (debugmode)
     Bukkit.getLogger()
         .log(
             Level.INFO,
             "[TeleportSigns] Removed TeleportSign at {0}",
             ts.getLocation().toString());
 }
Ejemplo n.º 24
0
  private void listPubsAndBeers() {
    List<Pub> pubs = server.find(Pub.class).findList();

    for (Pub pub : pubs) {
      System.out.println(pub.getName());
      for (Beer beer : pub.getBeers()) {
        System.out.println("  -" + beer.getName());
      }
    }
  }
Ejemplo n.º 25
0
  private void removeBeerFromPub() {
    System.out.print("which pub: ");
    String pub = scanner.nextLine();
    Pub foundPub = server.find(Pub.class).where().like("name", pub).findUnique();
    if (foundPub == null) {
      System.out.println("pub doesn't exist");
      return;
    }

    System.out.print("which beer:");
    String beer = scanner.nextLine();
    Beer foundBeer = server.find(Beer.class).where().like("name", beer).findUnique();
    if (foundBeer == null) {
      System.out.println("Beer doesn't exist");
      return;
    }
    foundPub.removeBeer(foundBeer);
    server.save(foundPub);
  }
  private boolean isAnyConsumption(DateTime date, Goods goods) {
    int size =
        server
            .find(GoodsConsumption.class)
            .where()
            .gt("consumptionDate", date.toDate())
            .eq("goods", goods)
            .findRowCount();

    return size > 0 ? true : false;
  }
Ejemplo n.º 27
0
  private User login(String firstCommand) {
    User foundUser = server.find(User.class).where().like("username", firstCommand).findUnique();

    if (foundUser == null) {
      System.out.println("username " + firstCommand + " does not exist");
      return null;
    } else {
      System.out.println("Welcome to ratebeer " + firstCommand + " !");
      return foundUser;
    }
  }
Ejemplo n.º 28
0
 public static Official findByName(
     String lastName, String firstName, ProcessingType processingType) {
   Official official;
   Query<Official> query = null;
   if (processingType.equals(ProcessingType.batch)) query = ebeanServer.find(Official.class);
   else if (processingType.equals(ProcessingType.online)) query = Ebean.find(Official.class);
   query.where().eq("lastName", lastName);
   query.where().eq("firstName", firstName);
   official = query.findUnique();
   return official;
 }
  private boolean isAnyDeletion(DateTime date, Goods goods) {

    int size =
        server
            .find(DeletedGoods.class)
            .where()
            .eq("goods", goods)
            .gt("approvalDate", date.toDate())
            .eq("acceptance", accept.getAcceptedByAllCriteria())
            .findRowCount();
    return size > 0 ? true : false;
  }
 /** {@inheritDoc} */
 @Override
 @Transactional
 public Session create(String name, String description, String estimations) {
   Session session = new Session();
   session.setName(name);
   session.setDescription(description);
   session.setEstimates(estimations);
   session.setCode(newCode(SESSION_CODE_LENGTH.asInt().or(SESSION_CODE_DEFAULT_LENGTH)));
   session.setAuthor(userService.getCurrentUser());
   ebean.save(session);
   return session;
 }