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