001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.property;
029
030import org.opencms.gwt.client.ui.input.I_CmsFormField;
031import org.opencms.gwt.client.validation.I_CmsValidationController;
032import org.opencms.gwt.client.validation.I_CmsValidator;
033import org.opencms.gwt.shared.CmsValidationResult;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.util.CmsUUID;
036
037/**
038 * Validator class for the URL name field in the property editor.<p>
039 *
040 * @since 8.0.0
041 */
042public class CmsUrlNameValidator implements I_CmsValidator {
043
044    /** The server-side validator class. */
045    private static final String SERVER_VALIDATOR = "org.opencms.gwt.CmsUrlNameValidationService";
046
047    /** The path of the parent folder of the edited resource. */
048    private String m_parentPath;
049
050    /** The structure id of the edited resource. */
051    private CmsUUID m_structureId;
052
053    /**
054     * Creates a new URL name validator.<p>
055     *
056     * @param parentPath the parent path of the resource for which the URL name is being validated
057     * @param id the id of the resource whose URL name is being validated
058     */
059    public CmsUrlNameValidator(String parentPath, CmsUUID id) {
060
061        m_parentPath = parentPath;
062        m_structureId = id;
063    }
064
065    /**
066     * @see org.opencms.gwt.client.validation.I_CmsValidator#validate(org.opencms.gwt.client.ui.input.I_CmsFormField, org.opencms.gwt.client.validation.I_CmsValidationController)
067     */
068    public void validate(I_CmsFormField field, I_CmsValidationController controller) {
069
070        String value = field.getWidget().getFormValueAsString();
071        if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
072            String message = org.opencms.gwt.client.Messages.get().key(
073                org.opencms.gwt.client.Messages.GUI_URLNAME_CANT_BE_EMPTY_0);
074            controller.provideValidationResult(field.getId(), new CmsValidationResult(message));
075            return;
076        }
077        controller.validateAsync(
078            field.getId(),
079            value,
080            SERVER_VALIDATOR,
081            "parent:" + m_parentPath + "|id:" + m_structureId);
082    }
083
084}