Search in sources :

Example 1 with Category

use of io.bssw.psip.backend.data.Category in project psip-automation by bssw-psip.

the class Assessment method generateSaveUrl.

/**
 * Generate a url that can be used to resume assessment
 * @param activity
 * @return url
 */
private String generateSaveUrl(Activity activity) {
    StringBuilder query = new StringBuilder();
    Iterator<Category> categoryIter = activity.getCategories().iterator();
    while (categoryIter.hasNext()) {
        Category category = categoryIter.next();
        query.append(category.getPath() + "=");
        StringBuilder nested = new StringBuilder();
        Iterator<Item> itemIter = category.getItems().iterator();
        while (itemIter.hasNext()) {
            Item item = itemIter.next();
            nested.append(item.getPath() + ":" + item.getScore().orElse(0));
            if (itemIter.hasNext()) {
                nested.append("+");
            }
        }
        query.append(nested.toString());
        if (categoryIter.hasNext()) {
            query.append("&");
        }
    }
    return getLocation() + RouteConfiguration.forSessionScope().getUrl(Assessment.class, "?" + query.toString());
}
Also used : ScoreItem(io.bssw.psip.ui.components.ScoreItem) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) Item(io.bssw.psip.backend.data.Item) Category(io.bssw.psip.backend.data.Category)

Example 2 with Category

use of io.bssw.psip.backend.data.Category in project psip-automation by bssw-psip.

the class Assessment method setParameter.

@Override
public void setParameter(BeforeEvent event, @WildcardParameter String parameter) {
    String desc = "";
    if (parameter.isEmpty()) {
        Activity activity = activityService.getActivity("Assessment");
        desc = activity.getDescription();
        // Get query parameters and restore state if there are any
        Location location = event.getLocation();
        QueryParameters queryParameters = location.getQueryParameters();
        Map<String, List<String>> parametersMap = queryParameters.getParameters();
        if (!parametersMap.isEmpty()) {
            restoreSaveUrl(activity, parametersMap);
        }
        createActivityLayout(activity);
    } else if (parameter.contains("/")) {
        // Item
        Item item = activityService.getItem("assessment/" + parameter);
        if (item != null) {
            desc = item.getDescription();
            createItemLayout(item);
        } else {
            System.out.println("invalid parameter=" + parameter);
        }
    } else {
        // Category
        Category category = activityService.getCategory("assessment/" + parameter);
        desc = category.getDescription();
        createCategoryLayout(category);
    }
    description.getElement().setText(desc);
}
Also used : ScoreItem(io.bssw.psip.ui.components.ScoreItem) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) Item(io.bssw.psip.backend.data.Item) Category(io.bssw.psip.backend.data.Category) Activity(io.bssw.psip.backend.data.Activity) List(java.util.List) QueryParameters(com.vaadin.flow.router.QueryParameters) Location(com.vaadin.flow.router.Location)

Example 3 with Category

use of io.bssw.psip.backend.data.Category in project psip-automation by bssw-psip.

the class Assessment method createActivitySummaryAsProgress.

@SuppressWarnings("unused")
private Component createActivitySummaryAsProgress(Activity activity) {
    FormLayout form = new FormLayout();
    for (Category category : activity.getCategories()) {
        int score = 0;
        for (Item item : category.getItems()) {
            if (item.getScore().isPresent()) {
                score += item.getScore().get();
            }
        }
        ProgressBar bar = new ProgressBar(0, category.getItems().size() > 0 ? 100 * category.getItems().size() : 1);
        bar.getElement().getStyle().set("height", "10px");
        bar.setValue(score);
        if (category.getItems().isEmpty()) {
            bar.getElement().setEnabled(false);
        }
        FormItem formItem = form.addFormItem(bar, category.getName());
        // Set width of label otherwise it will wrap
        formItem.getElement().getStyle().set("--vaadin-form-item-label-width", "15em");
        // FormLayout defaults to 2 columns so span both
        form.setColspan(formItem, 2);
    }
    return form;
}
Also used : FormLayout(com.vaadin.flow.component.formlayout.FormLayout) ScoreItem(io.bssw.psip.ui.components.ScoreItem) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) Item(io.bssw.psip.backend.data.Item) Category(io.bssw.psip.backend.data.Category) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) ProgressBar(com.vaadin.flow.component.progressbar.ProgressBar)

Example 4 with Category

use of io.bssw.psip.backend.data.Category in project psip-automation by bssw-psip.

the class GenData method run.

public void run(String input, Optional<String> output) throws IOException {
    Yaml yaml = new Yaml(new Constructor(Content.class));
    InputStream inputStream = FileUtils.openInputStream(new File(input));
    Content content = yaml.load(inputStream);
    if (output.isPresent()) {
        BufferedWriter bw = Files.newBufferedWriter(Paths.get(output.get()));
        PrintWriter writer = new PrintWriter(bw);
        int activity_id = 1;
        for (Activity activity : content.getActivities()) {
            writer.println("insert into activity (id, name, icon, path, description) values (" + activity_id++ + ", '" + activity.getName() + "', '" + activity.getIcon() + "', '" + activity.getPath() + "', '" + activity.getDescription() + "');");
        }
        writer.println("commit;");
        activity_id = 1;
        int score_id = 1;
        for (Activity activity : content.getActivities()) {
            for (Score score : activity.getScores()) {
                writer.println("insert into score (id, name, boost, value, color, activity_id) values (" + score_id++ + ", '" + score.getName() + "', '" + score.getBoost() + "', " + score.getValue() + ", '" + score.getColor() + "', " + activity_id + ");");
            }
            activity_id++;
        }
        writer.println("commit;");
        activity_id = 1;
        int category_id = 1;
        for (Activity activity : content.getActivities()) {
            for (Category category : activity.getCategories()) {
                writer.println("insert into category (id, name, activity_id, icon, path, description) values (" + category_id++ + ", '" + category.getName() + "', " + activity_id + ", '', '" + category.getPath() + "', '" + category.getDescription() + "');");
            }
            activity_id++;
        }
        writer.println("commit;");
        category_id = 1;
        int item_id = 1;
        for (Activity activity : content.getActivities()) {
            for (Category category : activity.getCategories()) {
                for (Item item : category.getItems()) {
                    writer.println("insert into item (id, name, category_id, icon, path, description) values (" + item_id++ + ", '" + item.getName() + "', " + category_id + ", '', '" + item.getPath() + "', '" + item.getDescription() + "');");
                }
                category_id++;
            }
        }
        writer.println("commit;");
        item_id = 1;
        for (Activity activity : content.getActivities()) {
            for (Category category : activity.getCategories()) {
                for (Item item : category.getItems()) {
                    for (String question : item.getQuestions()) {
                        writer.println("insert into item_questions (item_id, questions) values (" + item_id + ", '" + question + "');");
                    }
                    item_id++;
                }
            }
        }
        writer.println("commit;");
        writer.close();
    }
}
Also used : Category(io.bssw.psip.backend.data.Category) Constructor(org.yaml.snakeyaml.constructor.Constructor) InputStream(java.io.InputStream) Activity(io.bssw.psip.backend.data.Activity) Yaml(org.yaml.snakeyaml.Yaml) BufferedWriter(java.io.BufferedWriter) Item(io.bssw.psip.backend.data.Item) Score(io.bssw.psip.backend.data.Score) Content(io.bssw.psip.backend.data.Content) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 5 with Category

use of io.bssw.psip.backend.data.Category in project psip-automation by bssw-psip.

the class MainLayout method initNaviItems.

/**
 * Initialize the navigation items.
 */
private <C extends Component & HasUrlParameter<String>> void initNaviItems() {
    NaviMenu menu = naviDrawer.getMenu();
    menu.addNaviItem(VaadinIcon.HOME, "Home", Home.class);
    for (Activity activity : activityService.getActivities()) {
        Optional<Class<? extends Component>> optRoute = RouteConfiguration.forSessionScope().getRoute(activity.getPath(), Collections.singletonList(""));
        if (optRoute.isPresent()) {
            activityService.setActivity(activity.getName(), activity);
            Class<? extends Component> route = optRoute.get();
            NaviItem actNav = menu.addNaviItem(VaadinIcon.valueOf(activity.getIcon()), activity.getName(), optRoute.get());
            naviItems.put(activity.getPath(), actNav);
            ListIterator<Category> categoryIter = activity.getCategories().listIterator();
            Item prev = null;
            Item next = null;
            while (categoryIter.hasNext()) {
                Category category = categoryIter.next();
                if (HasUrlParameter.class.isAssignableFrom(route)) {
                    @SuppressWarnings("unchecked") NaviItem catNav = menu.addNaviItem(actNav, category.getName(), (Class<? extends C>) route, category.getPath());
                    String catPath = activity.getPath() + "/" + category.getPath();
                    activityService.setCategory(catPath, category);
                    naviItems.put(catPath, catNav);
                    ListIterator<Item> itemIter = category.getItems().listIterator();
                    while (itemIter.hasNext()) {
                        Item item = itemIter.next();
                        next = findNextItem(itemIter, categoryIter, category, activity);
                        String itemNavPath = category.getPath() + "/" + item.getPath();
                        NaviItem naviItem = menu.addNaviItem(catNav, item.getName(), (Class<? extends C>) route, itemNavPath);
                        String itemPath = activity.getPath() + "/" + itemNavPath;
                        activityService.setItem(itemPath, item);
                        activityService.setPrevItem(itemPath, prev);
                        activityService.setNextItem(itemPath, next);
                        naviItems.put(itemPath, naviItem);
                        String p = prev != null ? prev.getName() : "";
                        String n = next != null ? next.getName() : "";
                        prev = item;
                    }
                    catNav.setExpanded(false);
                }
            }
            actNav.setExpanded(false);
        }
    }
}
Also used : Category(io.bssw.psip.backend.data.Category) NaviMenu(io.bssw.psip.ui.components.navigation.drawer.NaviMenu) NaviItem(io.bssw.psip.ui.components.navigation.drawer.NaviItem) Activity(io.bssw.psip.backend.data.Activity) NaviItem(io.bssw.psip.ui.components.navigation.drawer.NaviItem) Item(io.bssw.psip.backend.data.Item) Component(com.vaadin.flow.component.Component)

Aggregations

Category (io.bssw.psip.backend.data.Category)5 Item (io.bssw.psip.backend.data.Item)5 FormItem (com.vaadin.flow.component.formlayout.FormLayout.FormItem)3 Activity (io.bssw.psip.backend.data.Activity)3 ScoreItem (io.bssw.psip.ui.components.ScoreItem)3 Component (com.vaadin.flow.component.Component)1 FormLayout (com.vaadin.flow.component.formlayout.FormLayout)1 ProgressBar (com.vaadin.flow.component.progressbar.ProgressBar)1 Location (com.vaadin.flow.router.Location)1 QueryParameters (com.vaadin.flow.router.QueryParameters)1 Content (io.bssw.psip.backend.data.Content)1 Score (io.bssw.psip.backend.data.Score)1 NaviItem (io.bssw.psip.ui.components.navigation.drawer.NaviItem)1 NaviMenu (io.bssw.psip.ui.components.navigation.drawer.NaviMenu)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 List (java.util.List)1 Yaml (org.yaml.snakeyaml.Yaml)1