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

knowledgepixels / nanodash / 18939026354

30 Oct 2025 11:24AM UTC coverage: 14.184% (-0.1%) from 14.315%
18939026354

push

github

ashleycaselli
Merge branch 'master' of github.com:knowledgepixels/nanodash

507 of 4504 branches covered (11.26%)

Branch coverage included in aggregate %.

1322 of 8391 relevant lines covered (15.75%)

0.71 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/component/LiteralDateTimeItem.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.Utils;
4
import com.knowledgepixels.nanodash.template.Template;
5
import com.knowledgepixels.nanodash.template.TemplateContext;
6
import com.knowledgepixels.nanodash.template.UnificationException;
7
import org.apache.wicket.AttributeModifier;
8
import org.apache.wicket.markup.html.basic.Label;
9
import org.apache.wicket.model.IModel;
10
import org.apache.wicket.model.Model;
11
import org.eclipse.rdf4j.model.IRI;
12
import org.eclipse.rdf4j.model.Literal;
13
import org.eclipse.rdf4j.model.Value;
14
import org.eclipse.rdf4j.model.vocabulary.XSD;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

18
import java.text.SimpleDateFormat;
19
import java.time.ZonedDateTime;
20

21
/**
22
 * A component that represents a text field for entering literal values.
23
 */
24
public class LiteralDateTimeItem extends AbstractContextComponent {
25

26
    private final static Logger logger = LoggerFactory.getLogger(LiteralDateTimeItem.class);
×
27
    public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
×
28
    private final String DATE_PATTERN = "d MMM yyyy";
×
29
    private final String TIME_PATTERN = "HH:mm:ss";
×
30

31
    private AjaxZonedDateTimePicker zonedDateTimePicker;
32
    private final Label datatypeComp;
33
    private final IModel<String> datatypeModel;
34
    private final String regex;
35
    private final IRI iri;
36

37
    /**
38
     * Constructs a LiteralDateTimeItem with the specified ID, IRI, optional flag, and template context.
39
     *
40
     * @param id       the component ID
41
     * @param iri      the IRI associated with this text field
42
     * @param optional whether this field is optional
43
     * @param context  the template context containing models and parameters
44
     */
45
    public LiteralDateTimeItem(String id, final IRI iri, boolean optional, TemplateContext context) {
46
        super(id, context);
×
47
        final Template template = context.getTemplate();
×
48
        this.iri = iri;
×
49
        regex = template.getRegex(iri);
×
50
        IModel<ZonedDateTime> model = (IModel<ZonedDateTime>) context.getComponentModels().get(iri);
×
51
        if (model == null) {
×
52
            model = Model.of((ZonedDateTime) null);
×
53
            context.getComponentModels().put(iri, model);
×
54
        }
55
        String postfix = Utils.getUriPostfix(iri);
×
56
        if (context.hasParam(postfix)) {
×
57
            String zoncontext = context.getParam(postfix);
×
58
            if (zoncontext != null) {
×
59
                model.setObject(ZonedDateTime.parse(zoncontext));
×
60
            } else {
61
                model.setObject(null);
×
62
            }
63
        }
64
        initZonedDateTimePicker(model);
×
65
        if (!optional) {
×
66
            zonedDateTimePicker.setRequired(true);
×
67
        }
68
        if (context.getTemplate().getLabel(iri) != null) {
×
69
            zonedDateTimePicker.add(new AttributeModifier("placeholder", context.getTemplate().getLabel(iri)));
×
70
        }
71

72
        context.getComponentModels().put(iri, zonedDateTimePicker.getModel());
×
73
        context.getComponents().add(zonedDateTimePicker);
×
74
        zonedDateTimePicker.add(new ValueItem.KeepValueAfterRefreshBehavior());
×
75
        zonedDateTimePicker.add(new InvalidityHighlighting());
×
76

77
        add(zonedDateTimePicker);
×
78

79
        datatypeModel = Model.of("");
×
80
        datatypeComp = new Label("datatype", datatypeModel);
×
81
        add(datatypeComp);
×
82
    }
×
83

84
    /**
85
     * Initializes the DateTimePicker component with the given model.
86
     *
87
     * @param model the model for the DateTimePicker
88
     */
89
    protected void initZonedDateTimePicker(IModel<ZonedDateTime> model) {
90
        zonedDateTimePicker = new AjaxZonedDateTimePicker("zoned-datetime", model, DATE_PATTERN, TIME_PATTERN);
×
91
    }
×
92

93
    /**
94
     * {@inheritDoc}
95
     */
96
    @Override
97
    public void removeFromContext() {
98
        context.getComponents().remove(zonedDateTimePicker);
×
99
    }
×
100

101
    /**
102
     * {@inheritDoc}
103
     */
104
    @Override
105
    public boolean isUnifiableWith(Value v) {
106
        if (v == null) {
×
107
            return true;
×
108
        }
109
        if (v instanceof Literal vL) {
×
110
            if (regex != null && !vL.stringValue().matches(regex)) {
×
111
                return false;
×
112
            }
113
            if (zonedDateTimePicker.getModelObject() == null) {
×
114
                return true;
×
115
            }
116
            IRI datatype = context.getTemplate().getDatatype(iri);
×
117
            if (!vL.getDatatype().equals(datatype)) {
×
118
                return false;
×
119
            }
120
            return ZonedDateTime.from(vL.temporalAccessorValue()).equals(zonedDateTimePicker.getModelObject());
×
121
        }
122
        return false;
×
123
    }
124

125
    /**
126
     * {@inheritDoc}
127
     */
128
    @Override
129
    public void unifyWith(Value v) throws UnificationException {
130
        if (v == null) {
×
131
            return;
×
132
        }
133
        if (!isUnifiableWith(v)) {
×
134
            throw new UnificationException(v.stringValue());
×
135
        }
136
        Literal vL = (Literal) v;
×
137
        ZonedDateTime zdt;
138
        if (vL.stringValue().matches("^\\d{4}-\\d{2}-\\d{2}$")) {
×
139
            // this is for backward compatibility for date-only literals that were defined as datetime literals even though they are not valid datetime instances
140
            String dateTimeString = vL.stringValue() + "T00:00:00Z";
×
141
            zdt = ZonedDateTime.parse(dateTimeString);
×
142
        } else {
×
143
            zdt = ZonedDateTime.from(vL.temporalAccessorValue());
×
144
        }
145
        zonedDateTimePicker.setModelObject(zdt);
×
146

147
        if (context.getTemplate().getDatatype(iri) == null && !vL.getDatatype().equals(XSD.STRING)) {
×
148
            datatypeModel.setObject("(" + vL.getDatatype().stringValue().replace(XSD.NAMESPACE, "xsd:") + ")");
×
149
            datatypeComp.setVisible(true);
×
150
        }
151
    }
×
152

153
    /**
154
     * {@inheritDoc}
155
     */
156
    @Override
157
    public void fillFinished() {
158
    }
×
159

160
    /**
161
     * {@inheritDoc}
162
     */
163
    @Override
164
    public void finalizeValues() {
165
        Value defaultValue = context.getTemplate().getDefault(iri);
×
166
        if (isUnifiableWith(defaultValue)) {
×
167
            try {
168
                unifyWith(defaultValue);
×
169
            } catch (UnificationException ex) {
×
170
                logger.error("Could not unify with default value.", ex);
×
171
            }
×
172
        }
173
    }
×
174

175
    /**
176
     * {@inheritDoc}
177
     */
178
    @Override
179
    public String toString() {
180
        return "[Literal datetime item]";
×
181
    }
182

183
}
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