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