001
014
015 package com.liferay.portal.events;
016
017 import com.liferay.portal.kernel.events.SimpleAction;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.model.Group;
022 import com.liferay.portal.model.Layout;
023 import com.liferay.portal.model.LayoutConstants;
024 import com.liferay.portal.model.LayoutSet;
025 import com.liferay.portal.model.LayoutTypePortlet;
026 import com.liferay.portal.model.Portlet;
027 import com.liferay.portal.service.LayoutLocalServiceUtil;
028 import com.liferay.portal.service.PortletLocalServiceUtil;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portal.util.PortalUtil;
031 import com.liferay.portlet.PortletPreferencesFactoryUtil;
032
033 import java.util.HashMap;
034 import java.util.Locale;
035 import java.util.Map;
036
037 import javax.portlet.PortletPreferences;
038
039
042 public abstract class BaseDefaultLayoutPrototypesAction extends SimpleAction {
043
044 protected Layout addLayout(
045 LayoutSet layoutSet, String nameKey, String friendlyURL,
046 String layouteTemplateId)
047 throws Exception {
048
049 Group group = layoutSet.getGroup();
050
051 Map<Locale, String> nameMap = new HashMap<Locale, String>();
052
053 Locale[] locales = LanguageUtil.getAvailableLocales();
054
055 for (Locale locale : locales) {
056 nameMap.put(locale, LanguageUtil.get(locale, nameKey));
057 }
058
059 Map<Locale, String> friendlyURLMap = new HashMap<Locale, String>();
060
061 friendlyURLMap.put(LocaleUtil.getDefault(), friendlyURL);
062
063 ServiceContext serviceContext = new ServiceContext();
064
065 Layout layout = LayoutLocalServiceUtil.addLayout(
066 group.getCreatorUserId(), group.getGroupId(),
067 layoutSet.isPrivateLayout(),
068 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, nameMap, null, null, null,
069 null, LayoutConstants.TYPE_PORTLET, StringPool.BLANK, false,
070 friendlyURLMap, serviceContext);
071
072 LayoutTypePortlet layoutTypePortlet =
073 (LayoutTypePortlet)layout.getLayoutType();
074
075 layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
076
077 return layout;
078 }
079
080 protected String addPortletId(
081 Layout layout, String portletId, String columnId)
082 throws Exception {
083
084 LayoutTypePortlet layoutTypePortlet =
085 (LayoutTypePortlet)layout.getLayoutType();
086
087 portletId = layoutTypePortlet.addPortletId(
088 0, portletId, columnId, -1, false);
089
090 updateLayout(layout);
091
092 addResourcePermissions(layout, portletId);
093
094 return portletId;
095 }
096
097 protected void addResourcePermissions(Layout layout, String portletId)
098 throws Exception {
099
100 Portlet portlet = PortletLocalServiceUtil.getPortletById(
101 layout.getCompanyId(), portletId);
102
103 PortalUtil.addPortletDefaultResource(
104 layout.getCompanyId(), layout, portlet);
105 }
106
107 protected void updateLayout(Layout layout) throws Exception {
108 LayoutLocalServiceUtil.updateLayout(
109 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
110 layout.getTypeSettings());
111 }
112
113 protected PortletPreferences updatePortletSetup(
114 Layout layout, String portletId, Map<String, String> preferences)
115 throws Exception {
116
117 PortletPreferences portletSetup =
118 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
119 layout, portletId);
120
121 for (Map.Entry<String, String> entry : preferences.entrySet()) {
122 String key = entry.getKey();
123 String value = entry.getValue();
124
125 portletSetup.setValue(key, value);
126 }
127
128 portletSetup.store();
129
130 return portletSetup;
131 }
132
133 }