KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > export > SimpleListBuilder


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * "Felix Gnass [fgnass at neteye dot de]"
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.export;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.riotfamily.components.Component;
33 import org.riotfamily.components.ComponentList;
34 import org.riotfamily.components.ComponentRepository;
35 import org.riotfamily.components.ComponentVersion;
36 import org.riotfamily.components.VersionContainer;
37
38
39 /**
40  * Transforms a ComponentList into a SimpleComponentList.
41  *
42  * @author Felix Gnass [fgnass at neteye dot de]
43  * @since 6.5
44  */

45 public class SimpleListBuilder {
46
47     private ComponentRepository componentRepository;
48     
49     /**
50      * Use this constructor if you don't need to resolve the components' string
51      * properties. Note that invoking {@link SimpleComponent#getProperties()}
52      * will return <code>null</code>,
53      * use {@link SimpleComponent#getStringProperties()} instead.
54      */

55     public SimpleListBuilder() {
56     }
57     
58     /**
59      * Use this constructor if you want the components' properties to be
60      * resolved. In this case {@link SimpleComponent#getProperties()} will
61      * return the map created by {@link Component#buildModel(ComponentVersion)}.
62      */

63     public SimpleListBuilder(ComponentRepository componentRepository) {
64         this.componentRepository = componentRepository;
65     }
66
67     /**
68      * Transforms the given ComponentList into a {@link SimpleComponentList}.
69      * @param list the ComponentList to transform
70      * @param preview whether to use the preview version
71      */

72     public SimpleComponentList buildSimpleList(ComponentList list,
73             boolean preview) {
74         
75         SimpleComponentList simpleList = new SimpleComponentList();
76         simpleList.setLocation(list.getLocation());
77         List JavaDoc containers = preview
78                 ? list.getLiveContainers()
79                 : list.getLiveContainers();
80                 
81         simpleList.setComponents(buildSimpleComponents(containers, preview));
82         return simpleList;
83     }
84     
85     private List JavaDoc buildSimpleComponents(List JavaDoc containers, boolean preview) {
86         ArrayList JavaDoc result = new ArrayList JavaDoc();
87         Iterator JavaDoc it = containers.iterator();
88         while (it.hasNext()) {
89             VersionContainer container = (VersionContainer) it.next();
90             ComponentVersion version = preview
91                     ? container.getLatestVersion()
92                     : container.getLiveVersion();
93                     
94             result.add(buildSimpleComponent(version, preview));
95         }
96         return result;
97     }
98     
99     private SimpleComponent buildSimpleComponent(ComponentVersion version,
100             boolean preview) {
101         
102         SimpleComponent simpleComponent = new SimpleComponent();
103         simpleComponent.setType(version.getType());
104         simpleComponent.setStringProperties(version.getProperties());
105         if (componentRepository != null) {
106             Component component = componentRepository.getComponent(version);
107             simpleComponent.setProperties(component.buildModel(version));
108         }
109         Set JavaDoc childLists = version.getContainer().getChildLists();
110         if (childLists != null && !childLists.isEmpty()) {
111             simpleComponent.setChildLists(buildSimpleLists(childLists, preview));
112         }
113         return simpleComponent;
114     }
115     
116     private List JavaDoc buildSimpleLists(Collection JavaDoc lists, boolean preview) {
117         ArrayList JavaDoc result = new ArrayList JavaDoc();
118         Iterator JavaDoc it = lists.iterator();
119         while (it.hasNext()) {
120             ComponentList list = (ComponentList) it.next();
121             result.add(buildSimpleList(list, preview));
122         }
123         return result;
124     }
125 }
126
Popular Tags