Search in sources :

Example 1 with UpdatedAdminEntity

use of org.activityinfo.server.endpoint.rest.model.UpdatedAdminEntity in project activityinfo by bedatadriven.

the class AdminLevelResource method update.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
public Response update(@InjectParam AuthenticatedUser user, UpdatedAdminLevel updatedLevel) throws ParseException {
    assertAuthorized(user);
    EntityManager em = entityManager.get();
    em.getTransaction().begin();
    AdminLevel level = entityManager.get().merge(this.level);
    level.setName(updatedLevel.getName());
    for (LocationType boundLocationType : level.getBoundLocationTypes()) {
        boundLocationType.setName(updatedLevel.getName());
    }
    if (updatedLevel.getEntities() != null) {
        for (UpdatedAdminEntity updatedEntity : updatedLevel.getEntities()) {
            // check geometry
            if (updatedEntity.getGeometry() != null && !isValid(updatedEntity.getGeometry())) {
                throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("Geometry must be Polygon or MultiPolygon").build());
            }
            if (updatedEntity.isDeleted()) {
                // mark the entity as deleted. we can't remove it from
                // the database because we may have locations which refer to it
                // on distant clients
                em.find(AdminEntity.class, updatedEntity.getId()).setDeleted(true);
            } else if (updatedEntity.isNew()) {
                // create new entity
                AdminEntity entity = new AdminEntity();
                entity.setLevel(level);
                if (updatedEntity.getParentId() != null) {
                    entity.setParent(em.getReference(AdminEntity.class, updatedEntity.getParentId()));
                }
                entity.setName(updatedEntity.getName());
                entity.setCode(updatedEntity.getCode());
                entity.setBounds(updatedEntity.getBounds());
                entity.setGeometry(updatedEntity.getGeometry());
                em.persist(entity);
            } else {
                // update existing entity
                // TODO: bound locations that share this name?
                AdminEntity entity = em.find(AdminEntity.class, updatedEntity.getId());
                entity.setName(updatedEntity.getName());
                entity.setCode(updatedEntity.getCode());
                entity.setBounds(updatedEntity.getBounds());
                entity.setGeometry(updatedEntity.getGeometry());
            }
        }
    }
    int newVersion = level.getVersion() + 1;
    level.setVersion(newVersion);
    AdminLevelVersion version = new AdminLevelVersion();
    version.setLevel(level);
    version.setVersion(newVersion);
    version.setUser(em.getReference(User.class, user.getId()));
    version.setTimeCreated(new Date().getTime());
    VersionMetadata metadata = updatedLevel.getVersionMetadata();
    if (metadata != null) {
        version.setSourceUrl(metadata.getSourceUrl());
        version.setSourceFilename(metadata.getSourceFilename());
        version.setSourceHash(metadata.getSourceMD5());
        version.setMessage(metadata.getMessage());
        version.setSourceMetadata(metadata.getSourceMetadata());
    }
    em.persist(version);
    em.getTransaction().commit();
    return Response.ok().build();
}
Also used : EntityManager(javax.persistence.EntityManager) UpdatedAdminEntity(org.activityinfo.server.endpoint.rest.model.UpdatedAdminEntity) UpdatedAdminEntity(org.activityinfo.server.endpoint.rest.model.UpdatedAdminEntity) NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) AuthenticatedUser(org.activityinfo.shared.auth.AuthenticatedUser) User(org.activityinfo.server.database.hibernate.entity.User) WebApplicationException(javax.ws.rs.WebApplicationException) AdminLevel(org.activityinfo.server.database.hibernate.entity.AdminLevel) NewAdminLevel(org.activityinfo.server.endpoint.rest.model.NewAdminLevel) UpdatedAdminLevel(org.activityinfo.server.endpoint.rest.model.UpdatedAdminLevel) VersionMetadata(org.activityinfo.server.endpoint.rest.model.VersionMetadata) AdminLevelVersion(org.activityinfo.server.database.hibernate.entity.AdminLevelVersion) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) Date(java.util.Date) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

Date (java.util.Date)1 EntityManager (javax.persistence.EntityManager)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)1 AdminLevel (org.activityinfo.server.database.hibernate.entity.AdminLevel)1 AdminLevelVersion (org.activityinfo.server.database.hibernate.entity.AdminLevelVersion)1 LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)1 User (org.activityinfo.server.database.hibernate.entity.User)1 NewAdminEntity (org.activityinfo.server.endpoint.rest.model.NewAdminEntity)1 NewAdminLevel (org.activityinfo.server.endpoint.rest.model.NewAdminLevel)1 UpdatedAdminEntity (org.activityinfo.server.endpoint.rest.model.UpdatedAdminEntity)1 UpdatedAdminLevel (org.activityinfo.server.endpoint.rest.model.UpdatedAdminLevel)1 VersionMetadata (org.activityinfo.server.endpoint.rest.model.VersionMetadata)1 AuthenticatedUser (org.activityinfo.shared.auth.AuthenticatedUser)1