001    /**
002     * Copyright (c) 2000-present 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.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.model.Company;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.model.LayoutSet;
022    import com.liferay.portal.model.VirtualHost;
023    import com.liferay.portal.service.base.VirtualHostLocalServiceBaseImpl;
024    import com.liferay.portal.util.PropsValues;
025    
026    /**
027     * @author Alexander Chow
028     */
029    public class VirtualHostLocalServiceImpl
030            extends VirtualHostLocalServiceBaseImpl {
031    
032            @Override
033            public VirtualHost fetchVirtualHost(long companyId, long layoutSetId) {
034                    return virtualHostPersistence.fetchByC_L(companyId, layoutSetId);
035            }
036    
037            @Override
038            public VirtualHost fetchVirtualHost(String hostname) {
039                    return virtualHostPersistence.fetchByHostname(hostname);
040            }
041    
042            @Override
043            public VirtualHost getVirtualHost(long companyId, long layoutSetId)
044                    throws PortalException {
045    
046                    return virtualHostPersistence.findByC_L(companyId, layoutSetId);
047            }
048    
049            @Override
050            public VirtualHost getVirtualHost(String hostname) throws PortalException {
051                    return virtualHostPersistence.findByHostname(hostname);
052            }
053    
054            @Override
055            public VirtualHost updateVirtualHost(
056                    long companyId, long layoutSetId, String hostname) {
057    
058                    VirtualHost virtualHost = virtualHostPersistence.fetchByC_L(
059                            companyId, layoutSetId);
060    
061                    if (virtualHost == null) {
062                            long virtualHostId = counterLocalService.increment();
063    
064                            virtualHost = virtualHostPersistence.create(virtualHostId);
065    
066                            virtualHost.setCompanyId(companyId);
067                            virtualHost.setLayoutSetId(layoutSetId);
068                    }
069    
070                    virtualHost.setHostname(hostname);
071    
072                    virtualHostPersistence.update(virtualHost);
073    
074                    Company company = companyPersistence.fetchByPrimaryKey(companyId);
075    
076                    if (company != null) {
077                            companyPersistence.clearCache(company);
078                    }
079    
080                    LayoutSet layoutSet = layoutSetPersistence.fetchByPrimaryKey(
081                            layoutSetId);
082    
083                    if ((layoutSet == null) &&
084                            Validator.isNotNull(PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME)) {
085    
086                            Group group = groupPersistence.fetchByC_GK(
087                                    companyId, PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);
088    
089                            if (group != null) {
090                                    layoutSet = layoutSetPersistence.fetchByG_P(
091                                            group.getGroupId(), false);
092                            }
093                    }
094    
095                    if (layoutSet != null) {
096                            layoutSetPersistence.clearCache(layoutSet);
097                    }
098    
099                    return virtualHost;
100            }
101    
102    }