001
014
015 package com.liferay.portal.util.test;
016
017 import com.liferay.portal.NoSuchLayoutException;
018 import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.kernel.util.UnicodeProperties;
022 import com.liferay.portal.model.CustomizedPages;
023 import com.liferay.portal.model.Group;
024 import com.liferay.portal.model.Layout;
025 import com.liferay.portal.model.LayoutConstants;
026 import com.liferay.portal.model.LayoutPrototype;
027 import com.liferay.portal.model.LayoutSetPrototype;
028 import com.liferay.portal.model.LayoutTemplate;
029 import com.liferay.portal.model.LayoutTypePortlet;
030 import com.liferay.portal.model.Portlet;
031 import com.liferay.portal.service.LayoutLocalServiceUtil;
032 import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
033 import com.liferay.portal.service.LayoutServiceUtil;
034 import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
035 import com.liferay.portal.service.ServiceContext;
036 import com.liferay.portlet.PortletPreferencesFactoryUtil;
037
038 import java.util.HashMap;
039 import java.util.List;
040 import java.util.Locale;
041 import java.util.Map;
042
043 import javax.portlet.PortletPreferences;
044
045
049 public class LayoutTestUtil {
050
051 public static Layout addLayout(Group group) throws Exception {
052 return LayoutTestUtil.addLayout(
053 group.getGroupId(), RandomTestUtil.randomString(), false);
054 }
055
056 public static Layout addLayout(
057 long groupId, boolean privateLayout, Map<Locale, String> nameMap,
058 Map<Locale, String> friendlyURLMap)
059 throws Exception {
060
061 ServiceContext serviceContext =
062 ServiceContextTestUtil.getServiceContext(groupId);
063
064 return LayoutLocalServiceUtil.addLayout(
065 serviceContext.getUserId(), groupId, privateLayout,
066 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, nameMap, nameMap,
067 new HashMap<Locale, String>(), new HashMap<Locale, String>(),
068 new HashMap<Locale, String>(), LayoutConstants.TYPE_PORTLET,
069 StringPool.BLANK, false, friendlyURLMap, serviceContext);
070 }
071
072 public static Layout addLayout(long groupId, String name) throws Exception {
073 return addLayout(groupId, name, false);
074 }
075
076 public static Layout addLayout(
077 long groupId, String name, boolean privateLayout)
078 throws Exception {
079
080 return addLayout(groupId, name, privateLayout, null, false);
081 }
082
083 public static Layout addLayout(
084 long groupId, String name, boolean privateLayout,
085 LayoutPrototype layoutPrototype, boolean linkEnabled)
086 throws Exception {
087
088 String friendlyURL =
089 StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
090
091 Layout layout = null;
092
093 try {
094 layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
095 groupId, false, friendlyURL);
096
097 return layout;
098 }
099 catch (NoSuchLayoutException nsle) {
100 }
101
102 String description = "This is a test page.";
103
104 ServiceContext serviceContext =
105 ServiceContextTestUtil.getServiceContext();
106
107 if (layoutPrototype != null) {
108 serviceContext.setAttribute(
109 "layoutPrototypeLinkEnabled", linkEnabled);
110 serviceContext.setAttribute(
111 "layoutPrototypeUuid", layoutPrototype.getUuid());
112 }
113
114 return LayoutLocalServiceUtil.addLayout(
115 TestPropsValues.getUserId(), groupId, privateLayout,
116 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, name, null, description,
117 LayoutConstants.TYPE_PORTLET, false, friendlyURL, serviceContext);
118 }
119
120 public static Layout addLayout(
121 long groupId, String name, long parentLayoutPlid)
122 throws Exception {
123
124 Layout layout = addLayout(groupId, name, false);
125
126 LayoutLocalServiceUtil.updateParentLayoutId(
127 layout.getPlid(), parentLayoutPlid);
128
129 return LayoutLocalServiceUtil.fetchLayout(layout.getPlid());
130 }
131
132 public static LayoutPrototype addLayoutPrototype(String name)
133 throws Exception {
134
135 HashMap<Locale, String> nameMap = new HashMap<Locale, String>();
136
137 nameMap.put(LocaleUtil.getDefault(), name);
138
139 return LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
140 TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
141 nameMap, (Map<Locale, String>)null, true,
142 ServiceContextTestUtil.getServiceContext());
143 }
144
145 public static LayoutSetPrototype addLayoutSetPrototype(String name)
146 throws Exception {
147
148 HashMap<Locale, String> nameMap = new HashMap<Locale, String>();
149
150 nameMap.put(LocaleUtil.getDefault(), name);
151
152 return LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
153 TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
154 nameMap, (Map<Locale, String>)null, true, true,
155 ServiceContextTestUtil.getServiceContext());
156 }
157
158 public static String addPortletToLayout(Layout layout, String portletId)
159 throws Exception {
160
161 Map<String, String[]> preferenceMap = null;
162
163 return addPortletToLayout(layout, portletId, preferenceMap);
164 }
165
166 public static String addPortletToLayout(
167 Layout layout, String portletId,
168 Map<String, String[]> preferenceMap)
169 throws Exception {
170
171 long userId = TestPropsValues.getUserId();
172
173 LayoutTypePortlet layoutTypePortlet =
174 (LayoutTypePortlet)layout.getLayoutType();
175
176 LayoutTemplate layoutTemplate = layoutTypePortlet.getLayoutTemplate();
177
178 List<String> columns = layoutTemplate.getColumns();
179
180 String columnId = columns.get(0);
181
182 return addPortletToLayout(
183 userId, layout, portletId, columnId, preferenceMap);
184 }
185
186 public static String addPortletToLayout(
187 long userId, Layout layout, String portletId, String columnId,
188 Map<String, String[]> preferenceMap)
189 throws Exception {
190
191 LayoutTypePortlet layoutTypePortlet =
192 (LayoutTypePortlet)layout.getLayoutType();
193
194 String newPortletId = layoutTypePortlet.addPortletId(
195 userId, portletId, columnId, -1);
196
197 LayoutLocalServiceUtil.updateLayout(
198 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
199 layout.getTypeSettings());
200
201 if (preferenceMap == null) {
202 return newPortletId;
203 }
204
205 PortletPreferences portletPreferences = getPortletPreferences(
206 layout, newPortletId);
207
208 for (String key : preferenceMap.keySet()) {
209 portletPreferences.setValues(key, preferenceMap.get(key));
210 }
211
212 portletPreferences.store();
213
214 return newPortletId;
215 }
216
217 public static Layout addTypeArticleLayout(
218 long groupId, String name, String articleId)
219 throws Exception {
220
221 Layout layout = addLayout(groupId, name);
222
223 UnicodeProperties typeSettingsProperties =
224 layout.getTypeSettingsProperties();
225
226 typeSettingsProperties.setProperty("article-id", articleId);
227
228 layout.setType(LayoutConstants.TYPE_ARTICLE);
229
230 LayoutLocalServiceUtil.updateLayout(layout);
231
232 return layout;
233 }
234
235 public static Layout addTypeLinkToLayoutLayout(
236 long groupId, String name, long linkedToLayoutId)
237 throws Exception {
238
239 Layout layout = addLayout(groupId, name);
240
241 UnicodeProperties typeSettingsProperties =
242 layout.getTypeSettingsProperties();
243
244 typeSettingsProperties.setProperty(
245 "linkToLayoutId", String.valueOf(linkedToLayoutId));
246
247 layout.setType(LayoutConstants.TYPE_LINK_TO_LAYOUT);
248
249 LayoutLocalServiceUtil.updateLayout(layout);
250
251 return layout;
252 }
253
254 public static String getLayoutTemplateId(Layout layout) {
255 LayoutTypePortlet layoutTypePortlet =
256 (LayoutTypePortlet)layout.getLayoutType();
257
258 return layoutTypePortlet.getLayoutTemplateId();
259 }
260
261 public static PortletPreferences getPortletPreferences(
262 Layout layout, String portletId)
263 throws Exception {
264
265 return PortletPreferencesFactoryUtil.getPortletSetup(
266 layout, portletId, null);
267 }
268
269 public static PortletPreferences getPortletPreferences(
270 long plid, String portletId)
271 throws Exception {
272
273 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
274
275 return getPortletPreferences(layout, portletId);
276 }
277
278 public static List<Portlet> getPortlets(Layout layout) throws Exception {
279 LayoutTypePortlet layoutTypePortlet =
280 (LayoutTypePortlet)layout.getLayoutType();
281
282 return layoutTypePortlet.getPortlets();
283 }
284
285 public static boolean isLayoutColumnCustomizable(
286 Layout layout, String columnId) {
287
288 LayoutTypePortlet layoutTypePortlet =
289 (LayoutTypePortlet)layout.getLayoutType();
290
291 return layoutTypePortlet.isColumnCustomizable(columnId);
292 }
293
294 public static Layout updateLayoutColumnCustomizable(
295 Layout layout, String columnId, boolean customizable)
296 throws Exception {
297
298 LayoutTypePortlet layoutTypePortlet =
299 (LayoutTypePortlet)layout.getLayoutType();
300
301 layoutTypePortlet.setTypeSettingsProperty(
302 CustomizedPages.namespaceColumnId(columnId),
303 String.valueOf(customizable));
304
305 layoutTypePortlet.setUpdatePermission(customizable);
306
307 return LayoutServiceUtil.updateLayout(
308 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
309 layout.getTypeSettings());
310 }
311
312 public static Layout updateLayoutPortletPreference(
313 Layout layout, String portletId, String portletPreferenceName,
314 String portletPreferenceValue)
315 throws Exception {
316
317 PortletPreferences layoutPortletPreferences =
318 LayoutTestUtil.getPortletPreferences(layout, portletId);
319
320 layoutPortletPreferences.setValue(
321 portletPreferenceName, portletPreferenceValue);
322
323 layoutPortletPreferences.store();
324
325 return LayoutLocalServiceUtil.getLayout(layout.getPlid());
326 }
327
328 public static Layout updateLayoutPortletPreferences(
329 Layout layout, String portletId,
330 Map<String, String> portletPreferences)
331 throws Exception {
332
333 PortletPreferences layoutPortletPreferences =
334 LayoutTestUtil.getPortletPreferences(layout, portletId);
335
336 for (Map.Entry<String, String> entry : portletPreferences.entrySet()) {
337 layoutPortletPreferences.setValue(entry.getKey(), entry.getValue());
338 }
339
340 layoutPortletPreferences.store();
341
342 return LayoutLocalServiceUtil.getLayout(layout.getPlid());
343 }
344
345 public static Layout updateLayoutTemplateId(
346 Layout layout, String layoutTemplateId)
347 throws Exception {
348
349 LayoutTypePortlet layoutTypePortlet =
350 (LayoutTypePortlet)layout.getLayoutType();
351
352 layoutTypePortlet.setLayoutTemplateId(
353 TestPropsValues.getUserId(), layoutTemplateId);
354
355 return LayoutServiceUtil.updateLayout(
356 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
357 layout.getTypeSettings());
358 }
359
360 }