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