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.convert.database;
016    
017    import com.liferay.portal.convert.util.HibernateModelUtil;
018    import com.liferay.portal.convert.util.ModelMigrator;
019    import com.liferay.portal.kernel.servlet.ServletContextPool;
020    import com.liferay.portal.kernel.util.ClassLoaderUtil;
021    import com.liferay.portal.model.BaseModel;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import javax.servlet.ServletContext;
027    
028    import javax.sql.DataSource;
029    
030    /**
031     * @author Cristina Gonz??lez
032     */
033    public class PortalDatabaseConverter implements DatabaseConverter {
034    
035            @Override
036            public void convert(DataSource dataSource) throws Exception {
037                    _modelMigrator.migrate(dataSource, getModelClassNames(".*"));
038            }
039    
040            public void setModelMigrator(ModelMigrator modelMigrator) {
041                    _modelMigrator = modelMigrator;
042            }
043    
044            protected List<Class<? extends BaseModel<?>>> getModelClassNames(
045                    String regex) {
046    
047                    List<Class<? extends BaseModel<?>>> modelClassesName =
048                            new ArrayList<>();
049    
050                    modelClassesName.addAll(
051                            HibernateModelUtil.getModelClassNames(
052                                    ClassLoaderUtil.getPortalClassLoader(), ".*"));
053    
054                    for (String servletContextName : ServletContextPool.keySet()) {
055                            ServletContext servletContext = ServletContextPool.get(
056                                    servletContextName);
057    
058                            ClassLoader classLoader = servletContext.getClassLoader();
059    
060                            modelClassesName.addAll(
061                                    HibernateModelUtil.getModelClassNames(classLoader, ".*"));
062                    }
063    
064                    return modelClassesName;
065            }
066    
067            private ModelMigrator _modelMigrator;
068    
069    }