Skip to content

Commit

Permalink
Merge pull request #16 from UKGovLD/coherence-matching
Browse files Browse the repository at this point in the history
Coherence matching
  • Loading branch information
simonoakesepimorphics authored Jul 29, 2019
2 parents 6f6c66e + ed278c8 commit 1d8d022
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 22 deletions.
1 change: 1 addition & 0 deletions ldregistry/config/app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ config.registryName = Test Registry
config.showStatus = valid
#config.loginMaster = https://environment.data.gov.uk/registry
config.oauth2 = $oauth2
#config.similarityParam = 0.7

# The Registry configuration itself
registry = com.epimorphics.registry.core.Registry
Expand Down
41 changes: 39 additions & 2 deletions ldregistry/templates/actions/_form-for.vm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
## $register - the register to which the form should be sent
## $return - URL to return to

<form class="ajax-form" action="$register" method="post" data-return="$return">
<input type="hidden" name="action" value="register-inline" />
<form class="ajax-form" action="$register" method="post" data-return="$return" id="create-entry-form">
<input type="hidden" name="form-type" value="$spec.uRI" />

<p>
Expand All @@ -29,4 +28,42 @@
#end

<button class="btn">Create and register</button>
<button id="create-entry-form-compare" type="button" class="btn">Find Similar</button>
</form>

<div class="compare-results"></div>

<script type="text/javascript" src="$assets/js/registry-compare.js"></script>
<script type="text/javascript">
$(function() {
registryCompare({
selector: {
target: ".compare-results",
error: ".ajax-error",
button: "#create-entry-form-compare",
form: "#create-entry-form"
}
});

$("#create-entry-form").submit(function() {
var form = $(this);
var returnURL = form.attr('data-return');

form.ajaxSubmit({
data: { action: "register-inline" },
success: function(data, status, xhr) {
if (returnURL) {
window.location.href = returnURL;
} else {
location.reload();
}
},
error: function(xhr, status, error) {
$(".ajax-error").html("<div class='alert alert-warning'> <button type='button' class='close' data-dismiss='alert'>&times;</button>Action failed: " + error + " - " + xhr.responseText + "</div>");
}
});

return false;
});
});
</script>
2 changes: 1 addition & 1 deletion ldregistry/templates/actions/create-manual-page.vm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="col-md-10 col-md-offset-1 modal-page-panel">

<div class="modal-page-head">
<h3>Create a new entry in in $register</h3>
<h3>Create a new entry in $register</h3>
</div>

<div class="modal-page-body">
Expand Down
53 changes: 39 additions & 14 deletions ldregistry/templates/actions/create-register-page.vm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="ajax-error"></div>

<form id="create-register-form" action="$register" method="post" data-return="$return">
<input type="hidden" name="action" value="register-inline" />
<!-- <input type="hidden" name="action" value="register-inline" /> -->
<input type="hidden" name="form-type" value="$registry.baseURI/system/form-templates/register" />

<p>All fields are required.</p>
Expand Down Expand Up @@ -76,8 +76,10 @@
#end

<button class="btn form-submit">Create register</button>
<button class="btn" type="button" id="create-register-form-compare">Find similar</button>
</form>

<div class="compare-results"></div>
</div>

<div class="modal-page-footer">
Expand All @@ -88,6 +90,8 @@
</div>
<div class="modal-backdrop in"></div>

<script type="text/javascript" src="$assets/js/registry-compare.js"></script>

<script type="text/javascript">
$(function() {
var setOption = function(option, attrName, options, oName) {
Expand All @@ -96,17 +100,41 @@ $(function() {
}
};

$("#create-register-form").submit( function(){
var makeOptions = function() {
var selection = $("\#register-type option:selected").first();
var options = {};
setOption( selection, "data-membership", options, "membership" );
setOption( selection, "data-imembership", options, "inv-membership" );
setOption( selection, "data-type", options, "type" );
setOption( selection, "data-hRoot", options, "hRoot" );
setOption( selection, "data-hChild", options, "hChild" );

return options;
};

var onError = function(xhr, status, error) {
$(".ajax-error").html("<div class='alert alert-warning'> <button type='button' class='close' data-dismiss='alert'>&times;</button>Action failed: " + error + " - " + xhr.responseText + "</div>");
}

registryCompare({
selector: {
target: ".compare-results",
error: ".ajax-error",
button: "#create-register-form-compare",
form: "#create-register-form"
},
data: {
apply: function (data) {
return Object.assign(data, makeOptions());
}
}
});

$("#create-register-form").submit(function() {
var form = $(this);
var returnURL = form.attr('data-return');
var option = $("\#register-type option:selected").first();
var regType = option.val();
var options = {};
setOption( option, "data-membership", options, "membership" );
setOption( option, "data-imembership", options, "inv-membership" );
setOption( option, "data-type", options, "type" );
setOption( option, "data-hRoot", options, "hRoot" );
setOption( option, "data-hChild", options, "hChild" );
var options = makeOptions();
options.action = "register-inline";

form.ajaxSubmit(
{
Expand All @@ -120,10 +148,7 @@ $(function() {
}
},

error:
function(xhr, status, error){
$(".ajax-error").html("<div class='alert alert-warning'> <button type='button' class='close' data-dismiss='alert'>&times;</button>Action failed: " + error + " - " + xhr.responseText + "</div>");
}
error: onError
}
);
return false;
Expand Down
33 changes: 28 additions & 5 deletions ldregistry/templates/actions/register-page.vm
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@
</label>
<input id="patch-chooser" class="form-control" type="file" name="file" multiple />
</div>
<input id="action-field" type="hidden" name="action" value="register" />
<button class="btn form-submit">upload</button>
<button class="btn form-submit">Upload</button>
<button id="registration-form-compare" type="button" class="btn">Find Similar</button>
</form>
<div class="compare-result"></div>
</div>

<div class="modal-page-footer">
Expand All @@ -62,24 +63,46 @@
</div>
<div class="modal-backdrop in"></div>

<script type="text/javascript" src="$assets/js/registry-compare.js"></script>

<script type="text/javascript">
$(function() {
$("#registration-form").submit( function(){

var getUploadOption = function() {
return $("\#registration-form input[name=uploadOptions]:checked").val();
}

registryCompare({
selector: {
error: ".ajax-error",
target: ".compare-result",
button: "#registration-form-compare",
form: "#registration-form"
},
data: {
isEdit: function() {
return getUploadOption() === "edit";
}
}
});

$("#registration-form").submit( function() {
var form = $(this);
var returnURL = form.attr('data-return');
var target = form.attr('action');
var uploadOption = $("\#registration-form input[name=uploadOptions]:checked").val();
var uploadOption = getUploadOption();
var data = { action: "register" };
if (uploadOption) {
if (uploadOption === "edit") {
$("\#action-field").val( "edit" );
data.action = "edit";
} else if (uploadOption === "batch") {
target = target + "?batch-managed";
}
};
form.ajaxSubmit(
{
url: target,
data: data,
success:
function(data, status, xhr){
if (returnURL) {
Expand Down
96 changes: 96 additions & 0 deletions ldregistry/templates/registry-compare.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#*
$response | The Response instance to be rendered. The response entity is a Jena model.
$newItems | The RDF Nodes corresponding to the register items that were inferred from the request.
*#

#set($root=$registry.rootPath)
#set($uiroot="$root/ui")
#set($assets="$uiroot/assets")

#macro( resultRowValues $item )
#set( $itemEntity = $item.getPropertyValue("reg:definition").getPropertyValue("reg:entity") )
<td>
<a href="#linkhref($item)" title="$lib.reg.xssCleanURI($item.uRI)" target="_blank">$item.name</a>
</td>
<td>
#set( $registerPath = $item.getPropertyValue("reg:register").uRI.substring($registry.baseURI.length()) )
#set( $registerUri = "$root$registerPath" )
<a href="$lib.reg.xssCleanURI($registerUri)" title="$lib.reg.xssCleanURI($registerUri)" target="_blank">$registerPath</a>
</td>
<td>
#foreach($ty in $itemEntity.listPropertyValues("rdf:type"))
#linkfor($ty)#if( $foreach.hasNext ), #end
#end
</td>
<td>#showstatus($item.getPropertyValue("reg:status"))</td>
#end

#macro( resultRow $newItemW $property $isMulti )
#set( $items = $newItemW.listPropertyValues($property) )
#set( $firstItem = true )

#foreach( $item in $items )
<tr>
#set( $newEntity = $newItemW.getPropertyValue("reg:definition").getPropertyValue("reg:entity") )
#if( $firstItem && $isMulti )<td rowspan="$items.size()">$lib.reg.xssCleanURI($newEntity.uRI)</td>#end
#resultRowValues($item)
</tr>

#set( $firstItem = false )
#end
#end

#macro( resultTable $modelW $newItems $property )
#set( $isMulti = $newItems.size() > 1 )

<table class="table table-striped table-bordered" style="color: black; background-color: white;">
<thead>
<tr>
#if( $isMulti )<th>New</th>#end
<th>Suggested</th>
<th>Register</th>
<th>Types</th>
<th>Status</th>
</tr>
</thead>

<tbody>
#foreach( $newItem in $newItems )
#set( $newItemW = $modelW.getNode($newItem.uRI) )
#resultRow( $newItemW, $property, $isMulti )
#end
</tbody>
</table>
#end

<div>
#set( $model = $response.entity )
#set( $modelW = $lib.reg.wrapModel($response.entity) )
#set( $isExactMatch = $modelW.queryAsk("ASK { ?result a reg:CompareResult; rdfs:member/skos:exactMatch ?item }") )
#set( $isCloseMatch = $modelW.queryAsk("ASK { ?result a reg:CompareResult; rdfs:member/skos:closeMatch ?item }") )

#if( $isExactMatch || $isCloseMatch )
#if( $isExactMatch )
<div class='alert alert-warning'>
<button type='button' class='close' data-dismiss='alert'>&times;</button>
<span>One or more entries exist with the same label or identifier:</span>

#resultTable( $modelW, $newItems, "skos:exactMatch" )
</div>
#end

#if( $isCloseMatch )
<div class='alert alert-warning'>
<button type='button' class='close' data-dismiss='alert'>&times;</button>
<span>One or more entries exist with a similar label:</span>

#resultTable( $modelW, $newItems, "skos:closeMatch" )
</div>
#end
#else
<div class='alert'>
<button type='button' class='close' data-dismiss='alert'>&times;</button>
<span>No similar entries were found.</span>
</div>
#end
</div>
51 changes: 51 additions & 0 deletions ldregistry/ui/assets/js/registry-compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* This function provides a simplified interface for invoking the registry comparison feature for a single entity,
* and rendering the results as HTML within a containing document.
* @param {Object} opts The block of parameters.
* @param {Object} opts.selector The block of CSS selectors which determine the input data and output display. All selectors are required.
* @param {String} opts.selector.target The selector for the element to output the results of the comparison.
* @param {String} opts.selector.error The selector for the element to output error messages if the comparison fails.
* @param {String} opts.selector.button The selector for the button element which triggers the comparison.
* @param {String} opts.selector.form The selector for the form element which contains the data to be compared.
* @param {Object} opts.data The block of modifiers which alter the data submitted to the API. Optional.
* @param {Function} opts.data.apply A function which modifies the supplied set of key-value pairs which will be added to the form data.
* @param {Function} opts.data.isEdit A function which determines whether the submitted data contains edits to existing registry items.
*/
var registryCompare = function(opts) {
var renderError = function(msg) {
$(opts.selector.error).html("<div class='alert alert-warning'> <button type='button' class='close' data-dismiss='alert'>&times;</button>" + msg + "</div>");
};

var onError = function(xhr, status, error) {
renderError("Action failed: " + error + " - " + xhr.responseText);
};

$(opts.selector.button).click(function(event) {
var form = $(opts.selector.form);
var target = form.attr('action') + "?compare";
var data = {
action: "compare"
};

if (opts.data) {
if (opts.data.apply) {
opts.data.apply(data);
}

if (opts.data.isEdit && opts.data.isEdit()) {
target += "&compare-edit"
}
}

form.ajaxSubmit({
type: "post",
url: target,
headers: {
"Accept": "text/html"
},
target: opts.selector.target,
data: data,
error: onError
});
});
};

0 comments on commit 1d8d022

Please sign in to comment.