Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2009-2014 Pavel Ponec
   *
   *  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.ujorm.orm.metaModel;
 
 import java.awt.Color;
 import java.sql.*;
 import java.util.List;
 import java.util.Set;
 import org.ujorm.Key;
A logical database description.

Author(s):
Pavel Ponec
Composed:
1 - * MetaTable
Composed:
1 - * MetaProcedure
 
 final public class MetaDatabase extends AbstractMetaModel implements Comparable<MetaDatabase> {
     private static final Class<MetaDatabaseCLASS = MetaDatabase.class;

    
Logger
 
     private static final UjoLogger LOGGER = UjoLoggerFactory.getLogger(MetaDatabase.class);
    
Add a DB relation into table models. The true value allows to use a key RelationToMany of Database model by the same way as a RelationToMany on any persistent table. A sample of the use:
 for (Order order: database.get(Database.ORDER)) {
     // use the order instance
 }
 
 
     private static final boolean ADD_DB_MODEL = true;

    
Property Factory
 
     private static final KeyFactory<MetaDatabasefa = KeyFactory.CamelBuilder.get();
    
The meta-model id
 
     @XmlAttribute
     public static final Key<MetaDatabase,StringID = .newKey("id""");
    
MetaDatabase default schema
 
     public static final Key<MetaDatabase,StringSCHEMA = .newKey("schema""");
    
The default state read-only for the database.
 
     public static final Key<MetaDatabase,BooleanREAD_ONLY = .newKey("readOnly"false);
    
SQL dialect type of Class<SqlDialect>
 
     public static final Key<MetaDatabase,Class<? extends SqlDialect>> DIALECT = .newKey("dialect");
    
JDBC URL connection
 
     public static final Key<MetaDatabase,StringJDBC_URL = .newKey("jdbcUrl""");
    
JDBC Driver
 
     public static final Key<MetaDatabase,StringJDBC_DRIVER = .newKey("jdbcDriver""");
    
DB user
 
     public static final Key<MetaDatabase,StringUSER = .newKey("user""");
    
DB password of the user
 
     public static final Key<MetaDatabase,StringPASSWORD = .newKey("password""");
    
JNDI (java naming and directory interface) connection string
    public static final ListKey<MetaDatabase,StringJNDI = .newListKey("jndi");
    
The sequencer class for tables of the current database. A value can be a subtype of 'org.ujorm.orm.UjoSequencer' with one-parameter constructor type of MetaTable. If the NULL value is specified the then a default sequencer 'UjoSequencer' will be used.
    public static final Key<MetaDatabase,Class<? extends UjoSequencer>> SEQUENCER = .newClassKey("sequencer"UjoSequencer.class);
    
A policy to defining the database structure by a DDL.

See also:
org.ujorm.orm.ao.Orm2ddlPolicy Parameter values
    public static final Key<MetaDatabase,Orm2ddlPolicyORM2DLL_POLICY = .newKey("orm2ddlPolicy".);
    
List of tables
    public static final ListKey<MetaDatabase,MetaTableTABLES = .newListKey("table");
    
List of procedures
    public static final ListKey<MetaDatabase,MetaProcedurePROCEDURES = .newListKey("procedure");
    
Database order number
    @Transient
    public static final Key<MetaDatabase,IntegerORDER = .newKey("order", 0);
    
An instance of the DB class.
    @Transient
    public static final Key<MetaDatabase,OrmUjoROOT = .newKey("root");

    
The key initialization
    static{.lock();}
    // --------------------
    private OrmHandler ormHandler;
    private SqlDialect dialect;
    public MetaDatabase() {
    }

    
Create a new Database.

Parameters:
ormHandler ORM handler
database Database instance
param Configuration data from a XML file
    @SuppressWarnings("LeakingThisInConstructor")
    public MetaDatabase(OrmHandler ormHandlerOrmUjo databaseMetaDatabase paramInteger order) {
        this. = ormHandler;
        .setValue(thisdatabase);
        .setValue(thisorder);
        if (param!=null) {
            changeDefault(this  , .of(param));
            changeDefault(this.of(param));
            changeDefault(this.of(param));
            changeDefault(this , .of(param));
            changeDefault(this.of(param));
            changeDefault(this.of(param));
            changeDefault(this    , .of(param));
            changeDefault(this.of(param));
            changeDefault(this    , .of(param));
            changeDefault(this,.of(param));
        }
        Db annotDB = database.getClass().getAnnotation(Db.class);
        if (annotDB!=null) {
            changeDefault(this  , annotDB.schema());
            changeDefault(thisannotDB.readOnly());
            changeDefault(thisannotDB.Orm2ddlPolicy());
            changeDefault(this , annotDB.dialect());
            changeDefault(thisannotDB.jdbcUrl());
            changeDefault(thisannotDB.jdbcDriver());
            changeDefault(this    , annotDB.user());
            changeDefault(thisannotDB.password());
            changeDefault(this    , Arrays.asList(annotDB.jndi()));
            changeDefault(thisannotDB.sequencer());
        }
        changeDefault(this      , database.getClass().getSimpleName());
        changeDefault(thisgetDialect().getJdbcUrl());
        changeDefault(thisgetDialect().getJdbcDriver());
        for (Key tableProperty : database.readKeys()) {
            if (tableProperty instanceof RelationToMany) {
                RelationToMany tProperty = (RelationToManytableProperty;
                MetaTable par   = param!=null ? param.findTable(tProperty.getName()) : null;
                MetaTable table = new MetaTable(thistPropertypar);
                .addItem(thistable);
                ormHandler.addTableModel(table);
            }
            else if (tableProperty.isTypeOf(DbProcedure.class)) {
                Key tProcedure = tableProperty;
                MetaProcedure par = param!=null ? param.findProcedure(tProcedure.getName()) : null;
                MetaProcedure procedure = new MetaProcedure(thistProcedurepar);
                .addItem(thisprocedure);
                ormHandler.addProcedureModel(procedure);
            }
        }
        if () {
            // Add database relations:
            @SuppressWarnings("unchecked")
            String schemaKeyName = .of(this);
            RelationToMany relation = new RelationToMany
                    ( OrmTools.isFilled(schemaKeyName)
                    ? schemaKeyName
                    : RelationToMany.class.getSimpleName()
                    , database.getClass());
            MetaTable table = new MetaTable(thisrelationnull);
            table.setNotPersistent();
            .addItem(thistable);
            ormHandler.addTableModel(table);
        }
    }

    
Create a service method
        try {
            return getParams().get(.).newInstance();
        } catch (Throwable e) {
            throw new IllegalStateException("Can't create an instance of: " + MetaDbService.classe);
        }
    }

    
Returns a SQL dialect for the current database.
    public SqlDialect getDialect() {
        if (==nulltry {
             = (SqlDialect.of(this).newInstance();
            .setHandler();
        } catch (Exception e) {
            throw new IllegalStateException("Can't create an instance of " + e);
        }
        return ;
    }

    
Change DbType by a Java key
    public void changeDbType(MetaColumn column) {
        final Class type = column.getDbTypeClass();
        if (Void.class==type) {
            ..setValue(column.);
        }
        else if (String.class==type
              || StringWrapper.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (Integer.class==type || Color.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (Short.class==type) {
            ..setValue(column.);
        }
        else if (Float.class==type) {
            ..setValue(column.);
        }
        else if (Long.class==type
        || BigInteger.class.isAssignableFrom(type)
        ){
            ..setValue(column.);
        }
        else if (Double.class==type || BigDecimal.class==type) {
            ..setValue(column.);
        }
        else if (java.sql.Date.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (java.util.Date.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (Byte.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (Character.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (Boolean.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (Enum.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (Blob.class.isAssignableFrom(type)
        || BytesWrapper.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (Clob.class.isAssignableFrom(type)) {
            ..setValue(column.);
        }
        else if (OrmUjo.class.isAssignableFrom(type)) {
            // Make a later initialization!
        }
    }

    
Change DbType by a Java key
    public void changeDbLength(final MetaColumn column) {
        switch (..of(column)) {
            case :
                changeDefault(column., 8);
                changeDefault(column., 2);
                break;
            case :
            case :
                if (..isDefault(column)) {
                    final boolean isEnum = column.getType().isEnum();
                    ..setValue(columnisEnum ? maxEnumLenght4Db(column) : 128);
                }
                break;
            default:
        }
    }

    
Calculate database VARCHAR lenght for required column. Minimal lenght is 1 character
    private int maxEnumLenght4Db(final MetaColumn columnthrows IllegalArgumentException {
        try {
            int maxLenght = 1;
            final UjoStatement statement = new UjoStatement();
            for (Object enumValue : column.getType().getEnumConstants()) {
                column.getConverter().setValue(columnstatementenumValue, 1);
                final Object value = statement.getValue();
                if (value instanceof String) {
                    maxLenght = Math.max(maxLenght, ((String)value).length());
                }
            }
            return maxLenght;
        } catch (SQLException e) {
            throw new IllegalArgumentException(e);
        }
    }

    
Returns a full count of the database tables (views are excluded)
    public int getTableTotalCount() {
        int tableCount = 0;
        for (MetaTable metaTable : .getList(this)) {
            if (metaTable.isTable()) {
                ++tableCount;
            }
        }
        return tableCount;
    }

    
Create DB
    public void create(Session session) {
        createService().create(thissession);
    }

    
Close a connection, statement and a result set.
    public static void close
            ( final Connection connection
            , final JdbcStatement statement
            , final ResultSet rs
            , final boolean throwExceptonthrows IllegalStateException {
        try {
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (statement != null) {
                        statement.close();
                    }
                } finally {
                    if (connection != null) {
                        connection.close();
                    }
                }
            }
        } catch (Throwable e) {
            String msg = "Can't close a SQL object";
            if (throwExcepton) {
                throw new IllegalStateException(msge);
            } else {
                .log(.msge);
            }
        }
    }

    
Close a connection, statement and a result set.
    public static void close
            ( final Connection connection
            , final Statement statement
            , final ResultSet rs
            , final boolean throwExceptonthrows IllegalStateException {
        try {
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (statement != null) {
                        statement.close();
                    }
                } finally {
                    if (connection != null) {
                        connection.close();
                    }
                }
            }
        } catch (Throwable e) {
            String msg = "Can't close a SQL object";
            if (throwExcepton) {
                throw new IllegalStateException(msge);
            } else {
                .log(.msge);
            }
        }
    }

    
OrmHandler
    public OrmHandler getOrmHandler() {
        return ;
    }

    
Return the OrmHandler parameters
    public MetaParams getParams() {
        return .getParameters();
    }

    
Returns an ID of the MetaDatabase.
    public String getId() {
        return .of(this);
    }

    
Create connection with auto-commit false.
    public Connection createConnection() throws Exception {
        final Connection result = .createConnection(this);
        if (result.getAutoCommit()) {
            result.setAutoCommit(false);
        }
        return result;
    }

    
Call the method from SqlDialect only. Connection is set to autocommit to false.
    public Connection createInternalConnection() throws Exception {
        Connection result;
        final List<Stringjndi = .of(this);
        if (!jndi.isEmpty()) {
            .log(."JNDI: {0}"jndi);
            InitialContext initContext = .createJndiInitialContext(this);
            final int lastItem = jndi.size()-1;
            for (int i=0; i<lastItemi++) {
                initContext = (InitialContextinitContext.lookup(jndi.get(i));
                if (initContext==null) {
                    throw new IllegalStateException("JNDI problem: InitialContext was not found for the: " + jndi.get(i));
                }
            }
            DataSource dataSource = (DataSourceinitContext.lookup(jndi.get(lastItem));
            if (dataSource==null) {
                throw new IllegalStateException("JNDI problem: database connection was not found for the: " + jndi);
            }
            result = dataSource.getConnection();
        } else {
            Class.forName(.of(this));
            result = DriverManager.getConnection(.of(this), .of(this), .of(this));
        }
        return result;
    }

    
Equals
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof MetaDatabase) {
            MetaDatabase db = (MetaDatabaseobj;
            final Integer i1 = ..of(this);
            final Integer i2 = ..of(db);
            return i1.equals(i2);
        } else {
            return false;
        }
    }

    
Hash code
    @Override
    public int hashCode() {
        final Integer ir = ..of(this);
        return ir.hashCode();
    }


    
Returns a default handler session. It is a session of the first database.
    public Session getDefaultSession() {
        return .getDefaultSession();
    }

    
Get all table schemas
    public Set<StringgetSchemas(List<MetaTabletables) {
        final Set<Stringresult = new HashSet<String>();
        for (MetaTable table : tables) {
            if (table.isTable()) {
                String schema = ..of(table);
                if (OrmTools.isFilled(schema)) {
                    result.add(schema);
                }
            }
        }
        return result;
    }

    
Finds the first table by ID or returns null. The method is for internal use only.
    MetaTable findTable(String id) {
        if (OrmTools.isFilled(id)) for (MetaTable table : .getList(this)) {
            if (..equals(tableid)) {
                return table;
            }
        }
        return null;
    }

    
Finds the first procedure by ID or returns null. The method is for internal use only.
        if (OrmTools.isFilled(id)) for (MetaProcedure procedure : .getList(this)) {
            if (..equals(procedureid)) {
                return procedure;
            }
        }
        return null;
    }

    
Method returns true in case any table requires the internal table 'ujorm_pk_support' to get a next sequence value.
    public boolean isSequenceTableRequired() {
        for (MetaTable table : .of(this)) {
            if (table.isTable()
            &&  table.getSequencer().isSequenceTableRequired()) {
                return true;
            }
        }
        return false;
    }


    
Create a new sequencer for selected table
    @SuppressWarnings("unchecked")
    protected UjoSequencer createSequencer(MetaTable table) {
        UjoSequencer result;
        Class seqClass = .of(this);
        if (seqClass==UjoSequencer.class) {
            result = new UjoSequencer(table);
        } else try {
            Constructor<UjoSequencerconstr = seqClass.getConstructor(MetaTable.class);
            result = constr.newInstance(table);
        } catch (Exception e) {
            throw new IllegalStateException("Can't create sequencer for " + seqClasse);
        }
        return result;
    }

    
Returns all database indexes
    public List<MetaIndexgetIndexList() {
        final List<MetaIndexresult = new ArrayList<MetaIndex>(32);
        for (MetaTable table : .of(this)) {
            result.addAll(table.getIndexCollection());
        }
        return result;
    }
    @Override
    public String toString() {
        final String msg = .of(this)
            + '['
            + ..getItemCount(this)
            + ']'
            ;
	     return msg;
    }

    
Compare the object by ORDER.
    public int compareTo(MetaDatabase o) {
        final Integer i1 = .of(this);
        final Integer i2 = .of(o);
        return i1.compareTo(i2);
    }

    
The PASSWORD key is not exported to XML for a better security.
    @Override
    public boolean readAuthorization(final UjoAction actionfinal Key keyfinal Object value) {
        switch (action.getType()) {
            case .:
                return key != ;
            default:
                return super.readAuthorization(actionkeyvalue);
        }
    }
New to GrepCode? Check out our FAQ X