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.kernel.util.UnicodeProperties;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutPrototype;
025    import com.liferay.portal.model.LayoutTypePortlet;
026    import com.liferay.portal.model.LayoutTypePortletConstants;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.UserLocalServiceUtil;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PortletKeys;
033    import com.liferay.portlet.blogs.model.BlogsEntry;
034    import com.liferay.portlet.wiki.model.WikiPage;
035    
036    import java.util.HashMap;
037    import java.util.List;
038    import java.util.Locale;
039    import java.util.Map;
040    
041    /**
042     * @author Sergio Gonz??lez
043     * @author Juan Fern??ndez
044     */
045    public class AddDefaultLayoutPrototypesAction
046            extends BaseDefaultLayoutPrototypesAction {
047    
048            @Override
049            public void run(String[] ids) throws ActionException {
050                    try {
051                            doRun(GetterUtil.getLong(ids[0]));
052                    }
053                    catch (Exception e) {
054                            throw new ActionException(e);
055                    }
056            }
057    
058            protected void addBlogPage(
059                            long companyId, long defaultUserId,
060                            List<LayoutPrototype> layoutPrototypes)
061                    throws Exception {
062    
063                    Layout layout = addLayoutPrototype(
064                            companyId, defaultUserId, "layout-prototype-blog-title",
065                            "layout-prototype-blog-description", "2_columns_iii",
066                            layoutPrototypes);
067    
068                    if (layout == null) {
069                            return;
070                    }
071    
072                    addPortletId(layout, PortletKeys.BLOGS, "column-1");
073    
074                    String portletId = addPortletId(
075                            layout, PortletKeys.TAGS_CLOUD, "column-2");
076    
077                    Map<String, String> preferences = new HashMap<String, String>();
078    
079                    preferences.put(
080                            "classNameId",
081                            String.valueOf(PortalUtil.getClassNameId(BlogsEntry.class)));
082                    preferences.put("showAssetCount", Boolean.TRUE.toString());
083    
084                    updatePortletSetup(layout, portletId, preferences);
085    
086                    addPortletId(layout, PortletKeys.RECENT_BLOGGERS, "column-2");
087            }
088    
089            protected Layout addLayoutPrototype(
090                            long companyId, long defaultUserId, String nameKey,
091                            String descriptionKey, String layouteTemplateId,
092                            List<LayoutPrototype> layoutPrototypes)
093                    throws Exception {
094    
095                    String name = LanguageUtil.get(LocaleUtil.getDefault(), nameKey);
096                    String description = LanguageUtil.get(
097                            LocaleUtil.getDefault(), descriptionKey);
098    
099                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
100                            String curName = layoutPrototype.getName(LocaleUtil.getDefault());
101                            String curDescription = layoutPrototype.getDescription(
102                                    LocaleUtil.getDefault());
103    
104                            if (name.equals(curName) && description.equals(curDescription)) {
105                                    return null;
106                            }
107                    }
108    
109                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
110                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
111    
112                    Locale[] locales = LanguageUtil.getAvailableLocales();
113    
114                    for (Locale locale : locales) {
115                            nameMap.put(locale, LanguageUtil.get(locale, nameKey));
116                            descriptionMap.put(
117                                    locale, LanguageUtil.get(locale, descriptionKey));
118                    }
119    
120                    LayoutPrototype layoutPrototype =
121                            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
122                                    defaultUserId, companyId, nameMap, descriptionMap, true,
123                                    new ServiceContext());
124    
125                    Layout layout = layoutPrototype.getLayout();
126    
127                    LayoutTypePortlet layoutTypePortlet =
128                            (LayoutTypePortlet)layout.getLayoutType();
129    
130                    layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
131    
132                    return layout;
133            }
134    
135            protected void addWebContentPage(
136                            long companyId, long defaultUserId,
137                            List<LayoutPrototype> layoutPrototypes)
138                    throws Exception {
139    
140                    Layout layout = addLayoutPrototype(
141                            companyId, defaultUserId, "layout-prototype-web-content-title",
142                            "layout-prototype-web-content-description", "2_columns_ii",
143                            layoutPrototypes);
144    
145                    if (layout == null) {
146                            return;
147                    }
148    
149                    addPortletId(layout, PortletKeys.ASSET_TAGS_NAVIGATION, "column-1");
150                    addPortletId(
151                            layout, PortletKeys.ASSET_CATEGORIES_NAVIGATION, "column-1");
152                    addPortletId(layout, PortletKeys.SEARCH, "column-2");
153                    String portletId = addPortletId(
154                            layout, PortletKeys.ASSET_PUBLISHER, "column-2");
155    
156                    UnicodeProperties typeSettingsProperties =
157                            layout.getTypeSettingsProperties();
158    
159                    typeSettingsProperties.setProperty(
160                            LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
161                            portletId);
162    
163                    layout = LayoutLocalServiceUtil.updateLayout(
164                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
165                            layout.getTypeSettings());
166            }
167    
168            protected void addWikiPage(
169                            long companyId, long defaultUserId,
170                            List<LayoutPrototype> layoutPrototypes)
171                    throws Exception {
172    
173                    Layout layout = addLayoutPrototype(
174                            companyId, defaultUserId, "layout-prototype-wiki-title",
175                            "layout-prototype-wiki-description", "2_columns_iii",
176                            layoutPrototypes);
177    
178                    if (layout == null) {
179                            return;
180                    }
181    
182                    addPortletId(layout, PortletKeys.WIKI, "column-1");
183                    addPortletId(
184                            layout, PortletKeys.ASSET_CATEGORIES_NAVIGATION, "column-2");
185    
186                    String portletId = addPortletId(
187                            layout, PortletKeys.ASSET_TAGS_NAVIGATION, "column-2");
188    
189                    Map<String, String> preferences = new HashMap<String, String>();
190    
191                    preferences.put(
192                            "classNameId",
193                            String.valueOf(PortalUtil.getClassNameId(WikiPage.class)));
194                    preferences.put("showAssetCount", Boolean.TRUE.toString());
195    
196                    updatePortletSetup(layout, portletId, preferences);
197            }
198    
199            protected void doRun(long companyId) throws Exception {
200                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
201    
202                    List<LayoutPrototype> layoutPrototypes =
203                            LayoutPrototypeLocalServiceUtil.search(
204                                    companyId, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
205    
206                    addBlogPage(companyId, defaultUserId, layoutPrototypes);
207                    addWebContentPage(companyId, defaultUserId, layoutPrototypes);
208                    addWikiPage(companyId, defaultUserId, layoutPrototypes);
209            }
210    
211    }