/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.lenya.cms.repository;

import java.util.Arrays;

import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.lenya.cms.metadata.Element;
import org.apache.lenya.cms.metadata.ElementSet;
import org.apache.lenya.cms.metadata.MetaData;
import org.apache.lenya.cms.metadata.MetaDataException;
import org.apache.lenya.cms.metadata.MetaDataRegistry;

/**
 * Source-node-based meta data.
 */
public class SourceNodeMetaData extends AbstractLogEnabled implements MetaData {

    private String namespaceUri;
    private ServiceManager manager;
    private SourceNodeMetaDataHandler handler;

    /**
     * Ctor.
     * @param namespaceUri The namespace URI.
     * @param handler The meta data handler.
     * @param manager The service manager.
     */
    public SourceNodeMetaData(String namespaceUri, SourceNodeMetaDataHandler handler,
            ServiceManager manager) {
        this.namespaceUri = namespaceUri;
        this.handler = handler;
        this.manager = manager;
    }

    protected String getNamespaceUri() {
        return this.namespaceUri;
    }

    protected SourceNodeMetaDataHandler getHandler() {
        return this.handler;
    }

    private ElementSet elementSet;

    public ElementSet getElementSet() {
        if (this.elementSet == null) {
            try {
                MetaDataRegistry registry = (MetaDataRegistry) this.manager
                        .lookup(MetaDataRegistry.ROLE);
                this.elementSet = registry.getElementSet(this.namespaceUri);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
        return this.elementSet;
    }

    public String[] getValues(String key) throws MetaDataException {
        checkKey(key);
        return getHandler().getValues(this.namespaceUri, key);
    }

    public String getFirstValue(String key) throws MetaDataException {
        checkKey(key);
        String[] values = getValues(key);
        if (values.length == 0) {
            return null;
        } else {
            return values[0];
        }
    }

    public String[] getAvailableKeys() {
        Element[] elements;
        elements = getElementSet().getElements();
        String[] keys = new String[elements.length];
        for (int i = 0; i < elements.length; i++) {
            keys[i] = elements[i].getName();
        }
        return keys;
    }

    protected void checkKey(String key) throws MetaDataException {
        if (!isValidAttribute(key)) {
            throw new MetaDataException("The meta data element set ["
                    + getElementSet().getNamespaceUri() + "] does not support the key [" + key
                    + "]!");
        }
    }

    public void setValue(String key, String value) throws MetaDataException {
        checkKey(key);
        if (value == null) {
            throw new MetaDataException("The value for key [" + key + "] must not be null.");
        }
        getHandler().setValue(this.namespaceUri, key, value);
    }

    public void addValue(String key, String value) throws MetaDataException {
        checkKey(key);
        if (!getElementSet().getElement(key).isMultiple() && getValues(key).length > 0) {
            throw new MetaDataException("The element [" + key
                    + "] doesn't support multiple values!");
        }
        getHandler().addValue(this.namespaceUri, key, value);
    }

    public void replaceBy(MetaData other) throws MetaDataException {
        Element[] elements = getElementSet().getElements();
        for (int i = 0; i < elements.length; i++) {
            if (elements[i].getActionOnCopy() == Element.ONCOPY_COPY) {
                replaceBy(other, elements[i]);
            } else if (elements[i].getActionOnCopy() == Element.ONCOPY_DELETE) {
                String key = elements[i].getName();
                removeAllValues(key);
            }
        }
    }

    protected void replaceBy(MetaData other, Element element) throws MetaDataException {
        String key = element.getName();
        removeAllValues(key);
        String[] values = other.getValues(key);
        for (int j = 0; j < values.length; j++) {
            addValue(key, values[j]);
        }
    }

    public void forcedReplaceBy(MetaData other) throws MetaDataException {
        Element[] elements = getElementSet().getElements();
        for (int i = 0; i < elements.length; i++) {
            replaceBy(other, elements[i]);
        }
    }

    public String[] getPossibleKeys() {
        return getAvailableKeys();
    }

    public boolean isValidAttribute(String key) {
        return Arrays.asList(getAvailableKeys()).contains(key);
    }

    public long getLastModified() throws MetaDataException {
        try {
            return getHandler().getLastModified();
        } catch (RepositoryException e) {
            throw new MetaDataException(e);
        }
    }

    public void removeAllValues(String key) throws MetaDataException {
        checkKey(key);
        getHandler().removeAllValues(this.namespaceUri, key);
    }

}

@@ -875,16 +875,64 @@
 .Arrays;
+%0Aimport java.util.HashSet;%0Aimport java.util.Set;
 %0A%0Aimport
@@ -2682,37 +2682,25 @@
     
-checkKey(key);%0A        return
+String%5B%5D values =
  get
@@ -2740,24 +2740,118 @@
 eUri, key);%0A
+        if (values.length == 0) %7B%0A            checkKey(key);%0A        %7D%0A        return values;%0A
     %7D%0A%0A    p
@@ -2916,39 +2916,16 @@
 ption %7B%0A
-        checkKey(key);%0A
         
@@ -2984,32 +2984,59 @@
 .length == 0) %7B%0A
+            checkKey(key);%0A
             retu
@@ -3098,32 +3098,336 @@
 %0A        %7D%0A    %7D
+%0A    %0A    /**%0A     * Cache for better performance.%0A     */%0A    private Set availableKeys;%0A    %0A    protected Set availableKeys() %7B%0A        if (this.availableKeys == null) %7B%0A            this.availableKeys = new HashSet(Arrays.asList(getAvailableKeys()));%0A        %7D%0A        return this.availableKeys;%0A    %7D
 %0A%0A    public Str
@@ -5939,34 +5939,17 @@
  return 
-Arrays.asList(getA
+a
 vailable
@@ -5954,17 +5954,16 @@
 leKeys()
-)
 .contain

Check meta data key only if no values exist (performance)