Search in sources :

Example 1 with CreateLocation

use of org.activityinfo.legacy.shared.command.CreateLocation in project activityinfo by bedatadriven.

the class CreateSiteTest method test.

@Test
public void test() throws CommandException {
    LocationDTO location = LocationDTOs.newLocation();
    execute(new CreateLocation(location));
    SiteDTO newSite = SiteDTOs.newSite();
    newSite.setLocation(location);
    CreateSite cmd = new CreateSite(newSite);
    setUser(1);
    CreateResult result = execute(cmd);
    newSite.setId(result.getNewId());
    assertThat(result.getNewId(), not(equalTo(0)));
    SiteDTO secondRead = readSite(newSite.getId());
    SiteDTOs.validateNewSite(secondRead);
}
Also used : CreateLocation(org.activityinfo.legacy.shared.command.CreateLocation) CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) CreateSite(org.activityinfo.legacy.shared.command.CreateSite) Test(org.junit.Test)

Example 2 with CreateLocation

use of org.activityinfo.legacy.shared.command.CreateLocation in project activityinfo by bedatadriven.

the class XFormSubmissionResource method createLocation.

private VoidResult createLocation(int id, int locationTypeId, String name, Optional<GeoPoint> geoPoint) {
    Preconditions.checkNotNull(name);
    Preconditions.checkNotNull(geoPoint);
    // classical MySQL table.
    if (name.length() > LocationDTO.MAX_NAME_LENGTH) {
        name = name.substring(0, LocationDTO.MAX_NAME_LENGTH);
    }
    Map<String, Object> properties = Maps.newHashMap();
    properties.put("id", id);
    properties.put("locationTypeId", locationTypeId);
    properties.put("name", name);
    if (geoPoint.isPresent()) {
        properties.put("latitude", geoPoint.get().getLatitude());
        properties.put("longitude", geoPoint.get().getLongitude());
    }
    return dispatcher.execute(new CreateLocation(properties));
}
Also used : CreateLocation(org.activityinfo.legacy.shared.command.CreateLocation)

Example 3 with CreateLocation

use of org.activityinfo.legacy.shared.command.CreateLocation in project activityinfo by bedatadriven.

the class BoundLocationSection method save.

@Override
public void save(final AsyncCallback<Void> callback) {
    if (isDirty()) {
        newLocation();
        dispatcher.execute(new CreateLocation(location), new AsyncCallback<VoidResult>() {

            @Override
            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }

            @Override
            public void onSuccess(VoidResult result) {
                callback.onSuccess(null);
            }
        });
    } else {
        callback.onSuccess(null);
    }
}
Also used : CreateLocation(org.activityinfo.legacy.shared.command.CreateLocation) VoidResult(org.activityinfo.legacy.shared.command.result.VoidResult)

Example 4 with CreateLocation

use of org.activityinfo.legacy.shared.command.CreateLocation 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

CreateLocation (org.activityinfo.legacy.shared.command.CreateLocation)4 CreateSite (org.activityinfo.legacy.shared.command.CreateSite)1 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)1 VoidResult (org.activityinfo.legacy.shared.command.result.VoidResult)1 CommandException (org.activityinfo.legacy.shared.exception.CommandException)1 PropertyMap (org.activityinfo.server.command.handler.crud.PropertyMap)1 Location (org.activityinfo.server.database.hibernate.entity.Location)1 LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)1 Test (org.junit.Test)1