Index: topia/src/java/org/codelutin/topia/persistence/serialization/SerializablePersistenceHelper.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/serialization/SerializablePersistenceHelper.java:1.1 --- /dev/null Fri Jun 10 17:16:43 2005 +++ topia/src/java/org/codelutin/topia/persistence/serialization/SerializablePersistenceHelper.java Fri Jun 10 17:16:38 2005 @@ -0,0 +1,207 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* SerializablePersistenceHelper.java +* +* Created: 10 juin 2005 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +*/ + + +package org.codelutin.topia.persistence.serialization; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Properties; + +import org.codelutin.topia.AsynchronousLoader; +import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.TopiaEntity; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.TopiaQuery; +import org.codelutin.topia.persistence.AbstractPersistenceHelper; + + +public class SerializablePersistenceHelper extends AbstractPersistenceHelper { + + public void destroy() { + System.out.println("------------------------------------------------"); + File dbFile = new File(properties.getProperty("topia.persistence.xml.dbfile")); + System.out.println("!!!!!!!!!!!!!!!! " + dbFile); + ObjectOutputStream output; + try { + output = new ObjectOutputStream(new FileOutputStream(dbFile)); + output.writeObject(entities); + output.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + HashMap entities; + + /** + * @param context + * @param properties + */ + public SerializablePersistenceHelper(TopiaContext context, Properties properties) { + super(context, properties); + File dbFile = new File(properties.getProperty("topia.persistence.xml.dbfile")); + if (dbFile.exists()) { + try { + ObjectInputStream intput = new ObjectInputStream(new FileInputStream(dbFile)); + entities = (HashMap)intput.readObject(); + intput.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } else { + entities = new HashMap(); + } + } + +/* public void finalize() { + } +*/ + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#makePersistent(org.codelutin.topia.TopiaEntity) + */ + public TopiaEntity makePersistent(TopiaEntity entity) throws TopiaException { + entity.set_topiaLastUpdateDate_(new Date()); + String entityId = entity.get_topiaId_(); + TopiaEntity doEntity = (TopiaEntity)entities.get(entityId); + if ("0".equals(entity.get_topiaVersion_())) {//L'entité vient d'être créée + if (doEntity == null) { + doEntity = entity; + entities.put(entityId, doEntity); + } else { + throw new TopiaException("Can't persist new Entity, topiaId already in use : " + entityId); + } + } else { + if (doEntity == null) { + throw new TopiaException("Can't persist entity, can't find entity for topiaId : " + entityId); + } else { + //TODO Arno : Update ??? + entities.put(entityId, doEntity); + } + } + return entity; + } + + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#update(org.codelutin.topia.TopiaEntity) + */ + public TopiaEntity update(TopiaEntity entity) throws TopiaException { + return makePersistent(entity); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#delete(org.codelutin.topia.TopiaEntity) + */ + public void delete(TopiaEntity entity) throws TopiaException { + String entityId = entity.get_topiaId_(); + entities.remove(entityId); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#find(org.codelutin.topia.TopiaQuery) + */ + public List find(TopiaQuery query) throws TopiaException { + // TODO Arno : Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#findAsynchronously(org.codelutin.topia.TopiaQuery) + */ + public AsynchronousLoader findAsynchronously(TopiaQuery query) + throws TopiaException { + // TODO Arno : Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#findInRange(org.codelutin.topia.TopiaQuery, int, int) + */ + public List findInRange(TopiaQuery query, int startIndex, int endIndex) + throws TopiaException { + // TODO Arno : Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#findInRangeAsynchronously(org.codelutin.topia.TopiaQuery, int, int) + */ + public AsynchronousLoader findInRangeAsynchronously(TopiaQuery query, + int startIndex, int endIndex) throws TopiaException { + // TODO Arno : Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#size(org.codelutin.topia.TopiaQuery) + */ + public int size(TopiaQuery query) throws TopiaException { + // TODO Arno : Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#importXML(java.lang.String) + */ + public void importXML(String xml) throws TopiaException { + // TODO Arno : Auto-generated method stub + + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#exportXML() + */ + public String exportXML() throws TopiaException { + // TODO Arno : Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.persistence.PersistenceHelper#findByTopiaId(java.lang.String) + */ + public TopiaEntity findByTopiaId(String id) throws TopiaException { + return (TopiaEntity)entities.get(id); + } + +}