001
014
015 package com.liferay.portlet.dynamicdatamapping.util.test;
016
017 import com.liferay.portal.kernel.util.GetterUtil;
018 import com.liferay.portal.kernel.util.LocaleUtil;
019 import com.liferay.portal.kernel.util.PropsKeys;
020 import com.liferay.portal.kernel.util.PropsUtil;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.kernel.xml.Attribute;
026 import com.liferay.portal.kernel.xml.Document;
027 import com.liferay.portal.kernel.xml.Element;
028 import com.liferay.portal.kernel.xml.Node;
029 import com.liferay.portal.kernel.xml.SAXReaderUtil;
030 import com.liferay.portal.kernel.xml.XPath;
031 import com.liferay.portal.service.ServiceContext;
032 import com.liferay.portal.util.PortalUtil;
033 import com.liferay.portal.util.test.ServiceContextTestUtil;
034 import com.liferay.portal.util.test.TestPropsValues;
035 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
036 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
037 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
038
039 import java.util.HashMap;
040 import java.util.List;
041 import java.util.Locale;
042 import java.util.Map;
043
044
047 public class DDMStructureTestUtil {
048
049 public static DDMStructure addStructure(long groupId, String className)
050 throws Exception {
051
052 return addStructure(
053 groupId, className, 0, getSampleStructureDefinition(),
054 LocaleUtil.getSiteDefault(),
055 ServiceContextTestUtil.getServiceContext());
056 }
057
058 public static DDMStructure addStructure(
059 long groupId, String className, Locale defaultLocale)
060 throws Exception {
061
062 return addStructure(
063 groupId, className, 0, getSampleStructureDefinition(),
064 defaultLocale, ServiceContextTestUtil.getServiceContext());
065 }
066
067 public static DDMStructure addStructure(
068 long groupId, String className, long parentStructureId)
069 throws Exception {
070
071 return addStructure(
072 groupId, className, parentStructureId,
073 getSampleStructureDefinition(), LocaleUtil.getSiteDefault(),
074 ServiceContextTestUtil.getServiceContext());
075 }
076
077 public static DDMStructure addStructure(
078 long groupId, String className, long parentStructureId,
079 String definition, Locale defaultLocale,
080 ServiceContext serviceContext)
081 throws Exception {
082
083 Map<Locale, String> nameMap = new HashMap<Locale, String>();
084
085 nameMap.put(defaultLocale, "Test Structure");
086
087 String ddlStorageType = GetterUtil.getString(
088 PropsUtil.get(PropsKeys.DYNAMIC_DATA_LISTS_STORAGE_TYPE));
089
090 serviceContext.setAddGroupPermissions(true);
091 serviceContext.setAddGuestPermissions(true);
092
093 return DDMStructureLocalServiceUtil.addStructure(
094 TestPropsValues.getUserId(), groupId, parentStructureId,
095 PortalUtil.getClassNameId(className), null, nameMap, null,
096 definition, ddlStorageType, DDMStructureConstants.TYPE_DEFAULT,
097 serviceContext);
098 }
099
100 public static DDMStructure addStructure(
101 long groupId, String className, String definition)
102 throws Exception {
103
104 return addStructure(
105 groupId, className, 0, definition, LocaleUtil.getSiteDefault(),
106 ServiceContextTestUtil.getServiceContext());
107 }
108
109 public static DDMStructure addStructure(
110 long groupId, String className, String definition,
111 Locale defaultLocale)
112 throws Exception {
113
114 return addStructure(
115 groupId, className, 0, definition, defaultLocale,
116 ServiceContextTestUtil.getServiceContext());
117 }
118
119 public static DDMStructure addStructure(String className) throws Exception {
120 return addStructure(
121 TestPropsValues.getGroupId(), className, 0,
122 getSampleStructureDefinition(), LocaleUtil.getSiteDefault(),
123 ServiceContextTestUtil.getServiceContext());
124 }
125
126 public static DDMStructure addStructure(
127 String className, Locale defaultLocale)
128 throws Exception {
129
130 return addStructure(
131 TestPropsValues.getGroupId(), className, 0,
132 getSampleStructureDefinition(
133 "name", new Locale[] {LocaleUtil.US}, defaultLocale),
134 defaultLocale, ServiceContextTestUtil.getServiceContext());
135 }
136
137 public static DDMStructure addStructure(
138 String className, Locale[] availableLocales, Locale defaultLocale)
139 throws Exception {
140
141 return addStructure(
142 TestPropsValues.getGroupId(), className, 0,
143 getSampleStructureDefinition(
144 "name", availableLocales, defaultLocale),
145 defaultLocale, ServiceContextTestUtil.getServiceContext());
146 }
147
148 public static DDMStructure addStructure(String className, String definition)
149 throws Exception {
150
151 return addStructure(
152 TestPropsValues.getGroupId(), className, 0, definition,
153 LocaleUtil.getSiteDefault(),
154 ServiceContextTestUtil.getServiceContext());
155 }
156
157 public static DDMStructure addStructure(
158 String className, String definition, Locale defaultLocale)
159 throws Exception {
160
161 return addStructure(
162 TestPropsValues.getGroupId(), className, 0, definition,
163 defaultLocale, ServiceContextTestUtil.getServiceContext());
164 }
165
166 public static String getSampleStructuredContent() {
167 return getSampleStructuredContent("name", "title");
168 }
169
170 public static String getSampleStructuredContent(
171 Map<Locale, String> contents, String defaultLocale) {
172
173 return getSampleStructuredContent("name", contents, defaultLocale);
174 }
175
176 public static String getSampleStructuredContent(String keywords) {
177 return getSampleStructuredContent("name", keywords);
178 }
179
180 public static String getSampleStructuredContent(
181 String name, Map<Locale, String> contents, String defaultLocale) {
182
183 StringBundler sb = new StringBundler(2 * contents.size());
184
185 for (Map.Entry<Locale, String> content : contents.entrySet()) {
186 Locale locale = content.getKey();
187
188 sb.append(LocaleUtil.toLanguageId(locale));
189 sb.append(StringPool.COMMA);
190 }
191
192 sb.setIndex(sb.index() - 1);
193
194 Document document = createDocumentContent(sb.toString(), defaultLocale);
195
196 Element rootElement = document.getRootElement();
197
198 Element dynamicElementElement = rootElement.addElement(
199 "dynamic-element");
200
201 dynamicElementElement.addAttribute("index-type", "keyword");
202 dynamicElementElement.addAttribute("name", name);
203 dynamicElementElement.addAttribute("type", "text");
204
205 for (Map.Entry<Locale, String> content : contents.entrySet()) {
206 Element dynamicContentElement = dynamicElementElement.addElement(
207 "dynamic-content");
208
209 dynamicContentElement.addAttribute(
210 "language-id", LocaleUtil.toLanguageId(content.getKey()));
211 dynamicContentElement.addCDATA(content.getValue());
212 }
213
214 return document.asXML();
215 }
216
217 public static String getSampleStructuredContent(
218 String name, String keywords) {
219
220 Map<Locale, String> contents = new HashMap<Locale, String>();
221
222 contents.put(Locale.US, keywords);
223
224 return getSampleStructuredContent(name, contents, "en_US");
225 }
226
227 public static String getSampleStructureDefinition() {
228 return getSampleStructureDefinition("name");
229 }
230
231 public static String getSampleStructureDefinition(
232 Locale[] availableLocales, Locale defaultLocale) {
233
234 return getSampleStructureDefinition(
235 "name", availableLocales, defaultLocale);
236 }
237
238 public static String getSampleStructureDefinition(String name) {
239 return getSampleStructureDefinition(
240 name, new Locale[] {LocaleUtil.US}, LocaleUtil.US);
241 }
242
243 public static String getSampleStructureDefinition(
244 String name, Locale[] availableLocales, Locale defaultLocale) {
245
246 Document document = createDocumentStructure(
247 availableLocales, defaultLocale);
248
249 Element rootElement = document.getRootElement();
250
251 Element dynamicElementElement = rootElement.addElement(
252 "dynamic-element");
253
254 dynamicElementElement.addAttribute("dataType", "string");
255 dynamicElementElement.addAttribute("indexType", "text");
256 dynamicElementElement.addAttribute("name", name);
257 dynamicElementElement.addAttribute("repeatable", "true");
258 dynamicElementElement.addAttribute("required", "false");
259 dynamicElementElement.addAttribute("type", "text");
260
261 Element metaDataElement = dynamicElementElement.addElement("meta-data");
262
263 metaDataElement.addAttribute(
264 "locale", LocaleUtil.toLanguageId(defaultLocale));
265
266 Element labelElement = metaDataElement.addElement("entry");
267
268 labelElement.addAttribute("name", "label");
269 labelElement.addCDATA("Field");
270
271 return document.asXML();
272 }
273
274 public static Map<String, Map<String, String>> getXSDMap(String xsd)
275 throws Exception {
276
277 Map<String, Map<String, String>> map =
278 new HashMap<String, Map<String, String>>();
279
280 Document document = SAXReaderUtil.read(xsd);
281
282 XPath xPathSelector = SAXReaderUtil.createXPath("
283
284 List<Node> nodes = xPathSelector.selectNodes(document);
285
286 for (Node node : nodes) {
287 Element dynamicElementElement = (Element)node;
288
289 String elementName = getElementName(dynamicElementElement);
290
291 map.put(elementName, getElementMap(dynamicElementElement));
292 }
293
294 return map;
295 }
296
297 protected static Document createDocumentContent(
298 String availableLocales, String defaultLocale) {
299
300 Document document = SAXReaderUtil.createDocument();
301
302 Element rootElement = document.addElement("root");
303
304 rootElement.addAttribute("available-locales", availableLocales);
305 rootElement.addAttribute("default-locale", defaultLocale);
306 rootElement.addElement("request");
307
308 return document;
309 }
310
311 protected static Document createDocumentStructure(
312 Locale[] availableLocales, Locale defaultLocale) {
313
314 Document document = SAXReaderUtil.createDocument();
315
316 Element rootElement = document.addElement("root");
317
318 rootElement.addAttribute(
319 "available-locales",
320 StringUtil.merge(LocaleUtil.toLanguageIds(availableLocales)));
321 rootElement.addAttribute(
322 "default-locale", LocaleUtil.toLanguageId(defaultLocale));
323
324 return document;
325 }
326
327 protected static Map<String, String> getElementMap(Element element) {
328 Map<String, String> elementMap = new HashMap<String, String>();
329
330
331
332 for (Attribute attribute : element.attributes()) {
333 elementMap.put(attribute.getName(), attribute.getValue());
334 }
335
336
337
338 for (Element metadadataElement : element.elements("meta-data")) {
339 String metadataLanguageId = metadadataElement.attributeValue(
340 "locale");
341
342 for (Element entryElement : metadadataElement.elements("entry")) {
343 String entryName = entryElement.attributeValue("name");
344
345 elementMap.put(
346 entryName.concat(metadataLanguageId),
347 entryElement.getText());
348 }
349 }
350
351 return elementMap;
352 }
353
354 protected static String getElementName(Element element) {
355 StringBuilder sb = new StringBuilder();
356
357 sb.append(element.attributeValue("name"));
358
359 Element parentElement = element.getParent();
360
361 while (true) {
362 if ((parentElement == null) ||
363 parentElement.getName().equals("root")) {
364
365 break;
366 }
367
368 sb.insert(
369 0, parentElement.attributeValue("name") + StringPool.SLASH);
370
371 parentElement = parentElement.getParent();
372 }
373
374 String type = element.attributeValue("type");
375
376 if (Validator.equals(type, "option")) {
377 sb.append(StringPool.SLASH);
378
379 sb.append(element.attributeValue("value"));
380 }
381
382 return sb.toString();
383 }
384
385 }