1 package com.germinus.merlin.manager.layout;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Locale;
7 import java.util.Map;
8 import java.util.Properties;
9
10 import javax.portlet.RenderRequest;
11 import javax.portlet.RenderResponse;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import com.germinus.merlin.page.PagePortletConfiguration;
17 import com.germinus.liferay.util.ILiferayUtil;
18 import com.liferay.portal.PortalException;
19 import com.liferay.portal.SystemException;
20 import com.liferay.portal.kernel.language.LanguageUtil;
21 import com.liferay.portal.kernel.util.LocaleUtil;
22 import com.liferay.portal.kernel.util.ParamUtil;
23 import com.liferay.portal.kernel.util.StringMaker;
24 import com.liferay.portal.model.Layout;
25 import com.liferay.portal.model.LayoutTypePortlet;
26 import com.liferay.portal.model.Portlet;
27 import com.liferay.portal.service.LayoutServiceUtil;
28 import com.liferay.portal.service.PortletLocalServiceUtil;
29 import com.liferay.portal.theme.ThemeDisplay;
30 import com.liferay.portal.util.WebKeys;
31
32 public class LayoutManager implements ILayoutManager {
33
34 private ILiferayUtil liferayUtil;
35
36
37
38
39 private static final Log log = LogFactory.getLog(LayoutManager.class);
40
41 private Layout layout;
42 private ThemeDisplay themeDisplay;
43 private LayoutTypePortlet layoutTypePortlet;
44 private PagePortletConfiguration pageProperties;
45
46
47
48
49
50
51
52 public LayoutManager( PagePortletConfiguration pageProperties) {
53
54 setPageProperties(pageProperties);
55 }
56
57 public LayoutManager( PagePortletConfiguration pageProperties,
58 ILiferayUtil liferayUtil) {
59
60 setLiferayUtil(liferayUtil);
61 setPageProperties(pageProperties);
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75 public LayoutTypePortlet getLayoutTypePortlet() {
76 return layoutTypePortlet;
77 }
78
79
80
81
82 public void setLayoutTypePortlet(LayoutTypePortlet layoutTypePortlet) {
83 this.layoutTypePortlet = layoutTypePortlet;
84 }
85
86 @SuppressWarnings("unused")
87 private Layout addLayout(RenderRequest request,
88 RenderResponse response, long parentLayoutId,
89 PagePortletConfiguration pagePortletConfiguration)
90 throws Exception {
91
92 this.instanceCommonObject(request);
93
94
95 Properties layoutProperties = fillLayoutProperties(request,response,pagePortletConfiguration);
96
97 long companyId = liferayUtil.getCompanyId(layout);
98
99 Layout newLayout;
100
101 if (companyId != 0) {
102
103 newLayout = LayoutServiceUtil.addLayout(companyId,
104 pagePortletConfiguration.isPrivateLayout(),
105 parentLayoutId, pagePortletConfiguration.getName(),
106 pagePortletConfiguration.getTitle(),
107 pagePortletConfiguration.getDescription(),
108 pagePortletConfiguration.getType(),
109 pagePortletConfiguration.isHidden(),
110 pagePortletConfiguration.getFriendlyURL());
111
112 newLayout.setTypeSettingsProperties(layoutProperties);
113
114 LayoutServiceUtil.updateLayout(
115 newLayout.getGroupId(), newLayout.isPrivateLayout(),
116 newLayout.getLayoutId(), newLayout.getTypeSettings());
117
118 } else {
119 throw (new Exception("Incorrect companyID"));
120 }
121
122 log
123 .debug("New layout add correctly with PLID : "
124 + newLayout.getPlid());
125
126 return newLayout;
127 }
128 @SuppressWarnings("unused")
129 private Layout addLayoutLocale(RenderRequest request,
130 RenderResponse response, long parentLayoutId,
131 PagePortletConfiguration pagePortletConfiguration)
132 throws Exception {
133
134 Locale[] locales = LanguageUtil.getAvailableLocales();
135
136 Map<Locale, String> localeNamesMap = new HashMap<Locale, String>();
137 Map<Locale, String> localeTitlesMap = new HashMap<Locale, String>();
138
139 for (Locale locale : locales) {
140 String languageId = LocaleUtil.toLanguageId(locale);
141
142 localeNamesMap.put(
143 locale, ParamUtil.getString(request, "name_" + languageId));
144 localeTitlesMap.put(
145 locale, ParamUtil.getString(request, "title_" + languageId));
146 }
147
148 long companyId = liferayUtil.getCompanyId(layout);
149 Properties layoutProperties =
150 fillLayoutProperties(request,response,pagePortletConfiguration);
151
152 Layout layout = LayoutServiceUtil.addLayout(companyId,
153 pagePortletConfiguration.isPrivateLayout(),
154 parentLayoutId, pagePortletConfiguration.getName(),
155 pagePortletConfiguration.getTitle(),
156 pagePortletConfiguration.getDescription(),
157 pagePortletConfiguration.getType(),
158 pagePortletConfiguration.isHidden(),
159 pagePortletConfiguration.getFriendlyURL());
160
161 LayoutServiceUtil.updateLayout(
162 layout.getGroupId(), layout.isPrivateLayout(),
163 layout.getLayoutId(), layout.getTypeSettings());
164
165 return layout;
166 }
167
168
169
170
171
172
173
174
175 public long addPage(RenderRequest request, RenderResponse response,
176 PagePortletConfiguration pagePortletConfiguration)
177 throws Exception {
178 long parentLayoutId = layout.getLayoutId();
179
180 addLayoutLocale(request, response, parentLayoutId, pagePortletConfiguration);
181
182 return 1;
183 }
184
185
186
187
188
189
190
191 public String addPortlet(RenderRequest request, RenderResponse response,
192 String portletId) throws Exception {
193
194 long userId = themeDisplay.getUserId();
195 log.debug("User id: " + userId);
196
197 String ppid = layoutTypePortlet.addPortletId(userId, portletId);
198
199 if (layout.isShared()) {
200
201 layoutTypePortlet.resetStates();
202 }
203
204
205 String cm = layout.getGroup().getName();
206 log.debug("Community : " + cm);
207
208 long companyId = liferayUtil.getCompanyId(layout);
209
210
211
212
213 Portlet portlet = PortletLocalServiceUtil.getPortletById(companyId,
214 portletId);
215
216 log.debug("new portlet: " + ppid + " added to layout: "
217 + layout.getLayoutId() + "-" + layout.getPlid()
218 + " portletID: " + portlet.getPortletId());
219
220 if (portlet.getInstanceable())
221 {
222 return ppid;
223 }else{
224 return portlet.getPortletId();
225 }
226
227
228 }
229
230
231
232
233
234
235
236
237 public long addSubPage(RenderRequest request, RenderResponse response,
238 PagePortletConfiguration pagePortletConfiguration)
239 throws Exception
240 {
241 this.instanceCommonObject(request);
242
243 long parentLayoutId = layout.getLayoutId();
244
245 Layout newLayout = addLayoutLocale
246 (request, response, parentLayoutId, pagePortletConfiguration);
247
248 Properties layoutProperties = fillLayoutProperties(request,response,pagePortletConfiguration);
249
250 newLayout.setTypeSettingsProperties(layoutProperties);
251
252 LayoutServiceUtil.updateLayout(
253 newLayout.getGroupId(), newLayout.isPrivateLayout(),
254 newLayout.getLayoutId(), newLayout.getTypeSettings());
255
256 return newLayout.getLayoutId();
257 }
258
259 private String createPortletList(RenderRequest request, RenderResponse response,
260 String portletString) throws Exception
261 {
262
263 String[] array = portletString.split(",");
264
265 StringMaker resultString = new StringMaker();
266
267 for(int i=0; i<array.length; i++)
268 {
269 resultString.append(addPortlet(request, response, array[i]));
270 resultString.append(",");
271 }
272
273 if (array.length != 0)
274 {
275 return resultString.substring(0, resultString.length()-1);
276 }
277
278 return "";
279 }
280
281
282
283
284
285
286 public int deletePage(long layoutId) {
287
288 return 1;
289 }
290
291
292
293
294
295 private Properties fillLayoutProperties(RenderRequest request, RenderResponse response,
296 PagePortletConfiguration pageConfiguration) throws Exception {
297
298 Properties layoutProperties = new Properties();
299
300 if (pageConfiguration.getColumn1().length() != 0) {
301
302 layoutProperties.setProperty("column-1", createPortletList(request, response,
303 pageConfiguration.getColumn1()));
304 }
305
306 if (pageConfiguration.getColumn2().length() != 0) {
307
308 layoutProperties.setProperty("column-2", createPortletList(request, response,
309 pageConfiguration.getColumn2()));
310 }
311
312 if (pageConfiguration.getColumn3().length() != 0) {
313 layoutProperties.setProperty("column-3", createPortletList(request, response,
314 pageConfiguration.getColumn3()));
315 }
316
317 layoutProperties.setProperty("state-max-previous", "88");
318 layoutProperties.setProperty("state-max", "");
319 layoutProperties.setProperty("state-min", "");
320 layoutProperties.setProperty("layout-template-id", pageConfiguration
321 .getLayoutTemplate());
322
323 return layoutProperties;
324 }
325
326
327
328
329
330 private void instanceCommonObject(RenderRequest request) {
331 layout = (Layout) request.getAttribute(WebKeys.LAYOUT);
332
333 themeDisplay = (ThemeDisplay) request
334 .getAttribute(WebKeys.THEME_DISPLAY);
335
336 layoutTypePortlet = themeDisplay.getLayoutTypePortlet();
337
338 }
339
340
341
342
343
344
345 @SuppressWarnings("unchecked")
346 public int listLayoutChildren(Layout layout) throws SystemException,
347 PortalException {
348 List list = null;
349
350 list = layout.getChildren();
351
352 if (list != null) {
353
354 for (Iterator i = list.iterator(); i.hasNext();) {
355
356 log.debug(i.next().toString());
357 }
358
359 return list.size();
360 } else {
361 return 0;
362 }
363 }
364
365
366
367
368 public PagePortletConfiguration getPageProperties() {
369 return pageProperties;
370 }
371
372
373
374
375 public void setPageProperties(PagePortletConfiguration pageProperties) {
376 this.pageProperties = pageProperties;
377 }
378
379
380
381
382 public ILiferayUtil getLiferayUtil() {
383 return liferayUtil;
384 }
385
386
387
388
389 public void setLiferayUtil(ILiferayUtil liferayUtil) {
390 this.liferayUtil = liferayUtil;
391 }
392 }