001/*
002 * (C) Copyright 2006-20011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.reporting.datasource;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.storage.sql.coremodel.SQLRepositoryService;
025import org.nuxeo.runtime.api.Framework;
026
027/**
028 * Helper class used to extract JDBC DB settings from the Live SQLRepository configuration
029 *
030 * @author Tiry (tdelprat@nuxeo.com)
031 */
032public class DSHelper {
033
034    protected static Map<String, NuxeoDSConfig> detectedDS;
035
036    public static Map<String, NuxeoDSConfig> getDSForRepos() {
037        if (detectedDS == null) {
038            Map<String, NuxeoDSConfig> configs = new HashMap<String, NuxeoDSConfig>();
039            String singleDS = Framework.getProperty("nuxeo.db.singleDataSource", null);
040            if (singleDS == null || singleDS.isEmpty()) {
041                // look for ds in repositories
042                SQLRepositoryService sqlRepositoryService = Framework.getService(SQLRepositoryService.class);
043                for (String repositoryName : sqlRepositoryService.getRepositoryNames()) {
044                    Map<String, String> properties = new HashMap<>();
045                    String xaDataSourceName = sqlRepositoryService.getRepositoryDataSourceAndProperties(repositoryName,
046                            properties);
047                    NuxeoDSConfig config = new NuxeoDSConfig(xaDataSourceName, properties);
048                    configs.put(repositoryName, config);
049                }
050            }
051            detectedDS = configs;
052        }
053        return detectedDS;
054    }
055
056    public static NuxeoDSConfig getDefaultRepoDS(String repositoryName) {
057        Map<String, NuxeoDSConfig> configs = getDSForRepos();
058
059        if (configs.size() == 0) {
060            return null;
061        }
062        if (configs.size() == 1) {
063            return configs.get(configs.keySet().iterator().next());
064        }
065
066        if (repositoryName == null) {
067            repositoryName = "default";
068        }
069        return configs.get(repositoryName);
070    }
071
072    public static NuxeoDSConfig getReplacementDS(String birtDSName, String repositoryName) {
073        String name = birtDSName.toLowerCase();
074        if (name.equals("nuxeo") || name.equals("nuxeovcs")) {
075            return getDefaultRepoDS(repositoryName);
076        }
077        if (name.startsWith("nuxeo-")) {
078            name = name.replace("nuxeo-", "");
079        }
080        Map<String, NuxeoDSConfig> configs = getDSForRepos();
081        return configs.get(name);
082    }
083
084}