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 fetchLayoutSetByLogoId(boolean privateLayout, long logoId)
160                    throws SystemException {
161    
162                    return layoutSetPersistence.fetchByP_L(privateLayout, logoId);
163            }
164    
165            @Override
166            public LayoutSet getLayoutSet(long groupId, boolean privateLayout)
167                    throws PortalException, SystemException {
168    
169                    return layoutSetPersistence.findByG_P(groupId, privateLayout);
170            }
171    
172            @Override
173            public LayoutSet getLayoutSet(String virtualHostname)
174                    throws PortalException, SystemException {
175    
176                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
177    
178                    VirtualHost virtualHost = virtualHostPersistence.findByHostname(
179                            virtualHostname);
180    
181                    if (virtualHost.getLayoutSetId() == 0) {
182                            throw new LayoutSetVirtualHostException(
183                                    "Virtual host is associated with company " +
184                                            virtualHost.getCompanyId());
185                    }
186    
187                    return layoutSetPersistence.findByPrimaryKey(
188                            virtualHost.getLayoutSetId());
189            }
190    
191            @Override
192            public List<LayoutSet> getLayoutSetsByLayoutSetPrototypeUuid(
193                            String layoutSetPrototypeUuid)
194                    throws SystemException {
195    
196                    return layoutSetPersistence.findByLayoutSetPrototypeUuid(
197                            layoutSetPrototypeUuid);
198            }
199    
200            /**
201             * Updates the state of the layout set prototype link.
202             *
203             * <p>
204             * This method can disable the layout set prototype's link by setting
205             * <code>layoutSetPrototypeLinkEnabled</code> to <code>false</code>.
206             * However, this method can only enable the layout set prototype's link if
207             * the layout set prototype's current uuid is not <code>null</code>. Setting
208             * the <code>layoutSetPrototypeLinkEnabled</code> to <code>true</code> when
209             * the layout set prototype's current uuid is <code>null</code> will have no
210             * effect.
211             * </p>
212             *
213             * @param      groupId the primary key of the group
214             * @param      privateLayout whether the layout set is private to the group
215             * @param      layoutSetPrototypeLinkEnabled whether the layout set
216             *             prototype is link enabled
217             * @throws     PortalException if a portal exception occurred
218             * @throws     SystemException if a system exception occurred
219             * @deprecated As of 6.1.0, replaced by {@link
220             *             #updateLayoutSetPrototypeLinkEnabled(long, boolean, boolean,
221             *             String)}
222             */
223            @Override
224            public void updateLayoutSetPrototypeLinkEnabled(
225                            long groupId, boolean privateLayout,
226                            boolean layoutSetPrototypeLinkEnabled)
227                    throws PortalException, SystemException {
228    
229                    updateLayoutSetPrototypeLinkEnabled(
230                            groupId, privateLayout, layoutSetPrototypeLinkEnabled, null);
231            }
232    
233            /**
234             * Updates the state of the layout set prototype link.
235             *
236             * @param  groupId the primary key of the group
237             * @param  privateLayout whether the layout set is private to the group
238             * @param  layoutSetPrototypeLinkEnabled whether the layout set prototype is
239             *         link enabled
240             * @param  layoutSetPrototypeUuid the uuid of the layout set prototype to
241             *         link with
242             * @throws PortalException if a portal exception occurred
243             * @throws SystemException if a system exception occurred
244             */
245            @Override
246            public void updateLayoutSetPrototypeLinkEnabled(
247                            long groupId, boolean privateLayout,
248                            boolean layoutSetPrototypeLinkEnabled,
249                            String layoutSetPrototypeUuid)
250                    throws PortalException, SystemException {
251    
252                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
253                            groupId, privateLayout);
254    
255                    LayoutSetBranch layoutSetBranch = _getLayoutSetBranch(layoutSet);
256    
257                    if (layoutSetBranch == null) {
258                            if (Validator.isNull(layoutSetPrototypeUuid)) {
259                                    layoutSetPrototypeUuid = layoutSet.getLayoutSetPrototypeUuid();
260                            }
261    
262                            if (Validator.isNull(layoutSetPrototypeUuid)) {
263                                    layoutSetPrototypeLinkEnabled = false;
264                            }
265    
266                            layoutSet.setLayoutSetPrototypeLinkEnabled(
267                                    layoutSetPrototypeLinkEnabled);
268                            layoutSet.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
269    
270                            layoutSetPersistence.update(layoutSet);
271    
272                            return;
273                    }
274    
275                    if (Validator.isNull(layoutSetPrototypeUuid)) {
276                            layoutSetPrototypeUuid =
277                                    layoutSetBranch.getLayoutSetPrototypeUuid();
278                    }
279    
280                    if (Validator.isNull(layoutSetPrototypeUuid) &&
281                            layoutSetPrototypeLinkEnabled) {
282    
283                            throw new IllegalStateException(
284                                    "Cannot set layoutSetPrototypeLinkEnabled to true when " +
285                                            "layoutSetPrototypeUuid is null");
286                    }
287    
288                    layoutSetBranch.setLayoutSetPrototypeLinkEnabled(
289                            layoutSetPrototypeLinkEnabled);
290                    layoutSetBranch.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
291    
292                    layoutSetBranchPersistence.update(layoutSetBranch);
293            }
294    
295            @Override
296            public LayoutSet updateLogo(
297                            long groupId, boolean privateLayout, boolean logo, byte[] bytes)
298                    throws PortalException, SystemException {
299    
300                    InputStream is = null;
301    
302                    if (logo) {
303                            is = new ByteArrayInputStream(bytes);
304                    }
305    
306                    return updateLogo(groupId, privateLayout, logo, is);
307            }
308    
309            @Override
310            public LayoutSet updateLogo(
311                            long groupId, boolean privateLayout, boolean logo, File file)
312                    throws PortalException, SystemException {
313    
314                    InputStream is = null;
315    
316                    if (logo) {
317                            try {
318                                    is = new FileInputStream(file);
319                            }
320                            catch (IOException ioe) {
321                                    throw new SystemException(ioe);
322                            }
323                    }
324    
325                    return updateLogo(groupId, privateLayout, logo, is);
326            }
327    
328            @Override
329            public LayoutSet updateLogo(
330                            long groupId, boolean privateLayout, boolean logo, InputStream is)
331                    throws PortalException, SystemException {
332    
333                    return updateLogo(groupId, privateLayout, logo, is, true);
334            }
335    
336            @Override
337            public LayoutSet updateLogo(
338                            long groupId, boolean privateLayout, boolean logo, InputStream is,
339                            boolean cleanUpStream)
340                    throws PortalException, SystemException {
341    
342                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
343                            groupId, privateLayout);
344    
345                    LayoutSetBranch layoutSetBranch = _getLayoutSetBranch(layoutSet);
346    
347                    if (layoutSetBranch == null) {
348                            layoutSet.setLogo(logo);
349                            layoutSet.setModifiedDate(new Date());
350    
351                            if (logo) {
352                                    long logoId = layoutSet.getLogoId();
353    
354                                    if (logoId <= 0) {
355                                            logoId = counterLocalService.increment();
356    
357                                            layoutSet.setLogoId(logoId);
358                                    }
359                            }
360                            else {
361                                    layoutSet.setLogoId(0);
362                            }
363    
364                            if (logo) {
365                                    imageLocalService.updateImage(
366                                            layoutSet.getLogoId(), is, cleanUpStream);
367                            }
368                            else {
369                                    imageLocalService.deleteImage(layoutSet.getLogoId());
370                            }
371    
372                            return layoutSetPersistence.update(layoutSet);
373                    }
374    
375                    layoutSetBranch.setLogo(logo);
376                    layoutSetBranch.setModifiedDate(new Date());
377    
378                    if (logo) {
379                            long logoId = layoutSetBranch.getLogoId();
380    
381                            if (logoId <= 0) {
382                                    logoId = counterLocalService.increment();
383    
384                                    layoutSetBranch.setLogoId(logoId);
385                            }
386                    }
387                    else {
388                            layoutSetBranch.setLogoId(0);
389                    }
390    
391                    if (logo) {
392                            imageLocalService.updateImage(
393                                    layoutSetBranch.getLogoId(), is, cleanUpStream);
394                    }
395                    else {
396                            imageLocalService.deleteImage(layoutSet.getLogoId());
397                    }
398    
399                    layoutSetBranchPersistence.update(layoutSetBranch);
400    
401                    return layoutSet;
402            }
403    
404            @Override
405            public LayoutSet updateLookAndFeel(
406                            long groupId, boolean privateLayout, String themeId,
407                            String colorSchemeId, String css, boolean wapTheme)
408                    throws PortalException, SystemException {
409    
410                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
411                            groupId, privateLayout);
412    
413                    if (Validator.isNull(themeId)) {
414                            themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
415                                    layoutSet.getCompanyId());
416                    }
417    
418                    if (Validator.isNull(colorSchemeId)) {
419                            colorSchemeId =
420                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
421                    }
422    
423                    LayoutSetBranch layoutSetBranch = _getLayoutSetBranch(layoutSet);
424    
425                    if (layoutSetBranch == null) {
426                            layoutSet.setModifiedDate(new Date());
427    
428                            if (wapTheme) {
429                                    layoutSet.setWapThemeId(themeId);
430                                    layoutSet.setWapColorSchemeId(colorSchemeId);
431                            }
432                            else {
433                                    layoutSet.setThemeId(themeId);
434                                    layoutSet.setColorSchemeId(colorSchemeId);
435                                    layoutSet.setCss(css);
436                            }
437    
438                            layoutSetPersistence.update(layoutSet);
439    
440                            if (PrefsPropsUtil.getBoolean(
441                                            PropsKeys.THEME_SYNC_ON_GROUP,
442                                            PropsValues.THEME_SYNC_ON_GROUP)) {
443    
444                                    LayoutSet otherLayoutSet = layoutSetPersistence.findByG_P(
445                                            layoutSet.getGroupId(), layoutSet.isPrivateLayout());
446    
447                                    otherLayoutSet.setThemeId(themeId);
448                                    otherLayoutSet.setColorSchemeId(colorSchemeId);
449    
450                                    layoutSetPersistence.update(otherLayoutSet);
451                            }
452    
453                            return layoutSet;
454                    }
455    
456                    if (wapTheme) {
457                            layoutSetBranch.setWapColorSchemeId(colorSchemeId);
458                            layoutSetBranch.setWapThemeId(themeId);
459                    }
460                    else {
461                            layoutSetBranch.setColorSchemeId(colorSchemeId);
462                            layoutSetBranch.setCss(css);
463                            layoutSetBranch.setThemeId(themeId);
464                    }
465    
466                    layoutSetBranch.setModifiedDate(new Date());
467    
468                    layoutSetBranchPersistence.update(layoutSetBranch);
469    
470                    return layoutSet;
471            }
472    
473            @Override
474            public void updateLookAndFeel(
475                            long groupId, String themeId, String colorSchemeId, String css,
476                            boolean wapTheme)
477                    throws PortalException, SystemException {
478    
479                    updateLookAndFeel(
480                            groupId, false, themeId, colorSchemeId, css, wapTheme);
481                    updateLookAndFeel(groupId, true, themeId, colorSchemeId, css, wapTheme);
482            }
483    
484            @Override
485            public LayoutSet updatePageCount(long groupId, boolean privateLayout)
486                    throws PortalException, SystemException {
487    
488                    int pageCount = layoutPersistence.countByG_P(groupId, privateLayout);
489    
490                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
491                            groupId, privateLayout);
492    
493                    layoutSet.setModifiedDate(new Date());
494                    layoutSet.setPageCount(pageCount);
495    
496                    layoutSetPersistence.update(layoutSet);
497    
498                    return layoutSet;
499            }
500    
501            @Override
502            public LayoutSet updateSettings(
503                            long groupId, boolean privateLayout, String settings)
504                    throws PortalException, SystemException {
505    
506                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
507                            groupId, privateLayout);
508    
509                    LayoutSetBranch layoutSetBranch = _getLayoutSetBranch(layoutSet);
510    
511                    if (layoutSetBranch == null) {
512                            layoutSet.setModifiedDate(new Date());
513                            layoutSet.setSettings(settings);
514    
515                            layoutSetPersistence.update(layoutSet);
516    
517                            return layoutSet;
518                    }
519    
520                    layoutSetBranch.setModifiedDate(new Date());
521                    layoutSetBranch.setSettings(settings);
522    
523                    layoutSetBranchPersistence.update(layoutSetBranch);
524    
525                    return layoutSet;
526            }
527    
528            @Override
529            public LayoutSet updateVirtualHost(
530                            long groupId, boolean privateLayout, String virtualHostname)
531                    throws PortalException, SystemException {
532    
533                    virtualHostname = StringUtil.toLowerCase(virtualHostname.trim());
534    
535                    if (Validator.isNotNull(virtualHostname) &&
536                            !Validator.isDomain(virtualHostname)) {
537    
538                            throw new LayoutSetVirtualHostException();
539                    }
540    
541                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
542                            groupId, privateLayout);
543    
544                    if (Validator.isNotNull(virtualHostname)) {
545                            VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
546                                    virtualHostname);
547    
548                            if (virtualHost == null) {
549                                    virtualHostLocalService.updateVirtualHost(
550                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId(),
551                                            virtualHostname);
552                            }
553                            else {
554                                    if ((virtualHost.getCompanyId() != layoutSet.getCompanyId()) ||
555                                            (virtualHost.getLayoutSetId() !=
556                                                    layoutSet.getLayoutSetId())) {
557    
558                                            throw new LayoutSetVirtualHostException();
559                                    }
560                            }
561                    }
562                    else {
563                            try {
564                                    virtualHostPersistence.removeByC_L(
565                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
566                            }
567                            catch (NoSuchVirtualHostException nsvhe) {
568                            }
569                    }
570    
571                    return layoutSet;
572            }
573    
574            protected LayoutSet initLayoutSet(LayoutSet layoutSet)
575                    throws PortalException, SystemException {
576    
577                    Group group = layoutSet.getGroup();
578    
579                    boolean privateLayout = layoutSet.isPrivateLayout();
580    
581                    if (group.isStagingGroup()) {
582                            LayoutSet liveLayoutSet = null;
583    
584                            Group liveGroup = group.getLiveGroup();
585    
586                            if (privateLayout) {
587                                    liveLayoutSet = liveGroup.getPrivateLayoutSet();
588                            }
589                            else {
590                                    liveLayoutSet = liveGroup.getPublicLayoutSet();
591                            }
592    
593                            layoutSet.setLogo(liveLayoutSet.getLogo());
594                            layoutSet.setLogoId(liveLayoutSet.getLogoId());
595    
596                            if (liveLayoutSet.isLogo()) {
597                                    Image logoImage = imageLocalService.getImage(
598                                            liveLayoutSet.getLogoId());
599    
600                                    long logoId = counterLocalService.increment();
601    
602                                    imageLocalService.updateImage(
603                                            logoId, logoImage.getTextObj(), logoImage.getType(),
604                                            logoImage.getHeight(), logoImage.getWidth(),
605                                            logoImage.getSize());
606    
607                                    layoutSet.setLogoId(logoId);
608                            }
609    
610                            layoutSet.setThemeId(liveLayoutSet.getThemeId());
611                            layoutSet.setColorSchemeId(liveLayoutSet.getColorSchemeId());
612                            layoutSet.setWapThemeId(liveLayoutSet.getWapThemeId());
613                            layoutSet.setWapColorSchemeId(liveLayoutSet.getWapColorSchemeId());
614                            layoutSet.setCss(liveLayoutSet.getCss());
615                            layoutSet.setSettings(liveLayoutSet.getSettings());
616                    }
617                    else {
618                            layoutSet.setThemeId(
619                                    ThemeFactoryUtil.getDefaultRegularThemeId(
620                                            group.getCompanyId()));
621                            layoutSet.setColorSchemeId(
622                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId());
623                            layoutSet.setWapThemeId(
624                                    ThemeFactoryUtil.getDefaultWapThemeId(group.getCompanyId()));
625                            layoutSet.setWapColorSchemeId(
626                                    ColorSchemeFactoryUtil.getDefaultWapColorSchemeId());
627                            layoutSet.setCss(StringPool.BLANK);
628                            layoutSet.setSettings(StringPool.BLANK);
629                    }
630    
631                    return layoutSet;
632            }
633    
634            private LayoutSetBranch _getLayoutSetBranch(LayoutSet layoutSet)
635                    throws PortalException, SystemException {
636    
637                    LayoutSetStagingHandler layoutSetStagingHandler =
638                            LayoutStagingUtil.getLayoutSetStagingHandler(layoutSet);
639    
640                    if (layoutSetStagingHandler != null) {
641                            return layoutSetStagingHandler.getLayoutSetBranch();
642                    }
643    
644                    if (LayoutStagingUtil.isBranchingLayoutSet(
645                                    layoutSet.getGroup(), layoutSet.getPrivateLayout())) {
646    
647                            layoutSetStagingHandler = new LayoutSetStagingHandler(layoutSet);
648    
649                            return layoutSetStagingHandler.getLayoutSetBranch();
650                    }
651    
652                    return null;
653            }
654    
655            private static Log _log = LogFactoryUtil.getLog(
656                    LayoutSetLocalServiceImpl.class);
657    
658    }