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