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.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
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
127 Layout layout = layoutPrototype.getLayout();
128
129 LayoutTypePortlet layoutTypePortlet =
130 (LayoutTypePortlet)layout.getLayoutType();
131
132 layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
133
134 return layout;
135 }
136
137 protected String addPortletId(
138 Layout layout, String portletId, String columnId)
139 throws Exception {
140
141 LayoutTypePortlet layoutTypePortlet =
142 (LayoutTypePortlet)layout.getLayoutType();
143
144 portletId = layoutTypePortlet.addPortletId(
145 0, portletId, columnId, -1, false);
146
147 updateLayout(layout);
148
149 addResourcePermissions(layout, portletId);
150
151 return portletId;
152 }
153
154 protected void addResourcePermissions(Layout layout, String portletId)
155 throws Exception {
156
157 Portlet portlet = PortletLocalServiceUtil.getPortletById(
158 layout.getCompanyId(), portletId);
159
160 PortalUtil.addPortletDefaultResource(
161 layout.getCompanyId(), layout, portlet);
162 }
163
164 protected void addWebContentPage(
165 long companyId, long defaultUserId,
166 List<LayoutPrototype> layoutPrototypes)
167 throws Exception {
168
169 Layout layout = addLayoutPrototype(
170 companyId, defaultUserId, "Content Display Page",
171 "Create, edit, and explore web content with this page. Search " +
172 "available content, explore related content with tags, and " +
173 "browse content categories.",
174 "2_columns_ii", layoutPrototypes);
175
176 if (layout == null) {
177 return;
178 }
179
180 addPortletId(layout, PortletKeys.TAGS_ENTRIES_NAVIGATION, "column-1");
181 addPortletId(
182 layout, PortletKeys.TAGS_CATEGORIES_NAVIGATION, "column-1");
183 addPortletId(layout, PortletKeys.SEARCH, "column-2");
184 String portletId = addPortletId(
185 layout, PortletKeys.ASSET_PUBLISHER, "column-2");
186
187 UnicodeProperties typeSettingsProperties =
188 layout.getTypeSettingsProperties();
189
190 typeSettingsProperties.setProperty(
191 LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
192 portletId);
193
194 layout = LayoutLocalServiceUtil.updateLayout(
195 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
196 layout.getTypeSettings());
197 }
198
199 protected void addWikiPage(
200 long companyId, long defaultUserId,
201 List<LayoutPrototype> layoutPrototypes)
202 throws Exception {
203
204 Layout layout = addLayoutPrototype(
205 companyId, defaultUserId, "Wiki",
206 "Collaborate with members through the wiki on this page. " +
207 "Discover related content through tags, and navigate quickly " +
208 "and easily with categories.",
209 "2_columns_iii", layoutPrototypes);
210
211 if (layout == null) {
212 return;
213 }
214
215 addPortletId(layout, PortletKeys.WIKI, "column-1");
216 addPortletId(
217 layout, PortletKeys.TAGS_CATEGORIES_NAVIGATION, "column-2");
218 addPortletId(layout, PortletKeys.TAGS_ENTRIES_NAVIGATION, "column-2");
219 }
220
221 protected void doRun(long companyId) throws Exception {
222 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
223
224 List<LayoutPrototype> layoutPrototypes =
225 LayoutPrototypeLocalServiceUtil.search(
226 companyId, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
227
228 addBlogPage(companyId, defaultUserId, layoutPrototypes);
229 addWebContentPage(companyId, defaultUserId, layoutPrototypes);
230 addWikiPage(companyId, defaultUserId, layoutPrototypes);
231 }
232
233 protected void updateLayout(Layout layout) throws Exception {
234 LayoutLocalServiceUtil.updateLayout(
235 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
236 layout.getTypeSettings());
237 }
238
239 }