/*
 * 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.
 *
 */

/* @version $Id$ */

package org.apache.lenya.cms.site;

import org.apache.lenya.cms.publication.Document;
import org.apache.lenya.cms.publication.DocumentBuildException;
import org.apache.lenya.cms.publication.DocumentFactory;
import org.apache.lenya.cms.publication.Publication;

/**
 * The AbstractLink class encapsulates a string label and a associated language.
 */
public abstract class AbstractLink implements Link {
    private String label = null;
    private String language = null;

    /**
     * Creates a new AbstractLink object.
     * @param factory The document factory.
     * @param node The site node.
     * @param _label the actual label
     * @param _language the language
     */
    public AbstractLink(DocumentFactory factory, SiteNode node, String _label, String _language) {
        this.label = _label;
        this.language = _language;
        this.factory = factory;
        this.node = node;
    }

    /**
     * Get the actual label of the AbstractLink object
     * 
     * @return the actual label as a String
     */
    public String getLabel() {
        return this.label;
    }

    /**
     * Get the language of this AbstractLink object
     * 
     * @return the language
     */

    public String getLanguage() {
        return this.language;
    }

    /**
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return getLabel() + " " + getLanguage();
    }

    /**
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object obj) {
        boolean equals = false;

        if (getClass().isInstance(obj)) {
            AbstractLink otherLabel = (AbstractLink) obj;
            equals = getLabel().equals(otherLabel.getLabel())
                    && getLanguage().equals(otherLabel.getLanguage());
        }

        return equals;
    }

    /**
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        return getLabel().hashCode() + getLanguage().hashCode();
    }

    private SiteNode node;
    private DocumentFactory factory;

    public Document getDocument() {
        Publication pub = getNode().getStructure().getPublication();
        String area = getNode().getStructure().getArea();
        try {
            return this.factory.get(pub, area, getNode().getUuid(), getLanguage());
        } catch (DocumentBuildException e) {
            throw new RuntimeException(e);
        }
    }

    public SiteNode getNode() {
        return this.node;
    }
    
    public void setLabel(String label) {
        this.label = label;
        save();
    }

    protected void save() {}
    
}

@@ -2983,24 +2983,228 @@
 ocument() %7B%0A
+        SiteNode node = getNode();%0A        String uuid = node.getUuid();%0A        if (uuid == null) %7B%0A            throw new UnsupportedOperationException(%22The node %5B%22 + node + %22%5D has no UUID.%22);%0A        %7D%0A
         Publ
@@ -3217,25 +3217,20 @@
 n pub = 
-getN
+n
 ode
-()
 .getStru
@@ -3277,25 +3277,20 @@
  area = 
-getN
+n
 ode
-()
 .getStru
@@ -3373,27 +3373,12 @@
 ea, 
-getNode().getU
+u
 uid
-()
 , ge
@@ -3692,9 +3692,8 @@
 %7D%0A    %0A%7D
-%0A

throw UnsupportedOperationException in getDocument() if node has no UUID.