Search in sources :

Example 1 with Location

use of org.activityinfo.server.database.hibernate.entity.Location in project activityinfo by bedatadriven.

the class SyncIntegrationTest method locationsAreChunked.

@Test
@OnDataSet("/dbunit/locations.db.xml")
public void locationsAreChunked() throws SQLException, InterruptedException {
    addLocationsToServerDatabase(220);
    synchronize();
    assertThat(Integer.valueOf(queryString("select count(*) from Location")), equalTo(221));
    // update a location on the server
    serverEm.getTransaction().begin();
    Location location = (Location) serverEm.createQuery("select l from Location l where l.name = 'Penekusu 26'").getSingleResult();
    location.setAxe("Motown");
    location.setVersion(location.getLocationType().incrementVersion());
    serverEm.getTransaction().commit();
    newRequest();
    synchronize();
    assertThat(queryInt("select count(*) from Location where Name='Penekusu 26'"), equalTo(1));
    assertThat(queryString("select axe from Location where Name='Penekusu 26'"), equalTo("Motown"));
    // now create a new location
    Location newLocation = new Location();
    int locationId = keyGenerator.generateInt();
    newLocation.setName("Bukavu");
    newLocation.setId(123456789);
    newLocation.setLocationType(serverEm.find(LocationType.class, 1));
    newLocation.setVersion(newLocation.getLocationType().incrementVersion());
    newLocation.setId(locationId);
    serverEm.getTransaction().begin();
    serverEm.persist(newLocation);
    serverEm.getTransaction().commit();
    newRequest();
    synchronize();
    assertThat(queryString("select name from Location where LocationId = " + locationId), equalTo("Bukavu"));
}
Also used : LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) Location(org.activityinfo.server.database.hibernate.entity.Location) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 2 with Location

use of org.activityinfo.server.database.hibernate.entity.Location in project activityinfo by bedatadriven.

the class SyncIntegrationTest method addLocationsToServerDatabase.

private List<Integer> addLocationsToServerDatabase(int count) {
    final List<Integer> locationIds = Lists.newArrayList();
    EntityManager entityManager = serverEntityManagerFactory.createEntityManager();
    LocationType locationType = entityManager.find(LocationType.class, 1);
    entityManager.getTransaction().begin();
    for (int i = 1; i <= count; ++i) {
        Location loc = new Location();
        // first 10 ids are used by data set
        loc.setId(i + 10);
        loc.setVersion(locationType.incrementVersion());
        loc.setName("Penekusu " + i);
        loc.getAdminEntities().add(entityManager.getReference(AdminEntity.class, 2));
        loc.getAdminEntities().add(entityManager.getReference(AdminEntity.class, 12));
        loc.setLocationType(locationType);
        entityManager.persist(loc);
        entityManager.flush();
        locationIds.add(loc.getId());
        assertTrue(loc.getId() != 0);
        locationIds.add(loc.getId());
    }
    entityManager.getTransaction().commit();
    entityManager.close();
    return locationIds;
}
Also used : EntityManager(javax.persistence.EntityManager) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) Location(org.activityinfo.server.database.hibernate.entity.Location)

Example 3 with Location

use of org.activityinfo.server.database.hibernate.entity.Location in project activityinfo by bedatadriven.

the class LocationsResource method postNewLocations.

@POST
@Path("/{typeId}")
@Timed(name = "api.rest.locations.post")
public Response postNewLocations(@InjectParam EntityManager entityManager, @PathParam("typeId") int locationTypeId, List<NewLocation> locations) {
    KeyGenerator generator = new KeyGenerator();
    entityManager.getTransaction().begin();
    LocationType locationType = entityManager.getReference(LocationType.class, locationTypeId);
    for (NewLocation newLocation : locations) {
        Location location = new Location();
        location.setId(generator.generateInt());
        System.out.println(location.getId());
        location.setName(newLocation.getName());
        location.setLocationType(locationType);
        location.setX(newLocation.getLongitude());
        location.setY(newLocation.getLatitude());
        location.setTimeEdited(new Date());
        location.setAdminEntities(new HashSet<AdminEntity>());
        for (int entityId : newLocation.getAdminEntityIds()) {
            location.getAdminEntities().add(entityManager.getReference(AdminEntity.class, entityId));
        }
        entityManager.persist(location);
    }
    entityManager.getTransaction().commit();
    return Response.ok().build();
}
Also used : AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) NewLocation(org.activityinfo.server.endpoint.rest.model.NewLocation) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) Date(java.util.Date) NewLocation(org.activityinfo.server.endpoint.rest.model.NewLocation) Location(org.activityinfo.server.database.hibernate.entity.Location) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 4 with Location

use of org.activityinfo.server.database.hibernate.entity.Location in project activityinfo by bedatadriven.

the class CreateLocationHandler method execute.

@Override
public VoidResult execute(CreateLocation cmd, User user) throws CommandException {
    PropertyMap propertyMap = new PropertyMap(cmd.getProperties());
    List<Location> existingLocation = entityManager.createQuery("SELECT L from Location L LEFT JOIN FETCH L.adminEntities WHERE L.id = :id", Location.class).setParameter("id", cmd.getLocationId()).getResultList();
    if (existingLocation.isEmpty()) {
        /*
             * Create new Location
             */
        LocationType locationType = entityManager.find(LocationType.class, cmd.getLocationTypeId());
        if (locationType == null) {
            throw new CommandException("LocationType " + cmd.getLocationTypeId() + " does not exist");
        }
        Location location = new Location();
        location.setId(cmd.getLocationId());
        location.setLocationType(locationType);
        location.setVersion(locationType.incrementVersion());
        applyProperties(location, propertyMap);
        entityManager.persist(location);
    } else {
        Location location = existingLocation.get(0);
        location.setVersion(location.getLocationType().incrementVersion());
        if (cmd.getProperties().containsKey("locationTypeId") && location.getLocationType().getId() != cmd.getLocationTypeId()) {
            throw new CommandException("LocationType of a location cannot be changed");
        }
        applyProperties(location, propertyMap);
    }
    return VoidResult.EMPTY;
}
Also used : PropertyMap(org.activityinfo.server.command.handler.crud.PropertyMap) CommandException(org.activityinfo.legacy.shared.exception.CommandException) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) CreateLocation(org.activityinfo.legacy.shared.command.CreateLocation) Location(org.activityinfo.server.database.hibernate.entity.Location)

Aggregations

Location (org.activityinfo.server.database.hibernate.entity.Location)4 LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)4 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)2 Date (java.util.Date)1 EntityManager (javax.persistence.EntityManager)1 CreateLocation (org.activityinfo.legacy.shared.command.CreateLocation)1 CommandException (org.activityinfo.legacy.shared.exception.CommandException)1 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)1 PropertyMap (org.activityinfo.server.command.handler.crud.PropertyMap)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 NewLocation (org.activityinfo.server.endpoint.rest.model.NewLocation)1 Timed (org.activityinfo.server.util.monitoring.Timed)1 Test (org.junit.Test)1