Search in sources :

Example 1 with StaticView

use of com.structurizr.view.StaticView in project dsl by structurizr.

the class FilteredViewParser method parse.

FilteredView parse(DslContext context, Tokens tokens) {
    if (tokens.hasMoreThan(DESCRIPTION_INDEX)) {
        throw new RuntimeException("Too many tokens, expected: " + GRAMMAR);
    }
    if (!tokens.includes(TAGS_INDEX)) {
        throw new RuntimeException("Expected: " + GRAMMAR);
    }
    Workspace workspace = context.getWorkspace();
    String key;
    StaticView baseView;
    String baseKey = tokens.get(BASE_KEY_INDEX);
    String mode = tokens.get(MODE_INDEX);
    String tagsAsString = tokens.get(TAGS_INDEX);
    Set<String> tags = new HashSet<>();
    DecimalFormat format = new DecimalFormat("000");
    for (String tag : tagsAsString.split(",")) {
        if (!StringUtils.isNullOrEmpty(tag)) {
            tags.add(tag.trim());
        }
    }
    String description = "";
    if (tokens.includes(DESCRIPTION_INDEX)) {
        description = tokens.get(DESCRIPTION_INDEX);
    }
    FilterMode filterMode;
    if (FILTER_MODE_INCLUDE.equalsIgnoreCase(mode)) {
        filterMode = FilterMode.Include;
    } else if (FILTER_MODE_EXCLUDE.equalsIgnoreCase(mode)) {
        filterMode = FilterMode.Exclude;
    } else {
        throw new RuntimeException("Filter mode should be include or exclude");
    }
    if (workspace.getViews().getViews().stream().noneMatch(v -> v.getKey().equals(baseKey))) {
        throw new RuntimeException("The view \"" + baseKey + "\" does not exist");
    }
    baseView = (StaticView) workspace.getViews().getViews().stream().filter(v -> v instanceof StaticView && v.getKey().equals(baseKey)).findFirst().orElse(null);
    if (baseView == null) {
        throw new RuntimeException("The view \"" + baseKey + "\" must be a System Landscape, System Context, Container, or Component view");
    }
    if (tokens.includes(KEY_INDEX)) {
        key = tokens.get(KEY_INDEX);
    } else {
        key = baseView.getKey() + "-" + VIEW_TYPE + "-" + format.format(workspace.getViews().getDynamicViews().size() + 1);
    }
    validateViewKey(key);
    return workspace.getViews().createFilteredView(baseView, key, description, filterMode, tags.toArray(new String[0]));
}
Also used : DecimalFormat(java.text.DecimalFormat) FilterMode(com.structurizr.view.FilterMode) StaticView(com.structurizr.view.StaticView) Workspace(com.structurizr.Workspace) HashSet(java.util.HashSet)

Aggregations

Workspace (com.structurizr.Workspace)1 FilterMode (com.structurizr.view.FilterMode)1 StaticView (com.structurizr.view.StaticView)1 DecimalFormat (java.text.DecimalFormat)1 HashSet (java.util.HashSet)1