Changeset 2600


Ignore:
Timestamp:
01/27/10 23:23:31 (2 years ago)
Author:
mswertz
Message:

fixes molgenis:#65

Location:
molgenis/3.3/src/org/molgenis/model
Files:
2 edited

Legend:

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

    r2597 r2600  
    184184                                        while (rsXref.next()) 
    185185                                        { 
    186                                                  
    187186                                                Tuple xrefInfo = new ResultSetTuple(rsXref); 
    188                                                 logger.debug(xrefInfo); 
    189187                                                if (xrefInfo.getString("FKCOLUMN_NAME").equals(fieldInfo.getString("COLUMN_NAME"))) 
    190188                                                { 
     
    325323                                                String xrefEntityName = f.getXrefField().substring(0, f.getXrefField().indexOf(".")); 
    326324                                                String xrefFieldName = f.getXrefField().substring(f.getXrefField().indexOf(".") + 1); 
    327                                                 logger.info("trying to find fields for "+f.getXrefField()); 
    328                                                 logger.info("finding entity "+m.getEntity(xrefEntityName)); 
    329                                                  
    330325                                                //reset the xref entity to the uppercase version 
    331326                                                f.setXrefField(m.getEntity(xrefEntityName).getName()+"."+xrefFieldName); 
    332327                                                 
    333 //                                              if(m.getEntity(xrefEntityName) == null) 
    334 //                                              { 
    335 //                                                      for(Entity entity: m.getEntities()) 
    336 //                                                              logger.debug(entity.getName()); 
    337 //                                              } 
    338328                                                 
    339329                                                for (Field labelField : m.getEntity(xrefEntityName).getFields()) 
     
    374364 
    375365                        // TODO GUESS the type="mref" 
    376                         // rule: any entity with two xref fields should be a mref 
     366                        // rule: any entity with two xref fields and optional autoid field should be a mref 
    377367                        List<Entity> toBeRemoved = new ArrayList<Entity>(); 
    378368                        for (Entity e : m.getEntities()) 
    379369                        { 
    380370 
    381                                 // TODO also support three column many to many tables 
    382                                 if (e.getFields().size() == 2) 
     371                                if (e.getFields().size() <= 3) 
    383372                                { 
    384373                                        int xrefs = 0; 
    385                                         // String idField = null; 
     374                                        String idField = null; 
    386375                                        //the column refering to 'localEntity' 
    387376                                        String localIdField = null; 
     
    399388                                        for (Field f : e.getFields()) 
    400389                                        { 
    401 //                                              if (Field.Type.AUTOID.equals(f.getType())) 
    402 //                                              { 
    403 //                                                      idField = f.getName(); 
    404 //                                              } 
    405                                                 // else 
    406                                                 if (Field.Type.XREF_SINGLE.equals(f.getType())) 
     390                                                if (Field.Type.AUTOID.equals(f.getType())) 
     391                                                { 
     392                                                        idField = f.getName(); 
     393                                                } 
     394                                                else if (Field.Type.XREF_SINGLE.equals(f.getType())) 
    407395                                                { 
    408396                                                        xrefs++; 
     
    426414                                        // if valid mref, drop this entity and add mref field to 
    427415                                        // entity 
    428                                         if (xrefs == 2) 
     416                                        if (xrefs == 2 && (e.getFields().size() == 2 || idField != null)) 
    429417                                        { 
    430418                                                //add mref on 'local' end 
  • molgenis/3.3/src/org/molgenis/model/MolgenisModelValidator.java

    r2598 r2600  
    1111import org.molgenis.model.elements.Field; 
    1212import org.molgenis.model.elements.Model; 
     13import org.molgenis.model.elements.Module; 
     14import org.molgenis.model.elements.UISchema; 
    1315import org.molgenis.model.elements.Unique; 
    1416import org.molgenis.model.elements.View; 
     
    2224        { 
    2325                logger.debug("validating model and adding defaults:"); 
    24                  
     26 
    2527                // validate the model 
    26                 validateReservedWords(model, options); 
     28                validateNamesAndReservedWords(model, options); 
    2729                validateExtendsAndImplements(model); 
    2830                validateKeys(model); 
     
    4345                        addTypeFieldInSubclasses(model); 
    4446                } 
    45                  
     47 
    4648                copy_Fields_To_Subclass_To_Enforce_Constraints(model); 
    47                  
    48  
    4949 
    5050        } 
     
    5252        public static void copyDefaultXrefLabels(Model model) throws MolgenisModelException 
    5353        { 
    54                 for(Entity e: model.getEntities()) 
    55                 { 
    56                         for(Field f: e.getFields()) 
    57                         { 
    58                                 if(f.getType().equals(Field.Type.XREF_SINGLE) || f.getType().equals(Field.Type.XREF_MULTIPLE)) 
    59                                 { 
    60                                         if(f.getXRefLabelString() == null || f.getXRefLabelString().equals(f.getXRefField())) 
     54                for (Entity e : model.getEntities()) 
     55                { 
     56                        for (Field f : e.getFields()) 
     57                        { 
     58                                if (f.getType().equals(Field.Type.XREF_SINGLE) || f.getType().equals(Field.Type.XREF_MULTIPLE)) 
     59                                { 
     60                                        if (f.getXRefLabelString() == null || f.getXRefLabelString().equals(f.getXRefField())) 
    6161                                        { 
    6262                                                Entity xref_entity = model.getEntity(f.getXRefEntity()); 
     
    6666                        } 
    6767                } 
    68                  
     68 
    6969        } 
    7070 
     
    285285        { 
    286286                logger.debug("validate xref_field and xref_label references..."); 
    287                  
     287 
    288288                // validate foreign key relations 
    289289                for (Entity entity : model.getEntities()) 
     
    300300 
    301301                                        Entity xref_entity = model.getEntity(xref_entity_name); 
    302                                          
     302 
    303303                                        if (xref_entity == null) throw new MolgenisModelException("xref entity '" + xref_entity_name 
    304304                                                        + "' does not exist for field " + entityname + "." + fieldname); 
    305                                          
    306                                         if(!xref_entity.getName().equals(field.getXRefEntity())) throw new MolgenisModelException("xref entity '" + xref_entity_name 
    307                                                         + "' does not exist for field " + entityname + "." + fieldname +" (note: entity names are case-sensitive)"); 
     305 
     306                                        if (!xref_entity.getName().equals(field.getXRefEntity())) throw new MolgenisModelException( 
     307                                                        "xref entity '" + xref_entity_name + "' does not exist for field " + entityname + "." 
     308                                                                        + fieldname + " (note: entity names are case-sensitive)"); 
    308309 
    309310                                        if (xref_entity.isAbstract()) throw new MolgenisModelException( 
     
    318319                                        if (xref_label == null) throw new MolgenisModelException("xref label '" + xref_label_name 
    319320                                                        + "' does not exist for field " + entityname + "." + fieldname); 
    320                                          
     321 
    321322                                        if (xref_label.isNillable()) throw new MolgenisModelException("'" + xref_label_name 
    322                                                         + "' cannot be used as xref label for field '" + entityname + "." + fieldname+"' because it can be null (nullable='true')"); 
    323  
    324                                         if (xref_field.getType().equals(Field.Type.TEXT)) throw new MolgenisModelException( 
    325                                                         "xref field '" + xref_field_name + "' is of illegal type 'TEXT' for field " + entityname 
    326                                                                         + "." + fieldname); 
     323                                                        + "' cannot be used as xref label for field '" + entityname + "." + fieldname 
     324                                                        + "' because it can be null (nullable='true')"); 
     325 
     326                                        if (xref_field.getType().equals(Field.Type.TEXT)) throw new MolgenisModelException("xref field '" 
     327                                                        + xref_field_name + "' is of illegal type 'TEXT' for field " + entityname + "." + fieldname); 
    327328 
    328329                                        boolean isunique = false; 
     
    432433                                                        field.setHidden(true); 
    433434 
    434                                                         logger.debug( 
    435                                                                         "copy primary key " + field.getName() + " from interface " + iface.getName() 
    436                                                                                         + " to " + entity.getName()); 
     435                                                        logger.debug("copy primary key " + field.getName() + " from interface " + iface.getName() 
     436                                                                        + " to " + entity.getName()); 
    437437                                                        entity.addField(field); 
    438438                                                        entity.addKey(field, iface.getKeys().get(0).getDescription()); 
     
    486486 
    487487                                                        entity.addField(field); 
    488                                                         logger.debug( 
    489                                                                         "copy primary key " + field.getName() + " from superclass " + parent.getName() 
    490                                                                                         + " to " + entity.getName()); 
     488                                                        logger.debug("copy primary key " + field.getName() + " from superclass " + parent.getName() 
     489                                                                        + " to " + entity.getName()); 
    491490                                                        keys.add(field); 
    492491                                                } 
     
    582581        } 
    583582 
    584         public static void validateReservedWords(Model model, MolgenisOptions options) throws MolgenisModelException 
     583        public static void validateNamesAndReservedWords(Model model, MolgenisOptions options) 
     584                        throws MolgenisModelException 
    585585        { 
    586586                logger.debug("check for JAVA and SQL reserved words..."); 
     
    591591                if (options.db_driver.contains("hsql")) keywords.addAll(Arrays.asList(HSQL_KEYWORDS)); 
    592592 
     593                if (model.getName().contains(" "))  
     594                { 
     595                        throw new MolgenisModelException("model name '" + model.getName() 
     596                                        + "' illegal: it cannot contain spaces. Use 'label' if you want to show a name with spaces."); 
     597                } 
     598                 
     599                if(!containsOnlyLetters(model.getName())) 
     600                { 
     601                        throw new MolgenisModelException("model name '" + model.getName() 
     602                                        + "' illegal: it can only contain letters, no numbers or dots"); 
     603                } 
     604 
     605                for (Module m : model.getModules()) 
     606                { 
     607                        if (m.getName().contains(" ")) 
     608                        { 
     609                                throw new MolgenisModelException("module name '" + m.getName() 
     610                                                + "' illegal: it cannot contain spaces. Use 'label' if you want to show a name with spaces."); 
     611                        } 
     612                        if(!containsOnlyLetters(m.getName())) 
     613                        { 
     614                                throw new MolgenisModelException("module name '" + m.getName() 
     615                                                + "' illegal: it can only contain letters, no numbers or dots"); 
     616                        } 
     617                         
     618                } 
     619 
    593620                for (Entity e : model.getEntities()) 
    594621                { 
     622                        if (e.getName().contains(" ")) 
     623                        { 
     624                                throw new MolgenisModelException("entity name '" + e.getName() 
     625                                                + "' cannot contain spaces. Use 'label' if you want to show a name with spaces."); 
     626                        } 
     627 
    595628                        if (keywords.contains(e.getName().toUpperCase()) || keywords.contains(e.getName().toLowerCase())) 
    596629                        { 
    597 //                              e.setName(e.getName() + "_"); 
    598 //                              logger.warn("entity name '" + e.getName() + "' illegal:" + e.getName() + " is a reserved word"); 
    599                                  throw new MolgenisModelException("entity name '" + 
    600                                  e.getName() + "' illegal:" + e.getName() + " is a reserved JAVA and/or SQL word and cannot be used for entity name"); 
     630                                // e.setName(e.getName() + "_"); 
     631                                // logger.warn("entity name '" + e.getName() + "' illegal:" + 
     632                                // e.getName() + " is a reserved word"); 
     633                                throw new MolgenisModelException("entity name '" + e.getName() + "' illegal:" + e.getName() 
     634                                                + " is a reserved JAVA and/or SQL word and cannot be used for entity name"); 
    601635                        } 
    602636                        for (Field f : e.getFields()) 
    603637                        { 
     638                                if (f.getName().contains(" ")) 
     639                                { 
     640                                        throw new MolgenisModelException("field name '" + e.getName() + "." + f.getName() 
     641                                                        + "' cannot contain spaces. Use 'label' if you want to show a name with spaces."); 
     642                                } 
     643 
    604644                                if (keywords.contains(f.getName().toUpperCase()) || keywords.contains(f.getName().toLowerCase())) 
    605645                                { 
    606 //                                      f.setName(f.getName() + "_"); 
    607 //                                      logger.warn("field name '" + f.getName() + "' illegal:" + f.getName() + " is a reserved word"); 
    608                                          throw new MolgenisModelException("field name '" + 
    609                                          e.getName() + "." + f.getName() + "' illegal: " + 
    610                                         f.getName() + " is a reserved JAVA and/or SQL word"); 
     646                                        // f.setName(f.getName() + "_"); 
     647                                        // logger.warn("field name '" + f.getName() + "' illegal:" + 
     648                                        // f.getName() + " is a reserved word"); 
     649                                        throw new MolgenisModelException("field name '" + e.getName() + "." + f.getName() + "' illegal: " 
     650                                                        + f.getName() + " is a reserved JAVA and/or SQL word"); 
    611651                                } 
    612652 
     
    618658                                                                        .toLowerCase()))) 
    619659                                        { 
    620 //                                              f.setXRefEntity(f.getXRefEntity() + "_"); 
    621 //                                              logger.warn("field.xref-entity name '" + xref_entity + "' illegal:" + xref_entity 
    622 //                                                              + " is a reserved word"); 
    623                                                  throw new MolgenisModelException("xref_entity reference from field '" + 
    624                                                                  e.getName() + "." + f.getName() + "' illegal: " + xref_entity + " is a reserved JAVA and/or SQL word"); 
     660                                                // f.setXRefEntity(f.getXRefEntity() + "_"); 
     661                                                // logger.warn("field.xref-entity name '" + xref_entity 
     662                                                // + "' illegal:" + xref_entity 
     663                                                // + " is a reserved word"); 
     664                                                throw new MolgenisModelException("xref_entity reference from field '" + e.getName() + "." 
     665                                                                + f.getName() + "' illegal: " + xref_entity + " is a reserved JAVA and/or SQL word"); 
    625666                                        } 
    626667 
     
    667708                        } 
    668709                } 
     710                 
     711                for(UISchema screen: model.getUserinterface().getAllChildren()) 
     712                { 
     713                        if(screen.getName().contains(" ")) 
     714                        { 
     715                                throw new MolgenisModelException("ui element '"+screen.getName()+"illegal: it cannot contain spaces. If you want to have a name with spaces use the 'label' attribute"); 
     716                        } 
     717                } 
    669718        } 
    670719 
     
    723772                                        for (Field f : aKey.getFields()) 
    724773                                        { 
    725 //                                              f = f.getEntity().getField(f.getName()); 
    726 //                                              // f is 
    727 //                                              // a 
    728 //                                              // copy 
    729 //                                              // of 
    730 //                                              // the 
    731 //                                              // real 
    732 //                                              // field 
    733 //                                              // !!! 
     774                                                // f = f.getEntity().getField(f.getName()); 
     775                                                // // f is 
     776                                                // // a 
     777                                                // // copy 
     778                                                // // of 
     779                                                // // the 
     780                                                // // real 
     781                                                // // field 
     782                                                // // !!! 
    734783 
    735784                                                if (e.getField(f.getName()) == null) 
     
    774823         */ 
    775824        private static final String[] MYSQL_KEYWORDS = 
    776         { "ADD", "ALL", "ALTER", "ANALYZE", "AND", "AS", "ASC", "ASENSITIVE", "BEFORE", "BETWEEN", "BIGINT", "BINARY", 
    777                         "BLOB", "BOTH", "BY", "CALL", "CASCADE", "CASE", "CHANGE", "CHAR", "CHARACTER", "CHECK", "COLLATE", 
    778                         "COLUMN", "CONDITION", "CONNECTION", "CONSTRAINT", "CONTINUE", "CONVERT", "CREATE", "CROSS", 
     825        { "Type", "ADD", "ALL", "ALTER", "ANALYZE", "AND", "AS", "ASC", "ASENSITIVE", "BEFORE", "BETWEEN", "BIGINT", 
     826                        "BINARY", "BLOB", "BOTH", "BY", "CALL", "CASCADE", "CASE", "CHANGE", "CHAR", "CHARACTER", "CHECK", 
     827                        "COLLATE", "COLUMN", "CONDITION", "CONNECTION", "CONSTRAINT", "CONTINUE", "CONVERT", "CREATE", "CROSS", 
    779828                        "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "CURSOR", "DATABASE", "DATABASES", 
    780829                        "DAY_HOUR", "DAY_MICROSECOND", "DAY_MINUTE", "DAY_SECOND", "DEC", "DECIMAL", "DECLARE", "DEFAULT", 
     
    803852         */ 
    804853        protected static final String[] JAVA_KEYWORDS = 
    805         { "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized", "boolean", "do", 
    806                         "if", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", 
    807                         "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", 
    808                         "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", 
    809                         "volatile", "const", "float", "native", "super", "while" }; 
    810          
    811         protected static final String[] JAVASCRIPT_KEYWORDS= 
    812         { 
    813                 "function" 
    814         }; 
    815          
    816         private static String firstToUpper( String string ) 
    817         { 
    818                 if( string == null ) 
    819                         return " NULL "; 
    820                 if( string instanceof String && string.length() > 0 ) 
    821                         return string.substring(0, 1).toUpperCase() + string.substring(1); 
     854        { "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized", 
     855                        "boolean", "do", "if", "private", "this", "break", "double", "implements", "protected", "throw", "byte", 
     856                        "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", 
     857                        "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", 
     858                        "long", "strictfp", "volatile", "const", "float", "native", "super", "while" }; 
     859 
     860        protected static final String[] JAVASCRIPT_KEYWORDS = 
     861        { "function" }; 
     862 
     863        private static String firstToUpper(String string) 
     864        { 
     865                if (string == null) return " NULL "; 
     866                if (string instanceof String && string.length() > 0) return string.substring(0, 1).toUpperCase() 
     867                                + string.substring(1); 
    822868                else 
    823869                        return " ERROR[STRING EMPTY] "; 
    824870        } 
     871         
     872        private static boolean containsOnlyLetters(String name) 
     873        { 
     874                for (int i = 0; i < name.length(); i++) 
     875                { 
     876                        if (!Character.isLetter(name.charAt(i))) 
     877                        { 
     878 
     879                                return false; 
     880                        } 
     881                } 
     882                return true; 
     883        } 
    825884 
    826885} 
Note: See TracChangeset for help on using the changeset viewer.