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