Skip to content

Commit ccf550c

Browse files
committed
Service is optional in container generation form
Modal form validation fixes
1 parent 6d248c5 commit ccf550c

File tree

2 files changed

+54
-31
lines changed
  • src/main
    • java/com/atomgraph/linkeddatahub/resource
    • webapp/static/com/atomgraph/linkeddatahub/xsl/bootstrap/2.3.2/client

2 files changed

+54
-31
lines changed

src/main/java/com/atomgraph/linkeddatahub/resource/Generate.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,13 @@ public Generate(@Context Request request, @Context UriInfo uriInfo, MediaTypes m
102102
@Override
103103
public Response post(Model model, @QueryParam("default") @DefaultValue("false") Boolean defaultGraph, @QueryParam("graph") URI graphUri)
104104
{
105-
ResIterator it = model.listSubjectsWithProperty(LDH.service);
105+
ResIterator it = model.listSubjectsWithProperty(SIOC.HAS_PARENT);
106106
try
107107
{
108108
if (!it.hasNext()) throw new BadRequestException("Argument resource not provided");
109109

110110
Resource arg = it.next();
111111
Resource service = arg.getPropertyResourceValue(LDH.service);
112-
if (service == null) throw new BadRequestException("Service URI (ldh:service) not provided");
113-
114112
Resource parent = arg.getPropertyResourceValue(SIOC.HAS_PARENT);
115113
if (parent == null) throw new BadRequestException("Parent container (sioc:has_parent) not provided");
116114

@@ -177,16 +175,19 @@ public Response post(Model model, @QueryParam("default") @DefaultValue("false")
177175
* @param model RDF model
178176
* @param title query title
179177
* @param query query object
180-
* @param service SPARQL service resource
178+
* @param service optional SPARQL service resource
181179
* @return query resource
182180
*/
183181
public Resource createContainerSelect(Model model, String title, Query query, Resource service)
184182
{
185-
return model.createResource().
183+
Resource resource = model.createResource().
186184
addProperty(RDF.type, SP.Select).
187185
addLiteral(DCTerms.title, title).
188-
addProperty(SP.text, query.toString()).
189-
addProperty(LDH.service, service);
186+
addProperty(SP.text, query.toString());
187+
188+
if (service != null) resource.addProperty(LDH.service, service);
189+
190+
return resource;
190191
}
191192

192193
/**

src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/bootstrap/2.3.2/client/modal.xsl

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ LIMIT 10
284284
<span class="help-inline">xsd:integer</span>
285285
</div>
286286
</div>
287-
<div class="control-group required">
287+
<div class="control-group">
288288
<input name="pu" type="hidden" value="&ldh;service"/>
289289
<label class="control-label" for="source-service">
290290
<xsl:value-of>
@@ -735,18 +735,26 @@ LIMIT 10
735735
<!-- validate form before submitting it and show errors on required control-groups where input values are missing -->
736736
<xsl:template match="form[@id = 'form-add-data'] | form[@id = 'form-clone-data'] | form[@id = 'form-generate-containers']" mode="ixsl:onsubmit" priority="1">
737737
<xsl:sequence select="ixsl:call(ixsl:event(), 'preventDefault', [])"/>
738-
<xsl:variable name="control-groups" select="descendant::div[contains-token(@class, 'control-group')][contains-token(@class, 'required')]" as="element()*"/>
738+
<xsl:variable name="control-groups" select="descendant::div[contains-token(@class, 'control-group')]" as="element()*"/>
739+
<xsl:variable name="required-control-groups" select="$control-groups[contains-token(@class, 'required')]" as="element()*"/>
740+
741+
<!-- clear the errors initially -->
742+
<xsl:for-each select="$control-groups">
743+
<xsl:sequence select="ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', false() ])[current-date() lt xs:date('2000-01-01')]"/>
744+
</xsl:for-each>
745+
739746
<xsl:choose>
740-
<!-- input values missing, throw an error -->
741-
<xsl:when test="exists($control-groups/descendant::input[@name = ('ol', 'ou')][not(ixsl:get(., 'value'))])">
747+
<!-- required input values missing, throw an error -->
748+
<xsl:when test="exists($required-control-groups/descendant::input[@name = ('ol', 'ou')][not(ixsl:get(., 'value'))])">
742749
<xsl:sequence select="ixsl:call(ixsl:event(), 'preventDefault', [])"/>
743-
<xsl:sequence select="$control-groups[descendant::input[@name = ('ol', 'ou')][not(ixsl:get(., 'value'))]]/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', true() ])[current-date() lt xs:date('2000-01-01')]"/>
750+
<xsl:sequence select="$required-control-groups[descendant::input[@name = ('ol', 'ou')][not(ixsl:get(., 'value'))]]/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', true() ])[current-date() lt xs:date('2000-01-01')]"/>
744751
</xsl:when>
745752
<!-- all required values present, proceed to submit form-->
746753
<xsl:otherwise>
747754
<ixsl:set-style name="cursor" select="'progress'" object="ixsl:page()//body"/>
748755

749-
<xsl:sequence select="$control-groups/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', false() ])[current-date() lt xs:date('2000-01-01')]"/>
756+
<!-- pre-process form before submitting it -->
757+
<xsl:apply-templates select="." mode="ldh:FormPreSubmit"/>
750758

751759
<xsl:variable name="form" select="." as="element()"/>
752760
<xsl:variable name="method" select="ixsl:get(., 'method')" as="xs:string"/>
@@ -770,29 +778,32 @@ LIMIT 10
770778

771779
<xsl:template match="button[contains-token(@class, 'btn-load-endpoint-schema')]" mode="ixsl:onclick">
772780
<xsl:variable name="fieldset" select="ancestor::form/fieldset" as="element()"/>
773-
<xsl:variable name="service-control-group" select="$fieldset/descendant::div[contains-token(@class, 'control-group')][input[@name = 'pu'][@value = '&ldh;service']]" as="element()"/>
774-
<xsl:variable name="service-uri" select="$service-control-group/descendant::input[@name = 'ou']/ixsl:get(., 'value')" as="xs:anyURI"/>
775-
<xsl:variable name="limit-control-group" select="$fieldset/descendant::div[contains-token(@class, 'control-group')][input[@name = 'pu'][@value = '&sp;limit']]" as="element()"/>
776-
<xsl:variable name="limit-string" select="$limit-control-group/descendant::input[@name = 'ol']/ixsl:get(., 'value')" as="xs:string"/>
781+
<xsl:variable name="control-groups" select="descendant::div[contains-token(@class, 'control-group')]" as="element()*"/>
782+
<xsl:variable name="required-control-groups" select="$control-groups[contains-token(@class, 'required')]" as="element()*"/>
777783
<xsl:variable name="timeout" select="30000" as="xs:integer"/> <!-- schema load query timeout in milliseconds -->
778-
784+
785+
<!-- clear the errors initially -->
786+
<xsl:for-each select="$control-groups">
787+
<xsl:sequence select="ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', false() ])[current-date() lt xs:date('2000-01-01')]"/>
788+
</xsl:for-each>
789+
779790
<xsl:choose>
780-
<!-- service value missing, throw an error -->
781-
<xsl:when test="not($service-uri)">
782-
<xsl:sequence select="$service-control-group/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', true() ])[current-date() lt xs:date('2000-01-01')]"/>
783-
</xsl:when>
784-
<!-- limit value missing, throw an error -->
785-
<xsl:when test="not($limit-string) or not($limit-string castable as xs:integer)">
786-
<xsl:sequence select="$service-control-group/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', false() ])[current-date() lt xs:date('2000-01-01')]"/>
787-
<xsl:sequence select="$limit-control-group/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', true() ])[current-date() lt xs:date('2000-01-01')]"/>
791+
<!-- required input values missing, throw an error -->
792+
<xsl:when test="exists($required-control-groups/descendant::input[@name = ('ol', 'ou')][not(ixsl:get(., 'value'))])">
793+
<xsl:sequence select="ixsl:call(ixsl:event(), 'preventDefault', [])"/>
794+
<xsl:sequence select="$required-control-groups[descendant::input[@name = ('ol', 'ou')][not(ixsl:get(., 'value'))]]/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', true() ])[current-date() lt xs:date('2000-01-01')]"/>
788795
</xsl:when>
789796
<!-- all required values present/valid, load schema -->
790797
<xsl:otherwise>
791798
<ixsl:set-style name="cursor" select="'progress'" object="ixsl:page()//body"/>
792799

793-
<xsl:sequence select="$service-control-group/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', false() ])[current-date() lt xs:date('2000-01-01')]"/>
794-
<xsl:sequence select="$limit-control-group/ixsl:call(ixsl:get(., 'classList'), 'toggle', [ 'error', false() ])[current-date() lt xs:date('2000-01-01')]"/>
800+
<!-- pre-process form before submitting it -->
801+
<xsl:apply-templates select="ancestor::form" mode="ldh:FormPreSubmit"/>
795802

803+
<xsl:variable name="service-control-group" select="$fieldset/descendant::div[contains-token(@class, 'control-group')][input[@name = 'pu'][@value = '&ldh;service']]" as="element()"/>
804+
<xsl:variable name="service-uri" select="$service-control-group/descendant::input[@name = 'ou']/ixsl:get(., 'value')" as="xs:anyURI?"/>
805+
<xsl:variable name="limit-control-group" select="$fieldset/descendant::div[contains-token(@class, 'control-group')][input[@name = 'pu'][@value = '&sp;limit']]" as="element()"/>
806+
<xsl:variable name="limit-string" select="$limit-control-group/descendant::input[@name = 'ol']/ixsl:get(., 'value')" as="xs:string"/>
796807
<xsl:variable name="limit" select="xs:integer($limit-string)" as="xs:integer"/>
797808
<xsl:variable name="select-string" select="$endpoint-classes-string" as="xs:string"/>
798809
<xsl:variable name="select-json" as="item()">
@@ -812,10 +823,21 @@ LIMIT 10
812823
<xsl:variable name="query-json-string" select="xml-to-json($select-xml)" as="xs:string"/>
813824
<xsl:variable name="query-json" select="ixsl:call(ixsl:get(ixsl:window(), 'JSON'), 'parse', [ $query-json-string ])"/>
814825
<xsl:variable name="query-string" select="ixsl:call(ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'SelectBuilder'), 'fromQuery', [ $query-json ]), 'toString', [])" as="xs:string"/>
815-
<xsl:variable name="service-doc" select="document(ac:build-uri($ldt:base, map{ 'uri': ac:document-uri($service-uri), 'accept': 'application/rdf+xml' }))" as="document-node()"/> <!-- TO-DO: replace with <ixsl:schedule-action> -->
816-
<xsl:variable name="endpoint" select="key('resources', $service-uri, $service-doc)/sd:endpoint/@rdf:resource" as="xs:anyURI"/>
826+
<!-- use the specified endpoint if any, fall back to the internal one otherwise -->
827+
<xsl:variable name="endpoint" as="xs:anyURI">
828+
<xsl:choose>
829+
<xsl:when test="$service-uri">
830+
<!-- TO-DO: asynchronous request -->
831+
<xsl:variable name="service-doc" select="document(ac:build-uri($ldt:base, map{ 'uri': ac:document-uri($service-uri), 'accept': 'application/rdf+xml' }))" as="document-node()"/> <!-- TO-DO: replace with <ixsl:schedule-action> -->
832+
<xsl:sequence select="key('resources', $service-uri, $service-doc)/sd:endpoint/@rdf:resource"/>
833+
</xsl:when>
834+
<xsl:otherwise>
835+
<xsl:sequence select="sd:endpoint()"/>
836+
</xsl:otherwise>
837+
</xsl:choose>
838+
</xsl:variable>
817839
<xsl:variable name="results-uri" select="ac:build-uri($endpoint, map{ 'query': $query-string })" as="xs:anyURI"/>
818-
<xsl:variable name="request-uri" select="ldh:href($results-uri, map{})" as="xs:anyURI"/>
840+
<xsl:variable name="request-uri" select="ldh:href($results-uri)" as="xs:anyURI"/>
819841

820842
<xsl:variable name="request" as="item()*">
821843
<ixsl:schedule-action http-request="map{ 'method': 'GET', 'href': $request-uri, 'headers': map{ 'Accept': 'application/sparql-results+xml' } }" wait="$timeout">

0 commit comments

Comments
 (0)