Index: topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBCMultiTable.java diff -u topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBCMultiTable.java:1.1 topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBCMultiTable.java:1.2 --- topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBCMultiTable.java:1.1 Wed Aug 24 15:03:45 2005 +++ topia/src/java/org/codelutin/topia/persistence/PersistenceStorageJDBCMultiTable.java Fri Aug 26 17:53:35 2005 @@ -23,9 +23,9 @@ * Created: 20 juillet 2005 15:25:06 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update: $Date: 2005/08/24 15:03:45 $ + * Last update: $Date: 2005/08/26 17:53:35 $ * by : $Author: bpoussin $ */ @@ -169,11 +169,11 @@ SQL_ORACLE.put(SQL_TYPE_BYTES, "blob"); SQL_ORACLE.put(SQL_TYPE_OBJECT, "blob"); SQL_ORACLE.put(SQL_TYPE_LONG, "number(21)"); - SQL_ORACLE.put(SQL_TYPE_STRING, "varchar(255)"); + SQL_ORACLE.put(SQL_TYPE_STRING, "varchar(1024)"); SQL_DERBY.put(SQL_TYPE_BYTES, "blob"); SQL_DERBY.put(SQL_TYPE_OBJECT, "blob"); - SQL_DERBY.put(SQL_TYPE_STRING, "varchar(255)"); + SQL_DERBY.put(SQL_TYPE_STRING, "varchar(1024)"); SQL_DERBY.put(SQL_TYPE_BOOLEAN, "integer"); @@ -184,13 +184,13 @@ "SELECT count(*) FROM {0}"); SQL_DEFAULT.put(SQL_CREATE_TABLE_MAPPING, - "CREATE TABLE topia_mapping_table (topia_class varchar(255) NOT NULL, topia_table varchar(255) UNIQUE NOT NULL, topia_schemaVersion bigint, PRIMARY KEY (topia_class, topia_schemaVersion))"); + "CREATE TABLE topia_mapping_table (topia_class varchar(1024) NOT NULL, topia_table varchar(1024) UNIQUE NOT NULL, topia_schemaVersion bigint, PRIMARY KEY (topia_class, topia_schemaVersion))"); SQL_DEFAULT.put(SQL_CREATE_TABLE_HERITAGE, - "CREATE TABLE topia_heritage_table (topia_class varchar(255) NOT NULL, topia_child varchar(255) NOT NULL, PRIMARY KEY (topia_class, topia_child))"); + "CREATE TABLE topia_heritage_table (topia_class varchar(1024) NOT NULL, topia_child varchar(1024) NOT NULL, PRIMARY KEY (topia_class, topia_child))"); // {0} est a remplacer par le nom de la table // {1} est a remplacer par (, field_ field_)+ SQL_DEFAULT.put(SQL_CREATE_TABLE_DATA, - "CREATE TABLE {0} (topia_id varchar(255) NOT NULL, topia_longdate bigint, topia_class varchar(255), topia_isNew boolean, topia_isDeleted boolean{1}, PRIMARY KEY (topia_id, topia_longdate))"); + "CREATE TABLE {0} (topia_id varchar(1024) NOT NULL, topia_longdate bigint, topia_class varchar(1024), topia_isNew boolean, topia_isDeleted boolean{1}, PRIMARY KEY (topia_id, topia_longdate))"); // {0} est a remplacer par le nom de la table SQL_DEFAULT.put(SQL_LOAD_ALL, "SELECT * FROM {0}"); @@ -248,19 +248,19 @@ // SURCHARGE DES REQUETES PAR DEFAUT pour les specificités de chaque base SQL_MYSQL.put(SQL_CREATE_TABLE_DATA, - "CREATE TABLE {0} (topia_id varchar(255) NOT NULL, topia_longdate bigint, topia_class varchar(255), topia_isNew bool, topia_isDeleted bool{1} , PRIMARY KEY (topia_id, topia_longdate))"); + "CREATE TABLE {0} (topia_id varchar(1024) NOT NULL, topia_longdate bigint, topia_class varchar(1024), topia_isNew bool, topia_isDeleted bool{1} , PRIMARY KEY (topia_id, topia_longdate))"); SQL_ORACLE.put(SQL_CREATE_TABLE_MAPPING, - "CREATE TABLE topia_mapping_table (topia_class varchar(255) NOT NULL, topia_table varchar(255) UNIQUE NOT NULL, topia_schemaVersion number(21), PRIMARY KEY (topia_class, topia_schemaVersion))"); + "CREATE TABLE topia_mapping_table (topia_class varchar(1024) NOT NULL, topia_table varchar(1024) UNIQUE NOT NULL, topia_schemaVersion number(21), PRIMARY KEY (topia_class, topia_schemaVersion))"); SQL_ORACLE.put(SQL_CREATE_TABLE_DATA, - "CREATE TABLE {0} (topia_id varchar(255) NOT NULL, topia_longdate number(21), topia_class varchar(255), topia_isNew number(1), topia_isDeleted number(1){1} , PRIMARY KEY (topia_id, topia_longdate))"); + "CREATE TABLE {0} (topia_id varchar(1024) NOT NULL, topia_longdate number(21), topia_class varchar(1024), topia_isNew number(1), topia_isDeleted number(1){1} , PRIMARY KEY (topia_id, topia_longdate))"); SQL_ORACLE.put(SQL_GET_ALL_ID, "SELECT DISTINCT(topia_id) FROM {0} WHERE (topia_longdate in (" + Util.SQL_IDS_KEY + ") OR (topia_longdate>0 AND topia_longdate<=?)) AND topia_id NOT IN (SELECT topia_id FROM {0} WHERE topia_isDeleted=1 AND (topia_longdate in (" + Util.SQL_IDS_KEY + ") OR (topia_longdate>0 AND topia_longdate<=?)))"); SQL_DERBY.put(SQL_CREATE_TABLE_DATA, - "CREATE TABLE {0} (topia_id varchar(255) NOT NULL, topia_longdate bigint, topia_class varchar(255), topia_isNew integer, topia_isDeleted integer{1}, PRIMARY KEY (topia_id, topia_longdate))"); + "CREATE TABLE {0} (topia_id varchar(1024) NOT NULL, topia_longdate bigint, topia_class varchar(1024), topia_isNew integer, topia_isDeleted integer{1}, PRIMARY KEY (topia_id, topia_longdate))"); SQL_DERBY.put(SQL_GET_ALL_ID, "SELECT DISTINCT(topia_id) FROM {0} WHERE (topia_longdate in (" + Util.SQL_IDS_KEY + ") OR (topia_longdate>0 AND topia_longdate<=?)) AND topia_id NOT IN (SELECT topia_id FROM {0} WHERE topia_isDeleted=1 AND (topia_longdate in (" + Util.SQL_IDS_KEY + ") OR (topia_longdate>0 AND topia_longdate<=?)))"); } @@ -486,6 +486,32 @@ } /** + * Genere un nom de table convenable. La longueur du nom sera inferieur + * a 60 caractere au total, sinon le nom est tronqué par le debut. + */ + protected String generateTableName(Class clazz){ + String classname = clazz.getName(); + long schemaVersion = ClassInfoHelper.getSchemaVersion(clazz); + String result; + // on ne prend pas le nom de package donc il se peut qu'il y ait + // collision dans les noms de table, mais cela est très limité car, + // le nom de table final contient la version, qui est calculé + // de facon pratiquement unique pour chaque objet + if(ClassInfoHelper.isAssociationClass(clazz)){ + result = classname.replaceAll("^.*_(\\w+_\\w+)__.*_(\\w+_\\w+)$", "$1__$2"); + } else { + result= classname.substring(classname.lastIndexOf(".") + 1); + } + result += schemaVersion; + + if(result.length() > 60){ + result = result.substring(result.length() - 60); + } + + return result; + } + + /** * Donne le nom de la table a utiliser pour un objet donnés. * Si l'objet n'a pas encore de mapping les entrées adequate sont creées. * le nom de la table depend de la version du schema @@ -510,7 +536,7 @@ sta.close(); if(result == null){ - result = classname.substring(classname.lastIndexOf(".") + 1) + schemaVersion; + result = generateTableName(clazz); log.info("Ajout de la table " + result + " pour " + classname + "("+schemaVersion+")"); sql = SQL.getProperty(SQL_INSERT_MAPPING); sta = conn.prepareStatement(sql); @@ -779,6 +805,12 @@ } protected JDBCTransformer getTransformer(Class type) { + // Les entities sont transformer en String, dans le proxy + // Il faut donc gerer les entities comme des Strings + if(TopiaEntity.class.isAssignableFrom(type)){ + type = String.class; + } + JDBCTransformer result = JDBCTransformerFactory.getTransformer(type); if(result == null){ log.debug("Can't get transformer for " + type.getName() + " get Object transformer"); @@ -851,10 +883,10 @@ // si pas UNLOADED, ca veut dire que l'utilisateur a modifier // la valeur, et qu'il ne faut pas perdre cette valeur if(tpo.getData().getField(fieldname) == tpo.getData().UNLOADED){ - JDBCTransformer t = getTransformer(ClassInfoHelper - .fieldType(clazz, fieldname)); + Class fieldType = ClassInfoHelper.fieldType(clazz, fieldname); + JDBCTransformer t = getTransformer(fieldType); Object value = t.getValue(rs, - objectToDatabaseFieldName(fieldname)); + objectToDatabaseFieldName(fieldname)); setContext(context, value); tpo.getData().setField(fieldname, value); } @@ -862,57 +894,6 @@ tpo.getAskedFields().clear(); } - // /** - // * Charge les champs demandé dans management.getAskedFields(). Si le champs - // * existe déja dans data, alors il n'est pas chargé meme s'il est demandé - // * et vide la liste des champs demandée. - // * @param conn la connection a utiliser pour acceder a la base - // * @param tt la transaction pour lequel la demande est faite - // * @param tpo le tpo pour lequel il faut charger les données - // * @param data l'objet data a charger, on utilise pas l'objet data de tpo - // * car quelque fois il faut charger des données sans faire - // * attention a ce qu'il y a deja dans le tpo - // * @param forceLoadAllField force le chargement de tous les champs meme ceux - // * non demandé - // */ - // protected void loadData(Connection conn, TopiaContext context, - // TopiaPersistenceObject tpo, - // TopiaPersistenceObjectData data, - // boolean forceLoadAllField) throws SQLException, TopiaPersistenceException { - // synchronized(tpo){ - // Class clazz = tpo.getManagement().getEntityClass(); - // String tablename = getTableName(clazz); - // String sql = MessageFormat.format(SQL.getProperty(SQL_LOAD_DATA), tablename); - // PreparedStatement sta = conn.prepareStatement(sql); - // try{ - // sta.setString(1, tpo.getManagement().getId()); - // sta.setLong(2, tpo.getManagement().getDate()); - // ResultSet rr = sta.executeQuery(); - // if(rr.next()){ - // Collection fieldnames = null; - // if(forceLoadAllField) { - // fieldnames = Arrays.asList(ClassInfoHelper.fields(clazz)); - // else { - // fieldnames = tpo.getAskedFields(); - // } - // for(String fieldname: fieldnames){ - // if(data.getField(fieldname) == data.UNLOADED){ - // JDBCTransformer t = getTransformer(ClassInfoHelper - // .fieldType(clazz, fieldname)); - // Object value = t.getValue(rr, - // objectToDatabaseFieldName(fieldname)); - // setContext(context, value); - // data.setField(fieldname, value); - // } - // } - // tpo.getAskedFields().clear(); - // } - // }finally{ - // sta.close(); - // } - // } - // } - /** * Sauve tous les champs modifier et vide la liste des champs modifiée * @param forceInsert si vrai fait un insert, sinon fait un update @@ -963,8 +944,8 @@ insert.setBoolean(i++, tpo.getManagement().isDeleted()); for(String fieldname: fieldnames){ Object value = tpo.getData().get(fieldname); - JDBCTransformer t = getTransformer(ClassInfoHelper - .fieldType(clazz, fieldname)); + Class fieldType = ClassInfoHelper.fieldType(clazz, fieldname); + JDBCTransformer t = getTransformer(fieldType); t.setValue(insert, i++, value); } Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.9 topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.10 --- topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.9 Wed Aug 24 15:03:45 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java Fri Aug 26 17:53:35 2005 @@ -23,9 +23,9 @@ * Created: Jul 16, 2005 * * @author Benjamin POUSSIN - * @version $Revision: 1.9 $ + * @version $Revision: 1.10 $ * - * Last update : $Date: 2005/08/24 15:03:45 $ + * Last update : $Date: 2005/08/26 17:53:35 $ * by : $Author: bpoussin $ */ @@ -150,6 +150,8 @@ A result = TopiaPersistenceProxy.newProxy(this, entityClass, tpo); getCache().put(id, result); + + getStorage().store(getContext(), tpo); return result; } Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java:1.8 topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java:1.9 --- topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java:1.8 Wed Aug 24 15:03:45 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java Fri Aug 26 17:53:35 2005 @@ -23,9 +23,9 @@ * Created: 16 juillet 2005 23:52:35 CEST * * @author Benjamin POUSSIN -* @version $Revision: 1.8 $ +* @version $Revision: 1.9 $ * -* Last update: $Date: 2005/08/24 15:03:45 $ +* Last update: $Date: 2005/08/26 17:53:35 $ * by : $Author: bpoussin $ */ @@ -159,14 +159,23 @@ String otherField, TopiaEntity entity) throws Exception { // 1-0 ou 1-1 ou 1-N - TopiaEntity result = (TopiaEntity)MethodUtils.invokeMethod( - assoPS, - "findBy" + StringUtils.capitalize(otherField), - entity); - if(result != null){ - result = (TopiaEntity)MethodUtils.invokeExactMethod(result, - "get" + StringUtils.capitalize(thisField), null); + TopiaQuery query = assoPS.newQuery() + .select(thisField) + .where(otherField+"=?") + .addArg(entity); + List col = ph.find(query); + TopiaEntity result = null; + if(col.size() > 0){ + result = col.get(0); } + // TopiaEntity result = (TopiaEntity)MethodUtils.invokeMethod( + // assoPS, + // "findBy" + StringUtils.capitalize(otherField), + // entity); + // if(result != null){ + // result = (TopiaEntity)MethodUtils.invokeExactMethod(result, + // "get" + StringUtils.capitalize(thisField), null); + // } return result; } @@ -183,23 +192,38 @@ // pour conserver la coherence 1-1 // recuperation de l'objet a supprimer s'il existe - TopiaEntity s = (TopiaEntity)MethodUtils.invokeMethod( - assoPS, - "findBy" + StringUtils.capitalize(thisField), - value); + TopiaQuery query = assoPS.newQuery() + .where(thisField+"=?") + .addArg(value); + List col = ph.find(query); + TopiaEntity s = null; + if(col.size() > 0){ + s = col.get(0); + } + // TopiaEntity s = (TopiaEntity)MethodUtils.invokeMethod( + // assoPS, + // "findBy" + StringUtils.capitalize(thisField), + // value); if(s != null){ assoPS.delete(s); } } - TopiaEntity r; + TopiaEntity r = null; //On veut faire un entity.setXXX(value) // if(assoPS.getEntityClass() != entity.getEntityClass()){ // recuperation de l'objet a modifier s'il existe - r = (TopiaEntity)MethodUtils.invokeMethod( - assoPS, - "findBy" + StringUtils.capitalize(otherField), - entity); + TopiaQuery query = assoPS.newQuery() + .where(otherField+"=?") + .addArg(entity); + List col = ph.find(query); + if(col.size() > 0){ + r = col.get(0); + } + // r = (TopiaEntity)MethodUtils.invokeMethod( + // assoPS, + // "findBy" + StringUtils.capitalize(otherField), + // entity); if(r == null){ r = assoPS.create(); MethodUtils.invokeMethod(r, @@ -228,17 +252,26 @@ boolean isAnyToOne) throws Exception { TopiaEntity r = null; if(isAnyToOne){ - Object[] args = new Object[2]; - if(thisField.compareTo(otherField) < 0){ - args[0] = value; - args[1] = entity; - }else{ - args[0] = entity; - args[1] = value; + TopiaQuery query = assoPS.newQuery() + .where(thisField+"=? and "+otherField+"=?") + .addArg(value).addArg(entity); + List col = ph.find(query); + if(col.size() > 0){ + r = col.get(0); } - r = (TopiaEntity)MethodUtils.invokeMethod( - assoPS, "findByAttributes", args); - //TODO A tester dans le cas où la classe d'assoc existe en BD ! + // Object[] args = new Object[2]; + // if(thisField.compareTo(otherField) < 0){ + // args[0] = value; + // args[1] = entity; + // }else{ + // args[0] = entity; + // args[1] = value; + // } + // r = (TopiaEntity)MethodUtils.invokeMethod( + // assoPS, "findByAttributes", args); + + //TODO A tester dans le cas où la classe d'assoc existe en BD !, + // BB je pense que c fait, donc supprimer le TODO if(r == null){ r = assoPS.create(); MethodUtils.invokeMethod(r, @@ -250,8 +283,7 @@ TopiaQuery query = assoPS.newQuery() .where(thisField+"=? and "+otherField+"=?") .addArg(value).addArg(entity); - Collection col = assoPS.find(query); - if(col.size() == 0){ + if(ph.size(query) == 0){ // n'existe pas encore, donc on cree la relation r = assoPS.create(); MethodUtils.invokeMethod(r, @@ -279,7 +311,7 @@ TopiaQuery query = assoPS.newQuery() .where(thisField+"=? and "+otherField+"=?") .addArg(value).addArg(entity); - List col = assoPS.find(query); + List col = ph.find(query); if(col.size() != 0){ TopiaEntity r = col.get(0); assoPS.delete(r); @@ -291,10 +323,14 @@ String thisField, String otherField, TopiaEntity entity) throws Exception { - Collection col = (Collection)MethodUtils.invokeMethod( - assoPS, - "findAllBy" + StringUtils.capitalize(otherField), - entity); + TopiaQuery query = assoPS.newQuery() + .where(otherField) + .addArg(entity); + Collection col = ph.find(query); + // Collection col = (Collection)MethodUtils.invokeMethod( + // assoPS, + // "findAllBy" + StringUtils.capitalize(otherField), + // entity); for(TopiaEntity r: col){ assoPS.delete(r); } @@ -305,16 +341,22 @@ String thisField, String otherField, TopiaEntity entity) throws Exception { - Collection col = (Collection)MethodUtils.invokeMethod( - assoPS, - "findAllBy" + StringUtils.capitalize(otherField), - entity); - List result = new ArrayList(col.size()); - for(TopiaEntity r: col){ - TopiaEntity e = (TopiaEntity)MethodUtils.invokeExactMethod(r, - "get" + StringUtils.capitalize(thisField), null); - result.add(e); - } + TopiaQuery query = assoPS.newQuery() + .select(thisField) + .where(otherField+"=?") + .addArg(entity); + List result = ph.find(query); + + // Collection col = (Collection)MethodUtils.invokeMethod( + // assoPS, + // "findAllBy" + StringUtils.capitalize(otherField), + // entity); + // List result = new ArrayList(col.size()); + // for(TopiaEntity r: col){ + // TopiaEntity e = (TopiaEntity)MethodUtils.invokeExactMethod(r, + // "get" + StringUtils.capitalize(thisField), null); + // result.add(e); + // } return Collections.unmodifiableList(result); }