• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 43727992423

15 Nov 2026 03:06PM UTC coverage: 56.023% (+0.03%) from 55.99%
43727992423

push

github

web-flow
Merge pull request #926 from knowledgepixels/fix/publish-form-missing-template

fix(publish): say which template could not be loaded instead of failing

6798 of 13001 branches covered (52.29%)

Branch coverage included in aggregate %.

11750 of 20107 relevant lines covered (58.44%)

9.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.4
src/main/java/com/knowledgepixels/nanodash/component/ThreadNodePanel.java
1
package com.knowledgepixels.nanodash.component;
2

3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.List;
6

7
import org.apache.wicket.Component;
8
import org.apache.wicket.ajax.AjaxRequestTarget;
9
import org.apache.wicket.ajax.markup.html.AjaxLink;
10
import org.apache.wicket.behavior.AttributeAppender;
11
import org.apache.wicket.markup.html.WebMarkupContainer;
12
import org.apache.wicket.markup.html.basic.Label;
13
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
14
import org.apache.wicket.markup.html.panel.Panel;
15
import org.apache.wicket.markup.repeater.RepeatingView;
16
import org.apache.wicket.request.cycle.RequestCycle;
17
import org.apache.wicket.request.mapper.parameter.PageParameters;
18
import org.apache.wicket.request.resource.ContextRelativeResourceReference;
19
import org.apache.wicket.util.string.Strings;
20
import org.eclipse.rdf4j.model.IRI;
21

22
import com.knowledgepixels.nanodash.template.TemplateUnavailableException;
23
import com.knowledgepixels.nanodash.DiscussionThread;
24
import com.knowledgepixels.nanodash.SpaceMemberRole;
25
import com.knowledgepixels.nanodash.Utils;
26
import com.knowledgepixels.nanodash.View;
27
import com.knowledgepixels.nanodash.domain.IndividualAgent;
28
import com.knowledgepixels.nanodash.domain.ProfilePicture;
29
import com.knowledgepixels.nanodash.domain.User;
30
import com.knowledgepixels.nanodash.page.PublishPage;
31
import com.knowledgepixels.nanodash.page.UserPage;
32
import com.knowledgepixels.nanodash.page.ViewResultsPage;
33

34
public class ThreadNodePanel extends Panel {
35

36
    private final ThreadContext context;
37
    private final DiscussionThread.Node node;
38
    private final int depth;
39

40
    private int shown;
41
    private boolean collapsed = false;
9 ✔
42
    private IRI openAction = null;
9 ✔
43

44
    ThreadNodePanel(String markupId, ThreadContext context, DiscussionThread.Node node, int depth) {
45
        super(markupId);
9 ✔
46
        this.context = context;
9 ✔
47
        this.node = node;
9 ✔
48
        this.depth = depth;
9 ✔
49
        this.shown = Math.min(context.getPageSize(), node.getChildren().size());
24 ✔
50
        setOutputMarkupId(true);
12 ✔
51
        build();
6 ✔
52
    }
3 ✔
53

54
    private void rebuild(AjaxRequestTarget target) {
55
        build();
6 ✔
56
        target.add(this);
24 ✔
57
    }
3 ✔
58

59
    private void build() {
60
        addOrReplace(collapsed ? buildCollapsedLine() : new Label("collapsedLine").setVisible(false));
60 ✔
61
        addOrReplace(collapsed ? new Label("card").setVisible(false) : buildCard());
60 ✔
62
        addOrReplace(buildResponseForm());
30 ✔
63
        addOrReplace(collapsed ? new Label("childrenArea").setVisible(false) : buildChildrenArea());
60 ✔
64
    }
3 ✔
65

66
    private WebMarkupContainer buildCollapsedLine() {
67
        WebMarkupContainer line = new WebMarkupContainer("collapsedLine");
15 ✔
68
        AjaxLink<Void> expand = new AjaxLink<>("expand") {
39 ✔
69
            @Override
70
            public void onClick(AjaxRequestTarget target) {
71
                collapsed = false;
×
72
                rebuild(target);
×
73
            }
×
74
        };
75
        line.add(expand);
27 ✔
76
        Label badge = new Label("collapsedBadge", node.getBadge());
24 ✔
77
        badge.add(new org.apache.wicket.behavior.AttributeAppender("class", " " + node.getRelation().getCssClass()));
51 ✔
78
        line.add(badge);
27 ✔
79
        StringBuilder summary = new StringBuilder(node.getLabel());
21 ✔
80
        String author = displayAuthor();
9 ✔
81
        if (author != null) {
6 !
82
            summary.append(" · ").append(author);
×
83
        }
84
        summary.append(" (collapsed, ").append(countText(node.getDescendantCount())).append(")");
33 ✔
85
        line.add(new Label("collapsedSummary", summary.toString()));
42 ✔
86
        return line;
6 ✔
87
    }
88

89
    private WebMarkupContainer buildCard() {
90
        WebMarkupContainer card = new WebMarkupContainer("card");
15 ✔
91
        card.add(new org.apache.wicket.behavior.AttributeAppender("class", " " + node.getRelation().getCssClass()));
51 ✔
92

93
        AjaxLink<Void> collapse = new AjaxLink<>("collapse") {
39 ✔
94
            @Override
95
            public void onClick(AjaxRequestTarget target) {
96
                collapsed = true;
12 ✔
97
                openAction = null;
12 ✔
98
                rebuild(target);
12 ✔
99
            }
3 ✔
100
        };
101
        collapse.setVisible(!node.getChildren().isEmpty());
33 ✔
102
        card.add(collapse);
27 ✔
103

104
        Label badge = new Label("badge", node.getBadge());
24 ✔
105
        badge.add(new AttributeAppender("class", " " + node.getRelation().getCssClass()));
51 ✔
106
        card.add(badge);
27 ✔
107

108
        int direct = node.getChildren().size();
15 ✔
109
        Label count = new Label("count", depth == 0
18 ✔
110
                ? direct + " direct " + (direct == 1 ? "response" : "responses")
27 ✔
111
                : countText(node.getDescendantCount()));
18 ✔
112
        count.setVisible(direct > 0);
24 ✔
113
        card.add(count);
27 ✔
114

115
        card.add(new NanodashLink("statement", node.getId(), null, null, node.getLabel(), context.getContextId()));
69 ✔
116
        card.add(authorLabel());
30 ✔
117
        String date = node.getDate();
12 ✔
118
        Label dateLabel = new Label("date", date == null ? "" : Utils.friendlyDateHtml(date, date));
27 !
119
        dateLabel.setEscapeModelStrings(false);
12 ✔
120
        dateLabel.setVisible(date != null);
18 !
121
        card.add(dateLabel);
27 ✔
122

123
        card.add(buildActions());
30 ✔
124
        return card;
6 ✔
125
    }
126

127
    private WebMarkupContainer buildActions() {
128
        RepeatingView actions = new RepeatingView("actions");
15 ✔
129
        for (IRI actionIri : context.getResponseActions()) {
24 !
130
            DiscussionThread.Relation relation = context.getView().getResponseRelationForAction(actionIri);
×
131
            if (relation == null) {
×
132
                continue;
×
133
            }
134
            if (!SpaceMemberRole.isViewerEntitled(context.getView().getActionVisibleTo(actionIri),
×
135
                    context.getEntitlementResource(), context.getRefRoot())) {
×
136
                continue;
×
137
            }
138
            // An action whose mappings this response does not satisfy is not offered on it,
139
            // exactly as its button would be left out of a result row.
140
            if (ViewActionMappings.buildResponseActionParams(context.getView(), actionIri, node,
×
141
                    context.getQueryRef(), context.getContextId(), context.getPartId(),
×
142
                    context.getPostPublishTab()) == null) {
×
143
                continue;
×
144
            }
145
            WebMarkupContainer item = new WebMarkupContainer(actions.newChildId());
×
146
            AjaxLink<Void> link = new AjaxLink<>("action") {
×
147
                @Override
148
                public void onClick(AjaxRequestTarget target) {
149
                    // A second click on the open action closes its form again.
150
                    openAction = actionIri.equals(openAction) ? null : actionIri;
×
151
                    rebuild(target);
×
152
                }
×
153
            };
154
            if (actionIri.equals(openAction)) {
×
155
                link.add(new AttributeAppender("class", " thread-action-open"));
×
156
            }
157
            link.add(new Label("actionLabel", relation.getActionLabel() == null
×
158
                    ? relation.getId() : relation.getActionLabel()));
×
159
            item.add(link);
×
160
            actions.add(item);
×
161
        }
×
162
        WebMarkupContainer container = new WebMarkupContainer("actionsContainer");
15 ✔
163
        container.add(actions);
27 ✔
164
        container.setVisible(actions.size() > 0);
21 !
165
        return container;
6 ✔
166
    }
167

168
    private Component buildResponseForm() {
169
        if (openAction == null) {
9 !
170
            return new Label("responseForm").setVisible(false);
21 ✔
171
        }
172
        PageParameters params = ViewActionMappings.buildResponseActionParams(context.getView(), openAction,
×
173
                node, context.getQueryRef(), context.getContextId(), context.getPartId(),
×
174
                context.getPostPublishTab());
×
175
        if (params == null) {
×
176
            openAction = null;
×
177
            return new Label("responseForm").setVisible(false);
×
178
        }
179
        try {
180
            return new PublishForm("responseForm", params, PublishPage.class, null);
×
181
        } catch (TemplateUnavailableException ex) {
×
182
            return PublishForm.templateUnavailableNotice("responseForm", ex);
×
183
        }
184
    }
185

186
    private WebMarkupContainer buildChildrenArea() {
187
        WebMarkupContainer area = new WebMarkupContainer("childrenArea");
15 ✔
188
        List<DiscussionThread.Node> children = node.getChildren();
12 ✔
189

190
        RepeatingView childViews = new RepeatingView("children");
15 ✔
191
        boolean tooDeep = depth + 1 >= context.getMaxDepth();
36 ✔
192
        if (!tooDeep) {
6 ✔
193
            for (int i = 0; i < Math.min(shown, children.size()); i++) {
33 ✔
194
                childViews.add(new ThreadNodePanel(childViews.newChildId(), context, children.get(i), depth + 1));
69 ✔
195
            }
196
        }
197
        area.add(childViews);
27 ✔
198

199
        int remaining = children.size() - shown;
18 ✔
200
        AjaxLink<Void> loadMore = new AjaxLink<>("loadMore") {
39 ✔
201
            @Override
202
            public void onClick(AjaxRequestTarget target) {
203
                shown = Math.min(shown + context.getPageSize(), node.getChildren().size());
51 ✔
204
                rebuild(target);
12 ✔
205
            }
3 ✔
206
        };
207
        loadMore.add(new Label("loadMoreLabel", "+ load " + remaining + " more "
33 ✔
208
                + (remaining == 1 ? "response" : "responses")));
27 ✔
209
        loadMore.setVisible(!tooDeep && remaining > 0);
30 ✔
210
        area.add(loadMore);
27 ✔
211

212
        BookmarkablePageLink<Void> continueLink = new BookmarkablePageLink<>("continueThread",
15 ✔
213
                ViewResultsPage.class, continueParams());
9 ✔
214
        continueLink.add(new Label("continueLabel", "↳ continue this thread ("
36 ✔
215
                + countText(node.getDescendantCount()) + ", "
12 ✔
216
                + node.getDepthBelow() + " " + (node.getDepthBelow() == 1 ? "level" : "levels") + " deep)"));
36 ✔
217
        continueLink.setVisible(tooDeep && !children.isEmpty() && context.getViewId() != null);
45 !
218
        area.add(continueLink);
27 ✔
219

220
        area.setVisible(!children.isEmpty());
27 ✔
221
        return area;
6 ✔
222
    }
223

224
    private PageParameters continueParams() {
225
        PageParameters params = new PageParameters();
12 ✔
226
        if (context.getViewId() == null) {
12 ✔
227
            return params;
6 ✔
228
        }
229
        params.set("view", context.getViewId());
21 ✔
230
        params.set("queryparam_" + context.getQueryField(), node.getId());
30 ✔
231
        return params;
6 ✔
232
    }
233

234
    private static String countText(int count) {
235
        return count + " " + (count == 1 ? "response" : "responses");
27 ✔
236
    }
237

238
    private String displayAuthor() {
239
        String label = node.getUserLabel();
12 ✔
240
        if (label != null) {
6 !
241
            return label;
×
242
        }
243
        String iri = node.getUserIri();
12 ✔
244
        if (iri == null) {
6 !
245
            return null;
6 ✔
246
        }
247
        return User.getShortDisplayName(Utils.vf.createIRI(iri));
×
248
    }
249

250
    private Label authorLabel() {
251
        String iriString = node.getUserIri();
12 ✔
252
        if (iriString == null) {
6 !
253
            return (Label) new Label("author", "").setVisible(false);
27 ✔
254
        }
255
        IRI userIri = Utils.vf.createIRI(iriString);
×
256
        ProfilePicture picture = User.getProfilePicture(userIri);
×
257
        String imgSrc;
258
        String iconClass;
259
        if (picture != null) {
×
260
            imgSrc = Strings.escapeMarkup(picture.getSrc()).toString();
×
261
            iconClass = "user-icon";
×
262
        } else if (IndividualAgent.isSoftware(userIri)) {
×
263
            imgSrc = RequestCycle.get().urlFor(new ContextRelativeResourceReference("images/bot-icon.svg", false), null).toString();
×
264
            iconClass = "bot-icon";
×
265
        } else {
266
            imgSrc = RequestCycle.get().urlFor(new ContextRelativeResourceReference("images/user-icon.svg", false), null).toString();
×
267
            iconClass = "user-icon";
×
268
        }
269
        String name = displayAuthor();
×
270
        String url = UserPage.MOUNT_PATH + "?id=" + Utils.urlEncode(iriString);
×
271
        String html = "<img class=\"" + iconClass + "\" src=\"" + imgSrc + "\" /> <a href=\""
×
272
                + Strings.escapeMarkup(url) + "\">" + Strings.escapeMarkup(name) + "</a>";
×
273
        Label label = new Label("author", html);
×
274
        label.setEscapeModelStrings(false);
×
275
        return label;
×
276
    }
277

278
    static class ThreadContext implements Serializable {
279

280
        private static final long serialVersionUID = 1L;
281

282
        private final View view;
283
        private final String viewId;
284
        private final String queryField;
285
        private final org.nanopub.extra.services.QueryRef queryRef;
286
        private final String contextId;
287
        private final String partId;
288
        private final String postPublishTab;
289
        private final String refRoot;
290
        private final com.knowledgepixels.nanodash.domain.AbstractResourceWithProfile entitlementResource;
291
        private final int pageSize;
292
        private final int maxDepth;
293
        private final List<IRI> responseActions;
294

295
        ThreadContext(View view, org.nanopub.extra.services.QueryRef queryRef, String contextId, String partId,
296
                String postPublishTab, String refRoot,
297
                com.knowledgepixels.nanodash.domain.AbstractResourceWithProfile entitlementResource,
298
                int pageSize, int maxDepth) {
6 ✔
299
            this.view = view;
9 ✔
300
            this.viewId = view == null ? null : view.getId();
24 ✔
301
            String field = view == null ? null : view.getQueryField();
21 ✔
302
            this.queryField = field == null ? "resource" : field;
21 ✔
303
            this.queryRef = queryRef;
9 ✔
304
            this.contextId = contextId;
9 ✔
305
            this.partId = partId;
9 ✔
306
            this.postPublishTab = postPublishTab;
9 ✔
307
            this.refRoot = refRoot;
9 ✔
308
            this.entitlementResource = entitlementResource;
9 ✔
309
            this.pageSize = pageSize;
9 ✔
310
            this.maxDepth = maxDepth;
9 ✔
311
            List<IRI> actions = new ArrayList<>();
12 ✔
312
            if (view != null) {
6 ✔
313
                // Wherever an action is declared, naming a relation is what makes it a
314
                // response action here. A result action that names one is offered on every
315
                // card as well as at the top of the view: at the top it responds to the
316
                // page's resource, on a card to that response.
317
                for (IRI actionIri : view.getViewEntryActionList()) {
21 !
318
                    if (view.getResponseRelationForAction(actionIri) != null) {
×
319
                        actions.add(actionIri);
×
320
                    }
321
                }
×
322
                for (IRI actionIri : view.getViewResultActionList()) {
21 !
323
                    if (view.getResponseRelationForAction(actionIri) != null) {
×
324
                        actions.add(actionIri);
×
325
                    }
326
                }
×
327
            }
328
            this.responseActions = actions;
9 ✔
329
        }
3 ✔
330

331
        View getView() {
332
            return view;
×
333
        }
334

335
        String getViewId() {
336
            return viewId;
9 ✔
337
        }
338

339
        String getQueryField() {
340
            return queryField;
9 ✔
341
        }
342

343
        org.nanopub.extra.services.QueryRef getQueryRef() {
344
            return queryRef;
×
345
        }
346

347
        String getContextId() {
348
            return contextId;
9 ✔
349
        }
350

351
        String getPartId() {
352
            return partId;
×
353
        }
354

355
        String getPostPublishTab() {
356
            return postPublishTab;
×
357
        }
358

359
        String getRefRoot() {
360
            return refRoot;
×
361
        }
362

363
        com.knowledgepixels.nanodash.domain.AbstractResourceWithProfile getEntitlementResource() {
364
            return entitlementResource;
×
365
        }
366

367
        /**
368
         * How many responses of a level are shown before "load more".
369
         */
370
        int getPageSize() {
371
            return pageSize;
9 ✔
372
        }
373

374
        /**
375
         * The depth at which responses give way to a "continue this thread"
376
         * link.
377
         */
378
        int getMaxDepth() {
379
            return maxDepth;
9 ✔
380
        }
381

382
        List<IRI> getResponseActions() {
383
            return responseActions;
9 ✔
384
        }
385

386
    }
387

388
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc