Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
GTNPORTAL-2876: Add validation to redirect name so it won't allow ove…
Browse files Browse the repository at this point in the history
…rriding existing one.

Also, added a default name to a new redirect (hex string for currentTimeMillis).
  • Loading branch information
ammendonca authored and bdaw committed May 15, 2013
1 parent 0cf098e commit 89a0d27
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public void configRedirect() {
public void addRedirect(String site) {
this.siteName = site;
this.pr = new PortalRedirect();
this.pr.setName(site + "_" + Long.toHexString(System.currentTimeMillis()));
this.originalName = pr.getName();
this.pr.setConditions(new ArrayList<RedirectCondition>());
RedirectMappings rm = new RedirectMappings();
rm.setMappings(new ArrayList<NodeMap>());
Expand All @@ -114,6 +116,15 @@ public String getName() {
return this.pr != null ? this.pr.getName() : redirectName;
}

/**
* Returns the original name of the redirect being edited.
*
* @return
*/
public String getOriginalName() {
return originalName;
}

/**
* Sets the Redirect Name.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.gatein.ui.admin.redirect.beans.util;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.portal.config.DataStorage;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.config.model.PortalRedirect;

@FacesValidator("rdrNameValidator")
public class RedirectNameValidator implements Validator {

private DataStorage ds;

public RedirectNameValidator(){
ds = (DataStorage) ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(DataStorage.class);
}

@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String newName = (String) value;
// check for empty redirect name
if (newName.equals("")) {
((UIInput) component).setValid(false);
((UIInput) component).setValidatorMessage("The redirect name cannot be empty.");
return;
}

String oldName = (String) component.getAttributes().get("vOriginalRedirectName");
// if the name hasn't changed, just return
if(oldName != null && oldName.equals(newName)) {
((UIInput) component).setValid(true);
((UIInput) component).setValidatorMessage(null);
return;
}
// get site name and compare the new name with existing ones
String siteName = (String) component.getAttributes().get("vSiteName");

ds = (DataStorage) ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(DataStorage.class);

try {
PortalConfig cfg = ds.getPortalConfig(siteName);
List<PortalRedirect> prs = cfg.getPortalRedirects();
for (PortalRedirect pr : prs) {
if (pr.getName().equals(newName)) {
((UIInput) component).setValid(false);
((UIInput) component).setValidatorMessage("The redirect name '" + newName + "' already exists.");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
15 changes: 10 additions & 5 deletions portlet/admin/redirect/src/main/webapp/admin/redirects/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<strong id="main-alert-title"></strong> <span id="main-alert-message"></span>
</div><!-- End .alert -->

<a4j:commandLink action="#{rdrEdit.addRedirect(rdrs.siteName)}" styleClass="btn btn-small pull-right add-redirect" render=":edit_form" rendered="#{not empty rdrs.siteName}">Add Redirect</a4j:commandLink>
<a4j:commandLink action="#{rdrEdit.addRedirect(rdrs.siteName)}" styleClass="btn btn-small pull-right add-redirect" render=":edit_form" rendered="#{not empty rdrs.siteName}" oncomplete="configureRedirect();">Add Redirect</a4j:commandLink>
</h:form>
<h:form id="sum_form" styleClass="form-horizontal">
<!-- No site selected -->
Expand Down Expand Up @@ -106,10 +106,15 @@
<span class="onoffswitch-switch"></span>
</h:outputLabel>
</div>
<div class="control-group">
<h:panelGroup id="rdr-name-group" layout="block" styleClass="control-group rdr-name-group #{name.valid ? '' : 'error'}">
<h:outputLabel for="name" styleClass="control-label">Redirect Name</h:outputLabel>
<b:input scale="medium" id="name" placeholder="Redirect Name" value="#{rdrEdit.name}" />
</div>
<h:inputText binding="#{name}" id="name" value="#{rdrEdit.name}" validator="rdrNameValidator" styleClass="rdr-edit-name">
<f:ajax event="blur" render="name-error edit_save rdr-name-group" />
<f:attribute name="vOriginalRedirectName" value="#{rdrEdit.getOriginalName()}" />
<f:attribute name="vSiteName" value="#{rdrs.siteName}" />
</h:inputText>
<h:outputText id="name-error" styleClass="text-error rdr-name-error" value="#{name.valid ? '' : name.validatorMessage}" rendered="#{not name.valid}" />
</h:panelGroup>
<div class="control-group">
<h:outputLabel for="redirect" styleClass="control-label">Redirect to</h:outputLabel>
<h:selectOneMenu id="redirect" value="#{rdrEdit.redirectSite}" style="height: 30px; line-height: 30px;">
Expand Down Expand Up @@ -251,7 +256,7 @@
</fieldset>
<div class="form-actions">
<button id="edit_cancel" class="btn" onclick="closeRedirectEdit(false); return false;" >Cancel</button>
<b:commandButton value="Save changes" styleClass="btn btn-primary" action="#{rdrEdit.saveRedirect}" render="pgRedirectSummary" oncomplete="closeRedirectEdit(true);"/>
<b:commandButton id="edit_save" value="Save changes" styleClass="btn btn-primary" action="#{rdrEdit.saveRedirect}" render="pgRedirectSummary :edit_form" oncomplete="closeRedirectEdit(true)" disabled="#{not name.valid}" />
</div><!-- End .form-actions -->
</td>
</tr>
Expand Down
45 changes: 5 additions & 40 deletions portlet/admin/redirect/src/main/webapp/resources/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ function hideAlert(aclass) {
}

function configureRedirect() {
// FIXME: this is because the validation is not performed and last message still shows, we remove it manually
$('.rdr-name-error').hide();
$('.rdr-name-group').removeClass('error');
$('.rdr-edit-name').blur();

$('.add-redirect').css("visibility", "hidden");

// fade summary out...
Expand Down Expand Up @@ -429,51 +434,11 @@ feedback = function() {

// Redirect
editRedirect = function() {

// Fade out summary or initial and fade in edit on "Add Redirect" button click
$('.add-redirect').live('click', function(){
$('.add-redirect').css("visibility", "hidden");

// fade summary out (if present)...
$('#redirectSummaryWrapper').fadeOut(300, function() {
// .. and when done, fade config in
$('.edit-group').fadeIn(300);
});
// fade initial out (if present)...
$('.initial').fadeOut(300, function() {
// .. and when done, fade config in
$('.edit-group').fadeIn(300);
});

// clear the form
});

// Fade out summary and fade in edit on "Configure" link click
// $('.configure-redirect').live('click', function(){
// $('.add-redirect').css("visibility", "hidden");

// // fade summary out...
// $('#redirectSummaryWrapper').fadeOut(300, function() {
// // .. and when done, fade config in
// $('.edit-group').fadeIn(300, function() {
// sortable();
// });
// });
// });

// Avoid showing summary and edit when edit is loaded. maybe show modal to confirm if there are changes made ?
$('.site-link').live('click', function(){
$(".edit-group").hide();
});

// On "Cancel" hide the edit form and show the summary
$('#edit_cancel').click(function(){
$('.edit-group').fadeOut(300, function() {
$('#redirectSummaryWrapper').fadeIn(300);
});
$('.add-redirect').css("visibility", "visible");
});

$('#modal-delete-redirect .btn-primary').click(function(){
$(this).parent().parent().modal('hide');
$('.alert-container').removeClass('hidden-element');
Expand Down

0 comments on commit 89a0d27

Please sign in to comment.