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