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;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsVfsResourceNotFoundException;
033import org.opencms.gwt.shared.CmsValidationResult;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsRuntimeException;
036import org.opencms.main.OpenCms;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.util.CmsUUID;
039
040import java.util.Map;
041
042/**
043 * Validation class which both translates a sitemap URL name and checks whether it already exists in a '|'-separated
044 * list of URL names which is passed as a configuration parameter.<p>
045 *
046 * @since 8.0.0
047 */
048public class CmsUrlNameValidationService implements I_CmsValidationService {
049
050    /**
051     * @see org.opencms.gwt.I_CmsValidationService#validate(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
052     */
053    public CmsValidationResult validate(CmsObject cms, String value, String config) {
054
055        String name = cms.getRequestContext().getFileTranslator().translateResource(value);
056        name = name.replace('/', '_');
057
058        Map<String, String> configMap = CmsStringUtil.splitAsMap(config, "|", ":");
059        String parentPath = configMap.get("parent");
060        String id = configMap.get("id");
061        try {
062            CmsResource res = cms.readResource(CmsStringUtil.joinPaths(parentPath, name));
063            // file already exists
064            if (!CmsUUID.isValidUUID(id) || res.getStructureId().toString().equals(id)) {
065                // no problem, it's the same resource
066                return new CmsValidationResult(null, name);
067            } else {
068                // it's a different resource, so we fail
069                return new CmsValidationResult(
070                    org.opencms.gwt.Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
071                        org.opencms.gwt.Messages.ERR_URL_NAME_ALREADY_EXISTS_1,
072                        name));
073            }
074        } catch (CmsVfsResourceNotFoundException e) {
075            // ok, the resource was not found
076            return new CmsValidationResult(null, name);
077        } catch (CmsException e) {
078            throw new CmsRuntimeException(e.getMessageContainer());
079
080        }
081    }
082}