Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016 Bridje Framework.
*
* 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.bridje.orm.impl;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.bridje.ioc.Component;
import org.bridje.ioc.Inject;
import org.bridje.ioc.impl.ClassUtils;
import org.bridje.ioc.thls.Thls;
import org.bridje.ioc.thls.ThlsAction;
import org.bridje.ioc.thls.ThlsActionException;
import org.bridje.ioc.thls.ThlsActionException2;
import org.bridje.jdbc.JdbcService;
import org.bridje.orm.DataSourcesSetup;
import org.bridje.orm.DbObject;
import org.bridje.orm.Entity;
import org.bridje.orm.EntityContext;
import org.bridje.orm.OrmModel;
import org.bridje.orm.OrmService;
import org.bridje.orm.SQLDialect;
import org.bridje.orm.Table;
import org.bridje.orm.TableColumn;
@Component
class OrmServiceImpl implements OrmService
{
private static final Logger LOG = Logger.getLogger(OrmServiceImpl.class.getName());
public static final String ENTITYS_RESOURCE_FILE = "BRIDJE-INF/orm/entities.properties";
private Map, TableImpl>> tablesMap;
private Map, List>> modelsEntitysMap;
private Map, List>> modelsTablesMap;
@Inject
private JdbcService jdbcServ;
@Inject
private SQLDialect[] dialects;
@PostConstruct
public void init()
{
this.tablesMap = new HashMap<>();
try
{
createTables();
createModelsEntitys();
}
catch (IOException e)
{
LOG.log(Level.SEVERE, e.getMessage(), e);
}
}
@Override
public T createModel(String dsName, Class modelCls)
{
EntityContext ctx = createContext(dsName);
return instantiateModel(ctx, modelCls);
}
@Override
public T createModel(DataSource ds, Class modelCls)
{
EntityContext ctx = createContext(ds);
return instantiateModel(ctx, modelCls);
}
@Override
public T createModel(EntityContext ctx, Class modelCls)
{
return instantiateModel(ctx, modelCls);
}
private EntityContext createContext(String dsName)
{
return createContext(jdbcServ.getDataSource(dsName));
}
private EntityContext createContext(DataSource ds)
{
if(ds == null)
{
throw new IllegalArgumentException("No datasource was specified.");
}
for (SQLDialect dialect : dialects)
{
if(dialect.canHandle(ds))
{
return new EntityContextImpl(this, ds, dialect);
}
}
throw new IllegalArgumentException("Can´t find a valid dialect for this DataSource");
}
@Override
public boolean isEntityClass(Class> cls)
{
return this.tablesMap.containsKey(cls);
}
@Override
public TableImpl findTable(Class entity)
{
TableImpl result = (TableImpl)this.tablesMap.get(entity);
if(result == null)
{
throw new IllegalArgumentException(entity.getName() + " is not an entity class.");
}
return result;
}
@Override
public Class extends OrmModel> findModelClass(Class entity)
{
Entity annotation = entity.getAnnotation(Entity.class);
if(annotation == null)
{
throw new IllegalArgumentException(entity.getName() + " is not an entity class.");
}
return annotation.model();
}
@Override
public List> findEntitys(Class modelClass)
{
return Collections.unmodifiableList(modelsEntitysMap.get(modelClass));
}
@Override
public List