Search in sources :

Example 1 with Action

use of io.jawg.osmcontributor.model.entities.Action in project osm-contributor by jawg.

the class ActionDao method createIfNotExist.

public Action createIfNotExist(Action action) {
    if (action == null) {
        return action;
    }
    // Query for the source
    Map<String, Object> fields = new HashMap<>();
    fields.put(Action.KEY, action.getKey());
    fields.put(Action.TYPE, action.getType());
    fields.put(Action.VALUE, action.getValue());
    List<Action> db_data = queryForFieldValues(fields);
    Action db_action = db_data.size() > 0 ? db_data.get(0) : null;
    // Create the source doesn't exist, otherwise get the ID
    if (db_action == null) {
        createOrUpdate(action);
    } else {
        action.setId(db_action.getId());
    }
    return action;
}
Also used : Action(io.jawg.osmcontributor.model.entities.Action) HashMap(java.util.HashMap)

Example 2 with Action

use of io.jawg.osmcontributor.model.entities.Action in project osm-contributor by jawg.

the class EditPoiManager method applyConstraints.

private Map<String, String> applyConstraints(Poi poi) {
    Map<String, String> tagsMap = new HashMap<>();
    Collection<Constraint> constraints = poi.getType().getConstraints();
    if (constraints != null) {
        for (Constraint constraint : constraints) {
            Source source = constraint.getSource();
            Condition condition = constraint.getCondition();
            Action action = constraint.getAction();
            PoiTag poiTag = null;
            switch(source.getType()) {
                case TAG:
                    poiTag = findTagByKey(poi, source.getKey());
                    break;
            }
            switch(condition.getType()) {
                case EXISTS:
                    // if the value is true and tag exists
                    if (poiTag != null && condition.getValue().equalsIgnoreCase(Condition.ExistsValues.TRUE.getValue())) {
                        continue;
                    // if the value is false and tag doesn't exists
                    } else if (poiTag == null && condition.getValue().equalsIgnoreCase(Condition.ExistsValues.FALSE.getValue())) {
                        continue;
                    // If none is valid
                    } else {
                        break;
                    }
                case EQUALS:
                    if (poiTag != null && poiTag.getValue().equalsIgnoreCase(condition.getValue())) {
                        break;
                    }
                default:
                    continue;
            }
            switch(action.getType()) {
                case SET_TAG_VALUE:
                    // Add to map of tags
                    tagsMap.put(action.getKey(), action.getValue());
                    break;
                case REMOVE_TAG:
                    // Set value to empty string to remove
                    tagsMap.put(action.getKey(), "");
            }
        }
    }
    return tagsMap;
}
Also used : Condition(io.jawg.osmcontributor.model.entities.Condition) PoiTag(io.jawg.osmcontributor.model.entities.PoiTag) Action(io.jawg.osmcontributor.model.entities.Action) HashMap(java.util.HashMap) Constraint(io.jawg.osmcontributor.model.entities.Constraint) Source(io.jawg.osmcontributor.model.entities.Source)

Example 3 with Action

use of io.jawg.osmcontributor.model.entities.Action in project osm-contributor by jawg.

the class PoiTypeMapper method convert.

private PoiType convert(PoiTypeDto dto) {
    PoiType type = new PoiType();
    type.setName(getTranslationFormJson(dto.getLabel(), getName(dto.getName())));
    type.setTechnicalName(dto.getName());
    type.setDescription(getTranslationFormJson(dto.getDescription(), ""));
    type.setKeyWords(getKeywordsFormJson(dto.getKeywords()));
    type.setIcon(getName(dto.getName()));
    // When creating a new PoiType from file, put the same date of last use to all new PoiTypes : 1970-01-01T00:00:00Z
    type.setLastUse(dateTime);
    int ordinal = 0;
    if (dto.getTags() != null) {
        ArrayList<PoiTypeTag> tags = new ArrayList<>(dto.getTags().size());
        type.setTags(tags);
        type.setQuery(dto.getQuery());
        for (PoiTypeTagDto tagDto : dto.getTags()) {
            // If the tag is implied, do not keep it
            PoiTypeTag poiTypeTag = new PoiTypeTag();
            poiTypeTag.setPoiType(type);
            poiTypeTag.setKey(tagDto.getKey());
            poiTypeTag.setValue(tagDto.getValue());
            poiTypeTag.setMandatory(tagDto.getRequired());
            poiTypeTag.setOrdinal(ordinal++);
            poiTypeTag.setShow(tagDto.getShow());
            if (tagDto.getValues() != null && !tagDto.getValues().isEmpty()) {
                List<String> possibleValues = new ArrayList<>();
                for (Map<String, Map<String, String>> valuesMap : tagDto.getValues()) {
                    for (Map.Entry<String, Map<String, String>> value : valuesMap.entrySet()) {
                        possibleValues.add(value.getKey() + VALUE_SEPARATOR + getTranslationFormJson(value.getValue(), ""));
                    }
                }
                poiTypeTag.setPossibleValues(getPossibleValues(possibleValues));
            }
            poiTypeTag.setTagType(tagDto.getType());
            tags.add(poiTypeTag);
        }
    }
    ordinal = 0;
    if (dto.getConstraints() != null) {
        ArrayList<Constraint> constraints = new ArrayList<>();
        type.setConstraints(constraints);
        for (ConstraintDto constraintDto : dto.getConstraints()) {
            // Constraint's Source
            Source source = Source.builder().setType(constraintDto.getSource().getType()).setKey(constraintDto.getSource().getKey()).build();
            // Constraint's Condition
            Condition condition = Condition.builder().setType(constraintDto.getCondition().getType()).setValue(constraintDto.getCondition().getValue()).build();
            // Constraint's Action
            Action action = Action.builder().setType(constraintDto.getAction().getType()).setKey(constraintDto.getAction().getKey()).setValue(constraintDto.getAction().getValue()).build();
            // Complete constraint
            Constraint constraint = Constraint.builder().setPoiType(type).setSource(source).setCondition(condition).setAction(action).setOrdinal(ordinal++).build();
            // Add constraint to the list
            constraints.add(constraint);
        }
    }
    return type;
}
Also used : Condition(io.jawg.osmcontributor.model.entities.Condition) Action(io.jawg.osmcontributor.model.entities.Action) Constraint(io.jawg.osmcontributor.model.entities.Constraint) PoiTypeTagDto(io.jawg.osmcontributor.rest.dtos.dma.PoiTypeTagDto) PoiType(io.jawg.osmcontributor.model.entities.PoiType) ArrayList(java.util.ArrayList) ConstraintDto(io.jawg.osmcontributor.rest.dtos.dma.ConstraintDto) PoiTypeTag(io.jawg.osmcontributor.model.entities.PoiTypeTag) Constraint(io.jawg.osmcontributor.model.entities.Constraint) Source(io.jawg.osmcontributor.model.entities.Source) Map(java.util.Map)

Aggregations

Action (io.jawg.osmcontributor.model.entities.Action)3 Condition (io.jawg.osmcontributor.model.entities.Condition)2 Constraint (io.jawg.osmcontributor.model.entities.Constraint)2 Source (io.jawg.osmcontributor.model.entities.Source)2 HashMap (java.util.HashMap)2 PoiTag (io.jawg.osmcontributor.model.entities.PoiTag)1 PoiType (io.jawg.osmcontributor.model.entities.PoiType)1 PoiTypeTag (io.jawg.osmcontributor.model.entities.PoiTypeTag)1 ConstraintDto (io.jawg.osmcontributor.rest.dtos.dma.ConstraintDto)1 PoiTypeTagDto (io.jawg.osmcontributor.rest.dtos.dma.PoiTypeTagDto)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1