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.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.kernel.util.UnicodeProperties;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.LayoutConstants;
027    import com.liferay.portal.model.LayoutPrototype;
028    import com.liferay.portal.model.LayoutSet;
029    import com.liferay.portal.model.LayoutTypePortlet;
030    import com.liferay.portal.model.LayoutTypePortletConstants;
031    import com.liferay.portal.model.Portlet;
032    import com.liferay.portal.service.LayoutLocalServiceUtil;
033    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
034    import com.liferay.portal.service.PortletLocalServiceUtil;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.service.UserLocalServiceUtil;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.PortletKeys;
039    
040    import java.util.HashMap;
041    import java.util.List;
042    import java.util.Locale;
043    import java.util.Map;
044    
045    /**
046     * @author Sergio González
047     * @author Juan Fernández
048     */
049    public class AddDefaultLayoutPrototypesAction 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 void addBlogPage(
062                            long companyId, long defaultUserId,
063                            List<LayoutPrototype> layoutPrototypes)
064                    throws Exception {
065    
066                    Layout layout = addLayoutPrototype(
067                            companyId, defaultUserId, "Blog",
068                            "Create, edit, and view blogs from this page. Explore topics " +
069                                    "using tags, and connect with other members that blog.",
070                            "2_columns_iii", layoutPrototypes);
071    
072                    if (layout == null) {
073                            return;
074                    }
075    
076                    addPortletId(layout, PortletKeys.BLOGS, "column-1");
077                    addPortletId(layout, PortletKeys.TAGS_CLOUD, "column-2");
078                    addPortletId(layout, PortletKeys.RECENT_BLOGGERS, "column-2");
079            }
080    
081            protected Layout addLayout(
082                            LayoutSet layoutSet, String name, String friendlyURL,
083                            String layouteTemplateId)
084                    throws Exception {
085    
086                    Group group = layoutSet.getGroup();
087    
088                    ServiceContext serviceContext = new ServiceContext();
089    
090                    Layout layout = LayoutLocalServiceUtil.addLayout(
091                            group.getCreatorUserId(), group.getGroupId(),
092                            layoutSet.isPrivateLayout(),
093                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, name, StringPool.BLANK,
094                            StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
095                            serviceContext);
096    
097                    LayoutTypePortlet layoutTypePortlet =
098                            (LayoutTypePortlet)layout.getLayoutType();
099    
100                    layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
101    
102                    return layout;
103            }
104    
105            protected Layout addLayoutPrototype(
106                            long companyId, long defaultUserId, String name, String description,
107                            String layouteTemplateId, List<LayoutPrototype> layoutPrototypes)
108                    throws Exception {
109    
110                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
111                            String curName = layoutPrototype.getName(LocaleUtil.getDefault());
112                            String curDescription = layoutPrototype.getDescription();
113    
114                            if (name.equals(curName) && description.equals(curDescription)) {
115                                    return null;
116                            }
117                    }
118    
119                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
120    
121                    nameMap.put(LocaleUtil.getDefault(), name);
122    
123                    LayoutPrototype layoutPrototype =
124                            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
125                                    defaultUserId, companyId, nameMap, description, true,
126                                    new ServiceContext());
127    
128                    Layout layout = layoutPrototype.getLayout();
129    
130                    LayoutTypePortlet layoutTypePortlet =
131                            (LayoutTypePortlet)layout.getLayoutType();
132    
133                    layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
134    
135                    return layout;
136            }
137    
138            protected String addPortletId(
139                            Layout layout, String portletId, String columnId)
140                    throws Exception {
141    
142                    LayoutTypePortlet layoutTypePortlet =
143                            (LayoutTypePortlet)layout.getLayoutType();
144    
145                    portletId = layoutTypePortlet.addPortletId(
146                            0, portletId, columnId, -1, false);
147    
148                    updateLayout(layout);
149    
150                    addResourcePermissions(layout, portletId);
151    
152                    return portletId;
153            }
154    
155            protected void addResourcePermissions(Layout layout, String portletId)
156                    throws Exception {
157    
158                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
159                            layout.getCompanyId(), portletId);
160    
161                    PortalUtil.addPortletDefaultResource(
162                            layout.getCompanyId(), layout, portlet);
163            }
164    
165            protected void addWebContentPage(
166                            long companyId, long defaultUserId,
167                            List<LayoutPrototype> layoutPrototypes)
168                    throws Exception {
169    
170                    Layout layout = addLayoutPrototype(
171                            companyId, defaultUserId, "Content Display Page",
172                            "Create, edit, and explore web content with this page. Search " +
173                                    "available content, explore related content with tags, and " +
174                                            "browse content categories.",
175                            "2_columns_ii", layoutPrototypes);
176    
177                    if (layout == null) {
178                            return;
179                    }
180    
181                    addPortletId(layout, PortletKeys.TAGS_ENTRIES_NAVIGATION, "column-1");
182                    addPortletId(
183                            layout, PortletKeys.TAGS_CATEGORIES_NAVIGATION, "column-1");
184                    addPortletId(layout, PortletKeys.SEARCH, "column-2");
185                    String portletId = addPortletId(
186                            layout, PortletKeys.ASSET_PUBLISHER, "column-2");
187    
188                    UnicodeProperties typeSettingsProperties =
189                            layout.getTypeSettingsProperties();
190    
191                    typeSettingsProperties.setProperty(
192                            LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
193                            portletId);
194    
195                    layout = LayoutLocalServiceUtil.updateLayout(
196                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
197                            layout.getTypeSettings());
198            }
199    
200            protected void addWikiPage(
201                            long companyId, long defaultUserId,
202                            List<LayoutPrototype> layoutPrototypes)
203                    throws Exception {
204    
205                    Layout layout = addLayoutPrototype(
206                            companyId, defaultUserId, "Wiki",
207                            "Collaborate with members through the wiki on this page. " +
208                                    "Discover related content through tags, and navigate quickly " +
209                                            "and easily with categories.",
210                            "2_columns_iii", layoutPrototypes);
211    
212                    if (layout == null) {
213                            return;
214                    }
215    
216                    addPortletId(layout, PortletKeys.WIKI, "column-1");
217                    addPortletId(
218                            layout, PortletKeys.TAGS_CATEGORIES_NAVIGATION, "column-2");
219                    addPortletId(layout, PortletKeys.TAGS_ENTRIES_NAVIGATION, "column-2");
220            }
221    
222            protected void doRun(long companyId) throws Exception {
223                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
224    
225                    List<LayoutPrototype> layoutPrototypes =
226                            LayoutPrototypeLocalServiceUtil.search(
227                                    companyId, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
228    
229                    addBlogPage(companyId, defaultUserId, layoutPrototypes);
230                    addWebContentPage(companyId, defaultUserId, layoutPrototypes);
231                    addWikiPage(companyId, defaultUserId, layoutPrototypes);
232            }
233    
234            protected void updateLayout(Layout layout) throws Exception {
235                    LayoutLocalServiceUtil.updateLayout(
236                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
237                            layout.getTypeSettings());
238            }
239    
240    }