001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2007
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * "Felix Gnass [fgnass at neteye dot de]"
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.components.export;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Set;
031:
032: import org.riotfamily.components.config.ComponentRepository;
033: import org.riotfamily.components.config.component.Component;
034: import org.riotfamily.components.model.ComponentList;
035: import org.riotfamily.components.model.ComponentVersion;
036: import org.riotfamily.components.model.VersionContainer;
037:
038: /**
039: * Transforms a ComponentList into a SimpleComponentList.
040: *
041: * @author Felix Gnass [fgnass at neteye dot de]
042: * @since 6.5
043: */
044: public class SimpleListBuilder {
045:
046: private ComponentRepository componentRepository;
047:
048: /**
049: * Use this constructor if you don't need to resolve the components' string
050: * properties. Note that invoking {@link SimpleComponent#getProperties()}
051: * will return <code>null</code>,
052: * use {@link SimpleComponent#getStringProperties()} instead.
053: */
054: public SimpleListBuilder() {
055: }
056:
057: /**
058: * Use this constructor if you want the components' properties to be
059: * resolved. In this case {@link SimpleComponent#getProperties()} will
060: * return the map created by {@link Component#buildModel(ComponentVersion)}.
061: */
062: public SimpleListBuilder(ComponentRepository componentRepository) {
063: this .componentRepository = componentRepository;
064: }
065:
066: /**
067: * Transforms the given ComponentList into a {@link SimpleComponentList}.
068: * @param list the ComponentList to transform
069: * @param preview whether to use the preview version
070: */
071: public SimpleComponentList buildSimpleList(ComponentList list,
072: boolean preview) {
073:
074: SimpleComponentList simpleList = new SimpleComponentList();
075: simpleList.setLocation(list.getLocation());
076: List containers = preview ? list.getLiveContainers() : list
077: .getLiveContainers();
078:
079: simpleList.setComponents(buildSimpleComponents(containers,
080: preview));
081: return simpleList;
082: }
083:
084: private List buildSimpleComponents(List containers, boolean preview) {
085: ArrayList result = new ArrayList();
086: Iterator it = containers.iterator();
087: while (it.hasNext()) {
088: VersionContainer container = (VersionContainer) it.next();
089: ComponentVersion version = preview ? container
090: .getLatestVersion() : container.getLiveVersion();
091:
092: result.add(buildSimpleComponent(version, preview));
093: }
094: return result;
095: }
096:
097: private SimpleComponent buildSimpleComponent(
098: ComponentVersion version, boolean preview) {
099:
100: SimpleComponent simpleComponent = new SimpleComponent();
101: simpleComponent.setType(version.getType());
102: simpleComponent.setStringProperties(version.getProperties());
103: if (componentRepository != null) {
104: Component component = componentRepository
105: .getComponent(version);
106: simpleComponent
107: .setProperties(component.buildModel(version));
108: }
109: Set childLists = version.getContainer().getChildLists();
110: if (childLists != null && !childLists.isEmpty()) {
111: simpleComponent.setChildLists(buildSimpleLists(childLists,
112: preview));
113: }
114: return simpleComponent;
115: }
116:
117: private List buildSimpleLists(Collection lists, boolean preview) {
118: ArrayList result = new ArrayList();
119: Iterator it = lists.iterator();
120: while (it.hasNext()) {
121: ComponentList list = (ComponentList) it.next();
122: result.add(buildSimpleList(list, preview));
123: }
124: return result;
125: }
126: }
|