001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.LayoutSetVirtualHostException;
018    import com.liferay.portal.NoSuchImageException;
019    import com.liferay.portal.NoSuchVirtualHostException;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.ColorSchemeFactoryUtil;
025    import com.liferay.portal.kernel.util.FileUtil;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.ThemeFactoryUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.Image;
033    import com.liferay.portal.model.LayoutSet;
034    import com.liferay.portal.model.VirtualHost;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.service.base.LayoutSetLocalServiceBaseImpl;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.PrefsPropsUtil;
039    import com.liferay.portal.util.PropsValues;
040    
041    import java.io.File;
042    import java.io.IOException;
043    import java.io.InputStream;
044    
045    import java.util.Date;
046    import java.util.List;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Julio Camarero
051     * @author Ganesh Ram
052     */
053    public class LayoutSetLocalServiceImpl extends LayoutSetLocalServiceBaseImpl {
054    
055            @Override
056            public LayoutSet addLayoutSet(long groupId, boolean privateLayout)
057                    throws PortalException {
058    
059                    Group group = groupPersistence.findByPrimaryKey(groupId);
060    
061                    Date now = new Date();
062    
063                    long layoutSetId = counterLocalService.increment();
064    
065                    LayoutSet layoutSet = layoutSetPersistence.create(layoutSetId);
066    
067                    layoutSet.setGroupId(groupId);
068                    layoutSet.setCompanyId(group.getCompanyId());
069                    layoutSet.setCreateDate(now);
070                    layoutSet.setModifiedDate(now);
071                    layoutSet.setPrivateLayout(privateLayout);
072    
073                    layoutSet = initLayoutSet(layoutSet);
074    
075                    layoutSetPersistence.update(layoutSet);
076    
077                    return layoutSet;
078            }
079    
080            @Override
081            public void deleteLayoutSet(
082                            long groupId, boolean privateLayout, ServiceContext serviceContext)
083                    throws PortalException {
084    
085                    Group group = groupPersistence.findByPrimaryKey(groupId);
086    
087                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
088                            groupId, privateLayout);
089    
090                    // Layouts
091    
092                    serviceContext.setAttribute("updatePageCount", Boolean.FALSE);
093    
094                    layoutLocalService.deleteLayouts(
095                            groupId, privateLayout, serviceContext);
096    
097                    // Logo
098    
099                    if (group.isStagingGroup() || !group.isOrganization() ||
100                            !group.isSite()) {
101    
102                            try {
103                                    imageLocalService.deleteImage(layoutSet.getLogoId());
104                            }
105                            catch (NoSuchImageException nsie) {
106                                    if (_log.isWarnEnabled()) {
107                                            _log.warn(
108                                                    "Unable to delete image " + layoutSet.getLogoId());
109                                    }
110                            }
111                    }
112    
113                    // Layout set
114    
115                    if (!group.isStagingGroup() && group.isOrganization() &&
116                            group.isSite()) {
117    
118                            layoutSet = initLayoutSet(layoutSet);
119    
120                            layoutSet.setLogoId(layoutSet.getLogoId());
121    
122                            layoutSetPersistence.update(layoutSet);
123                    }
124                    else {
125                            layoutSetPersistence.removeByG_P(groupId, privateLayout);
126                    }
127    
128                    // Virtual host
129    
130                    try {
131                            virtualHostPersistence.removeByC_L(
132                                    layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
133                    }
134                    catch (NoSuchVirtualHostException nsvhe) {
135                    }
136            }
137    
138            @Override
139            public LayoutSet fetchLayoutSet(String virtualHostname) {
140                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
141    
142                    VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
143                            virtualHostname);
144    
145                    if ((virtualHost == null) || (virtualHost.getLayoutSetId() == 0)) {
146                            return null;
147                    }
148    
149                    return layoutSetPersistence.fetchByPrimaryKey(
150                            virtualHost.getLayoutSetId());
151            }
152    
153            @Override
154            public LayoutSet getLayoutSet(long groupId, boolean privateLayout)
155                    throws PortalException {
156    
157                    return layoutSetPersistence.findByG_P(groupId, privateLayout);
158            }
159    
160            @Override
161            public LayoutSet getLayoutSet(String virtualHostname)
162                    throws PortalException {
163    
164                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
165    
166                    VirtualHost virtualHost = virtualHostPersistence.findByHostname(
167                            virtualHostname);
168    
169                    if (virtualHost.getLayoutSetId() == 0) {
170                            throw new LayoutSetVirtualHostException(
171                                    "Virtual host is associated with company " +
172                                            virtualHost.getCompanyId());
173                    }
174    
175                    return layoutSetPersistence.findByPrimaryKey(
176                            virtualHost.getLayoutSetId());
177            }
178    
179            @Override
180            public List<LayoutSet> getLayoutSetsByLayoutSetPrototypeUuid(
181                    String layoutSetPrototypeUuid) {
182    
183                    return layoutSetPersistence.findByLayoutSetPrototypeUuid(
184                            layoutSetPrototypeUuid);
185            }
186    
187            /**
188             * Updates the state of the layout set prototype link.
189             *
190             * <p>
191             * This method can disable the layout set prototype's link by setting
192             * <code>layoutSetPrototypeLinkEnabled</code> to <code>false</code>.
193             * However, this method can only enable the layout set prototype's link if
194             * the layout set prototype's current uuid is not <code>null</code>. Setting
195             * the <code>layoutSetPrototypeLinkEnabled</code> to <code>true</code> when
196             * the layout set prototype's current uuid is <code>null</code> will have no
197             * effect.
198             * </p>
199             *
200             * @param      groupId the primary key of the group
201             * @param      privateLayout whether the layout set is private to the group
202             * @param      layoutSetPrototypeLinkEnabled whether the layout set
203             *             prototype is link enabled
204             * @throws     PortalException if a portal exception occurred
205             * @deprecated As of 6.1.0, replaced by {@link
206             *             #updateLayoutSetPrototypeLinkEnabled(long, boolean, boolean,
207             *             String)}
208             */
209            @Deprecated
210            @Override
211            public void updateLayoutSetPrototypeLinkEnabled(
212                            long groupId, boolean privateLayout,
213                            boolean layoutSetPrototypeLinkEnabled)
214                    throws PortalException {
215    
216                    updateLayoutSetPrototypeLinkEnabled(
217                            groupId, privateLayout, layoutSetPrototypeLinkEnabled, null);
218            }
219    
220            /**
221             * Updates the state of the layout set prototype link.
222             *
223             * @param  groupId the primary key of the group
224             * @param  privateLayout whether the layout set is private to the group
225             * @param  layoutSetPrototypeLinkEnabled whether the layout set prototype is
226             *         link enabled
227             * @param  layoutSetPrototypeUuid the uuid of the layout set prototype to
228             *         link with
229             * @throws PortalException if a portal exception occurred
230             */
231            @Override
232            public void updateLayoutSetPrototypeLinkEnabled(
233                            long groupId, boolean privateLayout,
234                            boolean layoutSetPrototypeLinkEnabled,
235                            String layoutSetPrototypeUuid)
236                    throws PortalException {
237    
238                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
239                            groupId, privateLayout);
240    
241                    if (Validator.isNull(layoutSetPrototypeUuid)) {
242                            layoutSetPrototypeUuid = layoutSet.getLayoutSetPrototypeUuid();
243                    }
244    
245                    if (Validator.isNull(layoutSetPrototypeUuid)) {
246                            layoutSetPrototypeLinkEnabled = false;
247                    }
248    
249                    layoutSet.setLayoutSetPrototypeLinkEnabled(
250                            layoutSetPrototypeLinkEnabled);
251                    layoutSet.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
252    
253                    layoutSetPersistence.update(layoutSet);
254            }
255    
256            @Override
257            public LayoutSet updateLogo(
258                            long groupId, boolean privateLayout, boolean logo, byte[] bytes)
259                    throws PortalException {
260    
261                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
262                            groupId, privateLayout);
263    
264                    layoutSet.setModifiedDate(new Date());
265    
266                    PortalUtil.updateImageId(layoutSet, logo, bytes, "logoId", 0, 0, 0);
267    
268                    return layoutSetPersistence.update(layoutSet);
269            }
270    
271            @Override
272            public LayoutSet updateLogo(
273                            long groupId, boolean privateLayout, boolean logo, File file)
274                    throws PortalException {
275    
276                    byte[] bytes = null;
277    
278                    try {
279                            bytes = FileUtil.getBytes(file);
280                    }
281                    catch (IOException ioe) {
282                            throw new SystemException(ioe);
283                    }
284    
285                    return updateLogo(groupId, privateLayout, logo, bytes);
286            }
287    
288            @Override
289            public LayoutSet updateLogo(
290                            long groupId, boolean privateLayout, boolean logo, InputStream is)
291                    throws PortalException {
292    
293                    return updateLogo(groupId, privateLayout, logo, is, true);
294            }
295    
296            @Override
297            public LayoutSet updateLogo(
298                            long groupId, boolean privateLayout, boolean logo, InputStream is,
299                            boolean cleanUpStream)
300                    throws PortalException {
301    
302                    byte[] bytes = null;
303    
304                    try {
305                            bytes = FileUtil.getBytes(is, -1, cleanUpStream);
306                    }
307                    catch (IOException ioe) {
308                            throw new SystemException(ioe);
309                    }
310    
311                    return updateLogo(groupId, privateLayout, logo, bytes);
312            }
313    
314            @Override
315            public LayoutSet updateLookAndFeel(
316                            long groupId, boolean privateLayout, String themeId,
317                            String colorSchemeId, String css, boolean wapTheme)
318                    throws PortalException {
319    
320                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
321                            groupId, privateLayout);
322    
323                    layoutSet.setModifiedDate(new Date());
324    
325                    if (Validator.isNull(themeId)) {
326                            themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
327                                    layoutSet.getCompanyId());
328                    }
329    
330                    if (Validator.isNull(colorSchemeId)) {
331                            colorSchemeId =
332                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
333                    }
334    
335                    if (wapTheme) {
336                            layoutSet.setWapThemeId(themeId);
337                            layoutSet.setWapColorSchemeId(colorSchemeId);
338                    }
339                    else {
340                            layoutSet.setThemeId(themeId);
341                            layoutSet.setColorSchemeId(colorSchemeId);
342                            layoutSet.setCss(css);
343                    }
344    
345                    layoutSetPersistence.update(layoutSet);
346    
347                    if (PrefsPropsUtil.getBoolean(
348                                    PropsKeys.THEME_SYNC_ON_GROUP,
349                                    PropsValues.THEME_SYNC_ON_GROUP)) {
350    
351                            LayoutSet otherLayoutSet = layoutSetPersistence.findByG_P(
352                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout());
353    
354                            if (wapTheme) {
355                                    otherLayoutSet.setWapThemeId(themeId);
356                                    otherLayoutSet.setWapColorSchemeId(colorSchemeId);
357                            }
358                            else {
359                                    otherLayoutSet.setThemeId(themeId);
360                                    otherLayoutSet.setColorSchemeId(colorSchemeId);
361                            }
362    
363                            layoutSetPersistence.update(otherLayoutSet);
364                    }
365    
366                    return layoutSet;
367            }
368    
369            @Override
370            public void updateLookAndFeel(
371                            long groupId, String themeId, String colorSchemeId, String css,
372                            boolean wapTheme)
373                    throws PortalException {
374    
375                    updateLookAndFeel(
376                            groupId, false, themeId, colorSchemeId, css, wapTheme);
377                    updateLookAndFeel(groupId, true, themeId, colorSchemeId, css, wapTheme);
378            }
379    
380            @Override
381            public LayoutSet updatePageCount(long groupId, boolean privateLayout)
382                    throws PortalException {
383    
384                    int pageCount = layoutPersistence.countByG_P(groupId, privateLayout);
385    
386                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
387                            groupId, privateLayout);
388    
389                    layoutSet.setModifiedDate(new Date());
390                    layoutSet.setPageCount(pageCount);
391    
392                    layoutSetPersistence.update(layoutSet);
393    
394                    return layoutSet;
395            }
396    
397            @Override
398            public LayoutSet updateSettings(
399                            long groupId, boolean privateLayout, String settings)
400                    throws PortalException {
401    
402                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
403                            groupId, privateLayout);
404    
405                    layoutSet.setModifiedDate(new Date());
406                    layoutSet.setSettings(settings);
407    
408                    layoutSetPersistence.update(layoutSet);
409    
410                    return layoutSet;
411            }
412    
413            @Override
414            public LayoutSet updateVirtualHost(
415                            long groupId, boolean privateLayout, String virtualHostname)
416                    throws PortalException {
417    
418                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
419    
420                    if (Validator.isNotNull(virtualHostname) &&
421                            !Validator.isDomain(virtualHostname)) {
422    
423                            throw new LayoutSetVirtualHostException();
424                    }
425    
426                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
427                            groupId, privateLayout);
428    
429                    if (Validator.isNotNull(virtualHostname)) {
430                            VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
431                                    virtualHostname);
432    
433                            if (virtualHost == null) {
434                                    virtualHostLocalService.updateVirtualHost(
435                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId(),
436                                            virtualHostname);
437                            }
438                            else {
439                                    if ((virtualHost.getCompanyId() != layoutSet.getCompanyId()) ||
440                                            (virtualHost.getLayoutSetId() !=
441                                                    layoutSet.getLayoutSetId())) {
442    
443                                            throw new LayoutSetVirtualHostException();
444                                    }
445                            }
446                    }
447                    else {
448                            try {
449                                    virtualHostPersistence.removeByC_L(
450                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
451                            }
452                            catch (NoSuchVirtualHostException nsvhe) {
453                            }
454                    }
455    
456                    return layoutSet;
457            }
458    
459            protected LayoutSet initLayoutSet(LayoutSet layoutSet)
460                    throws PortalException {
461    
462                    Group group = layoutSet.getGroup();
463    
464                    boolean privateLayout = layoutSet.isPrivateLayout();
465    
466                    if (group.isStagingGroup()) {
467                            LayoutSet liveLayoutSet = null;
468    
469                            Group liveGroup = group.getLiveGroup();
470    
471                            if (privateLayout) {
472                                    liveLayoutSet = liveGroup.getPrivateLayoutSet();
473                            }
474                            else {
475                                    liveLayoutSet = liveGroup.getPublicLayoutSet();
476                            }
477    
478                            layoutSet.setLogoId(liveLayoutSet.getLogoId());
479    
480                            if (liveLayoutSet.isLogo()) {
481                                    Image logoImage = imageLocalService.getImage(
482                                            liveLayoutSet.getLogoId());
483    
484                                    long logoId = counterLocalService.increment();
485    
486                                    imageLocalService.updateImage(
487                                            logoId, logoImage.getTextObj(), logoImage.getType(),
488                                            logoImage.getHeight(), logoImage.getWidth(),
489                                            logoImage.getSize());
490    
491                                    layoutSet.setLogoId(logoId);
492                            }
493    
494                            layoutSet.setThemeId(liveLayoutSet.getThemeId());
495                            layoutSet.setColorSchemeId(liveLayoutSet.getColorSchemeId());
496                            layoutSet.setWapThemeId(liveLayoutSet.getWapThemeId());
497                            layoutSet.setWapColorSchemeId(liveLayoutSet.getWapColorSchemeId());
498                            layoutSet.setCss(liveLayoutSet.getCss());
499                            layoutSet.setSettings(liveLayoutSet.getSettings());
500                    }
501                    else {
502                            layoutSet.setThemeId(
503                                    ThemeFactoryUtil.getDefaultRegularThemeId(
504                                            group.getCompanyId()));
505                            layoutSet.setColorSchemeId(
506                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId());
507                            layoutSet.setWapThemeId(
508                                    ThemeFactoryUtil.getDefaultWapThemeId(group.getCompanyId()));
509                            layoutSet.setWapColorSchemeId(
510                                    ColorSchemeFactoryUtil.getDefaultWapColorSchemeId());
511                            layoutSet.setCss(StringPool.BLANK);
512                            layoutSet.setSettings(StringPool.BLANK);
513                    }
514    
515                    return layoutSet;
516            }
517    
518            private static final Log _log = LogFactoryUtil.getLog(
519                    LayoutSetLocalServiceImpl.class);
520    
521    }