Show a warning if there is a new project version published.

It checks if there is a new version available via http://libreplan.org/VERSION,
if so it shows a warning message to admin users in order to update to new
version.

The request to URL is done just once a day.

FEA: ItEr76S10NewVersionsNotification
This commit is contained in:
Manuel Rego Casasnovas 2011-12-28 13:34:52 +01:00
parent 477260e808
commit 13102252a1
3 changed files with 87 additions and 2 deletions

View file

@ -19,18 +19,66 @@
package org.libreplan.business.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* It contains the current version of project and implements of singleton
* pattern.
* pattern. <br />
* It also has a cached value with information about last project version
* published. It checks the last version against a URL.
*
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
public class VersionInformation {
private static final Log LOG = LogFactory.getLog(VersionInformation.class);
/**
* URL with a text file only with last number version of LibrePlan
*/
private static final String LIBREPLAN_VERSION_URL = "http://libreplan.org/VERSION";
/**
* Delay to wait till we check the URL again
*/
private static final long DELAY_TO_CHECK_URL = 24 * 60 * 60 * 1000; // 1 day
private static final VersionInformation singleton = new VersionInformation();
private String projectVersion;
private boolean newVersionCached = false;
private Date lastVersionCachedDate = new Date();
private VersionInformation() {
loadNewVersionFromURL();
}
private void loadNewVersionFromURL() {
lastVersionCachedDate = new Date();
try {
URL url = new URL(LIBREPLAN_VERSION_URL);
String lastVersion = (new BufferedReader(new InputStreamReader(
url.openStream()))).readLine();
if (projectVersion != null && lastVersion != null) {
newVersionCached = !projectVersion.equals(lastVersion);
}
} catch (MalformedURLException e) {
LOG.warn("Problems reading LibrePlan version from "
+ LIBREPLAN_VERSION_URL, e);
} catch (IOException e) {
LOG.warn("Problems reading LibrePlan version from "
+ LIBREPLAN_VERSION_URL, e);
}
}
public static VersionInformation getInstance() {
@ -52,4 +100,27 @@ public class VersionInformation {
projectVersion = argVersion;
}
}
/**
* Returns true if a new version of the project is published.
*/
public static boolean isNewVersionAvailable() {
return singleton.checkIsNewVersionAvailable();
}
/**
* If there is a new version already detected, it doesn't check it again.
* Otherwise, during one day it returns the cached value. And it checks it
* again after that time.
*/
private boolean checkIsNewVersionAvailable() {
if (!newVersionCached) {
long oneDayLater = lastVersionCachedDate.getTime()
+ DELAY_TO_CHECK_URL;
if (oneDayLater < new Date().getTime()) {
loadNewVersionFromURL();
}
}
return newVersionCached;
}
}

View file

@ -28,6 +28,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.libreplan.business.common.VersionInformation;
import org.libreplan.business.scenarios.IScenarioManager;
import org.libreplan.business.scenarios.entities.Scenario;
import org.libreplan.web.common.ITemplateModel.IOnFinished;
@ -182,4 +183,8 @@ public class TemplateController extends GenericForwardComposer {
return templateModel.isUserAdmin();
}
public boolean isNewVersionAvailable() {
return VersionInformation.isNewVersionAvailable();
}
}

View file

@ -156,6 +156,15 @@ signature="java.lang.Boolean isDefaultPasswordsControl()"?>
</n:span>
</div>
</n:div>
<n:div if="${templateCtrl.newVersionAvailable}" id="warningNewVersion"
class="footer-messages-area">
<div>
<n:span>
${i18n:_("A new version of LibrePlan is avilable. Please check next link for more information:")}
<n:strong><n:a href="http://www.libreplan.com/download/" target="_blank">http://www.libreplan.com/download/</n:a></n:strong>
</n:span>
</div>
</n:div>
</n:td>
<n:td height="40" align="right" valign="bottom">
</n:td>