use of org.activityinfo.server.command.handler.crud.PropertyMap in project activityinfo by bedatadriven.
the class CreateEntityHandler method execute.
@Override
public CommandResult execute(CreateEntity cmd, User user) {
Map<String, Object> properties = cmd.getProperties().getTransientMap();
PropertyMap propertyMap = new PropertyMap(cmd.getProperties().getTransientMap());
switch(cmd.getEntityName()) {
case UserDatabaseDTO.ENTITY_NAME:
return createDatabase(user, propertyMap);
case FolderDTO.ENTITY_NAME:
return createFolder(user, properties);
case ActivityDTO.ENTITY_NAME:
return createActivity(user, propertyMap);
case AttributeGroupDTO.ENTITY_NAME:
return createAttributeGroup(properties);
case AttributeDTO.ENTITY_NAME:
return createAttribute(properties);
case IndicatorDTO.ENTITY_NAME:
return createIndicator(user, properties);
case LocationTypeDTO.ENTITY_NAME:
return createLocationType(user, propertyMap);
default:
throw new CommandException("Invalid entity class " + cmd.getEntityName());
}
}
use of org.activityinfo.server.command.handler.crud.PropertyMap in project activityinfo by bedatadriven.
the class BaseEntityHandler method updateIndicatorProperties.
protected void updateIndicatorProperties(Indicator indicator, Map<String, Object> changeMap) {
PropertyMap changes = new PropertyMap(changeMap);
if (changes.containsKey(NAME_PROPERTY)) {
indicator.setName(trimAndTruncate(changes.get(NAME_PROPERTY)));
}
if (changes.containsKey(TYPE_PROPERTY)) {
updateType(indicator, changes);
}
if (changes.containsKey(EXPRESSION_PROPERTY)) {
indicator.setExpression(trimAndTruncate(changes.get(EXPRESSION_PROPERTY)));
}
if (changes.containsKey(RELEVANCE_PROPERTY)) {
indicator.setRelevanceExpression(trimAndTruncate(changes.get(RELEVANCE_PROPERTY)));
}
if (changes.containsKey(CODE_PROPERTY)) {
indicator.setNameInExpression(trimAndTruncate(changes.get(CODE_PROPERTY)));
}
// Allow "code" as an alias from the JSON API
if (changes.containsKey("code")) {
indicator.setNameInExpression(trimAndTruncate(changes.get("code")));
}
if (changes.containsKey("calculatedAutomatically")) {
indicator.setCalculatedAutomatically(changes.get("calculatedAutomatically"));
}
if (changes.containsKey(AGGREGATION_PROPERTY)) {
indicator.setAggregation(changes.get(AGGREGATION_PROPERTY));
}
if (changes.containsKey(CATEGORY_PROPERTY)) {
indicator.setCategory(trimAndTruncate(changes.get(CATEGORY_PROPERTY)));
}
if (changes.containsKey(LIST_HEADER_PROPERTY)) {
indicator.setListHeader(trimAndTruncate(changes.get(LIST_HEADER_PROPERTY)));
}
if (changes.containsKey(IndicatorDTO.DESCRIPTION_PROPERTY)) {
indicator.setDescription(trimAndTruncate(changes.get(DESCRIPTION_PROPERTY)));
}
if (changes.containsKey(UNITS_PROPERTY)) {
indicator.setUnits(trimAndTruncate(changes.get(UNITS_PROPERTY)));
}
if (changes.containsKey(SORT_ORDER_PROPERTY)) {
indicator.setSortOrder(changes.get(SORT_ORDER_PROPERTY));
}
if (changes.containsKey(MANDATORY_PROPERTY)) {
indicator.setMandatory(changes.get(MANDATORY_PROPERTY));
}
if (changes.containsKey(VISIBLE_PROPERTY)) {
indicator.setVisible(changes.get(VISIBLE_PROPERTY));
}
indicator.getActivity().incrementSchemaVersion();
indicator.getActivity().getDatabase().setLastSchemaUpdate(new Date());
}
use of org.activityinfo.server.command.handler.crud.PropertyMap 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;
}
use of org.activityinfo.server.command.handler.crud.PropertyMap in project activityinfo by bedatadriven.
the class UpdateEntityHandler method execute.
@Override
public CommandResult execute(UpdateEntity cmd, User user) {
LOGGER.fine("[execute] Update command for entity: " + cmd.getEntityName() + ".");
Map<String, Object> changes = cmd.getChanges().getTransientMap();
PropertyMap changeMap = new PropertyMap(changes);
switch(cmd.getEntityName()) {
case UserDatabaseDTO.ENTITY_NAME:
updateDatabase(cmd, user, changeMap);
break;
case ActivityDTO.ENTITY_NAME:
updateActivity(cmd, user, changeMap);
break;
case FolderDTO.ENTITY_NAME:
updateFolder(user, cmd.getId(), changeMap);
break;
case AttributeGroupDTO.ENTITY_NAME:
updateAttributeGroup(user, cmd, changes);
break;
case AttributeDTO.ENTITY_NAME:
updateAttribute(user, cmd, changes);
break;
case IndicatorDTO.ENTITY_NAME:
updateIndicator(user, cmd, changes);
break;
case LockedPeriodDTO.ENTITY_NAME:
updateLockedPeriod(user, cmd, changes);
break;
case TargetDTO.ENTITY_NAME:
updateTarget(user, cmd, changes);
break;
case LocationTypeDTO.ENTITY_NAME:
updateLocationType(cmd, user, changeMap);
break;
default:
throw new UnsupportedOperationException("EntityType:" + cmd.getEntityName());
}
return null;
}
use of org.activityinfo.server.command.handler.crud.PropertyMap in project activityinfo by bedatadriven.
the class LocationBuilderTest method locationTypeChange.
@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void locationTypeChange() throws Exception {
EntityManager em = emf.createEntityManager();
User user = em.find(User.class, 1);
// Update the location type 1
Map<String, Object> changes = new HashMap<>();
changes.put("name", "Ishamael");
em.getTransaction().begin();
LocationTypePolicy locationTypePolicy = new LocationTypePolicy(em);
locationTypePolicy.update(user, 1, new PropertyMap(changes));
em.getTransaction().commit();
// First update should include this change
String regionId = "location/" + 1;
LocationUpdateBuilder builder = new LocationUpdateBuilder(em);
GetSyncRegionUpdates request = new GetSyncRegionUpdates(regionId, null);
SyncRegionUpdate update = builder.build(user, request);
assertThat(update.isComplete(), equalTo(true));
assertThat(update.getSql(), containsString("Ishamael"));
// We should be up to date now...
GetSyncRegionsHandler getSyncRegionsHandler = new GetSyncRegionsHandler(em);
SyncRegions syncRegions = getSyncRegionsHandler.execute(new GetSyncRegions(), user);
System.out.println(syncRegions.getList());
assertThat(syncRegions, hasItem(new SyncRegion(regionId, update.getVersion())));
}
Aggregations