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