Search in sources :

Example 1 with PropertyMap

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());
    }
}
Also used : PropertyMap(org.activityinfo.server.command.handler.crud.PropertyMap) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Example 2 with PropertyMap

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());
}
Also used : PropertyMap(org.activityinfo.server.command.handler.crud.PropertyMap) Date(java.util.Date) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate)

Example 3 with PropertyMap

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

Example 4 with PropertyMap

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;
}
Also used : PropertyMap(org.activityinfo.server.command.handler.crud.PropertyMap)

Example 5 with PropertyMap

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())));
}
Also used : GetSyncRegionUpdates(org.activityinfo.legacy.shared.command.GetSyncRegionUpdates) User(org.activityinfo.server.database.hibernate.entity.User) HashMap(java.util.HashMap) LocationTypePolicy(org.activityinfo.server.command.handler.crud.LocationTypePolicy) SyncRegions(org.activityinfo.legacy.shared.command.result.SyncRegions) GetSyncRegions(org.activityinfo.legacy.shared.command.GetSyncRegions) GetSyncRegions(org.activityinfo.legacy.shared.command.GetSyncRegions) SyncRegion(org.activityinfo.legacy.shared.command.result.SyncRegion) EntityManager(javax.persistence.EntityManager) PropertyMap(org.activityinfo.server.command.handler.crud.PropertyMap) SyncRegionUpdate(org.activityinfo.legacy.shared.command.result.SyncRegionUpdate) GetSyncRegionsHandler(org.activityinfo.server.command.handler.GetSyncRegionsHandler) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

PropertyMap (org.activityinfo.server.command.handler.crud.PropertyMap)5 CommandException (org.activityinfo.legacy.shared.exception.CommandException)2 LocalDate (com.bedatadriven.rebar.time.calendar.LocalDate)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 EntityManager (javax.persistence.EntityManager)1 CreateLocation (org.activityinfo.legacy.shared.command.CreateLocation)1 GetSyncRegionUpdates (org.activityinfo.legacy.shared.command.GetSyncRegionUpdates)1 GetSyncRegions (org.activityinfo.legacy.shared.command.GetSyncRegions)1 SyncRegion (org.activityinfo.legacy.shared.command.result.SyncRegion)1 SyncRegionUpdate (org.activityinfo.legacy.shared.command.result.SyncRegionUpdate)1 SyncRegions (org.activityinfo.legacy.shared.command.result.SyncRegions)1 GetSyncRegionsHandler (org.activityinfo.server.command.handler.GetSyncRegionsHandler)1 LocationTypePolicy (org.activityinfo.server.command.handler.crud.LocationTypePolicy)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Location (org.activityinfo.server.database.hibernate.entity.Location)1 LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)1 User (org.activityinfo.server.database.hibernate.entity.User)1 Test (org.junit.Test)1