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