From ef5f68a96c0b82ceb9764fa6996f1ef1f58d9ce0 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Tue, 5 Apr 2011 08:20:55 +0200 Subject: [PATCH] make date format configurable --- .../sonia/scm/config/ScmConfiguration.java | 31 +++++++ .../main/java/sonia/scm/ScmClientConfig.java | 90 +++++++++++++++++++ .../src/main/java/sonia/scm/ScmState.java | 30 ++++++- .../resources/AuthenticationResource.java | 35 ++++++-- .../main/webapp/resources/js/sonia.config.js | 10 +++ .../main/webapp/resources/js/sonia.util.js | 8 +- 6 files changed, 194 insertions(+), 10 deletions(-) create mode 100644 scm-webapp/src/main/java/sonia/scm/ScmClientConfig.java diff --git a/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java b/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java index 7e46613026..c9b079df5b 100644 --- a/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java +++ b/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java @@ -61,6 +61,9 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; public class ScmConfiguration { + /** Field description */ + public static final String DEFAULT_DATEFORMAT = "Y-m-d H:i:s"; + /** Field description */ public static final String DEFAULT_PLUGINURL = "http://plugins.scm-manager.org/scm-plugin-backend/api/{version}/plugins?os={os}&arch={arch}&snapshot=false"; @@ -84,6 +87,7 @@ public class ScmConfiguration public void load(ScmConfiguration other) { this.servername = other.servername; + this.dateFormat = other.dateFormat; this.pluginUrl = other.pluginUrl; this.sslPort = other.sslPort; this.enableSSL = other.enableSSL; @@ -118,6 +122,17 @@ public class ScmConfiguration return adminUsers; } + /** + * Method description + * + * + * @return + */ + public String getDateFormat() + { + return dateFormat; + } + /** * Method description * @@ -230,6 +245,17 @@ public class ScmConfiguration this.anonymousAccessEnabled = anonymousAccessEnabled; } + /** + * Method description + * + * + * @param dateFormat + */ + public void setDateFormat(String dateFormat) + { + this.dateFormat = dateFormat; + } + /** * Method description * @@ -327,6 +353,11 @@ public class ScmConfiguration /** Field description */ private int sslPort = 8181; + /** + * JavaScript date format, see http://jacwright.com/projects/javascript/date_format + */ + private String dateFormat = DEFAULT_DATEFORMAT; + /** Field description */ private boolean anonymousAccessEnabled = false; } diff --git a/scm-webapp/src/main/java/sonia/scm/ScmClientConfig.java b/scm-webapp/src/main/java/sonia/scm/ScmClientConfig.java new file mode 100644 index 0000000000..6a800f9d53 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/ScmClientConfig.java @@ -0,0 +1,90 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm; + +/** + * + * @author Sebastian Sdorra + */ +public class ScmClientConfig +{ + + /** + * Constructs ... + * + */ + public ScmClientConfig() {} + + /** + * Constructs ... + * + * + * @param dateFormat + */ + public ScmClientConfig(String dateFormat) + { + this.dateFormat = dateFormat; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public String getDateFormat() + { + return dateFormat; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param dateFormat + */ + public void setDateFormat(String dateFormat) + { + this.dateFormat = dateFormat; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String dateFormat; +} diff --git a/scm-webapp/src/main/java/sonia/scm/ScmState.java b/scm-webapp/src/main/java/sonia/scm/ScmState.java index 459d6672c1..9d768e0820 100644 --- a/scm-webapp/src/main/java/sonia/scm/ScmState.java +++ b/scm-webapp/src/main/java/sonia/scm/ScmState.java @@ -71,19 +71,33 @@ public class ScmState * @param provider * @param securityContext * @param repositoryTypes + * @param clientConfig */ public ScmState(SCMContextProvider provider, WebSecurityContext securityContext, - Collection repositoryTypes) + Collection repositoryTypes, + ScmClientConfig clientConfig) { this.version = provider.getVersion(); this.user = securityContext.getUser(); this.groups = securityContext.getGroups(); this.repositoryTypes = repositoryTypes; + this.clientConfig = clientConfig; } //~--- get methods ---------------------------------------------------------- + /** + * Method description + * + * + * @return + */ + public ScmClientConfig getClientConfig() + { + return clientConfig; + } + /** * Method description * @@ -141,6 +155,17 @@ public class ScmState //~--- set methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param clientConfig + */ + public void setClientConfig(ScmClientConfig clientConfig) + { + this.clientConfig = clientConfig; + } + /** * Method description * @@ -198,6 +223,9 @@ public class ScmState //~--- fields --------------------------------------------------------------- + /** Field description */ + private ScmClientConfig clientConfig; + /** Field description */ private Collection groups; diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AuthenticationResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AuthenticationResource.java index a4e34d0560..ec87c5ae4f 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AuthenticationResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AuthenticationResource.java @@ -44,7 +44,9 @@ import org.slf4j.LoggerFactory; import sonia.scm.SCMContext; import sonia.scm.SCMContextProvider; +import sonia.scm.ScmClientConfig; import sonia.scm.ScmState; +import sonia.scm.config.ScmConfiguration; import sonia.scm.repository.RepositoryManager; import sonia.scm.user.User; import sonia.scm.web.security.WebSecurityContext; @@ -85,16 +87,18 @@ public class AuthenticationResource * * * @param contextProvider + * @param configuration * @param repositoryManger * @param securityContextProvider */ @Inject public AuthenticationResource( - SCMContextProvider contextProvider, + SCMContextProvider contextProvider, ScmConfiguration configuration, RepositoryManager repositoryManger, Provider securityContextProvider) { this.contextProvider = contextProvider; + this.configuration = configuration; this.repositoryManger = repositoryManger; this.securityContextProvider = securityContextProvider; } @@ -126,8 +130,7 @@ public class AuthenticationResource if ((user != null) &&!SCMContext.USER_ANONYMOUS.equals(user.getName())) { - state = new ScmState(contextProvider, securityContext, - repositoryManger.getConfiguredTypes()); + state = createState(securityContext); } else { @@ -160,8 +163,7 @@ public class AuthenticationResource if (user != null) { - ScmState state = new ScmState(contextProvider, securityContext, - repositoryManger.getConfiguredTypes()); + ScmState state = createState(securityContext); resp = Response.ok(state).build(); } @@ -198,8 +200,7 @@ public class AuthenticationResource logger.debug("return state for user {}", user.getName()); } - state = new ScmState(contextProvider, securityContext, - repositoryManger.getConfiguredTypes()); + state = createState(securityContext); response = Response.ok(state).build(); } else @@ -210,8 +211,28 @@ public class AuthenticationResource return response; } + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param securityContext + * + * @return + */ + private ScmState createState(WebSecurityContext securityContext) + { + return new ScmState(contextProvider, securityContext, + repositoryManger.getConfiguredTypes(), + new ScmClientConfig(configuration.getDateFormat())); + } + //~--- fields --------------------------------------------------------------- + /** Field description */ + private ScmConfiguration configuration; + /** Field description */ private SCMContextProvider contextProvider; diff --git a/scm-webapp/src/main/webapp/resources/js/sonia.config.js b/scm-webapp/src/main/webapp/resources/js/sonia.config.js index b44ccf05fc..35d6a368d6 100644 --- a/scm-webapp/src/main/webapp/resources/js/sonia.config.js +++ b/scm-webapp/src/main/webapp/resources/js/sonia.config.js @@ -101,6 +101,8 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ titleText: 'General Settings', servnameText: 'Servername', + // TODO i18n + dateFormatText: 'Date format', enableForwardingText: 'Enable forwarding (mod_proxy)', forwardPortText: 'Forward Port', pluginRepositoryText: 'Plugin repository', @@ -116,6 +118,8 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ // help servernameHelpText: 'The name of this server. This name will be part of the repository url.', + // TODO i18n + dateFormatHelpText: 'JavaScript date format.', pluginRepositoryHelpText: 'The url of the plugin repository.
Explanation of the {placeholders}:\n\
version = SCM-Manager Version
os = Operation System
arch = Architecture', enableForwardingHelpText: 'Enbale mod_proxy port forwarding.', @@ -139,6 +143,12 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ name: 'servername', helpText: this.servernameHelpText, allowBlank: false + },{ + xtype: 'textfield', + fieldLabel: this.dateFormatText, + name: 'dateFormat', + helpText: this.dateFormatHelpText, + allowBlank: false },{ xtype: 'checkbox', fieldLabel: this.enableForwardingText, diff --git a/scm-webapp/src/main/webapp/resources/js/sonia.util.js b/scm-webapp/src/main/webapp/resources/js/sonia.util.js index a43ab29029..7c553cae95 100644 --- a/scm-webapp/src/main/webapp/resources/js/sonia.util.js +++ b/scm-webapp/src/main/webapp/resources/js/sonia.util.js @@ -33,8 +33,12 @@ formatTimestamp: function(value){ var result = ''; - if ( value != null && (value > 0 ||value.length > 0)){ - result = Ext.util.Format.date(new Date(value), "Y-m-d H:i:s"); + if ( value != null && (value > 0 || value.length > 0)){ + var df = state.clientConfig.dateFormat; + if ( df == null || df.length == 0 || ! Ext.isDefined(value) ){ + df = "Y-m-d H:i:s"; + } + result = Ext.util.Format.date(new Date(value), df); } return result; },