This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
JSONContainerAdapter.java
222 lines (203 loc) · 8.57 KB
/
JSONContainerAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.gatein.portal.web.page;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.UUID;
import juzu.impl.common.JSON;
import org.exoplatform.portal.config.model.ApplicationState;
import org.exoplatform.portal.config.model.ApplicationType;
import org.exoplatform.portal.config.model.TransientApplicationState;
import org.exoplatform.portal.pom.data.ComponentData;
import org.exoplatform.portal.pom.data.ContainerData;
import org.gatein.portal.mop.hierarchy.ModelAdapter;
import org.gatein.portal.mop.hierarchy.NodeContext;
import org.gatein.portal.mop.layout.ElementState;
/**
* @author <a href="mailto:haithanh0809@gmail.com">Nguyen Thanh Hai</a>
* @version $Id$
*
*/
public class JSONContainerAdapter implements ModelAdapter<JSON, ElementState>{
final JSON root;
final NodeContext<JSON, ElementState> pageStructure;
public JSONContainerAdapter(JSON root, NodeContext<JSON, ElementState> pageStructure) {
this.root = root;
this.pageStructure = pageStructure;
}
@Override
public String getId(JSON node) {
String type = node.getString("type");
if ("container".equals(type)) {
return node.getJSON("container").getString("storageId");
} else if ("application".equals(type)) {
return node.getJSON("application").getString("storageId");
}
return null;
}
@Override
public String getName(JSON node) {
String type = node.getString("type");
JSON data = "container".equalsIgnoreCase(type) ? node.getJSON("container") : node.getJSON("application");
String name = data.getString("storageName");
if (name == null) {
// For now we generate a name
// however the name should be fully provided by the node (possibly randomly generated)
name = UUID.randomUUID().toString();
}
return name;
}
@Override
public ElementState getState(JSON node) {
return create(node);
}
@Override
public JSON getPrevious(JSON parent, JSON node) {
List<JSON> children = (List<JSON>)parent.getJSON("container").getList("children");
int index = children.indexOf(node);
return index > 0 ? children.get(index - 1) : null;
}
@Override
public Iterator<JSON> getChildren(JSON node, boolean reverse) {
if ("container".equals(node.getString("type"))) {
List<JSON> list = (List<JSON>) node.getJSON("container").getList("children");
if (reverse) {
final ListIterator<JSON> iterator = list.listIterator(list.size());
return new Iterator<JSON>() {
@Override
public boolean hasNext() {
return iterator.hasPrevious();
}
@Override
public JSON next() {
return iterator.previous();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
} else {
return list.iterator();
}
} else {
return Collections.<JSON>emptyList().iterator();
}
}
@Override
public int size(JSON node) {
if ("container".equals(node.getString("type"))) {
return node.getJSON("container").getList("children").size();
} else {
return 0;
}
}
@Override
public JSON getParent(JSON node) {
return getParent(root, node);
}
private JSON getParent(JSON container, JSON node) {
for (JSON child : (List<JSON>)container.getJSON("container").getList("children")) {
if (child.equals(node)) {
return container;
} else if ("container".equals(child.getString("type"))) {
JSON parent = getParent(child, node);
if (parent != null) {
return parent;
}
}
}
return null;
}
private Map<String, String> createMap(JSON data) {
if (data == null) {
return new HashMap<String, String>();
}
Map<String, String> map = new HashMap<String, String>();
for (String name : data.names()) {
map.put(name, data.getString(name));
}
return map;
}
private NodeContext<JSON, ElementState> find(String id, NodeContext<JSON, ElementState> target) {
if (target.getName().equals(id)) {
return target;
} else {
for (NodeContext<JSON, ElementState> child : target) {
NodeContext<JSON, ElementState> tmp = this.find(id, child);
if (tmp != null) {
return tmp;
}
}
}
return null;
}
private ElementState create(JSON data) {
String id = data.getString("id");
String type = data.getString("type");
if ("application".equalsIgnoreCase(type)) {
JSON application = data.getJSON("application");
NodeContext<JSON, ElementState> appCtx = find(id, pageStructure);
ApplicationState state = null;
if (appCtx == null) {
String contentId = data.getString("contentId");
state = new TransientApplicationState(contentId);
} else {
ElementState.Window windowState = (ElementState.Window) appCtx.getState();
state = windowState.state;
}
return new ElementState.Window(
ApplicationType.getType(application.getString("type")),
state,
application.getString(ElementState.Window.TITLE.getName()),
application.getString(ElementState.Window.ICON.getName()),
application.getString(ElementState.Window.DESCRIPTION.getName()),
application.getBoolean(ElementState.Window.SHOW_INFO_BAR.getName()),
application.getBoolean(ElementState.Window.SHOW_APPLICATION_STATE.getName()),
application.getBoolean(ElementState.Window.SHOW_APPLICATION_MODE.getName()),
application.getString(ElementState.Window.THEME.getName()),
application.getString(ElementState.Window.WIDTH.getName()),
application.getString(ElementState.Window.HEIGHT.getName()),
createMap(data.getJSON("properties")),
application.getList("accessPermissions")
);
} else if ("body".equalsIgnoreCase(type)) {
return new ElementState.Body();
} else if ("container".equalsIgnoreCase(type)) {
JSON container = data.getJSON("container");
return new ElementState.Container(
container.getString("id"),
container.getString(ElementState.Container.NAME.getName()),
container.getString(ElementState.Container.ICON.getName()),
container.getString(ElementState.Container.TEMPLATE.getName()),
container.getString(ElementState.Container.FACTORY_ID.getName()),
container.getString(ElementState.Container.TITLE.getName()),
container.getString(ElementState.Container.DESCRIPTION.getName()),
container.getString(ElementState.Container.WIDTH.getName()),
container.getString(ElementState.Container.HEIGHT.getName()),
(List<String>)container.getList("accessPermissions"),
false);
} else {
throw new UnsupportedOperationException();
}
}
}