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