001    /**
002     * Copyright (c) 2000-2013 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.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutPrototype;
024    import com.liferay.portal.model.LayoutTypePortlet;
025    import com.liferay.portal.model.LayoutTypePortletConstants;
026    import com.liferay.portal.service.LayoutLocalServiceUtil;
027    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.UserLocalServiceUtil;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portlet.blogs.model.BlogsEntry;
033    import com.liferay.portlet.wiki.model.WikiPage;
034    
035    import java.util.HashMap;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Sergio Gonz??lez
042     * @author Juan Fern??ndez
043     */
044    public class AddDefaultLayoutPrototypesAction
045            extends BaseDefaultLayoutPrototypesAction {
046    
047            @Override
048            public void run(String[] ids) throws ActionException {
049                    try {
050                            doRun(GetterUtil.getLong(ids[0]));
051                    }
052                    catch (Exception e) {
053                            throw new ActionException(e);
054                    }
055            }
056    
057            protected void addBlogPage(
058                            long companyId, long defaultUserId,
059                            List<LayoutPrototype> layoutPrototypes)
060                    throws Exception {
061    
062                    Layout layout = addLayoutPrototype(
063                            companyId, defaultUserId, "Blog",
064                            "Create, edit, and view blogs from this page. Explore topics " +
065                                    "using tags, and connect with other members that blog.",
066                            "2_columns_iii", 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 name, String description,
091                            String layouteTemplateId, List<LayoutPrototype> layoutPrototypes)
092                    throws Exception {
093    
094                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
095                            String curName = layoutPrototype.getName(LocaleUtil.getDefault());
096                            String curDescription = layoutPrototype.getDescription();
097    
098                            if (name.equals(curName) && description.equals(curDescription)) {
099                                    return null;
100                            }
101                    }
102    
103                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
104    
105                    nameMap.put(LocaleUtil.getDefault(), name);
106    
107                    LayoutPrototype layoutPrototype =
108                            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
109                                    defaultUserId, companyId, nameMap, description, true,
110                                    new ServiceContext());
111    
112                    Layout layout = layoutPrototype.getLayout();
113    
114                    LayoutTypePortlet layoutTypePortlet =
115                            (LayoutTypePortlet)layout.getLayoutType();
116    
117                    layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
118    
119                    return layout;
120            }
121    
122            protected void addWebContentPage(
123                            long companyId, long defaultUserId,
124                            List<LayoutPrototype> layoutPrototypes)
125                    throws Exception {
126    
127                    Layout layout = addLayoutPrototype(
128                            companyId, defaultUserId, "Content Display Page",
129                            "Create, edit, and explore web content with this page. Search " +
130                                    "available content, explore related content with tags, and " +
131                                            "browse content categories.",
132                            "2_columns_ii", layoutPrototypes);
133    
134                    if (layout == null) {
135                            return;
136                    }
137    
138                    addPortletId(layout, PortletKeys.ASSET_TAGS_NAVIGATION, "column-1");
139                    addPortletId(
140                            layout, PortletKeys.ASSET_CATEGORIES_NAVIGATION, "column-1");
141                    addPortletId(layout, PortletKeys.SEARCH, "column-2");
142                    String portletId = addPortletId(
143                            layout, PortletKeys.ASSET_PUBLISHER, "column-2");
144    
145                    UnicodeProperties typeSettingsProperties =
146                            layout.getTypeSettingsProperties();
147    
148                    typeSettingsProperties.setProperty(
149                            LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
150                            portletId);
151    
152                    layout = LayoutLocalServiceUtil.updateLayout(
153                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
154                            layout.getTypeSettings());
155            }
156    
157            protected void addWikiPage(
158                            long companyId, long defaultUserId,
159                            List<LayoutPrototype> layoutPrototypes)
160                    throws Exception {
161    
162                    Layout layout = addLayoutPrototype(
163                            companyId, defaultUserId, "Wiki",
164                            "Collaborate with members through the wiki on this page. " +
165                                    "Discover related content through tags, and navigate quickly " +
166                                            "and easily with categories.",
167                            "2_columns_iii", layoutPrototypes);
168    
169                    if (layout == null) {
170                            return;
171                    }
172    
173                    addPortletId(layout, PortletKeys.WIKI, "column-1");
174                    addPortletId(
175                            layout, PortletKeys.ASSET_CATEGORIES_NAVIGATION, "column-2");
176    
177                    String portletId = addPortletId(
178                            layout, PortletKeys.ASSET_TAGS_NAVIGATION, "column-2");
179    
180                    Map<String, String> preferences = new HashMap<String, String>();
181    
182                    preferences.put(
183                            "classNameId",
184                            String.valueOf(PortalUtil.getClassNameId(WikiPage.class)));
185                    preferences.put("showAssetCount", Boolean.TRUE.toString());
186    
187                    updatePortletSetup(layout, portletId, preferences);
188            }
189    
190            protected void doRun(long companyId) throws Exception {
191                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
192    
193                    List<LayoutPrototype> layoutPrototypes =
194                            LayoutPrototypeLocalServiceUtil.search(
195                                    companyId, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
196    
197                    addBlogPage(companyId, defaultUserId, layoutPrototypes);
198                    addWebContentPage(companyId, defaultUserId, layoutPrototypes);
199                    addWikiPage(companyId, defaultUserId, layoutPrototypes);
200            }
201    
202    }