/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.broadleafcommerce.common.site.service;

import org.broadleafcommerce.common.site.dao.SiteDao;
import org.broadleafcommerce.common.site.domain.Site;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.annotation.Resource;

@Service("blSiteService")
public class SiteServiceImpl implements SiteService {

    @Resource(name = "blSiteDao")
    protected SiteDao siteDao;

    @Override
    public Site retrieveSiteById(Long id) {
        Site response = siteDao.retrieve(id);
        if (response != null) {
            response = response.clone();
        }

        return response;
    }

    @Override
    public Site retrieveSiteByDomainName(String domainName) {
        String domainPrefix = null;
        if (domainName != null) {
            int pos = domainName.indexOf('.');
            if (pos >= 0) {
                domainPrefix = domainName.substring(0, pos);
            } else {
                domainPrefix = domainName;
            }
        }

        Site response = siteDao.retrieveSiteByDomainOrDomainPrefix(domainName, domainPrefix);
        if (response != null) {
            response = response.clone();
        }

        return response;
    }

    @Override
    @Transactional("blTransactionManager")
    public Site save(Site site) {
        return siteDao.save(site);
    }

    @Override
    public Site retrieveDefaultSite() {
        return siteDao.retrieveDefaultSite();
    }
    
    @Override
    public List<Site> findAllSites() {
        return siteDao.readAllSites();
    }

}
@@ -889,16 +889,50 @@
 ional;%0A%0A
+import javax.annotation.Resource;%0A
 import j
@@ -944,15 +944,19 @@
 til.
+Array
 List;%0A
-%0A
 impo
@@ -962,37 +962,26 @@
 ort java
-x.annotation.Resource
+.util.List
 ;%0A%0A@Serv
@@ -2047,16 +2047,24 @@
 ve(site)
+.clone()
 ;%0A    %7D%0A
@@ -2070,32 +2070,100 @@
 %0A%0A    @Override%0A
+    @Transactional(value = %22blTransactionManager%22, readOnly = true)%0A
     public Site 
@@ -2230,16 +2230,24 @@
 ltSite()
+.clone()
 ;%0A    %7D%0A
@@ -2273,80 +2273,322 @@
     
-public List%3CSite%3E findAllSites() %7B%0A        return siteDao.readAllSites()
+@Transactional(value = %22blTransactionManager%22, readOnly = true)%0A    public List%3CSite%3E findAllSites() %7B%0A        List%3CSite%3E response = new ArrayList%3CSite%3E();%0A        List%3CSite%3E sites = siteDao.readAllSites();%0A        for (Site site : sites) %7B%0A            response.add(site.clone());%0A        %7D%0A        return response
 ;%0A  

update transactional boundaries for the multi-tenant solr index service.