001    /**
002     * Copyright (c) 2000-present 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.exception.LayoutSetVirtualHostException;
018    import com.liferay.portal.exception.NoSuchImageException;
019    import com.liferay.portal.exception.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.FileUtil;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
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.LayoutSet;
034    import com.liferay.portal.model.VirtualHost;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.service.base.LayoutSetLocalServiceBaseImpl;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.PrefsPropsUtil;
039    import com.liferay.portal.util.PropsValues;
040    
041    import java.io.File;
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 {
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 {
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                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
141    
142                    VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
143                            virtualHostname);
144    
145                    if ((virtualHost == null) || (virtualHost.getLayoutSetId() == 0)) {
146                            return null;
147                    }
148    
149                    return layoutSetPersistence.fetchByPrimaryKey(
150                            virtualHost.getLayoutSetId());
151            }
152    
153            @Override
154            public LayoutSet getLayoutSet(long groupId, boolean privateLayout)
155                    throws PortalException {
156    
157                    return layoutSetPersistence.findByG_P(groupId, privateLayout);
158            }
159    
160            @Override
161            public LayoutSet getLayoutSet(String virtualHostname)
162                    throws PortalException {
163    
164                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
165    
166                    VirtualHost virtualHost = virtualHostPersistence.findByHostname(
167                            virtualHostname);
168    
169                    if (virtualHost.getLayoutSetId() == 0) {
170                            throw new LayoutSetVirtualHostException(
171                                    "Virtual host is associated with company " +
172                                            virtualHost.getCompanyId());
173                    }
174    
175                    return layoutSetPersistence.findByPrimaryKey(
176                            virtualHost.getLayoutSetId());
177            }
178    
179            @Override
180            public List<LayoutSet> getLayoutSetsByLayoutSetPrototypeUuid(
181                    String layoutSetPrototypeUuid) {
182    
183                    return layoutSetPersistence.findByLayoutSetPrototypeUuid(
184                            layoutSetPrototypeUuid);
185            }
186    
187            /**
188             * Updates the state of the layout set prototype link.
189             *
190             * @param groupId the primary key of the group
191             * @param privateLayout whether the layout set is private to the group
192             * @param layoutSetPrototypeLinkEnabled whether the layout set prototype is
193             *        link enabled
194             * @param layoutSetPrototypeUuid the uuid of the layout set prototype to
195             *        link with
196             */
197            @Override
198            public void updateLayoutSetPrototypeLinkEnabled(
199                            long groupId, boolean privateLayout,
200                            boolean layoutSetPrototypeLinkEnabled,
201                            String layoutSetPrototypeUuid)
202                    throws PortalException {
203    
204                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
205                            groupId, privateLayout);
206    
207                    if (Validator.isNull(layoutSetPrototypeUuid)) {
208                            layoutSetPrototypeUuid = layoutSet.getLayoutSetPrototypeUuid();
209                    }
210    
211                    if (Validator.isNull(layoutSetPrototypeUuid)) {
212                            layoutSetPrototypeLinkEnabled = false;
213                    }
214    
215                    layoutSet.setLayoutSetPrototypeLinkEnabled(
216                            layoutSetPrototypeLinkEnabled);
217                    layoutSet.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
218    
219                    layoutSetPersistence.update(layoutSet);
220            }
221    
222            @Override
223            public LayoutSet updateLogo(
224                            long groupId, boolean privateLayout, boolean logo, byte[] bytes)
225                    throws PortalException {
226    
227                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
228                            groupId, privateLayout);
229    
230                    layoutSet.setModifiedDate(new Date());
231    
232                    PortalUtil.updateImageId(layoutSet, logo, bytes, "logoId", 0, 0, 0);
233    
234                    return layoutSetPersistence.update(layoutSet);
235            }
236    
237            @Override
238            public LayoutSet updateLogo(
239                            long groupId, boolean privateLayout, boolean logo, File file)
240                    throws PortalException {
241    
242                    byte[] bytes = null;
243    
244                    try {
245                            bytes = FileUtil.getBytes(file);
246                    }
247                    catch (IOException ioe) {
248                            throw new SystemException(ioe);
249                    }
250    
251                    return updateLogo(groupId, privateLayout, logo, bytes);
252            }
253    
254            @Override
255            public LayoutSet updateLogo(
256                            long groupId, boolean privateLayout, boolean logo, InputStream is)
257                    throws PortalException {
258    
259                    return updateLogo(groupId, privateLayout, logo, is, true);
260            }
261    
262            @Override
263            public LayoutSet updateLogo(
264                            long groupId, boolean privateLayout, boolean logo, InputStream is,
265                            boolean cleanUpStream)
266                    throws PortalException {
267    
268                    byte[] bytes = null;
269    
270                    try {
271                            bytes = FileUtil.getBytes(is, -1, cleanUpStream);
272                    }
273                    catch (IOException ioe) {
274                            throw new SystemException(ioe);
275                    }
276    
277                    return updateLogo(groupId, privateLayout, logo, bytes);
278            }
279    
280            @Override
281            public LayoutSet updateLookAndFeel(
282                            long groupId, boolean privateLayout, String themeId,
283                            String colorSchemeId, String css, boolean wapTheme)
284                    throws PortalException {
285    
286                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
287                            groupId, privateLayout);
288    
289                    layoutSet.setModifiedDate(new Date());
290    
291                    if (Validator.isNull(themeId)) {
292                            themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
293                                    layoutSet.getCompanyId());
294                    }
295    
296                    if (Validator.isNull(colorSchemeId)) {
297                            colorSchemeId =
298                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
299                    }
300    
301                    if (wapTheme) {
302                            layoutSet.setWapThemeId(themeId);
303                            layoutSet.setWapColorSchemeId(colorSchemeId);
304                    }
305                    else {
306                            layoutSet.setThemeId(themeId);
307                            layoutSet.setColorSchemeId(colorSchemeId);
308                            layoutSet.setCss(css);
309                    }
310    
311                    layoutSetPersistence.update(layoutSet);
312    
313                    if (PrefsPropsUtil.getBoolean(
314                                    PropsKeys.THEME_SYNC_ON_GROUP,
315                                    PropsValues.THEME_SYNC_ON_GROUP)) {
316    
317                            LayoutSet otherLayoutSet = layoutSetPersistence.findByG_P(
318                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout());
319    
320                            if (wapTheme) {
321                                    otherLayoutSet.setWapThemeId(themeId);
322                                    otherLayoutSet.setWapColorSchemeId(colorSchemeId);
323                            }
324                            else {
325                                    otherLayoutSet.setThemeId(themeId);
326                                    otherLayoutSet.setColorSchemeId(colorSchemeId);
327                            }
328    
329                            layoutSetPersistence.update(otherLayoutSet);
330                    }
331    
332                    return layoutSet;
333            }
334    
335            @Override
336            public void updateLookAndFeel(
337                            long groupId, String themeId, String colorSchemeId, String css,
338                            boolean wapTheme)
339                    throws PortalException {
340    
341                    updateLookAndFeel(
342                            groupId, false, themeId, colorSchemeId, css, wapTheme);
343                    updateLookAndFeel(groupId, true, themeId, colorSchemeId, css, wapTheme);
344            }
345    
346            @Override
347            public LayoutSet updatePageCount(long groupId, boolean privateLayout)
348                    throws PortalException {
349    
350                    int pageCount = layoutPersistence.countByG_P(groupId, privateLayout);
351    
352                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
353                            groupId, privateLayout);
354    
355                    layoutSet.setModifiedDate(new Date());
356                    layoutSet.setPageCount(pageCount);
357    
358                    layoutSetPersistence.update(layoutSet);
359    
360                    return layoutSet;
361            }
362    
363            @Override
364            public LayoutSet updateSettings(
365                            long groupId, boolean privateLayout, String settings)
366                    throws PortalException {
367    
368                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
369                            groupId, privateLayout);
370    
371                    layoutSet.setModifiedDate(new Date());
372                    layoutSet.setSettings(settings);
373    
374                    layoutSetPersistence.update(layoutSet);
375    
376                    return layoutSet;
377            }
378    
379            @Override
380            public LayoutSet updateVirtualHost(
381                            long groupId, boolean privateLayout, String virtualHostname)
382                    throws PortalException {
383    
384                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
385    
386                    if (Validator.isNotNull(virtualHostname) &&
387                            !Validator.isDomain(virtualHostname)) {
388    
389                            throw new LayoutSetVirtualHostException();
390                    }
391    
392                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
393                            groupId, privateLayout);
394    
395                    if (Validator.isNotNull(virtualHostname)) {
396                            VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
397                                    virtualHostname);
398    
399                            if (virtualHost == null) {
400                                    virtualHostLocalService.updateVirtualHost(
401                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId(),
402                                            virtualHostname);
403                            }
404                            else {
405                                    if ((virtualHost.getCompanyId() != layoutSet.getCompanyId()) ||
406                                            (virtualHost.getLayoutSetId() !=
407                                                    layoutSet.getLayoutSetId())) {
408    
409                                            throw new LayoutSetVirtualHostException();
410                                    }
411                            }
412                    }
413                    else {
414                            try {
415                                    virtualHostPersistence.removeByC_L(
416                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
417                            }
418                            catch (NoSuchVirtualHostException nsvhe) {
419                            }
420                    }
421    
422                    return layoutSet;
423            }
424    
425            protected LayoutSet initLayoutSet(LayoutSet layoutSet)
426                    throws PortalException {
427    
428                    Group group = layoutSet.getGroup();
429    
430                    boolean privateLayout = layoutSet.isPrivateLayout();
431    
432                    if (group.isStagingGroup()) {
433                            LayoutSet liveLayoutSet = null;
434    
435                            Group liveGroup = group.getLiveGroup();
436    
437                            if (privateLayout) {
438                                    liveLayoutSet = liveGroup.getPrivateLayoutSet();
439                            }
440                            else {
441                                    liveLayoutSet = liveGroup.getPublicLayoutSet();
442                            }
443    
444                            layoutSet.setLogoId(liveLayoutSet.getLogoId());
445    
446                            if (liveLayoutSet.isLogo()) {
447                                    Image logoImage = imageLocalService.getImage(
448                                            liveLayoutSet.getLogoId());
449    
450                                    long logoId = counterLocalService.increment();
451    
452                                    imageLocalService.updateImage(
453                                            logoId, logoImage.getTextObj(), logoImage.getType(),
454                                            logoImage.getHeight(), logoImage.getWidth(),
455                                            logoImage.getSize());
456    
457                                    layoutSet.setLogoId(logoId);
458                            }
459    
460                            layoutSet.setThemeId(liveLayoutSet.getThemeId());
461                            layoutSet.setColorSchemeId(liveLayoutSet.getColorSchemeId());
462                            layoutSet.setWapThemeId(liveLayoutSet.getWapThemeId());
463                            layoutSet.setWapColorSchemeId(liveLayoutSet.getWapColorSchemeId());
464                            layoutSet.setCss(liveLayoutSet.getCss());
465                            layoutSet.setSettings(liveLayoutSet.getSettings());
466                    }
467                    else {
468                            layoutSet.setThemeId(
469                                    ThemeFactoryUtil.getDefaultRegularThemeId(
470                                            group.getCompanyId()));
471                            layoutSet.setColorSchemeId(
472                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId());
473                            layoutSet.setWapThemeId(
474                                    ThemeFactoryUtil.getDefaultWapThemeId(group.getCompanyId()));
475                            layoutSet.setWapColorSchemeId(
476                                    ColorSchemeFactoryUtil.getDefaultWapColorSchemeId());
477                            layoutSet.setCss(StringPool.BLANK);
478                            layoutSet.setSettings(StringPool.BLANK);
479                    }
480    
481                    return layoutSet;
482            }
483    
484            private static final Log _log = LogFactoryUtil.getLog(
485                    LayoutSetLocalServiceImpl.class);
486    
487    }