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.events.SimpleAction;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutSet;
025    import com.liferay.portal.model.LayoutSetPrototype;
026    import com.liferay.portal.service.LayoutLocalServiceUtil;
027    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.UserLocalServiceUtil;
030    import com.liferay.portal.util.DefaultLayoutPrototypesUtil;
031    import com.liferay.portal.util.PortletKeys;
032    
033    import java.util.HashMap;
034    import java.util.List;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Sergio Gonz??lez
040     */
041    public class AddDefaultLayoutSetPrototypesAction extends SimpleAction {
042    
043            @Override
044            public void run(String[] ids) throws ActionException {
045                    try {
046                            doRun(GetterUtil.getLong(ids[0]));
047                    }
048                    catch (Exception e) {
049                            throw new ActionException(e);
050                    }
051            }
052    
053            protected LayoutSet addLayoutSetPrototype(
054                            long companyId, long defaultUserId, String nameKey,
055                            String descriptionKey, List<LayoutSetPrototype> layoutSetPrototypes)
056                    throws Exception {
057    
058                    String name = LanguageUtil.get(LocaleUtil.getDefault(), nameKey);
059                    String description = LanguageUtil.get(
060                            LocaleUtil.getDefault(), descriptionKey);
061    
062                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
063                            String curName = layoutSetPrototype.getName(
064                                    LocaleUtil.getDefault());
065                            String curDescription = layoutSetPrototype.getDescription(
066                                    LocaleUtil.getDefault());
067    
068                            if (name.equals(curName) && description.equals(curDescription)) {
069                                    return null;
070                            }
071                    }
072    
073                    Map<Locale, String> nameMap = new HashMap<>();
074                    Map<Locale, String> descriptionMap = new HashMap<>();
075    
076                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
077                            nameMap.put(locale, LanguageUtil.get(locale, nameKey));
078                            descriptionMap.put(
079                                    locale, LanguageUtil.get(locale, descriptionKey));
080                    }
081    
082                    LayoutSetPrototype layoutSetPrototype =
083                            LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
084                                    defaultUserId, companyId, nameMap, descriptionMap, true, true,
085                                    new ServiceContext());
086    
087                    LayoutSet layoutSet = layoutSetPrototype.getLayoutSet();
088    
089                    ServiceContext serviceContext = new ServiceContext();
090    
091                    LayoutLocalServiceUtil.deleteLayouts(
092                            layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
093                            serviceContext);
094    
095                    return layoutSetPrototype.getLayoutSet();
096            }
097    
098            protected void addPrivateSite(
099                            long companyId, long defaultUserId,
100                            List<LayoutSetPrototype> layoutSetPrototypes)
101                    throws Exception {
102    
103                    LayoutSet layoutSet = addLayoutSetPrototype(
104                            companyId, defaultUserId,
105                            "layout-set-prototype-intranet-site-title",
106                            "layout-set-prototype-intranet-site-description",
107                            layoutSetPrototypes);
108    
109                    if (layoutSet == null) {
110                            return;
111                    }
112    
113                    // Home layout
114    
115                    DefaultLayoutPrototypesUtil.addLayout(
116                            layoutSet, "home", "/home", "2_columns_i");
117    
118                    // Documents layout
119    
120                    Layout layout = DefaultLayoutPrototypesUtil.addLayout(
121                            layoutSet, "documents-and-media", "/documents", "1_column");
122    
123                    String portletId = DefaultLayoutPrototypesUtil.addPortletId(
124                            layout, PortletKeys.DOCUMENT_LIBRARY, "column-1");
125    
126                    Map<String, String> preferences = new HashMap<>();
127    
128                    preferences.put("portletSetupShowBorders", Boolean.FALSE.toString());
129    
130                    DefaultLayoutPrototypesUtil.updatePortletSetup(
131                            layout, portletId, preferences);
132            }
133    
134            protected void addPublicSite(
135                            long companyId, long defaultUserId,
136                            List<LayoutSetPrototype> layoutSetPrototypes)
137                    throws Exception {
138    
139                    LayoutSet layoutSet = addLayoutSetPrototype(
140                            companyId, defaultUserId,
141                            "layout-set-prototype-community-site-title",
142                            "layout-set-prototype-community-site-description",
143                            layoutSetPrototypes);
144    
145                    if (layoutSet == null) {
146                            return;
147                    }
148    
149                    // Home layout
150    
151                    Layout layout = DefaultLayoutPrototypesUtil.addLayout(
152                            layoutSet, "home", "/home", "2_columns_iii");
153    
154                    DefaultLayoutPrototypesUtil.addPortletId(
155                            layout, PortletKeys.MESSAGE_BOARDS, "column-1");
156    
157                    DefaultLayoutPrototypesUtil.addPortletId(
158                            layout, PortletKeys.USER_STATISTICS, "column-2");
159    
160                    // Wiki layout
161    
162                    DefaultLayoutPrototypesUtil.addLayout(
163                            layoutSet, "wiki", "/wiki", "2_columns_iii");
164            }
165    
166            protected void doRun(long companyId) throws Exception {
167                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
168    
169                    List<LayoutSetPrototype> layoutSetPrototypes =
170                            LayoutSetPrototypeLocalServiceUtil.search(
171                                    companyId, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
172    
173                    addPublicSite(companyId, defaultUserId, layoutSetPrototypes);
174                    addPrivateSite(companyId, defaultUserId, layoutSetPrototypes);
175            }
176    
177    }