/*
 * 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.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 node The site node.
     * @param _label the actual label
     * @param _language the language
     */
    public AbstractLink(SiteNode node, String _label, String _language) {
        this.label = _label;
        this.language = _language;
        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;

    public Document getDocument() {
        SiteNode node = getNode();
        String uuid = node.getUuid();
        if (uuid == null) {
            throw new UnsupportedOperationException("The node [" + node + "] has no UUID.");
        }
        Publication pub = node.getStructure().getPublication();
        String area = node.getStructure().getArea();
        return pub.getArea(area).getDocument(uuid, getLanguage());
    }

    public SiteNode getNode() {
        return this.node;
    }

    public void setLabel(String label) {
        this.label = label;
        save();
    }

    protected void save() {
    }

}