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