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"));
}
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;
}
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();
}
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;
}
Aggregations