001
014
015 package com.liferay.portal.events;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.events.ActionException;
019 import com.liferay.portal.kernel.events.SimpleAction;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.LocaleUtil;
022 import com.liferay.portal.kernel.util.StringPool;
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.LayoutSet;
027 import com.liferay.portal.model.LayoutSetPrototype;
028 import com.liferay.portal.model.LayoutTypePortlet;
029 import com.liferay.portal.model.Portlet;
030 import com.liferay.portal.service.LayoutLocalServiceUtil;
031 import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
032 import com.liferay.portal.service.PortletLocalServiceUtil;
033 import com.liferay.portal.service.ServiceContext;
034 import com.liferay.portal.service.UserLocalServiceUtil;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portal.util.PortletKeys;
037 import com.liferay.portlet.PortletPreferencesFactoryUtil;
038 import com.liferay.portlet.calendar.model.CalEvent;
039
040 import java.util.HashMap;
041 import java.util.Iterator;
042 import java.util.List;
043 import java.util.Locale;
044 import java.util.Map;
045
046 import javax.portlet.PortletPreferences;
047
048
051 public class AddDefaultLayoutSetPrototypesAction extends SimpleAction {
052
053 @Override
054 public void run(String[] ids) throws ActionException {
055 try {
056 doRun(GetterUtil.getLong(ids[0]));
057 }
058 catch (Exception e) {
059 throw new ActionException(e);
060 }
061 }
062
063 protected Layout addLayout(
064 LayoutSet layoutSet, String name, String friendlyURL,
065 String layouteTemplateId)
066 throws Exception {
067
068 Group group = layoutSet.getGroup();
069
070 ServiceContext serviceContext = new ServiceContext();
071
072 Layout layout = LayoutLocalServiceUtil.addLayout(
073 group.getCreatorUserId(), group.getGroupId(),
074 layoutSet.isPrivateLayout(),
075 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, name, StringPool.BLANK,
076 StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
077 serviceContext);
078
079 LayoutTypePortlet layoutTypePortlet =
080 (LayoutTypePortlet)layout.getLayoutType();
081
082 layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
083
084 return layout;
085 }
086
087 protected LayoutSet addLayoutSetPrototype(
088 long companyId, long defaultUserId, String name, String description,
089 List<LayoutSetPrototype> layoutSetPrototypes)
090 throws Exception {
091
092 for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
093 String curName = layoutSetPrototype.getName(
094 LocaleUtil.getDefault());
095 String curDescription = layoutSetPrototype.getDescription();
096
097 if (name.equals(curName) && description.equals(curDescription)) {
098 return null;
099 }
100 }
101
102 Map<Locale, String> nameMap = new HashMap<Locale, String>();
103
104 nameMap.put(LocaleUtil.getDefault(), name);
105
106 LayoutSetPrototype layoutSetPrototype =
107 LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
108 defaultUserId, companyId, nameMap, description, true, true,
109 new ServiceContext());
110
111 LayoutSet layoutSet = layoutSetPrototype.getLayoutSet();
112
113 ServiceContext serviceContext = new ServiceContext();
114
115 LayoutLocalServiceUtil.deleteLayouts(
116 layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
117 serviceContext);
118
119 return layoutSetPrototype.getLayoutSet();
120 }
121
122 protected String addPortletId(
123 Layout layout, String portletId, String columnId)
124 throws Exception {
125
126 LayoutTypePortlet layoutTypePortlet =
127 (LayoutTypePortlet)layout.getLayoutType();
128
129 portletId = layoutTypePortlet.addPortletId(
130 0, portletId, columnId, -1, false);
131
132 updateLayout(layout);
133
134 addResourcePermissions(layout, portletId);
135
136 return portletId;
137 }
138
139 protected void addPrivateSite(
140 long companyId, long defaultUserId, List<LayoutSetPrototype>
141 layoutSetPrototypes)
142 throws Exception {
143
144 LayoutSet layoutSet = addLayoutSetPrototype(
145 companyId, defaultUserId, "Intranet Site",
146 "Site with Documents, Calendar and News", layoutSetPrototypes);
147
148 if (layoutSet == null) {
149 return;
150 }
151
152
153
154 Layout layout = addLayout(layoutSet, "Home", "/home", "2_columns_i");
155
156 addPortletId(layout, PortletKeys.ACTIVITIES, "column-1");
157
158 String portletId = addPortletId(layout, PortletKeys.SEARCH, "column-2");
159
160 Map<String, String> preferences = new HashMap<String, String>();
161
162 preferences.put("portletSetupShowBorders", Boolean.FALSE.toString());
163
164 updatePortletSetup(layout, portletId, preferences);
165
166 portletId = addPortletId(layout, PortletKeys.LANGUAGE, "column-2");
167
168 preferences = new HashMap<String, String>();
169
170 preferences.put("displayStyle", "3");
171
172 updatePortletSetup(layout, portletId, preferences);
173
174 portletId = addPortletId(
175 layout, PortletKeys.ASSET_PUBLISHER, "column-2");
176
177 preferences = new HashMap<String, String>();
178
179 preferences.put(
180 "portletSetupTitle_" + LocaleUtil.getDefault(), "Recent Content");
181 preferences.put("portletSetupUseCustomTitle", Boolean.TRUE.toString());
182
183 updatePortletSetup(layout, portletId, preferences);
184
185
186
187 layout = addLayout(
188 layoutSet, "Documents and Media", "/documents", "1_column");
189
190 portletId = addPortletId(
191 layout, PortletKeys.DOCUMENT_LIBRARY, "column-1");
192
193 preferences = new HashMap<String, String>();
194
195 preferences.put("portletSetupShowBorders", Boolean.FALSE.toString());
196
197 updatePortletSetup(layout, portletId, preferences);
198
199
200
201 layout = addLayout(layoutSet, "Calendar", "/calendar", "2_columns_iii");
202
203 addPortletId(layout, PortletKeys.CALENDAR, "column-1");
204
205 portletId = addPortletId(
206 layout, PortletKeys.ASSET_PUBLISHER, "column-2");
207
208 preferences = new HashMap<String, String>();
209
210 preferences.put("anyAssetType", Boolean.FALSE.toString());
211
212 long classNameId = PortalUtil.getClassNameId(CalEvent.class);
213
214 preferences.put("classNameIds", String.valueOf(classNameId));
215
216 preferences.put(
217 "portletSetupTitle_" + LocaleUtil.getDefault(), "Upcoming Events");
218 preferences.put("portletSetupUseCustomTitle", Boolean.TRUE.toString());
219
220 updatePortletSetup(layout, portletId, preferences);
221
222
223
224 layout = addLayout(layoutSet, "News", "/news", "2_columns_iii");
225
226 portletId = addPortletId(layout, PortletKeys.RSS, "column-1");
227
228 preferences = new HashMap<String, String>();
229
230 preferences.put("expandedEntriesPerFeed", "3");
231 preferences.put(
232 "portletSetupTitle_" + LocaleUtil.getDefault(), "Technology news");
233 preferences.put("portletSetupUseCustomTitle", Boolean.TRUE.toString());
234 preferences.put(
235 "urls", "http:
236
237 updatePortletSetup(layout, portletId, preferences);
238
239 portletId = addPortletId(layout, PortletKeys.RSS, "column-2");
240
241 preferences = new HashMap<String, String>();
242
243 preferences.put("expandedEntriesPerFeed", "0");
244 preferences.put(
245 "portletSetupTitle_" + LocaleUtil.getDefault(), "Liferay news");
246 preferences.put("portletSetupUseCustomTitle", Boolean.TRUE.toString());
247 preferences.put(
248 "urls", "http:
249 preferences.put("titles", "Liferay Press Releases");
250
251 updatePortletSetup(layout, portletId, preferences);
252 }
253
254 protected void addPublicSite(
255 long companyId, long defaultUserId, List<LayoutSetPrototype>
256 layoutSetPrototypes)
257 throws Exception {
258
259 LayoutSet layoutSet = addLayoutSetPrototype(
260 companyId, defaultUserId, "Community Site",
261 "Site with Forums, Calendar and Wiki", layoutSetPrototypes);
262
263 if (layoutSet == null) {
264 return;
265 }
266
267
268
269 Layout layout = addLayout(layoutSet, "Home", "/home", "2_columns_iii");
270
271 addPortletId(layout, PortletKeys.MESSAGE_BOARDS, "column-1");
272
273 String portletId = addPortletId(layout, PortletKeys.SEARCH, "column-2");
274
275 Map<String, String> preferences = new HashMap<String, String>();
276
277 preferences.put("portletSetupShowBorders", Boolean.FALSE.toString());
278
279 updatePortletSetup(layout, portletId, preferences);
280
281 addPortletId(layout, PortletKeys.POLLS_DISPLAY, "column-2");
282 addPortletId(layout, PortletKeys.USER_STATISTICS, "column-2");
283
284
285
286 layout = addLayout(layoutSet, "Calendar", "/calendar", "2_columns_iii");
287
288 addPortletId(layout, PortletKeys.CALENDAR, "column-1");
289
290 portletId = addPortletId(
291 layout, PortletKeys.ASSET_PUBLISHER, "column-2");
292
293 preferences = new HashMap<String, String>();
294
295 preferences.put("anyAssetType", Boolean.FALSE.toString());
296
297 long classNameId = PortalUtil.getClassNameId(CalEvent.class);
298
299 preferences.put("classNameIds", String.valueOf(classNameId));
300
301 preferences.put(
302 "portletSetupTitle_" + LocaleUtil.getDefault(), "Upcoming Events");
303 preferences.put("portletSetupUseCustomTitle", Boolean.TRUE.toString());
304
305 updatePortletSetup(layout, portletId, preferences);
306
307
308
309 layout = addLayout(layoutSet, "Wiki", "/wiki", "2_columns_iii");
310
311 addPortletId(layout, PortletKeys.WIKI, "column-1");
312 addPortletId(
313 layout, PortletKeys.TAGS_CATEGORIES_NAVIGATION, "column-2");
314 addPortletId(layout, PortletKeys.TAGS_CLOUD, "column-2");
315 }
316
317 protected void addResourcePermissions(Layout layout, String portletId)
318 throws Exception {
319
320 Portlet portlet = PortletLocalServiceUtil.getPortletById(
321 layout.getCompanyId(), portletId);
322
323 PortalUtil.addPortletDefaultResource(
324 layout.getCompanyId(), layout, portlet);
325 }
326
327 protected void doRun(long companyId) throws Exception {
328 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
329
330 List<LayoutSetPrototype> layoutSetPrototypes =
331 LayoutSetPrototypeLocalServiceUtil.search(
332 companyId, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
333
334 addPublicSite(companyId, defaultUserId, layoutSetPrototypes);
335 addPrivateSite(companyId, defaultUserId, layoutSetPrototypes);
336 }
337
338 protected void updateLayout(Layout layout) throws Exception {
339 LayoutLocalServiceUtil.updateLayout(
340 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
341 layout.getTypeSettings());
342 }
343
344 protected PortletPreferences updatePortletSetup(
345 Layout layout, String portletId, Map<String, String> preferences)
346 throws Exception {
347
348 PortletPreferences portletSetup =
349 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
350 layout, portletId);
351
352 Iterator<Map.Entry<String, String>> itr =
353 preferences.entrySet().iterator();
354
355 while (itr.hasNext()) {
356 Map.Entry<String, String> entry = itr.next();
357
358 String key = entry.getKey();
359 String value = entry.getValue();
360
361 portletSetup.setValue(key, value);
362 }
363
364 portletSetup.store();
365
366 return portletSetup;
367 }
368
369 }