Changeset 2608


Ignore:
Timestamp:
01/28/10 11:22:36 (2 years ago)
Author:
mswertz
Message:

enhancement: when extracting from mysql, default="CURRENT_TIMESTAMP" translates to auto="true" default=""

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molgenis/3.3/src/org/molgenis/model/JDBCModelExtractor.java

    r2605 r2608  
    3131 
    3232/** 
    33  * java.sql.Types 
    34 public static final int         ARRAY   2003 
    35 public static final int         BIGINT  -5 
    36 public static final int         BINARY  -2 
    37 public static final int         BIT     -7 
    38 public static final int         BLOB    2004 
    39 public static final int         BOOLEAN         16 
    40 public static final int         CHAR    1 
    41 public static final int         CLOB    2005 
    42 public static final int         DATALINK        70 
    43 public static final int         DATE    91 
    44 public static final int         DECIMAL         3 
    45 public static final int         DISTINCT        2001 
    46 public static final int         DOUBLE  8 
    47 public static final int         FLOAT   6 
    48 public static final int         INTEGER         4 
    49 public static final int         JAVA_OBJECT     2000 
    50 public static final int         LONGNVARCHAR    -16 
    51 public static final int         LONGVARBINARY   -4 
    52 public static final int         LONGVARCHAR     -1 
    53 public static final int         NCHAR   -15 
    54 public static final int         NCLOB   2011 
    55 public static final int         NULL    0 
    56 public static final int         NUMERIC         2 
    57 public static final int         NVARCHAR        -9 
    58 public static final int         OTHER   1111 
    59 public static final int         REAL    7 
    60 public static final int         REF     2006 
    61 public static final int         ROWID   -8 
    62 public static final int         SMALLINT        5 
    63 public static final int         SQLXML  2009 
    64 public static final int         STRUCT  2002 
    65 public static final int         TIME    92 
    66 public static final int         TIMESTAMP       93 
    67 public static final int         TINYINT         -6 
    68 public static final int         VARBINARY       -3 
    69 public static final int         VARCHAR         12 
     33 * java.sql.Types public static final int ARRAY 2003 public static final int 
     34 * BIGINT -5 public static final int BINARY -2 public static final int BIT -7 
     35 * public static final int BLOB 2004 public static final int BOOLEAN 16 public 
     36 * static final int CHAR 1 public static final int CLOB 2005 public static final 
     37 * int DATALINK 70 public static final int DATE 91 public static final int 
     38 * DECIMAL 3 public static final int DISTINCT 2001 public static final int 
     39 * DOUBLE 8 public static final int FLOAT 6 public static final int INTEGER 4 
     40 * public static final int JAVA_OBJECT 2000 public static final int LONGNVARCHAR 
     41 * -16 public static final int LONGVARBINARY -4 public static final int 
     42 * LONGVARCHAR -1 public static final int NCHAR -15 public static final int 
     43 * NCLOB 2011 public static final int NULL 0 public static final int NUMERIC 2 
     44 * public static final int NVARCHAR -9 public static final int OTHER 1111 public 
     45 * static final int REAL 7 public static final int REF 2006 public static final 
     46 * int ROWID -8 public static final int SMALLINT 5 public static final int 
     47 * SQLXML 2009 public static final int STRUCT 2002 public static final int TIME 
     48 * 92 public static final int TIMESTAMP 93 public static final int TINYINT -6 
     49 * public static final int VARBINARY -3 public static final int VARCHAR 12 
     50 *  
    7051 * @author Morris Swertz 
    71  * 
     52 *  
    7253 */ 
    73 public class JDBCModelExtractor 
    74 { 
     54public class JDBCModelExtractor { 
    7555        static Logger logger = Logger.getLogger("JDBCModelExtractor"); 
    7656 
    77         public static void main(String[] args) throws Exception 
    78         { 
     57        public static void main(String[] args) throws Exception { 
    7958                Properties props = new Properties(); 
    8059                props.load(new FileInputStream("molgenis.properties")); 
     
    8261        } 
    8362 
    84         public static String extract(MolgenisOptions options) 
    85         { 
     63        public static String extract(MolgenisOptions options) { 
    8664                BasicDataSource data_src = new BasicDataSource(); 
    8765 
     
    9573        } 
    9674 
    97         public static String extract(Properties p) 
    98         { 
     75        public static String extract(Properties p) { 
    9976                BasicDataSource data_src = new BasicDataSource(); 
    10077 
     
    10986        } 
    11087 
    111         private static String extract(BasicDataSource data_src) 
    112         { 
     88        private static String extract(BasicDataSource data_src) { 
    11389                Model m = new Model(); 
    11490 
    115                 try 
    116                 { 
     91                try { 
    11792                        // check conection 
    11893                        data_src.getConnection(); 
     
    138113                        m.setName(SCHEMA_NAME); 
    139114 
    140                         ResultSet rs = md.getTables(SCHEMA_NAME, null, null, new String[] 
    141                         { "TABLE" }); 
     115                        ResultSet rs = md.getTables(SCHEMA_NAME, null, null, 
     116                                        new String[] { "TABLE" }); 
    142117 
    143118                        Tuple tableInfo = new ResultSetTuple(rs); 
    144                         while (rs.next()) 
    145                         { 
     119                        while (rs.next()) { 
    146120                                logger.debug("TABLE: " + tableInfo); 
    147121 
     
    151125 
    152126                                // ADD THE COLUMNS 
    153                                 ResultSet rsCol = md.getColumns(SCHEMA_NAME, null, tableInfo.getString("TABLE_NAME"), null); 
    154                                 while (rsCol.next()) 
    155                                 { 
     127                                ResultSet rsCol = md.getColumns(SCHEMA_NAME, null, tableInfo 
     128                                                .getString("TABLE_NAME"), null); 
     129                                while (rsCol.next()) { 
    156130                                        Tuple fieldInfo = new ResultSetTuple(rsCol); 
    157131                                        logger.debug("COLUMN: " + fieldInfo); 
     
    159133                                        Field f = new Field(); 
    160134                                        f.setName(fieldInfo.getString("COLUMN_NAME")); 
    161                                         f.setType(Field.Type.getType(fieldInfo.getInt("DATA_TYPE"))); 
     135                                        f 
     136                                                        .setType(Field.Type.getType(fieldInfo 
     137                                                                        .getInt("DATA_TYPE"))); 
    162138                                        f.setDefaultValue(fieldInfo.getString("COLUMN_DEF")); 
    163                                          
    164                                         //accomodate mysql CURRENT_TIMESTAMP 
    165                                         if("CURRENT_TIMESTAMP".equals(f.getDefaultValue()) &&  
    166                                                         (f.getType().equals(Field.Type.DATETIME) || f.getType().equals(Field.Type.DATE))) 
    167                                         { 
    168                                                 f.setDefaultValue(null); 
    169                                                 f.setAuto(true); 
    170                                         } 
    171                                          
    172                                         if(fieldInfo.getString("REMARKS") != null && !"".equals(fieldInfo.getString("REMARKS").trim()) )f.setDescription(fieldInfo.getString("REMARKS")); 
    173                                         if (fieldInfo.getBool("NULLABLE")) f.setNillable(true); 
     139 
     140                                        if (md.getDatabaseProductName().toLowerCase().contains( 
     141                                                        "mysql")) { 
     142                                                // accomodate mysql CURRENT_TIMESTAMP 
     143                                                if ("CURRENT_TIMESTAMP".equals(f.getDefaultValue()) 
     144                                                                && (f.getType().equals(Field.Type.DATETIME) || f 
     145                                                                                .getType().equals(Field.Type.DATE))) { 
     146                                                        f.setDefaultValue(null); 
     147                                                        f.setAuto(true); 
     148                                                } 
     149 
     150                                                // accomodate mysql text/string fields + 
     151                                                // nillable="false" -> mysql ignore not null and so 
     152                                                // should we! 
     153                                        } 
     154 
     155                                        if (fieldInfo.getString("REMARKS") != null 
     156                                                        && !"" 
     157                                                                        .equals(fieldInfo.getString("REMARKS") 
     158                                                                                        .trim())) 
     159                                                f.setDescription(fieldInfo.getString("REMARKS")); 
     160                                        if (fieldInfo.getBool("NULLABLE")) 
     161                                                f.setNillable(true); 
    174162 
    175163                                        // auto increment? 
    176                                         if (f.getType().equals(Field.Type.INT)) 
    177                                         { 
    178                                                 if (fieldInfo.getObject("IS_AUTOINCREMENT") != null) f.setAuto(fieldInfo 
    179                                                                 .getBool("IS_AUTOINCREMENT")); 
    180                                         } 
    181  
    182                                         if (f.getType().equals(Field.Type.STRING) || f.getType().equals(Field.Type.CHAR)) 
    183                                         { 
    184                                                 if (fieldInfo.getInt("COLUMN_SIZE") > 255) f.setType(Field.Type.TEXT); 
    185                                                 else 
    186                                                 { 
     164                                        if (f.getType().equals(Field.Type.INT)) { 
     165                                                if (fieldInfo.getObject("IS_AUTOINCREMENT") != null) 
     166                                                        f.setAuto(fieldInfo.getBool("IS_AUTOINCREMENT")); 
     167                                        } 
     168 
     169                                        if (f.getType().equals(Field.Type.STRING) 
     170                                                        || f.getType().equals(Field.Type.CHAR)) { 
     171                                                if (fieldInfo.getInt("COLUMN_SIZE") > 255) 
     172                                                        f.setType(Field.Type.TEXT); 
     173                                                else { 
    187174                                                        // f.setLength(fieldInfo.getInt("COLUMN_SIZE")); 
    188175                                                        f.setType(null); 
     
    190177                                        } 
    191178 
    192                                         ResultSet rsXref = md.getImportedKeys(SCHEMA_NAME, null, tableInfo.getString("TABLE_NAME")); 
    193                                         while (rsXref.next()) 
    194                                         { 
     179                                        ResultSet rsXref = md.getImportedKeys(SCHEMA_NAME, null, 
     180                                                        tableInfo.getString("TABLE_NAME")); 
     181                                        while (rsXref.next()) { 
    195182                                                Tuple xrefInfo = new ResultSetTuple(rsXref); 
    196                                                 if (xrefInfo.getString("FKCOLUMN_NAME").equals(fieldInfo.getString("COLUMN_NAME"))) 
    197                                                 { 
     183                                                if (xrefInfo.getString("FKCOLUMN_NAME").equals( 
     184                                                                fieldInfo.getString("COLUMN_NAME"))) { 
    198185                                                        f.setType(Field.Type.XREF_SINGLE); 
    199                                                         //problem: PKTABLE_NAME is lowercase, need to be corrected later? 
    200                                                         f.setXrefField(xrefInfo.getString("PKTABLE_NAME") + "." 
    201                                                                         + xrefInfo.getString("PKCOLUMN_NAME")); 
     186                                                        // problem: PKTABLE_NAME is lowercase, need to be 
     187                                                        // corrected later? 
     188                                                        f 
     189                                                                        .setXrefField(xrefInfo 
     190                                                                                        .getString("PKTABLE_NAME") 
     191                                                                                        + "." 
     192                                                                                        + xrefInfo 
     193                                                                                                        .getString("PKCOLUMN_NAME")); 
    202194                                                } 
    203195                                        } 
     
    210202                                // mysql workaround 
    211203                                Statement stmt = null; 
    212                                 try 
    213                                 { 
     204                                try { 
    214205                                        String sql = "select * from " + e.getName() + " where 1=0"; 
    215206                                        stmt = conn.createStatement(); 
     
    217208                                        ResultSet autoincRs = stmt.executeQuery(sql); 
    218209                                        ResultSetMetaData rowMeta = autoincRs.getMetaData(); 
    219                                         for (int i = 1; i <= rowMeta.getColumnCount(); i++) 
    220                                         { 
    221                                                 if (rowMeta.isAutoIncrement(i)) 
    222                                                 { 
     210                                        for (int i = 1; i <= rowMeta.getColumnCount(); i++) { 
     211                                                if (rowMeta.isAutoIncrement(i)) { 
    223212                                                        e.getFields().get(i - 1).setAuto(true); 
    224213                                                } 
    225214                                        } 
    226                                 } 
    227                                 catch (Exception exc) 
    228                                 { 
    229                                         logger.error("didn't retrieve autoinc/sequence: " + exc.getMessage()); 
     215                                } catch (Exception exc) { 
     216                                        logger.error("didn't retrieve autoinc/sequence: " 
     217                                                        + exc.getMessage()); 
    230218                                        // e.printStackTrace(); 
    231                                 } 
    232                                 finally 
    233                                 { 
     219                                } finally { 
    234220                                        stmt.close(); 
    235221                                } 
    236222 
    237223                                // ADD UNIQUE CONTRAINTS 
    238                                 ResultSet rsIndex = md.getIndexInfo(SCHEMA_NAME, null, tableInfo.getString("TABLE_NAME"), true, false); 
     224                                ResultSet rsIndex = md.getIndexInfo(SCHEMA_NAME, null, 
     225                                                tableInfo.getString("TABLE_NAME"), true, false); 
    239226                                // indexed list of uniques 
    240227                                Map<String, List<String>> uniques = new LinkedHashMap<String, List<String>>(); 
    241                                 while (rsIndex.next()) 
    242                                 { 
     228                                while (rsIndex.next()) { 
    243229                                        Tuple index = new ResultSetTuple(rsIndex); 
    244230                                        logger.debug("UNIQUE: " + index); 
     
    249235                                        // ORDINAL_POSITION='1' COLUMN_NAME='id' ASC_OR_DESC='A' 
    250236                                        // CARDINALITY='0' PAGES='0' FILTER_CONDITION='null' 
    251                                         if (uniques.get(index.getString("INDEX_NAME")) == null) uniques.put(index.getString("INDEX_NAME"), 
    252                                                         new ArrayList<String>()); 
    253                                         uniques.get(index.getString("INDEX_NAME")).add(index.getString("COLUMN_NAME")); 
    254                                 } 
    255                                 for (List<String> index : uniques.values()) 
    256                                 { 
    257                                         if (index.size() == 1) 
    258                                         { 
     237                                        if (uniques.get(index.getString("INDEX_NAME")) == null) 
     238                                                uniques.put(index.getString("INDEX_NAME"), 
     239                                                                new ArrayList<String>()); 
     240                                        uniques.get(index.getString("INDEX_NAME")).add( 
     241                                                        index.getString("COLUMN_NAME")); 
     242                                } 
     243                                for (List<String> index : uniques.values()) { 
     244                                        if (index.size() == 1) { 
    259245                                                e.getField(index.get(0)).setUnique(true); 
    260                                         } 
    261                                         else 
    262                                         { 
     246                                        } else { 
    263247                                                String fields = ""; 
    264                                                 for (String field_name : index) 
    265                                                 { 
     248                                                for (String field_name : index) { 
    266249                                                        fields += "," + field_name; 
    267250                                                } 
     
    274257 
    275258                                // FIND type="autoid" 
    276                                 for (Field f : e.getFields()) 
    277                                 { 
    278                                         if (f.getAuto() != null && f.getAuto() && f.getType().equals(Type.INT) && f.getUnique() != null 
    279                                                         && f.getUnique()) 
    280                                         { 
     259                                for (Field f : e.getFields()) { 
     260                                        if (f.getAuto() != null && f.getAuto() 
     261                                                        && f.getType().equals(Type.INT) 
     262                                                        && f.getUnique() != null && f.getUnique()) { 
    281263                                                f.setType(Field.Type.AUTOID); 
    282264                                                f.setAuto(null); 
     
    292274                        // then other fields having the same name are likely to be xref to 
    293275                        // the autoid 
    294                         for (Entity e : m.getEntities()) 
    295                         { 
    296                                 for (Field f : e.getFields()) 
    297                                 { 
    298                                         if (Field.Type.AUTOID.equals(f.getType())) 
    299                                         { 
    300                                                 for (Entity otherE : m.getEntities()) 
    301                                                 { 
    302                                                         for (Field otherF : otherE.getFields()) 
    303                                                         { 
     276                        for (Entity e : m.getEntities()) { 
     277                                for (Field f : e.getFields()) { 
     278                                        if (Field.Type.AUTOID.equals(f.getType())) { 
     279                                                for (Entity otherE : m.getEntities()) { 
     280                                                        for (Field otherF : otherE.getFields()) { 
    304281                                                                // assume xref if 
    305282                                                                // name == name 
    306283                                                                // otherF.type == int 
    307                                                                 if (otherF.getName().equals(f.getName()) && otherF.getType().equals(Field.Type.INT)) 
    308                                                                 { 
    309                                                                         logger.debug("Guessed that " + otherE.getName() + "." + otherF.getName() 
    310                                                                                         + " references " + e.getName() + "." + f.getName()); 
     284                                                                if (otherF.getName().equals(f.getName()) 
     285                                                                                && otherF.getType().equals( 
     286                                                                                                Field.Type.INT)) { 
     287                                                                        logger.debug("Guessed that " 
     288                                                                                        + otherE.getName() + "." 
     289                                                                                        + otherF.getName() + " references " 
     290                                                                                        + e.getName() + "." + f.getName()); 
    311291                                                                        otherF.setType(Field.Type.XREF_SINGLE); 
    312292                                                                        // otherF.setXrefEntity(; 
    313                                                                         otherF.setXrefField(e.getName() + "." + f.getName()); 
     293                                                                        otherF.setXrefField(e.getName() + "." 
     294                                                                                        + f.getName()); 
    314295                                                                } 
    315296                                                        } 
     
    321302 
    322303                        // GUESS the xref labels 
    323                         // guess the xreflabel as being the non-autoid field that is unique and not null 
     304                        // guess the xreflabel as being the non-autoid field that is unique 
     305                        // and not null 
    324306                        // rule: if there is another unique field in the referenced table 
    325307                        // then that probably is usable as label 
    326                         for (Entity e : m.getEntities()) 
    327                         { 
    328                                 for (Field f : e.getFields()) 
    329                                 { 
    330                                         if (Field.Type.XREF_SINGLE.equals(f.getType())) 
    331                                         { 
    332                                                 String xrefEntityName = f.getXrefField().substring(0, f.getXrefField().indexOf(".")); 
    333                                                 String xrefFieldName = f.getXrefField().substring(f.getXrefField().indexOf(".") + 1); 
    334                                                 //reset the xref entity to the uppercase version 
    335                                                 f.setXrefField(m.getEntity(xrefEntityName).getName()+"."+xrefFieldName); 
    336                                                  
    337                                                  
    338                                                 for (Field labelField : m.getEntity(xrefEntityName).getFields()) 
    339                                                 { 
    340                                                         // find the other unique, nillable="false" field, if any 
     308                        for (Entity e : m.getEntities()) { 
     309                                for (Field f : e.getFields()) { 
     310                                        if (Field.Type.XREF_SINGLE.equals(f.getType())) { 
     311                                                String xrefEntityName = f.getXrefField().substring(0, 
     312                                                                f.getXrefField().indexOf(".")); 
     313                                                String xrefFieldName = f.getXrefField().substring( 
     314                                                                f.getXrefField().indexOf(".") + 1); 
     315                                                // reset the xref entity to the uppercase version 
     316                                                f.setXrefField(m.getEntity(xrefEntityName).getName() 
     317                                                                + "." + xrefFieldName); 
     318 
     319                                                for (Field labelField : m.getEntity(xrefEntityName) 
     320                                                                .getFields()) { 
     321                                                        // find the other unique, nillable="false" field, if 
     322                                                        // any 
    341323                                                        if (!labelField.getName().equals(xrefFieldName) 
    342                                                                         && Boolean.TRUE.equals(labelField.getUnique()) && Boolean.FALSE.equals(labelField.getNillable())) 
    343                                                         { 
    344                                                                 logger.debug("guessed label " + e.getName() + "." + labelField.getName()); 
     324                                                                        && Boolean.TRUE.equals(labelField 
     325                                                                                        .getUnique()) 
     326                                                                        && Boolean.FALSE.equals(labelField 
     327                                                                                        .getNillable())) { 
     328                                                                logger.debug("guessed label " + e.getName() 
     329                                                                                + "." + labelField.getName()); 
    345330                                                                f.setXrefLabel(labelField.getName()); 
    346331                                                        } 
     
    354339                        // probably inheriting... 
    355340                        // action: change to inheritance and remove the xref field 
    356                         for (Entity e : m.getEntities()) 
    357                         { 
     341                        for (Entity e : m.getEntities()) { 
    358342                                List<Field> toBeRemoved = new ArrayList<Field>(); 
    359                                 for (Field f : e.getFields()) 
    360                                 { 
    361                                         if (Field.Type.XREF_SINGLE.equals(f.getType()) && Boolean.TRUE.equals(f.getUnique())) 
    362                                         { 
    363                                                 String entityName = f.getXrefField().substring(0, f.getXrefField().indexOf(".")); 
     343                                for (Field f : e.getFields()) { 
     344                                        if (Field.Type.XREF_SINGLE.equals(f.getType()) 
     345                                                        && Boolean.TRUE.equals(f.getUnique())) { 
     346                                                String entityName = f.getXrefField().substring(0, 
     347                                                                f.getXrefField().indexOf(".")); 
    364348                                                e.setExtends(entityName); 
    365349                                                toBeRemoved.add(f); 
    366350                                        } 
    367351                                } 
    368                                 for (Field f : toBeRemoved) 
    369                                 { 
     352                                for (Field f : toBeRemoved) { 
    370353                                        e.getFields().remove(f); 
    371354                                } 
     
    373356 
    374357                        // TODO GUESS the type="mref" 
    375                         // rule: any entity with two xref fields and optional autoid field should be a mref 
     358                        // rule: any entity with two xref fields and optional autoid field 
     359                        // should be a mref 
    376360                        List<Entity> toBeRemoved = new ArrayList<Entity>(); 
    377                         for (Entity e : m.getEntities()) 
    378                         { 
    379  
    380                                 if (e.getFields().size() <= 3) 
    381                                 { 
     361                        for (Entity e : m.getEntities()) { 
     362 
     363                                if (e.getFields().size() <= 3) { 
    382364                                        int xrefs = 0; 
    383365                                        String idField = null; 
    384                                         //the column refering to 'localEntity' 
     366                                        // the column refering to 'localEntity' 
    385367                                        String localIdField = null; 
    386                                         //the localEntiy 
     368                                        // the localEntiy 
    387369                                        String localEntity = null; 
    388                                         //the column referring to 'remoteEntity' 
     370                                        // the column referring to 'remoteEntity' 
    389371                                        String localEntityField = null; 
    390                                         //the column the localIdField is referning to 
     372                                        // the column the localIdField is referning to 
    391373                                        String remoteIdField = null; 
    392                                         //the column remoteEntity 
     374                                        // the column remoteEntity 
    393375                                        String remoteEntity = null; 
    394                                         //the column the remoteIdField is referring to 
     376                                        // the column the remoteIdField is referring to 
    395377                                        String remoteEntityField = null; 
    396378 
    397                                         for (Field f : e.getFields()) 
    398                                         { 
    399                                                 if (Field.Type.AUTOID.equals(f.getType())) 
    400                                                 { 
     379                                        for (Field f : e.getFields()) { 
     380                                                if (Field.Type.AUTOID.equals(f.getType())) { 
    401381                                                        idField = f.getName(); 
    402                                                 } 
    403                                                 else if (Field.Type.XREF_SINGLE.equals(f.getType())) 
    404                                                 { 
     382                                                } else if (Field.Type.XREF_SINGLE.equals(f.getType())) { 
    405383                                                        xrefs++; 
    406                                                         if (xrefs == 1) 
    407                                                         { 
     384                                                        if (xrefs == 1) { 
    408385                                                                localIdField = f.getName(); 
    409                                                                 //localEntityField is just the idField of the localEntity 
    410                                                                 localEntity = f.getXrefField().substring(0, f.getXrefField().indexOf(".")); 
    411                                                                 localEntityField = f.getXrefField().substring(f.getXrefField().indexOf(".")+1); 
    412                                                         } 
    413                                                         else 
    414                                                         { 
     386                                                                // localEntityField is just the idField of the 
     387                                                                // localEntity 
     388                                                                localEntity = f.getXrefField().substring(0, 
     389                                                                                f.getXrefField().indexOf(".")); 
     390                                                                localEntityField = f.getXrefField().substring( 
     391                                                                                f.getXrefField().indexOf(".") + 1); 
     392                                                        } else { 
    415393                                                                remoteIdField = f.getName(); 
    416                                                                 //should be the id field of the remote entity 
    417                                                                 remoteEntity = f.getXrefField().substring(0, f.getXrefField().indexOf(".")); 
    418                                                                 remoteEntityField = f.getXrefField().substring(f.getXrefField().indexOf(".")+1); 
     394                                                                // should be the id field of the remote entity 
     395                                                                remoteEntity = f.getXrefField().substring(0, 
     396                                                                                f.getXrefField().indexOf(".")); 
     397                                                                remoteEntityField = f.getXrefField().substring( 
     398                                                                                f.getXrefField().indexOf(".") + 1); 
    419399                                                        } 
    420400                                                } 
     
    423403                                        // if valid mref, drop this entity and add mref field to 
    424404                                        // entity 
    425                                         if (xrefs == 2 && (e.getFields().size() == 2 || idField != null)) 
    426                                         { 
    427                                                 //add mref on 'local' end 
     405                                        if (xrefs == 2 
     406                                                        && (e.getFields().size() == 2 || idField != null)) { 
     407                                                // add mref on 'local' end 
    428408                                                Entity localContainer = m.getEntity(localEntity); 
    429409                                                Field localField = new Field(); 
    430                                                 if (localContainer.getField(e.getName()) == null) 
    431                                                 { 
     410                                                if (localContainer.getField(e.getName()) == null) { 
    432411                                                        localField.setName(e.getName()); 
    433412                                                } 
    434413                                                localField.setType(Field.Type.XREF_MULTIPLE); 
    435                                                 localField.setXrefField(remoteEntity+"."+remoteEntityField); 
     414                                                localField.setXrefField(remoteEntity + "." 
     415                                                                + remoteEntityField); 
    436416                                                localField.setMrefName(e.getName()); 
    437417                                                localField.setMrefLocalid(localIdField); 
    438418                                                localField.setMrefRemoteid(remoteIdField); 
    439419                                                localContainer.getFields().add(localField); 
    440                                                  
    441                                                 //add mref to remote end 
     420 
     421                                                // add mref to remote end 
    442422                                                Entity remoteContainer = m.getEntity(remoteEntity); 
    443423                                                Field remoteField = new Field(); 
    444                                                 if (remoteContainer.getField(e.getName()) == null) 
    445                                                 { 
     424                                                if (remoteContainer.getField(e.getName()) == null) { 
    446425                                                        remoteField.setName(e.getName()); 
    447426                                                } 
    448427                                                remoteField.setType(Field.Type.XREF_MULTIPLE); 
    449                                                 remoteField.setXrefField(localEntity+"."+localEntityField); 
     428                                                remoteField.setXrefField(localEntity + "." 
     429                                                                + localEntityField); 
    450430                                                remoteField.setMrefName(e.getName()); 
    451                                                 //don't need to add local id as it is refering back 
     431                                                // don't need to add local id as it is refering back 
    452432                                                remoteField.setMrefLocalid(remoteIdField); 
    453433                                                remoteField.setMrefRemoteid(localIdField); 
    454                                                 remoteContainer.getFields().add(remoteField);                                            
    455  
    456                                                 //remove the link table as separate entity 
     434                                                remoteContainer.getFields().add(remoteField); 
     435 
     436                                                // remove the link table as separate entity 
    457437                                                toBeRemoved.add(e); 
    458438                                                logger.debug("guessed mref " + e.getName()); 
     
    465445                        System.out.println(toString(m)); 
    466446                        return toString(m); 
    467                 } 
    468                 catch (Exception e) 
    469                 { 
     447                } catch (Exception e) { 
    470448                        logger.error(e); 
    471449                        e.printStackTrace(); 
     
    473451                } 
    474452        } 
    475          
    476          
    477  
    478         private static void logResultSet(ResultSet rs) throws SQLException 
    479         { 
    480                 while (rs.next()) 
    481                 { 
     453 
     454        private static void logResultSet(ResultSet rs) throws SQLException { 
     455                while (rs.next()) { 
    482456                        Tuple t = new ResultSetTuple(rs); // logger.debug(rs.get) 
    483457                        // logger.debug(t); 
     
    485459        } 
    486460 
    487         private static String toString(Model model) throws JAXBException 
    488         { 
     461        private static String toString(Model model) throws JAXBException { 
    489462                // save to xml (FIXME: now print only) 
    490463                ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    491                 JAXBContext jaxbContext = JAXBContext.newInstance("org.molgenis.model.jaxb"); 
     464                JAXBContext jaxbContext = JAXBContext 
     465                                .newInstance("org.molgenis.model.jaxb"); 
    492466                Marshaller marshaller = jaxbContext.createMarshaller(); 
    493467                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
Note: See TracChangeset for help on using the changeset viewer.