Skip to content

Commit

Permalink
Add buttons/forms to the object page for locks creation and viewing.
Browse files Browse the repository at this point in the history
  • Loading branch information
lsitu authored and Andrew Woods committed Jul 29, 2014
1 parent 3279475 commit 384c059
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
23 changes: 23 additions & 0 deletions fcrepo-http-api/src/main/resources/views/common-node-actions.vsl
Expand Up @@ -110,6 +110,7 @@ WHERE { }

#if ($writable == true)
#set ($serializations = $rdf.find($nodeany, $topic, $helpers.asNode($rdfLexicon.HAS_SERIALIZATION), $nodeany))
#set ($isLockable = $helpers.isRdfResource($rdf, $topic, $rdfLexicon.MIX_NAMESPACE, "lockable"))

#if($serializations.hasNext())
<form id="action_import" action="fcr:import" method="POST">
Expand Down Expand Up @@ -159,6 +160,28 @@ WHERE { }
<hr />
</form>
#end

#if($isLockable)
#set ($lockUrl = $helpers.getLockUrl($rdf, $topic))
#set ($viewDisplay = "none")
#set ($createDisplay = "block")
#if ($lockUrl && $lockUrl.length() > 0)
#set ($viewDisplay = "block")
#set ($createDisplay = "none")
#end
<div class="actions collapse visible-lg visible-md" id="div_lock">
<h3>Locks</h3>
<div class="form-group" id="div_view_lock" style="display:$viewDisplay">
<div class="control-label">Item is Locked!</div>
<button type="button" id="btn_view_lock" class="btn btn-primary" onClick="javascript:viewLock('$lockUrl');">View Lock</button>
</div>
<div class="form-group" id="div_create_lock" style="display:$createDisplay">
<div class="control-label"><input type="checkbox" id="deep_id" name="deep"/> Is Deep</div>
<button type="button" id="btn_create_lock" class="btn btn-primary" onClick="javascript:createLock();">Create Lock</button>
</div>
<hr />
</div>
#end
#end

#if ($writable == true)
Expand Down
26 changes: 26 additions & 0 deletions fcrepo-http-api/src/main/resources/views/common.js
Expand Up @@ -242,6 +242,32 @@ function deleteItem()
return false;
}

function createLock()
{
var uri = $('#main').attr('resource');
var isDeep = false;
if ($('#deep_id').prop('checked'))
isDeep = true;
$.ajax({
type: "POST",
url: uri + "/fcr:lock?deep=" + isDeep,
success: function() {
$('#div_create_lock').hide();
$('#div_view_lock').show();
}
}).fail( ajaxErrorHandler);
}

function viewLock(lockUrl)
{
$('#div_view_lock').hide();
if (!lockUrl || lockUrl.length === 0) {
var uri = $('#main').attr('resource');
lockUrl = uri + "/fcr:lock";
}
window.location.href = lockUrl;
}

function updateFile()
{
var update_file = document.getElementById("update_file").files[0];
Expand Down
Expand Up @@ -16,13 +16,15 @@
package org.fcrepo.http.commons.responses;

import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.fcrepo.jcr.FedoraJcrTypes.FCR_CONTENT;
import static org.fcrepo.kernel.RdfLexicon.DC_TITLE;
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION_LABEL;
import static org.fcrepo.kernel.RdfLexicon.LAST_MODIFIED_DATE;
import static org.fcrepo.kernel.RdfLexicon.RDFS_LABEL;
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION;
import static org.fcrepo.kernel.RdfLexicon.HAS_CONTENT;
import static org.fcrepo.kernel.RdfLexicon.RDF_NAMESPACE;
import static org.slf4j.LoggerFactory.getLogger;

import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -414,6 +416,31 @@ public String getPrefixPreamble(final PrefixMapping mapping) {
return sb.toString();
}

/**
* Determines whether the subject is kind of RDF resource
*/
public boolean isRdfResource(final DatasetGraph dataset,
final Node subject,
final String namespace,
final String resource) {
final Iterator<Quad> it = dataset.find(ANY,
subject,
createResource(RDF_NAMESPACE + "type").asNode(),
createResource(namespace + resource).asNode());
return it.hasNext();
}

/**
* Retrieve the uri for the locks
*/
public String getLockUrl(final DatasetGraph dataset, final Node subject) {
final Iterator<Quad> it = dataset.find(ANY, subject, RdfLexicon.HAS_LOCK.asNode(), ANY);
if (it.hasNext()) {
return it.next().getObject().getURI();
}
return "";
}

/**
* Convert an RDF resource to an RDF node
*
Expand Down
Expand Up @@ -33,6 +33,7 @@
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION;
import static org.fcrepo.kernel.RdfLexicon.HAS_CONTENT;
import static org.fcrepo.kernel.RdfLexicon.WRITABLE;
import static org.fcrepo.kernel.RdfLexicon.RDF_NAMESPACE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand All @@ -44,6 +45,7 @@

import javax.ws.rs.core.UriInfo;

import org.fcrepo.kernel.RdfLexicon;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -166,6 +168,32 @@ public void testIsNotFrozenNode() {
assertFalse("Node is not a frozen node.", testObj.isFrozenNode(mem, createURI("a/b/c")));
}

@Test
public void testRdfResource() {
final String ns = "http://any/namespace#";
final String type = "anyType";
final DatasetGraph mem = createMem();
mem.add(createAnon(),
createURI("a/b"),
createResource(RDF_NAMESPACE + "type").asNode(),
createResource(ns + type).asNode());

assertTrue("Node is a " + type + " node.",
testObj.isRdfResource(mem, createURI("a/b"), ns, type));
assertFalse("Node is not a " + type + " node.",
testObj.isRdfResource(mem, createURI("a/b"), ns, "otherType"));
}

@Test
public void testGetLockUrl() {
final Node lockUrl = createURI("a/b/fcr:lock");
final DatasetGraph mem = createMem();
mem.add(createAnon(), createURI("a/b"), RdfLexicon.HAS_LOCK.asNode(), lockUrl);

assertEquals("Wrong lock url returned!", lockUrl.getURI(),
testObj.getLockUrl(mem, createURI("a/b")));
}

@Test
public void shouldFindVersionRoot() {

Expand Down
Expand Up @@ -303,6 +303,30 @@ public void testCreateNewNamespace() throws IOException {
assertTrue("New uri was not found", page2.asText().contains(uri_value));
}

/**
* This test create a lock from the object page and examine the lock created.
*/
@Test
public void testLockCreationFromObjectPage() throws IOException {
final String pid = randomUUID().toString();
createAndVerifyObjectWithIdFromRootPage(pid);

final HtmlPage page = webClient.getPage(serverAddress + pid);
final HtmlButton createButton = (HtmlButton) page.getElementById("btn_create_lock");
assertTrue("Should have Create Lock button.", createButton != null);
createButton.click();

webClient.waitForBackgroundJavaScript(1000);
webClient.waitForBackgroundJavaScriptStartingBefore(10000);

final HtmlPage page1 = webClient.getPage(serverAddress + pid);
assertTrue("Should have the View Lock button.", page1.getElementById("btn_view_lock") != null);

final HtmlPage lockPage = webClient.getPage(serverAddress + pid + "/fcr:lock");
assertTrue("Should have fedora:locks property.", lockPage.asText().contains("lock"));
assertTrue("Should have fedora:isDeep property.", lockPage.asText().contains("isDeep"));
}

private void checkForHeaderSearch(final HtmlPage page) {
final HtmlForm form = page.getFirstByXPath("//form[@role='search']");
assertNotNull(form);
Expand Down

0 comments on commit 384c059

Please sign in to comment.