use of org.activityinfo.server.database.hibernate.entity.LocationType 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.LocationType 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.LocationType in project activityinfo by bedatadriven.
the class AdminLevelResource method postNewLevel.
@POST
@Timed(name = "site.rest.admin.child_levels")
@Path("/childLevels")
@Consumes(MediaType.APPLICATION_JSON)
public Response postNewLevel(@InjectParam EntityManager em, @InjectParam AuthenticatedUser user, NewAdminLevel newLevel) throws ParseException {
assertAuthorized(user);
em.getTransaction().begin();
em.setFlushMode(FlushModeType.COMMIT);
AdminLevel child = new AdminLevel();
child.setCountry(level.getCountry());
child.setName(newLevel.getName());
child.setParent(level);
em.persist(child);
for (NewAdminEntity entity : newLevel.getEntities()) {
AdminEntity childEntity = new AdminEntity();
childEntity.setName(entity.getName());
childEntity.setLevel(child);
childEntity.setCode(entity.getCode());
childEntity.setBounds(entity.getBounds());
childEntity.setParent(em.getReference(AdminEntity.class, entity.getParentId()));
childEntity.setGeometry(entity.getGeometry());
child.getEntities().add(childEntity);
em.persist(childEntity);
}
// create bound location type
LocationType boundType = new LocationType();
boundType.setBoundAdminLevel(child);
boundType.setCountry(level.getCountry());
boundType.setName(child.getName());
em.persist(boundType);
em.getTransaction().commit();
return Response.ok().build();
}
use of org.activityinfo.server.database.hibernate.entity.LocationType in project activityinfo by bedatadriven.
the class CountryResource method postNewLevel.
@POST
@Path("adminLevels")
@Consumes(MediaType.APPLICATION_JSON)
public Response postNewLevel(@InjectParam AuthenticatedUser user, @InjectParam EntityManager em, NewAdminLevel newLevel) {
// assertAuthorized(user);
em.getTransaction().begin();
em.setFlushMode(FlushModeType.COMMIT);
AdminLevel level = new AdminLevel();
level.setCountry(country);
level.setName(newLevel.getName());
level.setVersion(1);
em.persist(level);
for (NewAdminEntity newEntity : newLevel.getEntities()) {
AdminEntity entity = new AdminEntity();
entity.setName(newEntity.getName());
entity.setLevel(level);
entity.setCode(newEntity.getCode());
entity.setBounds(newEntity.getBounds());
entity.setGeometry(newEntity.getGeometry());
level.getEntities().add(entity);
em.persist(entity);
}
// create bound location type
LocationType boundType = new LocationType();
boundType.setBoundAdminLevel(level);
boundType.setCountry(level.getCountry());
boundType.setName(level.getName());
em.persist(boundType);
em.getTransaction().commit();
return Response.ok().build();
}
use of org.activityinfo.server.database.hibernate.entity.LocationType 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