Skip to content

Commit

Permalink
Final cleanup of the DatasetPage refresh mechanism;
Browse files Browse the repository at this point in the history
Switched a few noisy log messages to .fine.
  • Loading branch information
landreev committed Jan 19, 2016
1 parent b2fcf4d commit 6561f58
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 65 deletions.
86 changes: 47 additions & 39 deletions src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public enum DisplayMode {
private boolean noDVsAtAll = false;

private boolean noDVsRemaining = false;

private boolean stateChanged = false;

private List<Dataverse> dataversesForLinking = new ArrayList();
private Long linkingDataverseId;
Expand Down Expand Up @@ -1915,16 +1917,16 @@ public void refresh(ActionEvent e) {
refresh();
}

public void refresh() { // String flashmessage) {
logger.info("refreshing");
public void refresh() {
logger.fine("refreshing");

//dataset = datasetService.find(dataset.getId());
dataset = null;
dataset = null;

logger.fine("refreshing working version");

DatasetVersionServiceBean.RetrieveDatasetVersionResponse retrieveDatasetVersionResponse = null;

if (persistentId != null) {
//retrieveDatasetVersionResponse = datasetVersionService.retrieveDatasetVersionByPersistentId(persistentId, version);
dataset = datasetService.findByGlobalId(persistentId);
Expand All @@ -1949,15 +1951,15 @@ public void refresh() { // String flashmessage) {
if (this.workingVersion == null) {
// TODO:
// same as the above
return;
return;
}

if (dataset == null) {
// this would be the case if we were retrieving the version by
// the versionId, above.
this.dataset = this.workingVersion.getDataset();
}

if (readOnly) {
datafileService.findFileMetadataOptimizedExperimental(dataset);
fileMetadatasSearch = workingVersion.getFileMetadatas();
Expand All @@ -1966,8 +1968,9 @@ public void refresh() { // String flashmessage) {
}

displayCitation = dataset.getCitation(false, workingVersion);
stateChanged = false;
}

public String deleteDataset() {

Command cmd;
Expand Down Expand Up @@ -2560,54 +2563,59 @@ private HttpClient getClient() {
return new HttpClient();
}

public void refreshLock() {
//RequestContext requestContext = RequestContext.getCurrentInstance();
logger.fine("checking lock");
if (isStillLocked()) {
logger.fine("(still locked)");

} else {
// OK, the dataset is no longer locked.
// let's tell the page to refresh:
logger.fine("no longer locked!");
stateChanged = true;
//requestContext.execute("refreshPage();");
}
}

/*
*/
public boolean isLockedInProgress() {
if (dataset != null) {
public boolean isStillLocked() {
if (dataset != null && dataset.getId() != null) {
logger.fine("checking lock status of dataset " + dataset.getId());
if (dataset.isLocked()) {
if (datasetService.checkDatasetLock(dataset.getId())) {
return true;
}
}
return false;
}

public boolean isLocked() {
if (stateChanged) {
return false;
}

if (dataset != null) {
/*logger.fine("checking lock status of dataset " + dataset.getId());
if (dataset.isLocked()) {
// refresh the dataset and version, if the current working
// version of the dataset is locked:
refresh();
}*/
/* TODO:
optimized/improve the logic of this!
Looking up the entire dataset every time isLocked() is called
was costing us a lot of queries; it's called something like
15 times during the initial page load (and there are cascaded
queries for every DvObject/DataFile SELECT query). So that was
not cool... but perhaps there still should be some mechanism
for checking if this unlocked condition is already stale, i.e.
if the dataset has been locked since this copy of the dataset
object was instantiated?
-- L.A. 4.2.1
Dataset lookedupDataset = datasetService.find(dataset.getId());
DatasetLock datasetLock = null;
if (lookedupDataset != null) {
datasetLock = lookedupDataset.getDatasetLock();
if (datasetLock != null) {
logger.fine("locked!");
return true;
}
}*/
if (dataset.isLocked()) {
return true;
}
}
return false;
}

public void setLocked(boolean locked) {
// empty method, so that we can use DatasetPage.locked in a hidden
// input on the page.
}

public boolean isStateChanged() {
return stateChanged;
}

public void setStateChanged(boolean stateChanged) {
// empty method, so that we can use DatasetPage.stateChanged in a hidden
// input on the page.
}

public DatasetVersionUI getDatasetVersionUI() {
return datasetVersionUI;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/DatasetServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,22 @@ public List<DatasetLock> getDatasetLocks() {
return (List<DatasetLock>) em.createQuery(query).getResultList();
}

public boolean checkDatasetLock(Long datasetId) {
String nativeQuery = "SELECT sl.id FROM DatasetLock sl WHERE sl.dataset_id = " + datasetId + " LIMIT 1;";
Integer lockId = null;
try {
lockId = (Integer)em.createNativeQuery(nativeQuery).getSingleResult();
} catch (Exception ex) {
lockId = null;
}

if (lockId != null) {
return true;
}

return false;
}

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void addDatasetLock(Long datasetId, Long userId, String info) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ As of now (4.2.3), this is the ONLY situation where we check the remote
-- L.A. 4.2.3
*/

logger.info("DataverseRequest: Obtained remote address: "+remoteAddressStr);
logger.fine("DataverseRequest: Obtained remote address: "+remoteAddressStr);

if ( remoteAddressStr == null ) {
remoteAddressStr = "0.0.0.0";
Expand Down
83 changes: 58 additions & 25 deletions src/main/webapp/dataset.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,65 @@
<!-- View/Tabs infoMode -->
<!-- Tabs -->
<div id="contentTabs" jsf:rendered="#{DatasetPage.editMode != 'INFO'}">
<ui:fragment rendered="#{DatasetPage.lockedInProgress}">
</ui:fragment>

<ui:fragment rendered="#{!empty DatasetPage.dataset.id and !DatasetPage.readOnly}"> <!-- rendered="#{empty DatasetPage.editMode}" -->
<p:commandButton id="setLockedButton" widgetVar="setLockedButton" onclick="setLockedVar(#{locked});" style="display:none"/>
<p:commandButton id="refreshButton" widgetVar="refreshButton" actionListener="#{DatasetPage.refresh}" update="@all" style="display:none"/><!-- :datasetForm:tabView:filesTable,:messagePanel -->

</ui:fragment>
<p:fragment id="pageRefreshFragment"> <!-- rendered="_{empty DatasetPage.editMode}" -->
<h:inputHidden value="#{DatasetPage.locked}" id="datasetLockedVariable" />
<h:inputHidden value="#{DatasetPage.stateChanged}" id="datasetStateChangedVariable" />

<p:remoteCommand name="refreshLockCommand" process="@this" update=":#{p:component('pageRefreshFragment')}" actionListener="#{DatasetPage.refreshLock}"/>
<p:remoteCommand name="refreshAllCommand" process="@this" update="@all" actionListener="#{DatasetPage.refresh}"/>

<!-- see the comment in the javascript below that explains what this button is for -->
<p:commandButton id="refreshButton" process="@this" widgetVar="refreshButton" update="@all" style="display:none"/>

<script type="text/javascript">

$(this).ready(function() {
refreshIfStillLocked();
});

function refreshIfStillLocked() {
if($('input[id$="datasetLockedVariable"]').val() === 'true') {
// if dataset is locked, instruct the page to
// wait and check again:
waitAndCheckLockAgain();
} else {
// if not locked, has it just been unlocked?
if ($('input[id$="datasetStateChangedVariable"]').val() === 'true') {
// For whatever unknown PrimeFaces reason
// the page needs to be refreshed twice, for all
// the pull down menus to update properly:
refreshAllCommand();
// You can't just run 2 refreshAllCommand()s in a row
// either; because the command has an "update=@all" on it,
// so I guess if you try to execute the 2nd one right after
// the first one, this fragment is still going to be loading -
// so there would not yet be a command to run! (it needs to
// be rendered, before you can execute it, that is)
setTimeout(function() {
// this button doesn't do anything, but it has an update="@all"
// attribute:
$('button[id$="refreshButton"]').trigger('click');
//refreshAllCommand();
}, 1500);
}
}
}

function waitAndCheckLockAgain() {
setTimeout(function () {
// refresh the lock in the
// backing bean; i.e., check, if the ingest has
// already completed in the background:
//$('button[id$="refreshButton"]').trigger('click');
refreshLockCommand();
}, 10000);
}

</script>


</p:fragment>

<ui:fragment id="uploadFilesOnCreateTab" rendered="#{!DatasetPage.workingVersion.deaccessioned and
(DatasetPage.editMode == 'CREATE')}">
Expand Down Expand Up @@ -1418,24 +1469,6 @@
Dropbox.choose(options);
}

$(document).ready(function() {
refreshIfLocked();
});

function setLockedVar(status) {
window.locked = status;
}
function refreshIfLocked() {
setTimeout(function () {
// update the global variable "locked":
$('button[id$="setLockedButton"]').trigger('click');
// if dataset is still locked, click the refresh button:
if (window.locked === true) {
$('button[id$="refreshButton"]').trigger('click');
}
}, 10000);
}


</script>
</ui:define>
Expand Down

0 comments on commit 6561f58

Please sign in to comment.