All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cayenne.configuration.DataChannelDescriptor Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
/*****************************************************************
 *   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.cayenne.configuration;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.cayenne.map.DataMap;
import org.apache.cayenne.resource.Resource;
import org.apache.cayenne.util.XMLEncoder;
import org.apache.cayenne.util.XMLSerializable;

/**
 * A descriptor of a DataChannel normally loaded from XML configuration.
 * 
 * @since 3.1
 */
public class DataChannelDescriptor implements ConfigurationNode, Serializable,
        XMLSerializable {

    protected String name;
    protected Map properties;
    protected Collection dataMaps;
    protected Collection nodeDescriptors;
    protected Resource configurationSource;
    protected String defaultNodeName;

    public DataChannelDescriptor() {
        properties = new HashMap();
        dataMaps = new ArrayList(5);
        nodeDescriptors = new ArrayList(3);
    }

    public void encodeAsXML(XMLEncoder encoder) {

        encoder.print("");

        encoder.indent(1);
        boolean breakNeeded = false;

        if (!properties.isEmpty()) {
            breakNeeded = true;

            List keys = new ArrayList(properties.keySet());
            Collections.sort(keys);

            for (String key : keys) {
                encoder.printProperty(key, properties.get(key));
            }
        }

        if (!dataMaps.isEmpty()) {
            if (breakNeeded) {
                encoder.println();
            }
            else {
                breakNeeded = true;
            }

            List maps = new ArrayList(this.dataMaps);
            Collections.sort(maps);

            for (DataMap dataMap : maps) {

                encoder.print("");
            }
        }

        if (!nodeDescriptors.isEmpty()) {
            if (breakNeeded) {
                encoder.println();
            }
            else {
                breakNeeded = true;
            }

            List nodes = new ArrayList(
                    nodeDescriptors);
            Collections.sort(nodes);
            encoder.print(nodes);
        }

        encoder.indent(-1);
        encoder.println("");
    }

    public  T acceptVisitor(ConfigurationNodeVisitor visitor) {
        return visitor.visitDataChannelDescriptor(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map getProperties() {
        return properties;
    }

    public Collection getDataMaps() {
        return dataMaps;
    }

    public DataMap getDataMap(String name) {
        for (DataMap map : dataMaps) {
            if (name.equals(map.getName())) {
                return map;
            }
        }
        return null;
    }

    public Collection getNodeDescriptors() {
        return nodeDescriptors;
    }

    public DataNodeDescriptor getNodeDescriptor(String name) {
        for (DataNodeDescriptor node : nodeDescriptors) {
            if (name.equals(node.getName())) {
                return node;
            }
        }

        return null;
    }

    public Resource getConfigurationSource() {
        return configurationSource;
    }

    public void setConfigurationSource(Resource configurationSource) {
        this.configurationSource = configurationSource;
    }

    /**
     * Returns the name of the DataNode that should be used as the default if a DataMap is
     * not explicitly linked to a node.
     */
    public String getDefaultNodeName() {
        return defaultNodeName;
    }

    public void setDefaultNodeName(String defaultDataNodeName) {
        this.defaultNodeName = defaultDataNodeName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy