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