001    /**
002     * Copyright (c) 2000-present 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.language.LanguageUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutSet;
024    import com.liferay.portal.model.LayoutSetPrototype;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    import com.liferay.portal.util.PortletKeys;
030    
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Locale;
034    import java.util.Map;
035    
036    /**
037     * @author Sergio Gonz??lez
038     */
039    public class AddDefaultLayoutSetPrototypesAction
040            extends BaseDefaultLayoutPrototypesAction {
041    
042            @Override
043            public void run(String[] ids) throws ActionException {
044                    try {
045                            doRun(GetterUtil.getLong(ids[0]));
046                    }
047                    catch (Exception e) {
048                            throw new ActionException(e);
049                    }
050            }
051    
052            protected LayoutSet addLayoutSetPrototype(
053                            long companyId, long defaultUserId, String nameKey,
054                            String descriptionKey, List<LayoutSetPrototype> layoutSetPrototypes)
055                    throws Exception {
056    
057                    String name = LanguageUtil.get(LocaleUtil.getDefault(), nameKey);
058                    String description = LanguageUtil.get(
059                            LocaleUtil.getDefault(), descriptionKey);
060    
061                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
062                            String curName = layoutSetPrototype.getName(
063                                    LocaleUtil.getDefault());
064                            String curDescription = layoutSetPrototype.getDescription(
065                                    LocaleUtil.getDefault());
066    
067                            if (name.equals(curName) && description.equals(curDescription)) {
068                                    return null;
069                            }
070                    }
071    
072                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
073                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
074    
075                    Locale[] locales = LanguageUtil.getAvailableLocales();
076    
077                    for (Locale locale : locales) {
078                            nameMap.put(locale, LanguageUtil.get(locale, nameKey));
079                            descriptionMap.put(
080                                    locale, LanguageUtil.get(locale, descriptionKey));
081                    }
082    
083                    LayoutSetPrototype layoutSetPrototype =
084                            LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
085                                    defaultUserId, companyId, nameMap, descriptionMap, true, true,
086                                    new ServiceContext());
087    
088                    LayoutSet layoutSet = layoutSetPrototype.getLayoutSet();
089    
090                    ServiceContext serviceContext = new ServiceContext();
091    
092                    LayoutLocalServiceUtil.deleteLayouts(
093                            layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
094                            serviceContext);
095    
096                    return layoutSetPrototype.getLayoutSet();
097            }
098    
099            protected void addPrivateSite(
100                            long companyId, long defaultUserId,
101                            List<LayoutSetPrototype> layoutSetPrototypes)
102                    throws Exception {
103    
104                    LayoutSet layoutSet = addLayoutSetPrototype(
105                            companyId, defaultUserId,
106                            "layout-set-prototype-intranet-site-title",
107                            "layout-set-prototype-intranet-site-description",
108                            layoutSetPrototypes);
109    
110                    if (layoutSet == null) {
111                            return;
112                    }
113    
114                    // Home layout
115    
116                    Layout layout = addLayout(layoutSet, "home", "/home", "2_columns_i");
117    
118                    String portletId = addPortletId(layout, PortletKeys.SEARCH, "column-2");
119    
120                    Map<String, String> preferences = new HashMap<String, String>();
121    
122                    preferences.put("portletSetupShowBorders", Boolean.FALSE.toString());
123    
124                    updatePortletSetup(layout, portletId, preferences);
125    
126                    portletId = addPortletId(layout, PortletKeys.LANGUAGE, "column-2");
127    
128                    preferences = new HashMap<String, String>();
129    
130                    preferences.put("displayStyle", "3");
131    
132                    updatePortletSetup(layout, portletId, preferences);
133    
134                    portletId = addPortletId(
135                            layout, PortletKeys.ASSET_PUBLISHER, "column-2");
136    
137                    preferences = new HashMap<String, String>();
138    
139                    Locale[] locales = LanguageUtil.getAvailableLocales();
140    
141                    for (Locale locale : locales) {
142                            preferences.put(
143                                    "portletSetupTitle_" + locale,
144                                    LanguageUtil.get(locale, "recent-content"));
145                    }
146    
147                    preferences.put("portletSetupUseCustomTitle", Boolean.TRUE.toString());
148    
149                    updatePortletSetup(layout, portletId, preferences);
150    
151                    // Documents layout
152    
153                    layout = addLayout(
154                            layoutSet, "documents-and-media", "/documents", "1_column");
155    
156                    portletId = addPortletId(
157                            layout, PortletKeys.DOCUMENT_LIBRARY, "column-1");
158    
159                    preferences = new HashMap<String, String>();
160    
161                    preferences.put("portletSetupShowBorders", Boolean.FALSE.toString());
162    
163                    updatePortletSetup(layout, portletId, preferences);
164    
165                    portletId = addPortletId(
166                            layout, PortletKeys.ASSET_PUBLISHER, "column-2");
167    
168                    preferences = new HashMap<String, String>();
169    
170                    preferences.put("anyAssetType", Boolean.FALSE.toString());
171    
172                    for (Locale locale : locales) {
173                            preferences.put(
174                                    "portletSetupTitle_" + locale,
175                                    LanguageUtil.get(locale, "upcoming-events"));
176                    }
177    
178                    preferences.put("portletSetupUseCustomTitle", Boolean.TRUE.toString());
179    
180                    updatePortletSetup(layout, portletId, preferences);
181            }
182    
183            protected void addPublicSite(
184                            long companyId, long defaultUserId,
185                            List<LayoutSetPrototype> layoutSetPrototypes)
186                    throws Exception {
187    
188                    LayoutSet layoutSet = addLayoutSetPrototype(
189                            companyId, defaultUserId,
190                            "layout-set-prototype-community-site-title",
191                            "layout-set-prototype-community-site-description",
192                            layoutSetPrototypes);
193    
194                    if (layoutSet == null) {
195                            return;
196                    }
197    
198                    // Home layout
199    
200                    Layout layout = addLayout(layoutSet, "home", "/home", "2_columns_iii");
201    
202                    addPortletId(layout, PortletKeys.MESSAGE_BOARDS, "column-1");
203    
204                    String portletId = addPortletId(layout, PortletKeys.SEARCH, "column-2");
205    
206                    Map<String, String> preferences = new HashMap<String, String>();
207    
208                    preferences.put("portletSetupShowBorders", Boolean.FALSE.toString());
209    
210                    updatePortletSetup(layout, portletId, preferences);
211    
212                    addPortletId(layout, PortletKeys.USER_STATISTICS, "column-2");
213    
214                    portletId = addPortletId(
215                            layout, PortletKeys.ASSET_PUBLISHER, "column-2");
216    
217                    preferences = new HashMap<String, String>();
218    
219                    preferences.put("anyAssetType", Boolean.FALSE.toString());
220    
221                    Locale[] locales = LanguageUtil.getAvailableLocales();
222    
223                    for (Locale locale : locales) {
224                            preferences.put(
225                                    "portletSetupTitle_" + locale,
226                                    LanguageUtil.get(locale, "upcoming-events"));
227                    }
228    
229                    preferences.put("portletSetupUseCustomTitle", Boolean.TRUE.toString());
230    
231                    updatePortletSetup(layout, portletId, preferences);
232    
233                    // Wiki layout
234    
235                    layout = addLayout(layoutSet, "wiki", "/wiki", "2_columns_iii");
236    
237                    addPortletId(layout, PortletKeys.WIKI, "column-1");
238                    addPortletId(
239                            layout, PortletKeys.ASSET_CATEGORIES_NAVIGATION, "column-2");
240                    addPortletId(layout, PortletKeys.TAGS_CLOUD, "column-2");
241            }
242    
243            protected void doRun(long companyId) throws Exception {
244                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
245    
246                    List<LayoutSetPrototype> layoutSetPrototypes =
247                            LayoutSetPrototypeLocalServiceUtil.search(
248                                    companyId, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
249    
250                    addPublicSite(companyId, defaultUserId, layoutSetPrototypes);
251                    addPrivateSite(companyId, defaultUserId, layoutSetPrototypes);
252            }
253    
254    }