001    /**
002     * Copyright (c) 2000-2012 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.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 (int i = layouts.size() - 1; i >= 0; i--) {
140                            Layout layout = layouts.get(i);
141    
142                            try {
143                                    layoutLocalService.deleteLayout(layout, false, serviceContext);
144                            }
145                            catch (NoSuchLayoutException nsle) {
146                            }
147                    }
148    
149                    // Logo
150    
151                    imageLocalService.deleteImage(layoutSet.getLogoId());
152    
153                    // Layout set
154    
155                    if (group.isOrganization() && group.isSite()) {
156                            layoutSet.setPageCount(0);
157    
158                            layoutSetPersistence.update(layoutSet, false);
159                    }
160                    else {
161                            layoutSetPersistence.removeByG_P(groupId, privateLayout);
162                    }
163    
164                    // Counter
165    
166                    counterLocalService.reset(
167                            LayoutLocalServiceImpl.getCounterName(groupId, privateLayout));
168    
169                    // Virtual host
170    
171                    try {
172                            virtualHostPersistence.removeByC_L(
173                                    layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
174                    }
175                    catch (NoSuchVirtualHostException nsvhe) {
176                    }
177            }
178    
179            public LayoutSet fetchLayoutSet(String virtualHostname)
180                    throws SystemException {
181    
182                    virtualHostname = virtualHostname.trim().toLowerCase();
183    
184                    VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
185                            virtualHostname);
186    
187                    if ((virtualHost == null) || (virtualHost.getLayoutSetId() == 0)) {
188                            return null;
189                    }
190    
191                    return layoutSetPersistence.fetchByPrimaryKey(
192                            virtualHost.getLayoutSetId());
193            }
194    
195            public LayoutSet getLayoutSet(long groupId, boolean privateLayout)
196                    throws PortalException, SystemException {
197    
198                    return layoutSetPersistence.findByG_P(groupId, privateLayout);
199            }
200    
201            public LayoutSet getLayoutSet(String virtualHostname)
202                    throws PortalException, SystemException {
203    
204                    virtualHostname = virtualHostname.trim().toLowerCase();
205    
206                    VirtualHost virtualHost = virtualHostPersistence.findByHostname(
207                            virtualHostname);
208    
209                    if (virtualHost.getLayoutSetId() == 0) {
210                            throw new LayoutSetVirtualHostException(
211                                    "Virtual host is associated with company " +
212                                            virtualHost.getCompanyId());
213                    }
214    
215                    return layoutSetPersistence.findByPrimaryKey(
216                            virtualHost.getLayoutSetId());
217            }
218    
219            public List<LayoutSet> getLayoutSetsByLayoutSetPrototypeUuid(
220                            String layoutSetPrototypeUuid)
221                    throws SystemException {
222    
223                    return layoutSetPersistence.findByLayoutSetPrototypeUuid(
224                            layoutSetPrototypeUuid);
225            }
226    
227            /**
228             * Updates the state of the layout set prototype link.
229             *
230             * <p>
231             * This method can disable the layout set prototype's link by setting
232             * <code>layoutSetPrototypeLinkEnabled</code> to <code>false</code>.
233             * However, this method can only enable the layout set prototype's link if
234             * the layout set prototype's current uuid is not <code>null</code>. Setting
235             * the <code>layoutSetPrototypeLinkEnabled</code> to <code>true</code> when
236             * the layout set prototype's current uuid is <code>null</code> will have no
237             * effect.
238             * </p>
239             *
240             * @param      groupId the primary key of the group
241             * @param      privateLayout whether the layout set is private to the group
242             * @param      layoutSetPrototypeLinkEnabled whether the layout set
243             *             prototype is link enabled
244             * @throws     PortalException if a portal exception occurred
245             * @throws     SystemException if a system exception occurred
246             * @deprecated As of 6.1, replaced by {@link
247             *             #updateLayoutSetPrototypeLinkEnabled(long, boolean, boolean,
248             *             String)}
249             */
250            public void updateLayoutSetPrototypeLinkEnabled(
251                            long groupId, boolean privateLayout,
252                            boolean layoutSetPrototypeLinkEnabled)
253                    throws PortalException, SystemException {
254    
255                    updateLayoutSetPrototypeLinkEnabled(
256                            groupId, privateLayout, layoutSetPrototypeLinkEnabled, null);
257            }
258    
259            /**
260             * Updates the state of the layout set prototype link.
261             *
262             * @param  groupId the primary key of the group
263             * @param  privateLayout whether the layout set is private to the group
264             * @param  layoutSetPrototypeLinkEnabled whether the layout set prototype is
265             *         link enabled
266             * @param  layoutSetPrototypeUuid the uuid of the layout set prototype to
267             *         link with
268             * @throws PortalException if a portal exception occurred
269             * @throws SystemException if a system exception occurred
270             */
271            public void updateLayoutSetPrototypeLinkEnabled(
272                            long groupId, boolean privateLayout,
273                            boolean layoutSetPrototypeLinkEnabled,
274                            String layoutSetPrototypeUuid)
275                    throws PortalException, SystemException {
276    
277                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
278                            groupId, privateLayout);
279    
280                    if (Validator.isNull(layoutSetPrototypeUuid)) {
281                            layoutSetPrototypeUuid = layoutSet.getLayoutSetPrototypeUuid();
282                    }
283    
284                    if (Validator.isNull(layoutSetPrototypeUuid)) {
285                            layoutSetPrototypeLinkEnabled = false;
286                    }
287    
288                    layoutSet.setLayoutSetPrototypeLinkEnabled(
289                            layoutSetPrototypeLinkEnabled);
290                    layoutSet.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
291    
292                    layoutSetPersistence.update(layoutSet, false);
293            }
294    
295            public LayoutSet updateLogo(
296                            long groupId, boolean privateLayout, boolean logo, File file)
297                    throws PortalException, SystemException {
298    
299                    InputStream is = null;
300    
301                    if (logo) {
302                            try {
303                                    is = new FileInputStream(file);
304                            }
305                            catch (IOException ioe) {
306                                    throw new SystemException(ioe);
307                            }
308                    }
309    
310                    return updateLogo(groupId, privateLayout, logo, is);
311            }
312    
313            public LayoutSet updateLogo(
314                            long groupId, boolean privateLayout, boolean logo, InputStream is)
315                    throws PortalException, SystemException {
316    
317                    return updateLogo(groupId, privateLayout, logo, is, true);
318            }
319    
320            public LayoutSet updateLogo(
321                            long groupId, boolean privateLayout, boolean logo, InputStream is,
322                            boolean cleanUpStream)
323                    throws PortalException, SystemException {
324    
325                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
326                            groupId, privateLayout);
327    
328                    layoutSet.setModifiedDate(new Date());
329                    layoutSet.setLogo(logo);
330    
331                    if (logo) {
332                            long logoId = layoutSet.getLogoId();
333    
334                            if (logoId <= 0) {
335                                    logoId = counterLocalService.increment();
336    
337                                    layoutSet.setLogoId(logoId);
338                            }
339                    }
340                    else {
341                            layoutSet.setLogoId(0);
342                    }
343    
344                    if (logo) {
345                            imageLocalService.updateImage(
346                                    layoutSet.getLogoId(), is, cleanUpStream);
347                    }
348                    else {
349                            imageLocalService.deleteImage(layoutSet.getLogoId());
350                    }
351    
352                    return layoutSetPersistence.update(layoutSet, false);
353            }
354    
355            public LayoutSet updateLookAndFeel(
356                            long groupId, boolean privateLayout, String themeId,
357                            String colorSchemeId, String css, boolean wapTheme)
358                    throws PortalException, SystemException {
359    
360                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
361                            groupId, privateLayout);
362    
363                    layoutSet.setModifiedDate(new Date());
364    
365                    if (Validator.isNull(themeId)) {
366                            themeId = ThemeImpl.getDefaultRegularThemeId(
367                                    layoutSet.getCompanyId());
368                    }
369    
370                    if (Validator.isNull(colorSchemeId)) {
371                            colorSchemeId = ColorSchemeImpl.getDefaultRegularColorSchemeId();
372                    }
373    
374                    if (wapTheme) {
375                            layoutSet.setWapThemeId(themeId);
376                            layoutSet.setWapColorSchemeId(colorSchemeId);
377                    }
378                    else {
379                            layoutSet.setThemeId(themeId);
380                            layoutSet.setColorSchemeId(colorSchemeId);
381                            layoutSet.setCss(css);
382                    }
383    
384                    layoutSetPersistence.update(layoutSet, false);
385    
386                    if (PrefsPropsUtil.getBoolean(
387                                    PropsKeys.THEME_SYNC_ON_GROUP,
388                                    PropsValues.THEME_SYNC_ON_GROUP)) {
389    
390                            LayoutSet otherLayoutSet = layoutSetPersistence.findByG_P(
391                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout());
392    
393                            if (wapTheme) {
394                                    otherLayoutSet.setWapThemeId(themeId);
395                                    otherLayoutSet.setWapColorSchemeId(colorSchemeId);
396                            }
397                            else {
398                                    otherLayoutSet.setThemeId(themeId);
399                                    otherLayoutSet.setColorSchemeId(colorSchemeId);
400                            }
401    
402                            layoutSetPersistence.update(otherLayoutSet, false);
403                    }
404    
405                    return layoutSet;
406            }
407    
408            public void updateLookAndFeel(
409                            long groupId, String themeId, String colorSchemeId, String css,
410                            boolean wapTheme)
411                    throws PortalException, SystemException {
412    
413                    updateLookAndFeel(
414                            groupId, false, themeId, colorSchemeId, css, wapTheme);
415                    updateLookAndFeel(groupId, true, themeId, colorSchemeId, css, wapTheme);
416            }
417    
418            public LayoutSet updatePageCount(long groupId, boolean privateLayout)
419                    throws PortalException, SystemException {
420    
421                    int pageCount = layoutPersistence.countByG_P(groupId, privateLayout);
422    
423                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
424                            groupId, privateLayout);
425    
426                    layoutSet.setModifiedDate(new Date());
427                    layoutSet.setPageCount(pageCount);
428    
429                    layoutSetPersistence.update(layoutSet, false);
430    
431                    return layoutSet;
432            }
433    
434            public LayoutSet updateSettings(
435                            long groupId, boolean privateLayout, String settings)
436                    throws PortalException, SystemException {
437    
438                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
439                            groupId, privateLayout);
440    
441                    layoutSet.setModifiedDate(new Date());
442                    layoutSet.setSettings(settings);
443    
444                    layoutSetPersistence.update(layoutSet, false);
445    
446                    return layoutSet;
447            }
448    
449            public LayoutSet updateVirtualHost(
450                            long groupId, boolean privateLayout, String virtualHostname)
451                    throws PortalException, SystemException {
452    
453                    virtualHostname = virtualHostname.trim().toLowerCase();
454    
455                    if (virtualHostname.startsWith(Http.HTTP_WITH_SLASH) ||
456                            virtualHostname.startsWith(Http.HTTPS_WITH_SLASH)) {
457    
458                            throw new LayoutSetVirtualHostException();
459                    }
460    
461                    LayoutSet layoutSet = layoutSetPersistence.findByG_P(
462                            groupId, privateLayout);
463    
464                    if (Validator.isNotNull(virtualHostname)) {
465                            try {
466                                    VirtualHost virtualHost = virtualHostPersistence.findByHostname(
467                                            virtualHostname);
468    
469                                    if ((virtualHost.getCompanyId() != layoutSet.getCompanyId()) ||
470                                            (virtualHost.getLayoutSetId() !=
471                                                    layoutSet.getLayoutSetId())) {
472    
473                                            throw new LayoutSetVirtualHostException();
474                                    }
475                            }
476                            catch (NoSuchVirtualHostException nsvhe) {
477                                    virtualHostLocalService.updateVirtualHost(
478                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId(),
479                                            virtualHostname);
480                            }
481                    }
482                    else {
483                            try {
484                                    virtualHostPersistence.removeByC_L(
485                                            layoutSet.getCompanyId(), layoutSet.getLayoutSetId());
486                            }
487                            catch (NoSuchVirtualHostException nsvhe) {
488                            }
489                    }
490    
491                    return layoutSet;
492            }
493    
494    }