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