001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.staging.LayoutStagingUtil;
025    import com.liferay.portal.kernel.util.ColorSchemeFactoryUtil;
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.LayoutSetBranch;
035    import com.liferay.portal.model.LayoutSetStagingHandler;
036    import com.liferay.portal.model.VirtualHost;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portal.service.base.LayoutSetLocalServiceBaseImpl;
039    import com.liferay.portal.util.PrefsPropsUtil;
040    import com.liferay.portal.util.PropsValues;
041    
042    import java.io.ByteArrayInputStream;
043    import java.io.File;
044    import java.io.FileInputStream;
045    import java.io.IOException;
046    import java.io.InputStream;
047    
048    import java.util.Date;
049    import java.util.List;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     * @author Julio Camarero
054     * @author Ganesh Ram
055     */
056    public class LayoutSetLocalServiceImpl extends LayoutSetLocalServiceBaseImpl {
057    
058            @Override
059            public LayoutSet addLayoutSet(long groupId, boolean privateLayout)
060                    throws PortalException, SystemException {
061    
062                    Group group = groupPersistence.findByPrimaryKey(groupId);
063    
064                    Date now = new Date();
065    
066                    long layoutSetId = counterLocalService.increment();
067    
068                    LayoutSet layoutSet = layoutSetPersistence.create(layoutSetId);
069    
070                    layoutSet.setGroupId(groupId);
071                    layoutSet.setCompanyId(group.getCompanyId());
072                    layoutSet.setCreateDate(now);
073                    layoutSet.setModifiedDate(now);
074                    layoutSet.setPrivateLayout(privateLayout);
075    
076                    layoutSet = initLayoutSet(layoutSet);
077    
078                    layoutSetPersistence.update(layoutSet);
079    
080                    return layoutSet;
081            }
082    
083            @Override
084            public void deleteLayoutSet(
085                            long groupId, boolean privateLayout, ServiceContext serviceContext)
086                    throws PortalException, SystemException {
087    
088                    Group group = groupPersistence.findByPrimaryKey(groupId);
089    
090                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
091                            groupId, privateLayout);
092    
093                    // Layouts
094    
095                    serviceContext.setAttribute("updatePageCount", Boolean.FALSE);
096    
097                    layoutLocalService.deleteLayouts(
098                            groupId, privateLayout, serviceContext);
099    
100                    // Logo
101    
102                    if (group.isStagingGroup() || !group.isOrganization() ||
103                            !group.isSite()) {
104    
105                            try {
106                                    imageLocalService.deleteImage(layoutSet.getLogoId());
107                            }
108                            catch (NoSuchImageException nsie) {
109                                    if (_log.isWarnEnabled()) {
110                                            _log.warn(
111                                                    "Unable to delete image " + layoutSet.getLogoId());
112                                    }
113                            }
114                    }
115    
116                    // Layout set
117    
118                    if (!group.isStagingGroup() && group.isOrganization() &&
119                            group.isSite()) {
120    
121                            layoutSet = initLayoutSet(layoutSet);
122    
123                            layoutSet.setLogoId(layoutSet.getLogoId());
124    
125                            layoutSetPersistence.update(layoutSet);
126                    }
127                    else {
128                            layoutSetPersistence.removeByG_P(groupId, privateLayout);
129                    }
130    
131                    // Virtual host
132    
133                    try {
134                            virtualHostPersistence.removeByC_L(
135                                    layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
136                    }
137                    catch (NoSuchVirtualHostException nsvhe) {
138                    }
139            }
140    
141            @Override
142            public LayoutSet fetchLayoutSet(String virtualHostname)
143                    throws SystemException {
144    
145                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
146    
147                    VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
148                            virtualHostname);
149    
150                    if ((virtualHost == null) || (virtualHost.getLayoutSetId() == 0)) {
151                            return null;
152                    }
153    
154                    return layoutSetPersistence.fetchByPrimaryKey(
155                            virtualHost.getLayoutSetId());
156            }
157    
158            @Override
159            public LayoutSet getLayoutSet(long groupId, boolean privateLayout)
160                    throws PortalException, SystemException {
161    
162                    return layoutSetPersistence.findByG_P(groupId, privateLayout);
163            }
164    
165            @Override
166            public LayoutSet getLayoutSet(String virtualHostname)
167                    throws PortalException, SystemException {
168    
169                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
170    
171                    VirtualHost virtualHost = virtualHostPersistence.findByHostname(
172                            virtualHostname);
173    
174                    if (virtualHost.getLayoutSetId() == 0) {
175                            throw new LayoutSetVirtualHostException(
176                                    "Virtual host is associated with company " +
177                                            virtualHost.getCompanyId());
178                    }
179    
180                    return layoutSetPersistence.findByPrimaryKey(
181                            virtualHost.getLayoutSetId());
182            }
183    
184            @Override
185            public List<LayoutSet> getLayoutSetsByLayoutSetPrototypeUuid(
186                            String layoutSetPrototypeUuid)
187                    throws SystemException {
188    
189                    return layoutSetPersistence.findByLayoutSetPrototypeUuid(
190                            layoutSetPrototypeUuid);
191            }
192    
193            /**
194             * Updates the state of the layout set prototype link.
195             *
196             * <p>
197             * This method can disable the layout set prototype's link by setting
198             * <code>layoutSetPrototypeLinkEnabled</code> to <code>false</code>.
199             * However, this method can only enable the layout set prototype's link if
200             * the layout set prototype's current uuid is not <code>null</code>. Setting
201             * the <code>layoutSetPrototypeLinkEnabled</code> to <code>true</code> when
202             * the layout set prototype's current uuid is <code>null</code> will have no
203             * effect.
204             * </p>
205             *
206             * @param      groupId the primary key of the group
207             * @param      privateLayout whether the layout set is private to the group
208             * @param      layoutSetPrototypeLinkEnabled whether the layout set
209             *             prototype is link enabled
210             * @throws     PortalException if a portal exception occurred
211             * @throws     SystemException if a system exception occurred
212             * @deprecated As of 6.1.0, replaced by {@link
213             *             #updateLayoutSetPrototypeLinkEnabled(long, boolean, boolean,
214             *             String)}
215             */
216            @Override
217            public void updateLayoutSetPrototypeLinkEnabled(
218                            long groupId, boolean privateLayout,
219                            boolean layoutSetPrototypeLinkEnabled)
220                    throws PortalException, SystemException {
221    
222                    updateLayoutSetPrototypeLinkEnabled(
223                            groupId, privateLayout, layoutSetPrototypeLinkEnabled, null);
224            }
225    
226            /**
227             * Updates the state of the layout set prototype link.
228             *
229             * @param  groupId the primary key of the group
230             * @param  privateLayout whether the layout set is private to the group
231             * @param  layoutSetPrototypeLinkEnabled whether the layout set prototype is
232             *         link enabled
233             * @param  layoutSetPrototypeUuid the uuid of the layout set prototype to
234             *         link with
235             * @throws PortalException if a portal exception occurred
236             * @throws SystemException if a system exception occurred
237             */
238            @Override
239            public void updateLayoutSetPrototypeLinkEnabled(
240                            long groupId, boolean privateLayout,
241                            boolean layoutSetPrototypeLinkEnabled,
242                            String layoutSetPrototypeUuid)
243                    throws PortalException, SystemException {
244    
245                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
246                            groupId, privateLayout);
247    
248                    LayoutSetBranch layoutSetBranch = _getLayoutSetBranch(layoutSet);
249    
250                    if (layoutSetBranch == null) {
251                            if (Validator.isNull(layoutSetPrototypeUuid)) {
252                                    layoutSetPrototypeUuid = layoutSet.getLayoutSetPrototypeUuid();
253                            }
254    
255                            if (Validator.isNull(layoutSetPrototypeUuid)) {
256                                    layoutSetPrototypeLinkEnabled = false;
257                            }
258    
259                            layoutSet.setLayoutSetPrototypeLinkEnabled(
260                                    layoutSetPrototypeLinkEnabled);
261                            layoutSet.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
262    
263                            layoutSetPersistence.update(layoutSet);
264    
265                            return;
266                    }
267    
268                    if (Validator.isNull(layoutSetPrototypeUuid)) {
269                            layoutSetPrototypeUuid =
270                                    layoutSetBranch.getLayoutSetPrototypeUuid();
271                    }
272    
273                    if (Validator.isNull(layoutSetPrototypeUuid) &&
274                            layoutSetPrototypeLinkEnabled) {
275    
276                            throw new IllegalStateException(
277                                    "Cannot set layoutSetPrototypeLinkEnabled to true when " +
278                                            "layoutSetPrototypeUuid is null");
279                    }
280    
281                    layoutSetBranch.setLayoutSetPrototypeLinkEnabled(
282                            layoutSetPrototypeLinkEnabled);
283                    layoutSetBranch.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
284    
285                    layoutSetBranchPersistence.update(layoutSetBranch);
286            }
287    
288            @Override
289            public LayoutSet updateLogo(
290                            long groupId, boolean privateLayout, boolean logo, byte[] bytes)
291                    throws PortalException, SystemException {
292    
293                    InputStream is = null;
294    
295                    if (logo) {
296                            is = new ByteArrayInputStream(bytes);
297                    }
298    
299                    return updateLogo(groupId, privateLayout, logo, is);
300            }
301    
302            @Override
303            public LayoutSet updateLogo(
304                            long groupId, boolean privateLayout, boolean logo, File file)
305                    throws PortalException, SystemException {
306    
307                    InputStream is = null;
308    
309                    if (logo) {
310                            try {
311                                    is = new FileInputStream(file);
312                            }
313                            catch (IOException ioe) {
314                                    throw new SystemException(ioe);
315                            }
316                    }
317    
318                    return updateLogo(groupId, privateLayout, logo, is);
319            }
320    
321            @Override
322            public LayoutSet updateLogo(
323                            long groupId, boolean privateLayout, boolean logo, InputStream is)
324                    throws PortalException, SystemException {
325    
326                    return updateLogo(groupId, privateLayout, logo, is, true);
327            }
328    
329            @Override
330            public LayoutSet updateLogo(
331                            long groupId, boolean privateLayout, boolean logo, InputStream is,
332                            boolean cleanUpStream)
333                    throws PortalException, SystemException {
334    
335                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
336                            groupId, privateLayout);
337    
338                    LayoutSetBranch layoutSetBranch = _getLayoutSetBranch(layoutSet);
339    
340                    if (layoutSetBranch == null) {
341                            layoutSet.setLogo(logo);
342                            layoutSet.setModifiedDate(new Date());
343    
344                            if (logo) {
345                                    long logoId = layoutSet.getLogoId();
346    
347                                    if (logoId <= 0) {
348                                            logoId = counterLocalService.increment();
349    
350                                            layoutSet.setLogoId(logoId);
351                                    }
352                            }
353                            else {
354                                    layoutSet.setLogoId(0);
355                            }
356    
357                            if (logo) {
358                                    imageLocalService.updateImage(
359                                            layoutSet.getLogoId(), is, cleanUpStream);
360                            }
361                            else {
362                                    imageLocalService.deleteImage(layoutSet.getLogoId());
363                            }
364    
365                            return layoutSetPersistence.update(layoutSet);
366                    }
367    
368                    layoutSetBranch.setLogo(logo);
369                    layoutSetBranch.setModifiedDate(new Date());
370    
371                    if (logo) {
372                            long logoId = layoutSetBranch.getLogoId();
373    
374                            if (logoId <= 0) {
375                                    logoId = counterLocalService.increment();
376    
377                                    layoutSetBranch.setLogoId(logoId);
378                            }
379                    }
380                    else {
381                            layoutSetBranch.setLogoId(0);
382                    }
383    
384                    if (logo) {
385                            imageLocalService.updateImage(
386                                    layoutSetBranch.getLogoId(), is, cleanUpStream);
387                    }
388                    else {
389                            imageLocalService.deleteImage(layoutSet.getLogoId());
390                    }
391    
392                    layoutSetBranchPersistence.update(layoutSetBranch);
393    
394                    return layoutSet;
395            }
396    
397            @Override
398            public LayoutSet updateLookAndFeel(
399                            long groupId, boolean privateLayout, String themeId,
400                            String colorSchemeId, String css, boolean wapTheme)
401                    throws PortalException, SystemException {
402    
403                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
404                            groupId, privateLayout);
405    
406                    if (Validator.isNull(themeId)) {
407                            themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
408                                    layoutSet.getCompanyId());
409                    }
410    
411                    if (Validator.isNull(colorSchemeId)) {
412                            colorSchemeId =
413                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
414                    }
415    
416                    LayoutSetBranch layoutSetBranch = _getLayoutSetBranch(layoutSet);
417    
418                    if (layoutSetBranch == null) {
419                            layoutSet.setColorSchemeId(colorSchemeId);
420                            layoutSet.setCss(css);
421                            layoutSet.setModifiedDate(new Date());
422                            layoutSet.setThemeId(themeId);
423    
424                            layoutSetPersistence.update(layoutSet);
425    
426                            if (PrefsPropsUtil.getBoolean(
427                                            PropsKeys.THEME_SYNC_ON_GROUP,
428                                            PropsValues.THEME_SYNC_ON_GROUP)) {
429    
430                                    LayoutSet otherLayoutSet = layoutSetPersistence.findByG_P(
431                                            layoutSet.getGroupId(), layoutSet.isPrivateLayout());
432    
433                                    otherLayoutSet.setThemeId(themeId);
434                                    otherLayoutSet.setColorSchemeId(colorSchemeId);
435    
436                                    layoutSetPersistence.update(otherLayoutSet);
437                            }
438    
439                            return layoutSet;
440                    }
441    
442                    if (wapTheme) {
443                            layoutSetBranch.setWapColorSchemeId(colorSchemeId);
444                            layoutSetBranch.setWapThemeId(themeId);
445                    }
446                    else {
447                            layoutSetBranch.setColorSchemeId(colorSchemeId);
448                            layoutSetBranch.setCss(css);
449                            layoutSetBranch.setThemeId(themeId);
450                    }
451    
452                    layoutSetBranch.setModifiedDate(new Date());
453    
454                    layoutSetBranchPersistence.update(layoutSetBranch);
455    
456                    return layoutSet;
457            }
458    
459            @Override
460            public void updateLookAndFeel(
461                            long groupId, String themeId, String colorSchemeId, String css,
462                            boolean wapTheme)
463                    throws PortalException, SystemException {
464    
465                    updateLookAndFeel(
466                            groupId, false, themeId, colorSchemeId, css, wapTheme);
467                    updateLookAndFeel(groupId, true, themeId, colorSchemeId, css, wapTheme);
468            }
469    
470            @Override
471            public LayoutSet updatePageCount(long groupId, boolean privateLayout)
472                    throws PortalException, SystemException {
473    
474                    int pageCount = layoutPersistence.countByG_P(groupId, privateLayout);
475    
476                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
477                            groupId, privateLayout);
478    
479                    layoutSet.setModifiedDate(new Date());
480                    layoutSet.setPageCount(pageCount);
481    
482                    layoutSetPersistence.update(layoutSet);
483    
484                    return layoutSet;
485            }
486    
487            @Override
488            public LayoutSet updateSettings(
489                            long groupId, boolean privateLayout, String settings)
490                    throws PortalException, SystemException {
491    
492                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
493                            groupId, privateLayout);
494    
495                    LayoutSetBranch layoutSetBranch = _getLayoutSetBranch(layoutSet);
496    
497                    if (layoutSetBranch == null) {
498                            layoutSet.setModifiedDate(new Date());
499                            layoutSet.setSettings(settings);
500    
501                            layoutSetPersistence.update(layoutSet);
502    
503                            return layoutSet;
504                    }
505    
506                    layoutSetBranch.setModifiedDate(new Date());
507                    layoutSetBranch.setSettings(settings);
508    
509                    layoutSetBranchPersistence.update(layoutSetBranch);
510    
511                    return layoutSet;
512            }
513    
514            @Override
515            public LayoutSet updateVirtualHost(
516                            long groupId, boolean privateLayout, String virtualHostname)
517                    throws PortalException, SystemException {
518    
519                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
520    
521                    if (Validator.isNotNull(virtualHostname) &&
522                            !Validator.isDomain(virtualHostname)) {
523    
524                            throw new LayoutSetVirtualHostException();
525                    }
526    
527                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
528                            groupId, privateLayout);
529    
530                    if (Validator.isNotNull(virtualHostname)) {
531                            VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
532                                    virtualHostname);
533    
534                            if (virtualHost == null) {
535                                    virtualHostLocalService.updateVirtualHost(
536                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId(),
537                                            virtualHostname);
538                            }
539                            else {
540                                    if ((virtualHost.getCompanyId() != layoutSet.getCompanyId()) ||
541                                            (virtualHost.getLayoutSetId() !=
542                                                    layoutSet.getLayoutSetId())) {
543    
544                                            throw new LayoutSetVirtualHostException();
545                                    }
546                            }
547                    }
548                    else {
549                            try {
550                                    virtualHostPersistence.removeByC_L(
551                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
552                            }
553                            catch (NoSuchVirtualHostException nsvhe) {
554                            }
555                    }
556    
557                    return layoutSet;
558            }
559    
560            protected LayoutSet initLayoutSet(LayoutSet layoutSet)
561                    throws PortalException, SystemException {
562    
563                    Group group = layoutSet.getGroup();
564    
565                    boolean privateLayout = layoutSet.isPrivateLayout();
566    
567                    if (group.isStagingGroup()) {
568                            LayoutSet liveLayoutSet = null;
569    
570                            Group liveGroup = group.getLiveGroup();
571    
572                            if (privateLayout) {
573                                    liveLayoutSet = liveGroup.getPrivateLayoutSet();
574                            }
575                            else {
576                                    liveLayoutSet = liveGroup.getPublicLayoutSet();
577                            }
578    
579                            layoutSet.setLogo(liveLayoutSet.getLogo());
580                            layoutSet.setLogoId(liveLayoutSet.getLogoId());
581    
582                            if (liveLayoutSet.isLogo()) {
583                                    Image logoImage = imageLocalService.getImage(
584                                            liveLayoutSet.getLogoId());
585    
586                                    long logoId = counterLocalService.increment();
587    
588                                    imageLocalService.updateImage(
589                                            logoId, logoImage.getTextObj(), logoImage.getType(),
590                                            logoImage.getHeight(), logoImage.getWidth(),
591                                            logoImage.getSize());
592    
593                                    layoutSet.setLogoId(logoId);
594                            }
595    
596                            layoutSet.setThemeId(liveLayoutSet.getThemeId());
597                            layoutSet.setColorSchemeId(liveLayoutSet.getColorSchemeId());
598                            layoutSet.setWapThemeId(liveLayoutSet.getWapThemeId());
599                            layoutSet.setWapColorSchemeId(liveLayoutSet.getWapColorSchemeId());
600                            layoutSet.setCss(liveLayoutSet.getCss());
601                            layoutSet.setSettings(liveLayoutSet.getSettings());
602                    }
603                    else {
604                            layoutSet.setThemeId(
605                                    ThemeFactoryUtil.getDefaultRegularThemeId(
606                                            group.getCompanyId()));
607                            layoutSet.setColorSchemeId(
608                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId());
609                            layoutSet.setWapThemeId(
610                                    ThemeFactoryUtil.getDefaultWapThemeId(group.getCompanyId()));
611                            layoutSet.setWapColorSchemeId(
612                                    ColorSchemeFactoryUtil.getDefaultWapColorSchemeId());
613                            layoutSet.setCss(StringPool.BLANK);
614                            layoutSet.setSettings(StringPool.BLANK);
615                    }
616    
617                    return layoutSet;
618            }
619    
620            private LayoutSetBranch _getLayoutSetBranch(LayoutSet layoutSet)
621                    throws PortalException, SystemException {
622    
623                    LayoutSetStagingHandler layoutSetStagingHandler =
624                            LayoutStagingUtil.getLayoutSetStagingHandler(layoutSet);
625    
626                    if (layoutSetStagingHandler != null) {
627                            return layoutSetStagingHandler.getLayoutSetBranch();
628                    }
629    
630                    if (LayoutStagingUtil.isBranchingLayoutSet(
631                                    layoutSet.getGroup(), layoutSet.getPrivateLayout())) {
632    
633                            layoutSetStagingHandler = new LayoutSetStagingHandler(layoutSet);
634    
635                            return layoutSetStagingHandler.getLayoutSetBranch();
636                    }
637    
638                    return null;
639            }
640    
641            private static Log _log = LogFactoryUtil.getLog(
642                    LayoutSetLocalServiceImpl.class);
643    
644    }