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.exception.LayoutSetVirtualHostException;
018    import com.liferay.portal.exception.NoSuchImageException;
019    import com.liferay.portal.exception.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             * @deprecated As of 6.1.0, replaced by {@link
205             *             #updateLayoutSetPrototypeLinkEnabled(long, boolean, boolean,
206             *             String)}
207             */
208            @Deprecated
209            @Override
210            public void updateLayoutSetPrototypeLinkEnabled(
211                            long groupId, boolean privateLayout,
212                            boolean layoutSetPrototypeLinkEnabled)
213                    throws PortalException {
214    
215                    updateLayoutSetPrototypeLinkEnabled(
216                            groupId, privateLayout, layoutSetPrototypeLinkEnabled, null);
217            }
218    
219            /**
220             * Updates the state of the layout set prototype link.
221             *
222             * @param groupId the primary key of the group
223             * @param privateLayout whether the layout set is private to the group
224             * @param layoutSetPrototypeLinkEnabled whether the layout set prototype is
225             *        link enabled
226             * @param layoutSetPrototypeUuid the uuid of the layout set prototype to
227             *        link with
228             */
229            @Override
230            public void updateLayoutSetPrototypeLinkEnabled(
231                            long groupId, boolean privateLayout,
232                            boolean layoutSetPrototypeLinkEnabled,
233                            String layoutSetPrototypeUuid)
234                    throws PortalException {
235    
236                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
237                            groupId, privateLayout);
238    
239                    if (Validator.isNull(layoutSetPrototypeUuid)) {
240                            layoutSetPrototypeUuid = layoutSet.getLayoutSetPrototypeUuid();
241                    }
242    
243                    if (Validator.isNull(layoutSetPrototypeUuid)) {
244                            layoutSetPrototypeLinkEnabled = false;
245                    }
246    
247                    layoutSet.setLayoutSetPrototypeLinkEnabled(
248                            layoutSetPrototypeLinkEnabled);
249                    layoutSet.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
250    
251                    layoutSetPersistence.update(layoutSet);
252            }
253    
254            @Override
255            public LayoutSet updateLogo(
256                            long groupId, boolean privateLayout, boolean logo, byte[] bytes)
257                    throws PortalException {
258    
259                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
260                            groupId, privateLayout);
261    
262                    layoutSet.setModifiedDate(new Date());
263    
264                    PortalUtil.updateImageId(layoutSet, logo, bytes, "logoId", 0, 0, 0);
265    
266                    return layoutSetPersistence.update(layoutSet);
267            }
268    
269            @Override
270            public LayoutSet updateLogo(
271                            long groupId, boolean privateLayout, boolean logo, File file)
272                    throws PortalException {
273    
274                    byte[] bytes = null;
275    
276                    try {
277                            bytes = FileUtil.getBytes(file);
278                    }
279                    catch (IOException ioe) {
280                            throw new SystemException(ioe);
281                    }
282    
283                    return updateLogo(groupId, privateLayout, logo, bytes);
284            }
285    
286            @Override
287            public LayoutSet updateLogo(
288                            long groupId, boolean privateLayout, boolean logo, InputStream is)
289                    throws PortalException {
290    
291                    return updateLogo(groupId, privateLayout, logo, is, true);
292            }
293    
294            @Override
295            public LayoutSet updateLogo(
296                            long groupId, boolean privateLayout, boolean logo, InputStream is,
297                            boolean cleanUpStream)
298                    throws PortalException {
299    
300                    byte[] bytes = null;
301    
302                    try {
303                            bytes = FileUtil.getBytes(is, -1, cleanUpStream);
304                    }
305                    catch (IOException ioe) {
306                            throw new SystemException(ioe);
307                    }
308    
309                    return updateLogo(groupId, privateLayout, logo, bytes);
310            }
311    
312            @Override
313            public LayoutSet updateLookAndFeel(
314                            long groupId, boolean privateLayout, String themeId,
315                            String colorSchemeId, String css, boolean wapTheme)
316                    throws PortalException {
317    
318                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
319                            groupId, privateLayout);
320    
321                    layoutSet.setModifiedDate(new Date());
322    
323                    if (Validator.isNull(themeId)) {
324                            themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
325                                    layoutSet.getCompanyId());
326                    }
327    
328                    if (Validator.isNull(colorSchemeId)) {
329                            colorSchemeId =
330                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
331                    }
332    
333                    if (wapTheme) {
334                            layoutSet.setWapThemeId(themeId);
335                            layoutSet.setWapColorSchemeId(colorSchemeId);
336                    }
337                    else {
338                            layoutSet.setThemeId(themeId);
339                            layoutSet.setColorSchemeId(colorSchemeId);
340                            layoutSet.setCss(css);
341                    }
342    
343                    layoutSetPersistence.update(layoutSet);
344    
345                    if (PrefsPropsUtil.getBoolean(
346                                    PropsKeys.THEME_SYNC_ON_GROUP,
347                                    PropsValues.THEME_SYNC_ON_GROUP)) {
348    
349                            LayoutSet otherLayoutSet = layoutSetPersistence.findByG_P(
350                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout());
351    
352                            if (wapTheme) {
353                                    otherLayoutSet.setWapThemeId(themeId);
354                                    otherLayoutSet.setWapColorSchemeId(colorSchemeId);
355                            }
356                            else {
357                                    otherLayoutSet.setThemeId(themeId);
358                                    otherLayoutSet.setColorSchemeId(colorSchemeId);
359                            }
360    
361                            layoutSetPersistence.update(otherLayoutSet);
362                    }
363    
364                    return layoutSet;
365            }
366    
367            @Override
368            public void updateLookAndFeel(
369                            long groupId, String themeId, String colorSchemeId, String css,
370                            boolean wapTheme)
371                    throws PortalException {
372    
373                    updateLookAndFeel(
374                            groupId, false, themeId, colorSchemeId, css, wapTheme);
375                    updateLookAndFeel(groupId, true, themeId, colorSchemeId, css, wapTheme);
376            }
377    
378            @Override
379            public LayoutSet updatePageCount(long groupId, boolean privateLayout)
380                    throws PortalException {
381    
382                    int pageCount = layoutPersistence.countByG_P(groupId, privateLayout);
383    
384                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
385                            groupId, privateLayout);
386    
387                    layoutSet.setModifiedDate(new Date());
388                    layoutSet.setPageCount(pageCount);
389    
390                    layoutSetPersistence.update(layoutSet);
391    
392                    return layoutSet;
393            }
394    
395            @Override
396            public LayoutSet updateSettings(
397                            long groupId, boolean privateLayout, String settings)
398                    throws PortalException {
399    
400                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
401                            groupId, privateLayout);
402    
403                    layoutSet.setModifiedDate(new Date());
404                    layoutSet.setSettings(settings);
405    
406                    layoutSetPersistence.update(layoutSet);
407    
408                    return layoutSet;
409            }
410    
411            @Override
412            public LayoutSet updateVirtualHost(
413                            long groupId, boolean privateLayout, String virtualHostname)
414                    throws PortalException {
415    
416                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
417    
418                    if (Validator.isNotNull(virtualHostname) &&
419                            !Validator.isDomain(virtualHostname)) {
420    
421                            throw new LayoutSetVirtualHostException();
422                    }
423    
424                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
425                            groupId, privateLayout);
426    
427                    if (Validator.isNotNull(virtualHostname)) {
428                            VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
429                                    virtualHostname);
430    
431                            if (virtualHost == null) {
432                                    virtualHostLocalService.updateVirtualHost(
433                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId(),
434                                            virtualHostname);
435                            }
436                            else {
437                                    if ((virtualHost.getCompanyId() != layoutSet.getCompanyId()) ||
438                                            (virtualHost.getLayoutSetId() !=
439                                                    layoutSet.getLayoutSetId())) {
440    
441                                            throw new LayoutSetVirtualHostException();
442                                    }
443                            }
444                    }
445                    else {
446                            try {
447                                    virtualHostPersistence.removeByC_L(
448                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
449                            }
450                            catch (NoSuchVirtualHostException nsvhe) {
451                            }
452                    }
453    
454                    return layoutSet;
455            }
456    
457            protected LayoutSet initLayoutSet(LayoutSet layoutSet)
458                    throws PortalException {
459    
460                    Group group = layoutSet.getGroup();
461    
462                    boolean privateLayout = layoutSet.isPrivateLayout();
463    
464                    if (group.isStagingGroup()) {
465                            LayoutSet liveLayoutSet = null;
466    
467                            Group liveGroup = group.getLiveGroup();
468    
469                            if (privateLayout) {
470                                    liveLayoutSet = liveGroup.getPrivateLayoutSet();
471                            }
472                            else {
473                                    liveLayoutSet = liveGroup.getPublicLayoutSet();
474                            }
475    
476                            layoutSet.setLogoId(liveLayoutSet.getLogoId());
477    
478                            if (liveLayoutSet.isLogo()) {
479                                    Image logoImage = imageLocalService.getImage(
480                                            liveLayoutSet.getLogoId());
481    
482                                    long logoId = counterLocalService.increment();
483    
484                                    imageLocalService.updateImage(
485                                            logoId, logoImage.getTextObj(), logoImage.getType(),
486                                            logoImage.getHeight(), logoImage.getWidth(),
487                                            logoImage.getSize());
488    
489                                    layoutSet.setLogoId(logoId);
490                            }
491    
492                            layoutSet.setThemeId(liveLayoutSet.getThemeId());
493                            layoutSet.setColorSchemeId(liveLayoutSet.getColorSchemeId());
494                            layoutSet.setWapThemeId(liveLayoutSet.getWapThemeId());
495                            layoutSet.setWapColorSchemeId(liveLayoutSet.getWapColorSchemeId());
496                            layoutSet.setCss(liveLayoutSet.getCss());
497                            layoutSet.setSettings(liveLayoutSet.getSettings());
498                    }
499                    else {
500                            layoutSet.setThemeId(
501                                    ThemeFactoryUtil.getDefaultRegularThemeId(
502                                            group.getCompanyId()));
503                            layoutSet.setColorSchemeId(
504                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId());
505                            layoutSet.setWapThemeId(
506                                    ThemeFactoryUtil.getDefaultWapThemeId(group.getCompanyId()));
507                            layoutSet.setWapColorSchemeId(
508                                    ColorSchemeFactoryUtil.getDefaultWapColorSchemeId());
509                            layoutSet.setCss(StringPool.BLANK);
510                            layoutSet.setSettings(StringPool.BLANK);
511                    }
512    
513                    return layoutSet;
514            }
515    
516            private static final Log _log = LogFactoryUtil.getLog(
517                    LayoutSetLocalServiceImpl.class);
518    
519    }