Search in sources :

Example 1 with LocationType

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;
}
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 2 with LocationType

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();
}
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 3 with LocationType

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();
}
Also used : NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) AdminLevel(org.activityinfo.server.database.hibernate.entity.AdminLevel) NewAdminLevel(org.activityinfo.server.endpoint.rest.model.NewAdminLevel) NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 4 with LocationType

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();
}
Also used : NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) AdminLevel(org.activityinfo.server.database.hibernate.entity.AdminLevel) NewAdminLevel(org.activityinfo.server.endpoint.rest.model.NewAdminLevel) NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType)

Example 5 with LocationType

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;
}
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

LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)7 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)4 Location (org.activityinfo.server.database.hibernate.entity.Location)3 AdminLevel (org.activityinfo.server.database.hibernate.entity.AdminLevel)2 NewAdminEntity (org.activityinfo.server.endpoint.rest.model.NewAdminEntity)2 NewAdminLevel (org.activityinfo.server.endpoint.rest.model.NewAdminLevel)2 Timed (org.activityinfo.server.util.monitoring.Timed)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 Database (org.activityinfo.server.database.hibernate.entity.Database)1 NewLocation (org.activityinfo.server.endpoint.rest.model.NewLocation)1