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

knowledgepixels / nanodash / 28574924141

02 Jul 2026 08:02AM UTC coverage: 28.037% (-0.02%) from 28.06%
28574924141

push

github

web-flow
Merge pull request #528 from knowledgepixels/fix/derive-root-definition-527

fix: reset root definition when deriving; add override fill mode

1736 of 7061 branches covered (24.59%)

Branch coverage included in aggregate %.

3625 of 12060 relevant lines covered (30.06%)

4.45 hits per line

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

0.52
src/main/java/com/knowledgepixels/nanodash/component/PublishForm.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.*;
4
import com.knowledgepixels.nanodash.domain.AbstractResourceWithProfile;
5
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
6
import com.knowledgepixels.nanodash.domain.IndividualAgent;
7
import com.knowledgepixels.nanodash.domain.User;
8
import com.knowledgepixels.nanodash.page.*;
9
import com.knowledgepixels.nanodash.repository.MaintainedResourceRepository;
10
import com.knowledgepixels.nanodash.repository.SpaceRepository;
11
import com.knowledgepixels.nanodash.template.*;
12
import org.apache.commons.lang3.Strings;
13
import org.apache.wicket.Component;
14
import org.apache.wicket.RestartResponseException;
15
import org.apache.wicket.ajax.AjaxEventBehavior;
16
import org.apache.wicket.ajax.AjaxRequestTarget;
17
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
18
import org.apache.wicket.feedback.FeedbackMessage;
19
import org.apache.wicket.markup.html.WebMarkupContainer;
20
import org.apache.wicket.markup.html.WebPage;
21
import org.apache.wicket.markup.html.basic.Label;
22
import org.apache.wicket.markup.html.form.Button;
23
import org.apache.wicket.markup.html.form.CheckBox;
24
import org.apache.wicket.markup.html.form.Form;
25
import org.apache.wicket.markup.html.form.FormComponent;
26
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
27
import org.apache.wicket.markup.html.list.ListItem;
28
import org.apache.wicket.markup.html.list.ListView;
29
import org.apache.wicket.markup.html.panel.FeedbackPanel;
30
import org.apache.wicket.markup.html.panel.Panel;
31
import org.apache.wicket.markup.repeater.Item;
32
import org.apache.wicket.markup.repeater.data.DataView;
33
import org.apache.wicket.markup.repeater.data.ListDataProvider;
34
import org.apache.wicket.model.IModel;
35
import org.apache.wicket.model.Model;
36
import org.apache.wicket.request.mapper.parameter.PageParameters;
37
import org.eclipse.rdf4j.model.IRI;
38
import org.eclipse.rdf4j.model.Literal;
39
import org.eclipse.rdf4j.model.Statement;
40
import org.eclipse.rdf4j.model.ValueFactory;
41
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
42
import org.eclipse.rdf4j.model.vocabulary.FOAF;
43
import org.eclipse.rdf4j.model.vocabulary.RDF;
44
import org.eclipse.rdf4j.model.vocabulary.RDFS;
45
import org.nanopub.MalformedNanopubException;
46
import org.nanopub.Nanopub;
47
import org.nanopub.NanopubAlreadyFinalizedException;
48
import org.nanopub.NanopubCreator;
49
import org.nanopub.extra.security.SignNanopub;
50
import org.nanopub.extra.security.SignatureAlgorithm;
51
import org.nanopub.extra.security.TransformContext;
52
import org.nanopub.extra.server.PublishNanopub;
53
import org.nanopub.extra.services.ApiResponse;
54
import org.nanopub.extra.services.ApiResponseEntry;
55
import org.nanopub.extra.services.QueryRef;
56
import org.nanopub.vocabulary.NPX;
57
import org.nanopub.vocabulary.NTEMPLATE;
58
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60
import org.wicketstuff.select2.ChoiceProvider;
61
import org.wicketstuff.select2.Response;
62
import org.wicketstuff.select2.Select2Choice;
63

64
import java.lang.reflect.InvocationTargetException;
65
import java.util.*;
66

67
/**
68
 * Form for publishing a nanopublication.
69
 */
70
public class PublishForm extends Panel {
71

72
    private static final Logger logger = LoggerFactory.getLogger(PublishForm.class);
×
73

74
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
×
75

76
    public static final String CREATOR_PUB_INFO_TEMPLATE = "https://w3id.org/np/RAukAcWHRDlkqxk7H2XNSegc1WnHI569INvNr-xdptDGI";
77
    public static final String LICENSE_PUB_INFO_TEMPLATE = "https://w3id.org/np/RACJ58Gvyn91LqCKIO9zu1eijDQIeEff28iyDrJgjSJF8";
78
    public static final String DEFAULT_PROV_TEMPLATE = "https://w3id.org/np/RA7lSq6MuK_TIC6JMSHvLtee3lpLoZDOqLJCLXevnrPoU";
79
    private static final String supersedesPubInfoTemplateId = "https://w3id.org/np/RAoTD7udB2KtUuOuAe74tJi1t3VzK0DyWS7rYVAq1GRvw";
80
    private static final String derivesFromPubInfoTemplateId = "https://w3id.org/np/RARW4MsFkHuwjycNElvEVtuMjpf4yWDL10-0C5l2MqqRQ";
81

82
    private static final String[] fixedPubInfoTemplates = new String[]{CREATOR_PUB_INFO_TEMPLATE, LICENSE_PUB_INFO_TEMPLATE};
×
83

84
    /**
85
     * Fill modes for the nanopublication to be created.
86
     */
87
    public enum FillMode {
9✔
88
        /**
89
         * Use fill mode
90
         */
91
        USE,
18✔
92
        /**
93
         * Supersede fill mode
94
         */
95
        SUPERSEDE,
18✔
96
        /**
97
         * Derive fill mode
98
         */
99
        DERIVE,
18✔
100
        /**
101
         * Override fill mode: like {@link #DERIVE} in that it records a
102
         * {@code prov:wasDerivedFrom} link, but (like {@link #SUPERSEDE}) it keeps the
103
         * source nanopub's introduced resource IRIs and its root definition nanopub.
104
         */
105
        OVERRIDE
18✔
106
    }
107

108
    protected Form<?> form;
109
    protected FeedbackPanel feedbackPanel;
110
    private final TemplateContext assertionContext;
111
    private TemplateContext provenanceContext;
112
    private final List<TemplateContext> pubInfoContexts = new ArrayList<>();
×
113
    private final Map<String, TemplateContext> pubInfoContextMap = new HashMap<>();
×
114
    private final List<TemplateContext> requiredPubInfoContexts = new ArrayList<>();
×
115
    private String targetNamespace;
116
    private final Class<? extends WebPage> confirmPageClass;
117

118
    /**
119
     * Constructor for the PublishForm.
120
     *
121
     * @param id               the Wicket component ID
122
     * @param pageParams       the parameters for the page, which may include information on how to fill the form
123
     * @param publishPageClass the class of the page to redirect to after successful publication
124
     * @param confirmPageClass the class of the confirmation page to show after publication
125
     */
126
    public PublishForm(String id, final PageParameters pageParams, Class<? extends WebPage> publishPageClass, Class<? extends WebPage> confirmPageClass) {
127
        super(id);
×
128
        setOutputMarkupId(true);
×
129
        this.confirmPageClass = confirmPageClass;
×
130

131
        WebMarkupContainer linkMessageItem = new WebMarkupContainer("link-message-item");
×
132
        if (pageParams.get("link-message").isNull()) {
×
133
            linkMessageItem.add(new Label("link-message", ""));
×
134
            linkMessageItem.setVisible(false);
×
135
        } else {
136
            linkMessageItem.add(new Label("link-message", Utils.sanitizeHtml(pageParams.get("link-message").toString())).setEscapeModelStrings(false));
×
137
        }
138
        add(linkMessageItem);
×
139

140
        final Nanopub fillNp;
141
        final FillMode fillMode;
142
        boolean fillOnlyAssertion = false;
×
143
        if (!pageParams.get("use").isNull()) {
×
144
            fillNp = Utils.getNanopub(pageParams.get("use").toString());
×
145
            fillMode = FillMode.USE;
×
146
        } else if (!pageParams.get("use-a").isNull()) {
×
147
            fillNp = Utils.getNanopub(pageParams.get("use-a").toString());
×
148
            fillMode = FillMode.USE;
×
149
            fillOnlyAssertion = true;
×
150
        } else if (!pageParams.get("supersede").isNull()) {
×
151
            fillNp = Utils.getNanopub(pageParams.get("supersede").toString());
×
152
            fillMode = FillMode.SUPERSEDE;
×
153
            targetNamespace = pageParams.get("supersede").toString().replaceFirst("RA[A-Za-z0-9-_]{43}$", "");
×
154
        } else if (!pageParams.get("supersede-a").isNull()) {
×
155
            fillNp = Utils.getNanopub(pageParams.get("supersede-a").toString());
×
156
            fillMode = FillMode.SUPERSEDE;
×
157
            fillOnlyAssertion = true;
×
158
        } else if (!pageParams.get("derive").isNull()) {
×
159
            fillNp = Utils.getNanopub(pageParams.get("derive").toString());
×
160
            fillMode = FillMode.DERIVE;
×
161
        } else if (!pageParams.get("derive-a").isNull()) {
×
162
            fillNp = Utils.getNanopub(pageParams.get("derive-a").toString());
×
163
            fillMode = FillMode.DERIVE;
×
164
            fillOnlyAssertion = true;
×
165
        } else if (!pageParams.get("override").isNull()) {
×
166
            fillNp = Utils.getNanopub(pageParams.get("override").toString());
×
167
            fillMode = FillMode.OVERRIDE;
×
168
        } else if (!pageParams.get("override-a").isNull()) {
×
169
            fillNp = Utils.getNanopub(pageParams.get("override-a").toString());
×
170
            fillMode = FillMode.OVERRIDE;
×
171
            fillOnlyAssertion = true;
×
172
        } else if (!pageParams.get("fill-all").isNull()) {
×
173
            // TODO: This is deprecated and should be removed at some point
174
            fillNp = Utils.getNanopub(pageParams.get("fill-all").toString());
×
175
            fillMode = FillMode.USE;
×
176
        } else if (!pageParams.get("fill").isNull()) {
×
177
            // TODO: This is deprecated and should be removed at some point
178
            fillNp = Utils.getNanopub(pageParams.get("fill").toString());
×
179
            fillMode = FillMode.SUPERSEDE;
×
180
        } else {
181
            fillNp = null;
×
182
            fillMode = null;
×
183
        }
184

185
        final TemplateData td = TemplateData.get();
×
186

187
        // TODO Properly integrate this namespace feature:
188
        String templateId = pageParams.get("template").toString();
×
189
        if (td.getTemplate(templateId).getTargetNamespace() != null) {
×
190
            targetNamespace = td.getTemplate(templateId).getTargetNamespace();
×
191
        }
192
        if (!pageParams.get("target-namespace").isNull()) {
×
193
            targetNamespace = pageParams.get("target-namespace").toString();
×
194
        }
195
        if (targetNamespace == null) {
×
196
            targetNamespace = Template.DEFAULT_TARGET_NAMESPACE;
×
197
        }
198
        String targetNamespaceLabel = targetNamespace + "...";
×
199
        targetNamespace = targetNamespace + "~~~ARTIFACTCODE~~~/";
×
200

201
        assertionContext = new TemplateContext(ContextType.ASSERTION, templateId, "statement", targetNamespace);
×
202
        assertionContext.setFillMode(fillMode);
×
203
        final String prTemplateId;
204
        if (pageParams.get("prtemplate").toString() != null) {
×
205
            prTemplateId = pageParams.get("prtemplate").toString();
×
206
        } else {
207
            if (fillNp != null && !fillOnlyAssertion) {
×
208
                if (td.getProvenanceTemplateId(fillNp) != null) {
×
209
                    prTemplateId = td.getProvenanceTemplateId(fillNp).stringValue();
×
210
                } else {
211
                    prTemplateId = "http://purl.org/np/RAcm8OurwUk15WOgBM9wySo-T3a5h6as4K8YR5MBrrxUc";
×
212
                }
213
            } else if (assertionContext.getTemplate().getDefaultProvenance() != null) {
×
214
                prTemplateId = assertionContext.getTemplate().getDefaultProvenance().stringValue();
×
215
            } else {
216
                prTemplateId = DEFAULT_PROV_TEMPLATE;
×
217
            }
218
        }
219
        provenanceContext = new TemplateContext(ContextType.PROVENANCE, prTemplateId, "pr-statement", targetNamespace);
×
220
        for (String t : fixedPubInfoTemplates) {
×
221
            // TODO consistently check for latest versions of templates here and below:
222
            TemplateContext c = new TemplateContext(ContextType.PUBINFO, t, "pi-statement", targetNamespace);
×
223
            pubInfoContexts.add(c);
×
224
            pubInfoContextMap.put(c.getTemplate().getId(), c);
×
225
            requiredPubInfoContexts.add(c);
×
226
            if (t.equals(LICENSE_PUB_INFO_TEMPLATE) && fillMode != FillMode.SUPERSEDE && fillMode != FillMode.DERIVE && fillMode != FillMode.OVERRIDE) {
×
227
                IRI defaultLicense = User.getDefaultLicense(NanodashSession.get().getUserIri());
×
228
                if (defaultLicense != null) {
×
229
                    c.setParam("license", defaultLicense.stringValue());
×
230
                }
231
            }
232
        }
233
        if (fillMode == FillMode.SUPERSEDE) {
×
234
            TemplateContext c = new TemplateContext(ContextType.PUBINFO, supersedesPubInfoTemplateId, "pi-statement", targetNamespace);
×
235
            pubInfoContexts.add(c);
×
236
            pubInfoContextMap.put(supersedesPubInfoTemplateId, c);
×
237
            //requiredPubInfoContexts.add(c);
238
            c.setParam("np", fillNp.getUri().stringValue());
×
239
        } else if (fillMode == FillMode.DERIVE || fillMode == FillMode.OVERRIDE) {
×
240
            TemplateContext c = new TemplateContext(ContextType.PUBINFO, derivesFromPubInfoTemplateId, "pi-statement", targetNamespace);
×
241
            pubInfoContexts.add(c);
×
242
            pubInfoContextMap.put(derivesFromPubInfoTemplateId, c);
×
243
            c.setParam("np", fillNp.getUri().stringValue());
×
244
        }
245
        for (IRI r : assertionContext.getTemplate().getRequiredPubInfoElements()) {
×
246
            String latestId = QueryApiAccess.getLatestVersionId(r.stringValue());
×
247
            if (pubInfoContextMap.containsKey(r.stringValue()) || pubInfoContextMap.containsKey(latestId)) continue;
×
248
            TemplateContext c = new TemplateContext(ContextType.PUBINFO, r.stringValue(), "pi-statement", targetNamespace);
×
249
            pubInfoContexts.add(c);
×
250
            pubInfoContextMap.put(c.getTemplateId(), c);
×
251
            requiredPubInfoContexts.add(c);
×
252
        }
×
253
        Map<Integer, TemplateContext> piParamIdMap = new HashMap<>();
×
254
        for (String k : pageParams.getNamedKeys()) {
×
255
            if (!k.matches("pitemplate[1-9][0-9]*")) continue;
×
256
            Integer i = Integer.parseInt(k.replaceFirst("^pitemplate([1-9][0-9]*)$", "$1"));
×
257
            String tid = pageParams.get(k).toString();
×
258
            // TODO Allow for automatically using latest template version:
259
            //String piTemplateIdLatest = QueryApiAccess.getLatestVersionId(tid);
260
            TemplateContext c = createPubInfoContext(tid);
×
261
            if (piParamIdMap.containsKey(i)) {
×
262
                // TODO: handle this error better
263
                logger.error("Publication Info template param identifier defined multiple times: {}", i);
×
264
            }
265
            piParamIdMap.put(i, c);
×
266
        }
×
267
        if (fillNp != null && !fillOnlyAssertion) {
×
268
            for (IRI piTemplateId : td.getPubinfoTemplateIds(fillNp)) {
×
269
                String piTemplateIdLatest = QueryApiAccess.getLatestVersionId(piTemplateId.stringValue());
×
270
                if (piTemplateIdLatest.equals(supersedesPubInfoTemplateId)) continue;
×
271
                if (!pubInfoContextMap.containsKey(piTemplateIdLatest)) {
×
272
                    // TODO Allow for automatically using latest template version
273
                    createPubInfoContext(piTemplateId.stringValue());
×
274
                }
275
            }
×
276
        }
277
        if (!pageParams.get("values-from-query").isEmpty() && !pageParams.get("values-from-query-mapping").isEmpty()) {
×
278
            String querySpec = pageParams.get("values-from-query").toString();
×
279

280
            String mapping = pageParams.get("values-from-query-mapping").toString();
×
281
            String mapsFrom, mapsTo;
282
            if (mapping.contains(":")) {
×
283
                mapsFrom = mapping.split(":")[0];
×
284
                mapsTo = mapping.split(":")[1];
×
285
            } else {
286
                mapsFrom = mapping;
×
287
                mapsTo = mapping;
×
288
            }
289
            ApiResponse resp = ApiCache.retrieveResponseSync(QueryRef.parseString(querySpec), false);
×
290
            int i = 0;
×
291
            for (ApiResponseEntry e : resp.getData()) {
×
292
                String mapsToSuffix = "";
×
293
                if (i > 0) mapsToSuffix = "__" + i;
×
294
                assertionContext.setParam(mapsTo + mapsToSuffix, e.get(mapsFrom));
×
295
                i++;
×
296
            }
×
297
        }
298
        for (String k : pageParams.getNamedKeys()) {
×
299
            if (k.startsWith("param_")) assertionContext.setParam(k.substring(6), pageParams.get(k).toString());
×
300
            if (k.startsWith("prparam_")) provenanceContext.setParam(k.substring(8), pageParams.get(k).toString());
×
301
            if (k.matches("piparam[1-9][0-9]*_.*")) {
×
302
                Integer i = Integer.parseInt(k.replaceFirst("^piparam([1-9][0-9]*)_.*$", "$1"));
×
303
                if (!piParamIdMap.containsKey(i)) {
×
304
                    logger.error("Parameter {} of the publication info template not found", i);
×
305
                    continue;
×
306
                }
307
                String n = k.replaceFirst("^piparam[1-9][0-9]*_(.*)$", "$1");
×
308
                logger.info(n);
×
309
                piParamIdMap.get(i).setParam(n, pageParams.get(k).toString());
×
310
            }
311
        }
×
312

313
        final Nanopub improveNp;
314
        if (!pageParams.get("improve").isNull()) {
×
315
            improveNp = Utils.getNanopub(pageParams.get("improve").toString());
×
316
        } else {
317
            improveNp = null;
×
318
        }
319

320
        // Propagate fill source (supersede/derive/improve) so contexts can resolve
321
        // the `local:nanopub`/`local:assertion` sentinels to the fill nanopub's URIs.
322
        Nanopub fillSource = fillNp != null ? fillNp : improveNp;
×
323
        if (fillSource != null) {
×
324
            assertionContext.setFillSource(fillSource);
×
325
            provenanceContext.setFillSource(fillSource);
×
326
            for (TemplateContext c : pubInfoContexts) {
×
327
                c.setFillSource(fillSource);
×
328
            }
×
329
        }
330

331
        // Init statements only now, in order to pick up parameter values:
332
        assertionContext.initStatements();
×
333
        provenanceContext.initStatements();
×
334
        for (TemplateContext c : pubInfoContexts) {
×
335
            c.initStatements();
×
336
        }
×
337

338
        String latestAssertionId = QueryApiAccess.getLatestVersionId(assertionContext.getTemplateId());
×
339
        if (!assertionContext.getTemplateId().equals(latestAssertionId)) {
×
340
            add(new Label("newversion", "There is a new version of this assertion template:"));
×
341
            PageParameters params = new PageParameters(pageParams);
×
342
            params.set("template", latestAssertionId);
×
343
            add(new BookmarkablePageLink<Void>("newversionlink", publishPageClass, params));
×
344
            if ("latest".equals(pageParams.get("template-version").toString())) {
×
345
                throw new RestartResponseException(publishPageClass, params);
×
346
            }
347
        } else {
×
348
            add(new Label("newversion", "").setVisible(false));
×
349
            add(new Label("newversionlink", "").setVisible(false));
×
350
        }
351

352
        final List<Statement> unusedStatementList = new ArrayList<>();
×
353
        final List<Statement> unusedPrStatementList = new ArrayList<>();
×
354
        final List<Statement> unusedPiStatementList = new ArrayList<>();
×
355
        if (fillNp != null) {
×
356
            ValueFiller filler = new ValueFiller(fillNp, ContextType.ASSERTION, true, fillMode);
×
357
            filler.fill(assertionContext);
×
358
            unusedStatementList.addAll(filler.getUnusedStatements());
×
359

360
            if (!fillOnlyAssertion) {
×
361
                ValueFiller prFiller = new ValueFiller(fillNp, ContextType.PROVENANCE, true);
×
362
                prFiller.fill(provenanceContext);
×
363
                unusedPrStatementList.addAll(prFiller.getUnusedStatements());
×
364

365
                ValueFiller piFiller = new ValueFiller(fillNp, ContextType.PUBINFO, true);
×
366
                if (!assertionContext.getTemplate().getTargetNanopubTypes().isEmpty()) {
×
367
                    for (Statement st : new ArrayList<>(piFiller.getUnusedStatements())) {
×
368
                        if (st.getSubject().stringValue().equals(LocalUri.of("nanopub").stringValue()) && st.getPredicate().equals(NPX.HAS_NANOPUB_TYPE)) {
×
369
                            if (assertionContext.getTemplate().getTargetNanopubTypes().contains(st.getObject())) {
×
370
                                piFiller.removeUnusedStatement(st);
×
371
                            }
372
                        }
373
                    }
×
374
                }
375
                for (TemplateContext c : pubInfoContexts) {
×
376
                    piFiller.fill(c);
×
377
                }
×
378
                piFiller.removeUnusedStatements(NanodashSession.get().getUserIri(), FOAF.NAME, null);
×
379
                if (piFiller.hasUnusedStatements()) {
×
380
                    final String handcodedStatementsTemplateId = "https://w3id.org/np/RAMEgudZsQ1bh1fZhfYnkthqH6YSXpghSE_DEN1I-6eAI";
×
381
                    if (!pubInfoContextMap.containsKey(handcodedStatementsTemplateId)) {
×
382
                        TemplateContext c = createPubInfoContext(handcodedStatementsTemplateId);
×
383
                        c.setFillSource(fillNp);
×
384
                        c.initStatements();
×
385
                        piFiller.fill(c);
×
386
                    }
387
                }
388
                unusedPiStatementList.addAll(piFiller.getUnusedStatements());
×
389
                // TODO: Also use pubinfo templates stated in nanopub to be filled in?
390
//                                Set<IRI> pubinfoTemplateIds = Template.getPubinfoTemplateIds(fillNp);
391
//                                if (!pubinfoTemplateIds.isEmpty()) {
392
//                                        ValueFiller piFiller = new ValueFiller(fillNp, ContextType.PUBINFO);
393
//                                        for (IRI pubinfoTemplateId : pubinfoTemplateIds) {
394
//                                                // TODO: Make smart choice on the ordering in trying to fill in all pubinfo elements
395
//                                                piFiller.fill(pubInfoContextMap.get(pubinfoTemplateId.stringValue()));
396
//                                        }
397
//                                        warningMessage += (piFiller.getWarningMessage() == null ? "" : "Publication info: " + piFiller.getWarningMessage() + " ");
398
//                                }
399
            }
400
        } else if (improveNp != null) {
×
401
            ValueFiller filler = new ValueFiller(improveNp, ContextType.ASSERTION, true);
×
402
            filler.fill(assertionContext);
×
403
            unusedStatementList.addAll(filler.getUnusedStatements());
×
404
        }
405
        if (!unusedStatementList.isEmpty()) {
×
406
            add(new Label("warnings", "Some content from the existing nanopublication could not be filled in:"));
×
407
        } else {
408
            add(new Label("warnings", "").setVisible(false));
×
409
        }
410
        add(new DataView<Statement>("unused-statements", new ListDataProvider<Statement>(unusedStatementList)) {
×
411

412
            @Override
413
            protected void populateItem(Item<Statement> item) {
414
                item.add(new TripleItem("unused-statement", item.getModelObject(), (fillNp != null ? fillNp : improveNp), null));
×
415
            }
×
416

417
        });
418
        add(new DataView<Statement>("unused-prstatements", new ListDataProvider<Statement>(unusedPrStatementList)) {
×
419

420
            @Override
421
            protected void populateItem(Item<Statement> item) {
422
                item.add(new TripleItem("unused-prstatement", item.getModelObject(), (fillNp != null ? fillNp : improveNp), null));
×
423
            }
×
424

425
        });
426
        add(new DataView<Statement>("unused-pistatements", new ListDataProvider<Statement>(unusedPiStatementList)) {
×
427

428
            @Override
429
            protected void populateItem(Item<Statement> item) {
430
                item.add(new TripleItem("unused-pistatement", item.getModelObject(), (fillNp != null ? fillNp : improveNp), null));
×
431
            }
×
432

433
        });
434

435
        // Finalize statements, which picks up parameter values in repetitions:
436
        assertionContext.finalizeStatements();
×
437
        provenanceContext.finalizeStatements();
×
438
        for (TemplateContext c : pubInfoContexts) {
×
439
            c.finalizeStatements();
×
440
        }
×
441

442
        final CheckBox consentCheck = new CheckBox("consentcheck", new Model<>(false));
×
443
        consentCheck.add(new InvalidityHighlighting());
×
444

445
        form = new Form<Void>("form") {
×
446

447
            @Override
448
            protected void onConfigure() {
449
                super.onConfigure();
×
450
                form.getFeedbackMessages().clear();
×
451
//                                formComponents.clear();
452
            }
×
453

454
            protected void onSubmit() {
455
                if (!Boolean.TRUE.equals(consentCheck.getModelObject())) {
×
456
                    feedbackPanel.error("You need to check the checkbox that you understand the consequences.");
×
457
                    return;
×
458
                }
459
                logger.info("Publish form submitted");
×
460
                Nanopub signedNp;
461
                try {
462
                    Nanopub np = createNanopub();
×
463
                    logger.info("Nanopublication created: {}", np.getUri());
×
464
                    TransformContext tc = new TransformContext(SignatureAlgorithm.RSA, NanodashSession.get().getKeyPair(), NanodashSession.get().getUserIri(), false, false, false);
×
465
                    signedNp = SignNanopub.signAndTransform(np, tc);
×
466
                    logger.info("Nanopublication signed: {}", signedNp.getUri());
×
467
                    String npUrl = PublishNanopub.publish(signedNp);
×
468
                    logger.info("Nanopublication published: {}", npUrl);
×
469
                    Utils.cacheNanopub(signedNp);
×
470
                } catch (Exception ex) {
×
471
                    signedNp = null;
×
472
                    logger.error("Nanopublication publishing failed: {}", ex.getMessage());
×
473
                    String message = ex.getClass().getName();
×
474
                    if (ex.getMessage() != null) {
×
475
                        message = ex.getMessage();
×
476
                    }
477
                    feedbackPanel.error(message);
×
478
                }
×
479
                if (signedNp != null) {
×
480
                    String toRefresh = pageParams.get("refresh-upon-publish").toString("");
×
481
                    if (!toRefresh.isEmpty()) {
×
482
                        WicketApplication.get().notifyNanopubPublished(signedNp, toRefresh, 5 * 1000);
×
483
                    }
484
                    String contextId = pageParams.get("context").toString("");
×
485
                    // Broaden the refresh: also force-refresh the context resource's own
486
                    // data (e.g. a space's roles/members) so the page we redirect to —
487
                    // typically its Content tab — reflects the just-published change, not
488
                    // only the specific view query that was acted on.
489
                    if (!contextId.isEmpty() && !contextId.equals(toRefresh)
×
490
                            && AbstractResourceWithProfile.isResourceWithProfile(contextId)) {
×
491
                        WicketApplication.get().notifyNanopubPublished(signedNp, contextId, 5 * 1000);
×
492
                    }
493
                    String partId = pageParams.get("part").toString("");
×
494
                    if (!contextId.isEmpty() && pageParams.get("postpub-redirect-url").isEmpty()) {
×
495
                        PageParameters redirectParams = new PageParameters().set("just-published", signedNp.getUri().stringValue());
×
496
                        if (!partId.isEmpty()) {
×
497
                            // User was on a part page (e.g. paper collection); redirect back to the part page
498
                            redirectParams.set("id", partId).set("context", contextId);
×
499
                            throw new RestartResponseException(ResourcePartPage.class, redirectParams);
×
500
                        }
501
                        redirectParams.set("id", contextId);
×
502
                        // Return to the tab the action asked for (e.g. "about" for a
503
                        // space's About-tab role actions); default is the Content tab.
504
                        String postpubTab = pageParams.get("postpub-tab").toString("");
×
505
                        if (!postpubTab.isEmpty()) redirectParams.set("tab", postpubTab);
×
506
                        if (SpaceRepository.get().findById(contextId) != null) {
×
507
                            throw new RestartResponseException(SpacePage.class, redirectParams);
×
508
                        }
509
                        if (MaintainedResourceRepository.get().findById(contextId) != null) {
×
510
                            throw new RestartResponseException(MaintainedResourcePage.class, redirectParams);
×
511
                        }
512
                        if (IndividualAgent.isUser(contextId)) {
×
513
                            throw new RestartResponseException(UserPage.class, redirectParams);
×
514
                        }
515
                    }
516
                    NanodashPage confirmPage = getConfirmPage(signedNp, pageParams);
×
517
                    if (confirmPage != null) {
×
518
                        throw new RestartResponseException(confirmPage);
×
519
                    }
520
                    throw new RestartResponseException(ExplorePage.class,
×
521
                            new PageParameters(pageParams).set("id", signedNp.getUri().stringValue()));
×
522
                }
523
            }
×
524

525
            @Override
526
            protected void onValidate() {
527
                super.onValidate();
×
528
                for (Component fc : assertionContext.getComponents()) {
×
529
                    processFeedback(fc);
×
530
                }
×
531
                for (Component fc : provenanceContext.getComponents()) {
×
532
                    processFeedback(fc);
×
533
                }
×
534
                for (TemplateContext c : pubInfoContexts) {
×
535
                    for (Component fc : c.getComponents()) {
×
536
                        processFeedback(fc);
×
537
                    }
×
538
                }
×
539
            }
×
540

541
            private void processFeedback(Component c) {
542
                if (c instanceof FormComponent) {
×
543
                    ((FormComponent<?>) c).processInput();
×
544
                }
545
                for (FeedbackMessage fm : c.getFeedbackMessages()) {
×
546
                    form.getFeedbackMessages().add(fm);
×
547
                }
×
548
            }
×
549

550
        };
551
        form.setOutputMarkupId(true);
×
552

553
        //form.add(new Label("nanopub-namespace", targetNamespaceLabel));
554

555
        form.add(new BookmarkablePageLink<Void>("templatelink", ExplorePage.class, new PageParameters().set("id", assertionContext.getTemplate().getId())));
×
556
        form.add(new Label("templatename", assertionContext.getTemplate().getLabel()));
×
557
        String description = assertionContext.getTemplate().getLabel();
×
558
        form.add(new Label("templatedesc", assertionContext.getTemplate().getDescription()).setEscapeModelStrings(false));
×
559

560
        form.add(new ListView<StatementItem>("statements", assertionContext.getStatementItems()) {
×
561

562
            protected void populateItem(ListItem<StatementItem> item) {
563
                item.add(item.getModelObject());
×
564
            }
×
565

566
        });
567

568
        final Map<String, Boolean> handledProvTemplates = new HashMap<>();
×
569
        final String defaultProvTemplateId;
570
        if (assertionContext.getTemplate().getDefaultProvenance() != null) {
×
571
            defaultProvTemplateId = assertionContext.getTemplate().getDefaultProvenance().stringValue();
×
572
            handledProvTemplates.put(defaultProvTemplateId, true);
×
573
        } else {
574
            defaultProvTemplateId = null;
×
575
        }
576
        final List<String> recommendedProvTemplateOptionIds = new ArrayList<>();
×
577
        final List<String> provTemplateOptionIds = new ArrayList<>();
×
578
        if (pageParams.get("prtemplate-options").isNull()) {
×
579
            // TODO Make this dynamic and consider updated templates:
580
            recommendedProvTemplateOptionIds.add(DEFAULT_PROV_TEMPLATE);
×
581
            recommendedProvTemplateOptionIds.add("http://purl.org/np/RAcTpoh5Ra0ssqmcpOgWdaZ_YiPE6demO6cpw-2RvSNs8");
×
582
            recommendedProvTemplateOptionIds.add("http://purl.org/np/RA4LGtuOqTIMqVAkjnfBXk1YDcAPNadP5CGiaJiBkdHCQ");
×
583
            recommendedProvTemplateOptionIds.add("http://purl.org/np/RAl_-VTw9Re_uRF8r8y0rjlfnu7FlhTa8xg_8xkcweqiE");
×
584
            recommendedProvTemplateOptionIds.add("https://w3id.org/np/RASORV2mMEVpS4lWh2bwUTEcV-RWjbD9RPbN7J0PIeYAU");
×
585
            recommendedProvTemplateOptionIds.add("http://purl.org/np/RAjkBbM5yQm7hKH1l_Jk3HAUqWi3Bd57TPmAOZCsZmi_M");
×
586
            recommendedProvTemplateOptionIds.add("http://purl.org/np/RAGXx_k9eQMnXaCbsXMsJbGClwZtQEGNg0GVJu6amdAVw");
×
587
            recommendedProvTemplateOptionIds.add("http://purl.org/np/RA1fnITI3Pu1UQ0CHghNpys3JwQrM32LBnjmDLoayp9-4");
×
588
            recommendedProvTemplateOptionIds.add("http://purl.org/np/RAJgbsGeGdTG-zq_gU0TLw4s3raMgoRk-mPlc2DSLXvE0");
×
589
            recommendedProvTemplateOptionIds.add("http://purl.org/np/RA6SXfhUY-xeblZU8HhPddw6tsu-C5NXevG6C_zv4bMxU");
×
590
            for (String s : recommendedProvTemplateOptionIds) {
×
591
                handledProvTemplates.put(s, true);
×
592
            }
×
593

594
            for (ApiResponseEntry t : td.getProvenanceTemplates()) {
×
595
                String tid = t.get("np");
×
596
                if (handledProvTemplates.containsKey(tid)) continue;
×
597
                provTemplateOptionIds.add(tid);
×
598
                handledProvTemplates.put(tid, true);
×
599
            }
×
600
        } else {
601
            for (String s : pageParams.get("prtemplate-options").toString().split(" ")) {
×
602
                if (handledProvTemplates.containsKey(s)) continue;
×
603
                recommendedProvTemplateOptionIds.add(s);
×
604
                handledProvTemplates.put(s, true);
×
605
            }
606
        }
607

608
        ChoiceProvider<String> prTemplateChoiceProvider = new ChoiceProvider<String>() {
×
609

610
            @Override
611
            public String getDisplayValue(String object) {
612
                if (object == null || object.isEmpty()) return "";
×
613
                Template t = td.getTemplate(object);
×
614
                if (t != null) return t.getLabel();
×
615
                return object;
×
616
            }
617

618
            @Override
619
            public String getIdValue(String object) {
620
                return object;
×
621
            }
622

623
            // Getting strange errors with Tomcat if this method is not overridden:
624
            @Override
625
            public void detach() {
626
            }
×
627

628
            @Override
629
            public void query(String term, int page, Response<String> response) {
630
                if (term == null) term = "";
×
631
                term = term.toLowerCase();
×
632
                if (pageParams.get("prtemplate").toString() != null) {
×
633
                    // Using this work-around with "——" because 'optgroup' is not available through Wicket's Select2 classes
634
                    response.add("—— default for this link ——");
×
635
                    response.add(prTemplateId);
×
636
                }
637
                if (defaultProvTemplateId != null) {
×
638
                    response.add("—— default for this template ——");
×
639
                    response.add(defaultProvTemplateId);
×
640
                }
641
                if (!recommendedProvTemplateOptionIds.isEmpty()) {
×
642
                    if (pageParams.get("prtemplate-options").isNull()) {
×
643
                        response.add("—— recommended ——");
×
644
                    }
645
                    for (String s : recommendedProvTemplateOptionIds) {
×
646
                        if (s.toLowerCase().contains(term) || getDisplayValue(s).toLowerCase().contains(term)) {
×
647
                            response.add(s);
×
648
                        }
649
                    }
×
650
                }
651
                if (!provTemplateOptionIds.isEmpty()) {
×
652
                    response.add("—— others ——");
×
653
                    for (String s : provTemplateOptionIds) {
×
654
                        if (s.toLowerCase().contains(term) || getDisplayValue(s).toLowerCase().contains(term)) {
×
655
                            response.add(s);
×
656
                        }
657
                    }
×
658
                }
659
            }
×
660

661
            @Override
662
            public Collection<String> toChoices(Collection<String> ids) {
663
                return ids;
×
664
            }
665

666
        };
667
        final IModel<String> prTemplateModel = Model.of(provenanceContext.getTemplate().getId());
×
668
        Select2Choice<String> prTemplateChoice = new Select2Choice<String>("prtemplate", prTemplateModel, prTemplateChoiceProvider);
×
669
        prTemplateChoice.setRequired(true);
×
670
        prTemplateChoice.getSettings().setCloseOnSelect(true);
×
671
        prTemplateChoice.add(new AjaxFormComponentUpdatingBehavior("change") {
×
672

673
            @Override
674
            protected void onUpdate(AjaxRequestTarget target) {
675
                String o = prTemplateModel.getObject();
×
676
                if (o.startsWith("——")) {
×
677
                    o = provenanceContext.getTemplate().getId();
×
678
                    prTemplateModel.setObject(o);
×
679
                }
680
                provenanceContext = new TemplateContext(ContextType.PROVENANCE, prTemplateModel.getObject(), "pr-statement", targetNamespace);
×
681
                provenanceContext.initStatements();
×
682
                refreshProvenance(target);
×
683
                provenanceContext.finalizeStatements();
×
684
            }
×
685

686
        });
687
        form.add(prTemplateChoice);
×
688
        refreshProvenance(null);
×
689

690
        final Map<String, Boolean> handledPiTemplates = new HashMap<>();
×
691
        final List<String> recommendedPiTemplateOptionIds = new ArrayList<>();
×
692
        final List<String> piTemplateOptionIds = new ArrayList<>();
×
693
        // TODO Make this dynamic and consider updated templates:
694
        recommendedPiTemplateOptionIds.add("http://purl.org/np/RAXflINqt3smqxV5Aq7E9lzje4uLdkKIOefa6Bp8oJ8CY");
×
695
        recommendedPiTemplateOptionIds.add("https://w3id.org/np/RARW4MsFkHuwjycNElvEVtuMjpf4yWDL10-0C5l2MqqRQ");
×
696
        recommendedPiTemplateOptionIds.add("https://w3id.org/np/RA16U9Wo30ObhrK1NzH7EsmVRiRtvEuEA_Dfc-u8WkUCA");
×
697
        recommendedPiTemplateOptionIds.add("http://purl.org/np/RAdyqI6k07V5nAS82C6hvIDtNWk179EIV4DV-sLbOFKg4");
×
698
        recommendedPiTemplateOptionIds.add("https://w3id.org/np/RAjvEpLZUE7rMoa8q6mWSsN6utJDp-5FmgO47YGsbgw3w");
×
699
        recommendedPiTemplateOptionIds.add("http://purl.org/np/RAxuGRKID6yNg63V5Mf0ot2NjncOnodh-mkN3qT_1txGI");
×
700
        for (TemplateContext c : pubInfoContexts) {
×
701
            String s = c.getTemplate().getId();
×
702
            handledPiTemplates.put(s, true);
×
703
        }
×
704
        for (String s : recommendedPiTemplateOptionIds) {
×
705
            handledPiTemplates.put(s, true);
×
706
        }
×
707

708
        for (ApiResponseEntry entry : td.getPubInfoTemplates()) {
×
709
            String tid = entry.get("np");
×
710
            if (handledPiTemplates.containsKey(tid)) continue;
×
711
            piTemplateOptionIds.add(tid);
×
712
            handledPiTemplates.put(tid, true);
×
713
        }
×
714

715
        ChoiceProvider<String> piTemplateChoiceProvider = new ChoiceProvider<String>() {
×
716

717
            @Override
718
            public String getDisplayValue(String object) {
719
                if (object == null || object.isEmpty()) return "";
×
720
                Template t = td.getTemplate(object);
×
721
                if (t != null) return t.getLabel();
×
722
                return object;
×
723
            }
724

725
            @Override
726
            public String getIdValue(String object) {
727
                return object;
×
728
            }
729

730
            // Getting strange errors with Tomcat if this method is not overridden:
731
            @Override
732
            public void detach() {
733
            }
×
734

735
            @Override
736
            public void query(String term, int page, Response<String> response) {
737
                if (term == null) term = "";
×
738
                term = term.toLowerCase();
×
739
                if (!recommendedPiTemplateOptionIds.isEmpty()) {
×
740
                    response.add("—— recommended ——");
×
741
                    for (String s : recommendedPiTemplateOptionIds) {
×
742
                        boolean isAlreadyUsed = false;
×
743
                        for (TemplateContext c : pubInfoContexts) {
×
744
                            // TODO: make this more efficient/nicer
745
                            if (c.getTemplate().getId().equals(s)) {
×
746
                                isAlreadyUsed = true;
×
747
                                break;
×
748
                            }
749
                        }
×
750
                        if (isAlreadyUsed) continue;
×
751
                        if (s.toLowerCase().contains(term) || getDisplayValue(s).toLowerCase().contains(term)) {
×
752
                            response.add(s);
×
753
                        }
754
                    }
×
755
                }
756
                if (!piTemplateOptionIds.isEmpty()) {
×
757
                    response.add("—— others ——");
×
758
                    for (String s : piTemplateOptionIds) {
×
759
                        boolean isAlreadyUsed = false;
×
760
                        for (TemplateContext c : pubInfoContexts) {
×
761
                            // TODO: make this more efficient/nicer
762
                            if (c.getTemplate().getId().equals(s)) {
×
763
                                isAlreadyUsed = true;
×
764
                                break;
×
765
                            }
766
                        }
×
767
                        if (isAlreadyUsed) continue;
×
768
                        if (s.toLowerCase().contains(term) || getDisplayValue(s).toLowerCase().contains(term)) {
×
769
                            response.add(s);
×
770
                        }
771
                    }
×
772
                }
773
            }
×
774

775
            @Override
776
            public Collection<String> toChoices(Collection<String> ids) {
777
                return ids;
×
778
            }
779

780
        };
781
        final IModel<String> newPiTemplateModel = Model.of();
×
782
        Select2Choice<String> piTemplateChoice = new Select2Choice<String>("pitemplate", newPiTemplateModel, piTemplateChoiceProvider);
×
783
        piTemplateChoice.getSettings().setCloseOnSelect(true);
×
784
        piTemplateChoice.getSettings().setPlaceholder("add element...");
×
785
        piTemplateChoice.add(new AjaxFormComponentUpdatingBehavior("change") {
×
786

787
            @Override
788
            protected void onUpdate(AjaxRequestTarget target) {
789
                if (newPiTemplateModel.getObject().startsWith("——")) {
×
790
                    newPiTemplateModel.setObject(null);
×
791
                    refreshPubInfo(target);
×
792
                    return;
×
793
                }
794
                String id = newPiTemplateModel.getObject();
×
795
                TemplateContext c = new TemplateContext(ContextType.PUBINFO, id, "pi-statement", targetNamespace);
×
796
                c.initStatements();
×
797
                pubInfoContexts.add(c);
×
798
                newPiTemplateModel.setObject(null);
×
799
                refreshPubInfo(target);
×
800
                c.finalizeStatements();
×
801
            }
×
802

803
        });
804
        form.add(piTemplateChoice);
×
805
        refreshPubInfo(null);
×
806

807
        form.add(consentCheck);
×
808

809
        form.add(new Button("preview-button") {
×
810
            @Override
811
            public void onSubmit() {
812
                try {
813
                    Nanopub np = createNanopub();
×
814
                    TransformContext tc = new TransformContext(SignatureAlgorithm.RSA, NanodashSession.get().getKeyPair(), NanodashSession.get().getUserIri(), false, false, false);
×
815
                    Nanopub signedNp = SignNanopub.signAndTransform(np, tc);
×
816
                    String previewId = signedNp.getUri().stringValue();
×
817
                    NanodashSession.get().setPreviewNanopub(previewId,
×
818
                            new NanodashSession.PreviewNanopub(signedNp, pageParams, confirmPageClass, Boolean.TRUE.equals(consentCheck.getModelObject()), getPage().getPageReference()));
×
819
                    throw new RestartResponseException(PreviewPage.class, new PageParameters().set("id", previewId));
×
820
                } catch (RestartResponseException ex) {
×
821
                    throw ex;
×
822
                } catch (Exception ex) {
×
823
                    logger.error("Preview failed: {}", ex.getMessage());
×
824
                    String message = ex.getClass().getName();
×
825
                    if (ex.getMessage() != null) {
×
826
                        message = ex.getMessage();
×
827
                    }
828
                    feedbackPanel.error(message);
×
829
                }
830
            }
×
831
        });
832

833
        add(form);
×
834

835
        feedbackPanel = new FeedbackPanel("feedback");
×
836
        feedbackPanel.setOutputMarkupId(true);
×
837
        add(feedbackPanel);
×
838
    }
×
839

840
    private void refreshProvenance(AjaxRequestTarget target) {
841
        if (target != null) {
×
842
            form.remove("prtemplatelink");
×
843
            form.remove("pr-statements");
×
844
            target.add(form);
×
845
            target.appendJavaScript("updateElements();");
×
846
        }
847
        form.add(new BookmarkablePageLink<Void>("prtemplatelink", ExplorePage.class, new PageParameters().set("id", provenanceContext.getTemplate().getId())));
×
848
        ListView<StatementItem> list = new ListView<StatementItem>("pr-statements", provenanceContext.getStatementItems()) {
×
849

850
            protected void populateItem(ListItem<StatementItem> item) {
851
                item.add(item.getModelObject());
×
852
            }
×
853

854
        };
855
        list.setOutputMarkupId(true);
×
856
        form.add(list);
×
857
    }
×
858

859
    private void refreshPubInfo(AjaxRequestTarget target) {
860
        ListView<TemplateContext> list = new ListView<TemplateContext>("pis", pubInfoContexts) {
×
861

862
            protected void populateItem(ListItem<TemplateContext> item) {
863
                final TemplateContext pic = item.getModelObject();
×
864
                item.add(new Label("pitemplatename", pic.getTemplate().getLabel()));
×
865
                item.add(new BookmarkablePageLink<Void>("pitemplatelink", ExplorePage.class, new PageParameters().set("id", pic.getTemplate().getId())));
×
866
                Label remove = new Label("piremove", "×");
×
867
                item.add(remove);
×
868
                remove.add(new AjaxEventBehavior("click") {
×
869

870
                    @Override
871
                    protected void onEvent(AjaxRequestTarget target) {
872
                        pubInfoContexts.remove(pic);
×
873
                        target.add(PublishForm.this);
×
874
                        target.appendJavaScript("updateElements();");
×
875
                    }
×
876

877
                });
878
                if (requiredPubInfoContexts.contains(pic)) remove.setVisible(false);
×
879
                item.add(new ListView<StatementItem>("pi-statements", pic.getStatementItems()) {
×
880

881
                    protected void populateItem(ListItem<StatementItem> item) {
882
                        item.add(item.getModelObject());
×
883
                    }
×
884

885
                });
886
            }
×
887

888
        };
889
        list.setOutputMarkupId(true);
×
890
        if (target == null) {
×
891
            form.add(list);
×
892
        } else {
893
            form.remove("pis");
×
894
            form.add(list);
×
895
            target.add(form);
×
896
            target.appendJavaScript("updateElements();");
×
897
        }
898
    }
×
899

900
    private TemplateContext createPubInfoContext(String piTemplateId) {
901
        TemplateContext c;
902
        if (pubInfoContextMap.containsKey(piTemplateId)) {
×
903
            c = pubInfoContextMap.get(piTemplateId);
×
904
        } else {
905
            c = new TemplateContext(ContextType.PUBINFO, piTemplateId, "pi-statement", targetNamespace);
×
906
            pubInfoContextMap.put(piTemplateId, c);
×
907
            pubInfoContexts.add(c);
×
908
        }
909
        return c;
×
910
    }
911

912
    private synchronized Nanopub createNanopub() throws MalformedNanopubException, NanopubAlreadyFinalizedException {
913
        assertionContext.getIntroducedIris().clear();
×
914
        assertionContext.getRolePropertyPins().clear();
×
915
        NanopubCreator npCreator = new NanopubCreator(targetNamespace);
×
916
        npCreator.setAssertionUri(vf.createIRI(targetNamespace + "assertion"));
×
917
        assertionContext.propagateStatements(npCreator);
×
918
        provenanceContext.propagateStatements(npCreator);
×
919
        for (TemplateContext c : pubInfoContexts) {
×
920
            c.propagateStatements(npCreator);
×
921
        }
×
922
        for (IRI introducedIri : assertionContext.getIntroducedIris()) {
×
923
            npCreator.addPubinfoStatement(NPX.INTRODUCES, introducedIri);
×
924
        }
×
925
        for (IRI embeddedIri : assertionContext.getEmbeddedIris()) {
×
926
            npCreator.addPubinfoStatement(NPX.EMBEDS, embeddedIri);
×
927
        }
×
928
        Map<IRI, IRI> rolePropertyPins = assertionContext.getRolePropertyPins();
×
929
        if (!rolePropertyPins.isEmpty()) {
×
930
            // A role predicate carrying a direction pin makes this a role-instantiation
931
            // nanopub; nanopub-query's pin-resolution branch is gated on this type (#525).
932
            npCreator.addPubinfoStatement(NPX.HAS_NANOPUB_TYPE, KPXL_TERMS.ROLE_INSTANTIATION);
×
933
            for (Map.Entry<IRI, IRI> pin : rolePropertyPins.entrySet()) {
×
934
                npCreator.addPubinfoStatement(pin.getKey(), RDF.TYPE, pin.getValue());
×
935
            }
×
936
        }
937
        npCreator.addNamespace("this", targetNamespace);
×
938
        npCreator.addNamespace("sub", targetNamespace + "/");
×
939
        npCreator.addTimestampNow();
×
940
        IRI templateUri = assertionContext.getTemplate().getNanopub().getUri();
×
941
        npCreator.addPubinfoStatement(NTEMPLATE.WAS_CREATED_FROM_TEMPLATE, templateUri);
×
942
        IRI prTemplateUri = provenanceContext.getTemplate().getNanopub().getUri();
×
943
        npCreator.addPubinfoStatement(NTEMPLATE.WAS_CREATED_FROM_PROVENANCE_TEMPLATE, prTemplateUri);
×
944
        for (TemplateContext c : pubInfoContexts) {
×
945
            IRI piTemplateUri = c.getTemplate().getNanopub().getUri();
×
946
            npCreator.addPubinfoStatement(NTEMPLATE.WAS_CREATED_FROM_PUBINFO_TEMPLATE, piTemplateUri);
×
947
        }
×
948
        String nanopubLabel = getNanopubLabel(npCreator);
×
949
        if (nanopubLabel != null) {
×
950
            npCreator.addPubinfoStatement(RDFS.LABEL, vf.createLiteral(nanopubLabel));
×
951
        }
952
        for (IRI type : assertionContext.getTemplate().getTargetNanopubTypes()) {
×
953
            npCreator.addPubinfoStatement(NPX.HAS_NANOPUB_TYPE, type);
×
954
        }
×
955
        IRI userIri = NanodashSession.get().getUserIri();
×
956
        if (User.getName(userIri) != null) {
×
957
            npCreator.addPubinfoStatement(userIri, FOAF.NAME, vf.createLiteral(User.getName(userIri)));
×
958
            npCreator.addNamespace("foaf", FOAF.NAMESPACE);
×
959
        }
960
        String websiteUrl = NanodashPreferences.get().getWebsiteUrl();
×
961
        if (websiteUrl != null) {
×
962
            npCreator.addPubinfoStatement(NPX.WAS_CREATED_AT, vf.createIRI(websiteUrl));
×
963
        }
964
        return npCreator.finalizeNanopub();
×
965
    }
966

967
    private String getNanopubLabel(NanopubCreator npCreator) {
968
        if (assertionContext.getTemplate().getNanopubLabelPattern() == null) return null;
×
969

970
        Map<IRI, String> labelMap = new HashMap<>();
×
971
        for (Statement st : npCreator.getCurrentPubinfoStatements()) {
×
972
            if (st.getPredicate().equals(RDFS.LABEL) && st.getObject() instanceof Literal objL) {
×
973
                labelMap.put((IRI) st.getSubject(), objL.stringValue());
×
974
            }
975
        }
×
976

977
        String nanopubLabel = assertionContext.getTemplate().getNanopubLabelPattern();
×
978
        while (nanopubLabel.matches(".*\\$\\{[_a-zA-Z0-9-]+\\}.*")) {
×
979
            String placeholderPostfix = nanopubLabel.replaceFirst("^.*\\$\\{([_a-zA-Z0-9-]+)\\}.*$", "$1");
×
980
            IRI placeholderIriHash = vf.createIRI(assertionContext.getTemplateId() + "#" + placeholderPostfix);
×
981
            IRI placeholderIriSlash = vf.createIRI(assertionContext.getTemplateId() + "/" + placeholderPostfix);
×
982
            IRI placeholderIri;
983
            String placeholderValue = "";
×
984
            if (assertionContext.getComponentModels().get(placeholderIriSlash) != null) {
×
985
                placeholderIri = placeholderIriSlash;
×
986
            } else {
987
                placeholderIri = placeholderIriHash;
×
988
            }
989
            IModel<String> m = (IModel<String>) assertionContext.getComponentModels().get(placeholderIri);
×
990
            if (m != null) placeholderValue = m.orElse("").getObject();
×
991
            if (placeholderValue == null) placeholderValue = "";
×
992
            String placeholderLabel = placeholderValue;
×
993
            if (assertionContext.getTemplate().isUriPlaceholder(placeholderIri)) {
×
994
                try {
995
                    // TODO Fix this. It doesn't work for placeholders with auto-encode placeholders, etc.
996
                    //      Not sure we need labels for these, but this code should be improved anyway.
997
                    String prefix = assertionContext.getTemplate().getPrefix(placeholderIri);
×
998
                    if (prefix != null) placeholderValue = prefix + placeholderValue;
×
999
                    IRI placeholderValueIri = vf.createIRI(placeholderValue);
×
1000
                    String l = assertionContext.getTemplate().getLabel(placeholderValueIri);
×
1001
                    if (labelMap.containsKey(placeholderValueIri)) {
×
1002
                        l = labelMap.get(placeholderValueIri);
×
1003
                    }
1004
                    if (l == null) l = GuidedChoiceItem.getLabel(placeholderValue);
×
1005
                    if (assertionContext.getTemplate().isAgentPlaceholder(placeholderIri) && !placeholderValue.isEmpty()) {
×
1006
                        l = User.getName(vf.createIRI(placeholderValue));
×
1007
                    }
1008
                    if (l != null && !l.isEmpty()) {
×
1009
                        placeholderLabel = l.replaceFirst(" - .*$", "");
×
1010
                    } else {
1011
                        placeholderLabel = Utils.getShortNameFromURI(placeholderValueIri);
×
1012
                    }
1013
                } catch (Exception ex) {
×
1014
                    logger.error("Nanopub label placeholder IRI error: {}", ex.getMessage());
×
1015
                }
×
1016
            }
1017
            placeholderLabel = placeholderLabel.replaceAll("\\s+", " ");
×
1018
            if (placeholderLabel.length() > 100) placeholderLabel = placeholderLabel.substring(0, 97) + "...";
×
1019
            nanopubLabel = Strings.CS.replace(nanopubLabel, "${" + placeholderPostfix + "}", placeholderLabel);
×
1020
        }
×
1021
        return nanopubLabel;
×
1022
    }
1023

1024
    private NanodashPage getConfirmPage(Nanopub signedNp, PageParameters pageParams) {
1025
        try {
1026
            return (NanodashPage) confirmPageClass.getConstructor(Nanopub.class, PageParameters.class).newInstance(signedNp, pageParams);
×
1027
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException |
×
1028
                 InvocationTargetException | NoSuchMethodException | SecurityException ex) {
1029
            logger.error("Could not create instance of confirmation page: {}", ex.getMessage());
×
1030
        }
1031
        return null;
×
1032
    }
1033

1034
    /**
1035
     * Returns a hint whether the form is stateless or not.
1036
     *
1037
     * @return false if the form is stateful, true if it is stateless.
1038
     */
1039
    // This is supposed to solve the problem that sometimes (but only sometimes) form content is reset
1040
    // if the user browses other pages in parallel:
1041
    protected boolean getStatelessHint() {
1042
        return false;
×
1043
    }
1044

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

© 2026 Coveralls, Inc