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