public HandlingEvent createHandlingEvent(
        Date completionTime,
        TrackingId trackingId,
        CarrierMovementId carrierMovementId,
        UnLocode unlocode,
        HandlingEvent.Type type)
        throws UnknownTrackingIdException, UnknownCarrierMovementIdException,
            UnknownLocationException {
      Cargo cargo = validateCargoExists(trackingId);
      CarrierMovement carrierMovement = findCarrierMovement(carrierMovementId);
      Location location = findLocation(unlocode);
      Date registrationTime = new Date();

      // Create entity
      UnitOfWork uow = uowf.currentUnitOfWork();
      EntityBuilder<HandlingEvent> builder = uow.newEntityBuilder(HandlingEvent.class);
      HandlingEventState state = builder.instanceFor(HandlingEventState.class);
      state.cargo().set(cargo);

      // TODO: remove next line when query + association is implemented
      state.cargoTrackingId().set(trackingId.idString());

      state.carrierMovement().set(carrierMovement);
      state.location().set(location);
      state.registrationTime().set(registrationTime);
      state.completionTime().set(completionTime);
      state.eventType().set(type);

      return builder.newInstance();
    }
  @Test
  public void givenEntityWithImmutableManyAssociationWhenBuildingThenNoException()
      throws Exception {
    UnitOfWork unitOfWork = unitOfWorkFactory.newUnitOfWork();
    try {
      EntityBuilder<PersonEntity> builder = unitOfWork.newEntityBuilder(PersonEntity.class);
      PersonEntity person1 = builder.instance();
      person1 = builder.newInstance();

      builder = unitOfWork.newEntityBuilder(PersonEntity.class);
      PersonEntity person2 = builder.instance();
      person2.colleagues().add(0, person1);
      person2 = builder.newInstance();
    } finally {
      unitOfWork.discard();
    }
  }
    public void activate() throws Exception {
      UnitOfWork unitOfWork = uowf.newUnitOfWork();
      try {
        {
          ValueBuilder<TestValue> valueBuilder = vbf.newValueBuilder(TestValue.class);
          valueBuilder.prototype().longList().get().add(42L);
          valueBuilder.prototype().string().set("Foo bar value");
          valueBuilder.prototype().map().set(new HashMap());

          EntityBuilder<TestEntity> builder =
              unitOfWork.newEntityBuilder(TestEntity.class, "test1");
          builder.instance().name().set("Foo bar");
          builder.instance().age().set(42);
          builder.instance().value().set(valueBuilder.newInstance());
          TestEntity testEntity = builder.newInstance();

          EntityBuilder<TestEntity> builder2 =
              unitOfWork.newEntityBuilder(TestEntity.class, "test2");
          builder2.instance().name().set("Xyzzy");
          builder2.instance().age().set(12);
          builder2.instance().association().set(testEntity);
          builder2.instance().manyAssociation().add(0, testEntity);
          builder2.instance().manyAssociation().add(0, testEntity);

          EntityBuilder<TestRole> builder3 = unitOfWork.newEntityBuilder(TestRole.class);
          builder3.instance().name().set("A role");
          TestRole testRole = builder3.newInstance();

          builder2.newInstance();
        }

        {
          EntityBuilder<TestEntity2> builder =
              unitOfWork.newEntityBuilder(TestEntity2.class, "test3");
          builder.instance().name().set("Test3");
          builder.newInstance();
        }

        unitOfWork.complete();
      } catch (Exception e) {
        unitOfWork.discard();
        throw e;
      }
    }
  @Test(expected = IllegalStateException.class)
  public void givenEntityWithImmutableManyAssociationWhenChangingValueThenThrowException()
      throws Exception {
    UnitOfWork unitOfWork = unitOfWorkFactory.newUnitOfWork();
    try {
      EntityBuilder<PersonEntity> builder = unitOfWork.newEntityBuilder(PersonEntity.class);
      PersonEntity person1 = builder.instance();
      person1 = builder.newInstance();

      builder = unitOfWork.newEntityBuilder(PersonEntity.class);
      PersonEntity person2 = builder.instance();
      person2 = builder.newInstance();

      person1.colleagues().add(0, person2);

      unitOfWork.complete();
    } catch (Exception e) {
      unitOfWork.discard();
      throw e;
    }
  }
  @Test(expected = IllegalStateException.class)
  public void givenEntityWithImmutableAssociationWhenChangingValueThenThrowException()
      throws Exception {
    UnitOfWork unitOfWork = unitOfWorkFactory.newUnitOfWork();
    try {
      EntityBuilder<PersonEntity> builder = unitOfWork.newEntityBuilder(PersonEntity.class);
      PersonEntity father = builder.instance();
      father = builder.newInstance();

      builder = unitOfWork.newEntityBuilder(PersonEntity.class);
      PersonEntity child = builder.instance();
      child = builder.newInstance();

      child.father().set(father);

      unitOfWork.complete();
    } catch (Exception e) {
      unitOfWork.discard();
      throw e;
    }
  }
Beispiel #6
0
 public Voyage build() {
   final ValueBuilder<Schedule> builder = factory.newValueBuilder(Schedule.class);
   builder.prototype().carrierMovements().set(carrierMovements);
   final Schedule schedule = builder.newInstance();
   final EntityBuilder<Voyage> entityBuilder =
       uow.newEntityBuilder(Voyage.class, createVoyageIdentity(voyageNumberString));
   Voyage voyage = entityBuilder.instance();
   VoyageNumber voyageNumber = createVoyageNumber(voyageNumberString);
   voyage.voyageNumber().set(voyageNumber);
   voyage.schedule().set(schedule);
   return entityBuilder.newInstance();
 }
 @Override
 public void activate() throws Exception {
   UnitOfWork uow = uowf.newUnitOfWork();
   try {
     RootEntity root = rootEntity();
     LOGGER.info("Will use RootEntity: " + root.identity().get());
   } catch (NoSuchEntityException ex) {
     EntityBuilder<RootEntity> builder =
         uow.newEntityBuilder(RootEntity.class, RootEntity.IDENTITY);
     RootEntity root = builder.newInstance();
     LOGGER.info("Created new RootEntity: " + root.identity().get());
   }
   uow.complete();
 }
  @Test
  public void givenEntityWithImmutableAssociationWhenBuildingThenNoException() throws Exception {
    UnitOfWork unitOfWork = unitOfWorkFactory.newUnitOfWork();
    try {
      PersonEntity father = unitOfWork.newEntity(PersonEntity.class);

      EntityBuilder<PersonEntity> builder = unitOfWork.newEntityBuilder(PersonEntity.class);
      PersonEntity instance = builder.instance();
      instance.father().set(father);
      PersonEntity child = builder.newInstance();
    } finally {
      unitOfWork.discard();
    }
  }
  @Test
  public void testOperators()
      throws UnitOfWorkCompletionException, ActivationException, AssemblyException {
    SingletonAssembler assembler =
        new SingletonAssembler() {
          @Override
          public void assemble(ModuleAssembly module) throws AssemblyException {
            new EntityTestAssembler().assemble(module);

            module.entities(TestEntity.class);
            module.values(TestValue.class);
            module.forMixin(TestEntity.class).declareDefaults().foo().set("Bar");
            module.forMixin(TestValue.class).declareDefaults().bar().set("Xyz");
          }
        };

    UnitOfWork uow = assembler.module().newUnitOfWork();

    try {
      EntityBuilder<TestEntity> entityBuilder = uow.newEntityBuilder(TestEntity.class, "123");
      entityBuilder.instance().value().set(assembler.module().newValue(TestValue.class));
      TestEntity testEntity = entityBuilder.newInstance();

      uow.complete();
      uow = assembler.module().newUnitOfWork();

      Iterable<TestEntity> entities = Iterables.iterable(testEntity = uow.get(testEntity));

      QueryBuilder<TestEntity> builder = assembler.module().newQueryBuilder(TestEntity.class);

      {
        Specification<Composite> where =
            QueryExpressions.eq(QueryExpressions.templateFor(TestEntity.class).foo(), "Bar");
        Assert.assertTrue(where.satisfiedBy(testEntity));
        System.out.println(where);
      }
      {
        Specification<Composite> where =
            QueryExpressions.eq(
                QueryExpressions.templateFor(TestEntity.class).value().get().bar(), "Xyz");
        Assert.assertTrue(where.satisfiedBy(testEntity));
        System.out.println(where);

        Assert.assertTrue(builder.where(where).newQuery(entities).find().equals(testEntity));
      }
    } finally {
      uow.discard();
    }
  }
Beispiel #10
0
    @SuppressWarnings("unchecked")
    private <V> V initializeConfigurationInstance(
        ServiceComposite serviceComposite,
        UnitOfWork uow,
        ServiceDescriptor serviceModel,
        String identity)
        throws InstantiationException {
      Module module = api.moduleOf(serviceComposite);
      Usecase usecase = UsecaseBuilder.newUsecase("Configuration:" + me.identity().get());
      UnitOfWork buildUow = module.newUnitOfWork(usecase);

      EntityBuilder<V> configBuilder =
          buildUow.newEntityBuilder(serviceModel.<V>configurationType(), identity);

      // Check for defaults
      String s = identity + ".properties";
      Class<?> type = first(api.serviceDescriptorFor(serviceComposite).types());
      // Load defaults from classpath root if available
      if (type.getResource(s) == null && type.getResource("/" + s) != null) {
        s = "/" + s;
      }
      InputStream asStream = type.getResourceAsStream(s);
      if (asStream != null) {
        try {
          PropertyMapper.map(asStream, (Composite) configBuilder.instance());
        } catch (IOException e1) {
          InstantiationException exception =
              new InstantiationException("Could not read underlying Properties file.");
          exception.initCause(e1);
          throw exception;
        }
      }

      try {
        configBuilder.newInstance();
        buildUow.complete();

        // Try again
        return (V) findConfigurationInstanceFor(serviceComposite, uow);
      } catch (Exception e1) {
        InstantiationException ex =
            new InstantiationException(
                "Could not instantiate configuration, and no Properties file was found ("
                    + s
                    + ")");
        ex.initCause(e1);
        throw ex;
      }
    }
Beispiel #11
0
 public Cargo create(String originUnLocode, String destinationUnLocode, Date arrivalDeadline) {
   TrackingId trackingId = cargoRepository.nextTrackingId();
   Location origin = locationRepository.findLocationByUnLocode(originUnLocode);
   Location destination = locationRepository.findLocationByUnLocode(destinationUnLocode);
   RouteSpecification routeSpecification =
       routeSpecificationFactory.create(origin, destination, arrivalDeadline);
   UnitOfWork uow = uowf.currentUnitOfWork();
   EntityBuilder<Cargo> builder = uow.newEntityBuilder(Cargo.class, trackingId.id().get());
   Cargo.State instanceState = builder.instanceFor(Cargo.State.class);
   instanceState.routeSpecification().set(routeSpecification);
   instanceState.origin().set(origin);
   Cargo cargo = builder.newInstance();
   logger.info("Booked new cargo with tracking id " + cargo.trackingId().id().get());
   return cargo;
 }