Ejemplo n.º 1
0
/** User test utility */
public class TestUser {

  private static TestUser instance;

  private static User user1;

  // Date are initialized with java.sql.Timestamp as JPA get a Timestamp instance
  private static final Date now = Timestamp.from(ZonedDateTime.now().toInstant());
  private static final Date yesterday =
      Timestamp.from(ZonedDateTime.now().minusDays(1).toInstant());

  public static TestUser getInstance() {
    if (instance != null) return instance;

    EntityManager entityManager =
        Persistence.createEntityManagerFactory(UserPersistenceUnit.NAME).createEntityManager();

    entityManager.getTransaction().begin();

    Address address =
        new Address("21 Blue street", "Chicago", "78801", "John", "Doe", "M.", null, "FRA");
    user1 =
        new User(
            "*****@*****.**", "test", "John", "Doe", "+33616161616", null, yesterday, "fr_FR", null);
    user1.setGender("M.");

    entityManager.persist(address);
    entityManager.persist(user1);

    Role adminRole = new Role(RoleName.admin);
    Role userRole = new Role(RoleName.user);
    entityManager.persist(adminRole);
    entityManager.persist(userRole);

    entityManager.getTransaction().commit();

    instance = new TestUser();
    entityManager.close();
    return instance;
  }

  public User firstUser() {
    return user1;
  }
}
Ejemplo n.º 2
0
  public FileId register(InputStream in) {

    FileId fileId = new FileId(CommonUtils.getUniqueId());

    // DB に 登録
    dbService
        .insert(file)
        .set(file.fileId, fileId.longValue())
        .set(file.createDate, Timestamp.from(Instant.now()))
        .execute();

    // FileStorage に 登録
    try {
      storage.write(in, Long.toString(fileId.longValue()));
      return fileId;
    } catch (StorageException e) {
      throw Throwables.propagate(e);
    }
  }
 @Override
 public Timestamp to(Instant userObject) {
   return userObject == null ? null : Timestamp.from(userObject);
 }
  @Before
  public void init() {
    doInJPA(
        this::entityManagerFactory,
        entityManager -> {
          Session session = entityManager.unwrap(Session.class);
          session.doWork(
              connection -> {
                try (Statement statement = connection.createStatement()) {
                  statement.executeUpdate(
                      "CREATE OR REPLACE PROCEDURE count_phones (  "
                          + "   personId IN NUMBER,  "
                          + "   phoneCount OUT NUMBER )  "
                          + "AS  "
                          + "BEGIN  "
                          + "    SELECT COUNT(*) INTO phoneCount  "
                          + "    FROM person_phone  "
                          + "    WHERE person_id = personId; "
                          + "END;");
                  // tag::sql-sp-ref-cursor-oracle-example[]
                  statement.executeUpdate(
                      "CREATE OR REPLACE PROCEDURE person_phones ( "
                          + "   personId IN NUMBER, "
                          + "   personPhones OUT SYS_REFCURSOR ) "
                          + "AS  "
                          + "BEGIN "
                          + "    OPEN personPhones FOR "
                          + "    SELECT *"
                          + "    FROM person_phone "
                          + "    WHERE person_id = personId; "
                          + "END;");
                  // end::sql-sp-ref-cursor-oracle-example[]
                  statement.executeUpdate(
                      "CREATE OR REPLACE FUNCTION fn_count_phones ( "
                          + "    personId IN NUMBER ) "
                          + "    RETURN NUMBER "
                          + "IS "
                          + "    phoneCount NUMBER; "
                          + "BEGIN "
                          + "    SELECT COUNT(*) INTO phoneCount "
                          + "    FROM person_phone "
                          + "    WHERE person_id = personId; "
                          + "    RETURN( phoneCount ); "
                          + "END;");
                }
              });
        });
    doInJPA(
        this::entityManagerFactory,
        entityManager -> {
          Person person1 = new Person("John Doe");
          person1.setNickName("JD");
          person1.setAddress("Earth");
          person1.setCreatedOn(
              Timestamp.from(LocalDateTime.of(2000, 1, 1, 0, 0, 0).toInstant(ZoneOffset.UTC)));
          person1.getAddresses().put(AddressType.HOME, "Home address");
          person1.getAddresses().put(AddressType.OFFICE, "Office address");

          entityManager.persist(person1);

          Phone phone1 = new Phone("123-456-7890");
          phone1.setId(1L);
          phone1.setType(PhoneType.MOBILE);

          person1.addPhone(phone1);

          Phone phone2 = new Phone("098_765-4321");
          phone2.setId(2L);
          phone2.setType(PhoneType.LAND_LINE);

          person1.addPhone(phone2);
        });
  }
 @Override
 public void setUpdatedAt() {
   this.updatedAt = Timestamp.from(Instant.now());
 }
 @Override
 public void setCreatedAt() {
   this.createdAt = Timestamp.from(Instant.now());
 }