/** * Remote PIMCollection wrapper class * basically a pass-through class. * we can't avoid this since each remote method must * throw RemoteException (and the methods in * PIMCollection don't do this). * * RemotePIMCollection takes care of ensuring that * each PIMEntity has a unique id. * */ import java.util.*; import java.rmi.*; import java.rmi.server.*; import java.io.*; class RemotePIMCollectionImpl extends UnicastRemoteObject implements RemotePIMCollection { static int maxID; PIMCollection p; RemotePIMCollectionImpl() throws RemoteException { super(); p = new PIMCollection(); } RemotePIMCollectionImpl(PIMCollection initial) throws RemoteException { super(); // we are importing - assign new ids p = initial; assignIDs(); } public PIMCollection getNotes() throws RemoteException { PIMCollection pc = new PIMCollection(); pc.addAll(p.getNotes()); return pc; } public PIMCollection getNotes(String owner) throws RemoteException { PIMCollection pc = new PIMCollection(); pc.addAll(p.getNotes(owner)); return pc; } public PIMCollection getTodos() throws RemoteException { PIMCollection pc = new PIMCollection(); pc.addAll(p.getTodos()); return pc; } public PIMCollection getTodos(String owner) throws RemoteException { PIMCollection pc = new PIMCollection(); pc.addAll( p.getTodos(owner)); return pc; } public PIMCollection getAppointments() throws RemoteException { PIMCollection pc = new PIMCollection(); pc.addAll( p.getAppointments()); return pc; } public PIMCollection getAppointments(String owner) throws RemoteException { PIMCollection pc = new PIMCollection(); pc.addAll( p.getAppointments( owner)); return pc; } public PIMCollection getContacts() throws RemoteException { PIMCollection pc = new PIMCollection(); pc.addAll( p.getContacts()); return pc; } public PIMCollection getContacts(String owner) throws RemoteException { PIMCollection pc = new PIMCollection(); pc.addAll( p.getContacts( owner)); return pc; } public PIMCollection getItemsForDate(Date d) throws RemoteException { return p.getItemsForDate(d); } public PIMCollection getItemsForDate(Date d, String owner) throws RemoteException { return p.getItemsForDate(d,owner); } public PIMCollection getAll() throws RemoteException { return p.getAll(); } public PIMCollection getAllByOwner(String owner) throws RemoteException { return p.getAllByOwner( owner); } public boolean add(PIMEntity e) { e.setId(maxID++); boolean rval = p.add(e); if (rval) { writeCollection("pimdata"); } return rval; } /** * The only interesting method. * * Uses the id field in each PIMEntity */ public boolean update(PIMEntity e) { PIMEntity old = getById(e.getId()); if (old==null) { System.out.println("Updating item id not found"); // nothing found that matches return(false); } else { System.out.println("Updating item id " + e.getId()); // remove the old one p.remove(old); // add the new one p.add(e); writeCollection("pimdata"); return(true); } } /** * removal is done by ID only */ public boolean remove(PIMEntity e) { PIMEntity old = getById(e.getId()); if (old==null) { System.out.println("Removing item id not found"); // nothing found that matches return(false); } else { System.out.println("Updating item id " + e.getId()); // remove the old one p.remove(old); writeCollection("pimdata"); return(true); } } /* ============================================ * helper methods used to support the unqiue ID characteristic * of RemotePIMCollections */ /** * find a PIMEntity that has a given ID * returns null if none found */ PIMEntity getById(int id) { Iterator i = p.iterator(); while (i.hasNext()) { PIMEntity pe = (PIMEntity) i.next(); if ( pe.getId() == id) { return(pe); } } return(null); } /** * assigns unique IDs to each PIMentity */ void assignIDs() { maxID=1; Iterator i = p.iterator(); while (i.hasNext()) { PIMEntity pe = (PIMEntity) i.next(); pe.setId(maxID); maxID++; } } /** * this method is somewhat of a hack used to support the PIMServer * it saves the PIMCollection every time a change is made... * this should probably be a callback of some kind... */ void writeCollection(String filename) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename)); oos.writeObject(p); oos.close(); } catch (Exception e) { e.printStackTrace(); } } }