/*
 * $Header: /home/cvspublic/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/properties/AutoVersionProperty.java,v 1.1 2005/01/03 15:04:44 luetzkendorf Exp $
 * $Revision: 1.1 $
 * $Date: 2005/01/03 15:04:44 $
 *
 * ====================================================================
 *
 * Copyright 1999-2002 The Apache Software Foundation 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.apache.slide.webdav.util.properties;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.slide.common.Domain;
import org.apache.slide.common.NamespaceAccessToken;
import org.apache.slide.common.PropertyName;
import org.apache.slide.common.SlideToken;
import org.apache.slide.util.XMLValue;
import org.apache.slide.webdav.util.DeltavConstants;
import org.apache.slide.webdav.util.resourcekind.ResourceKind;
import org.jdom.Element;

/**
 * Validator for DeltaV <code>auto-version</code> property.
 */
public class AutoVersionProperty extends AbstractOptionValidator implements
        PropertyDefaultProvider
{

    private static Set allowedValues = new HashSet();
    static {
        allowedValues.add(DeltavConstants.E_CHECKOUT);
        allowedValues.add(DeltavConstants.E_CHECKOUT_IGNORE_UNLOCK);
        allowedValues.add(DeltavConstants.E_CHECKOUT_CHECKIN);
        allowedValues.add(DeltavConstants.E_CHECKOUT_UNLOCKED_CHECKIN);
        allowedValues.add(DeltavConstants.E_LOCKED_CHECKOUT);

        allowedValues = Collections.unmodifiableSet(allowedValues);
    }

    public PropertyName getPropertyName()
    {
        return DeltavConstants.PN_AUTO_VERSION;
    }

    protected Set getAllowedValues()
    {
        return allowedValues;
    }

    protected boolean allowsEmptyValue()
    {
        return true;
    }

    public Object createDefaultValue(NamespaceAccessToken nsaToken,
            SlideToken slideToken, ResourceKind resourceKind,
            String resourcePath)
    {
        XMLValue xmlValue = new XMLValue();

        String autoVersion = Domain.getParameter(
                DeltavConstants.I_AUTO_VERSION,
                DeltavConstants.I_AUTO_VERSION_DEFAULT, nsaToken.getUri(
                        slideToken, resourcePath).getStore());

        if (autoVersion.length() > 0) {
            if (!allowedValues.contains(autoVersion)) {
                Domain.warn("auto-version not configured properly; using "
                        + DeltavConstants.I_AUTO_VERSION_DEFAULT);
                autoVersion = DeltavConstants.I_AUTO_VERSION_DEFAULT;
            }

            xmlValue.add(new Element(autoVersion, "DAV:"));
        }

        return xmlValue;
    }
}