001    /**
002     * Copyright (c) 2000-2013 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.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    }