Author: batosai
Date: 2008-08-30 20:04:04 +0000 (Sat, 30 Aug 2008)
New Revision: 22256

Modified:
   trunk/apps/WoT/src/plugins/WoT/Identity.java
   trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
Log:
Keep database coherent when restoring an Identity from Freenet (bug #2534).

Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-08-30 17:27:42 UTC 
(rev 22255)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-08-30 20:04:04 UTC 
(rev 22256)
@@ -214,6 +214,14 @@
                return props.toString();
        }

+       public HashMap<String, String> getProps() {
+               return props;
+       }
+       
+       public void setProps(HashMap<String, String> newProps, ObjectContainer 
db) {
+               props = newProps;
+       }
+       
        /**
         * Removes a property from the list. Does nothing if it didn't exist.
         * @param key
@@ -252,6 +260,14 @@
                return contexts.toString();
        }

+       public ArrayList<String> getContexts() {
+               return contexts;
+       }
+       
+       public void setContexts(ArrayList<String> newContexts, ObjectContainer 
db) {
+               contexts = newContexts;
+       }
+       
        /**
         * Removes a context from this identity
         * @param context
@@ -264,7 +280,7 @@
                db.store(contexts);
        }

-       public Iterator<String> getContexts() {
+       public Iterator<String> getContextsIterator() {
                return contexts.iterator();
        }
 }

Modified: trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-08-30 17:27:42 UTC 
(rev 22255)
+++ trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-08-30 20:04:04 UTC 
(rev 22256)
@@ -177,10 +177,64 @@
                }
        }

-       private void restoreIdentity(String requestURI, String insertURI) 
throws InvalidParameterException {
+       private void restoreIdentity(String requestURI, String insertURI) 
throws InvalidParameterException, MalformedURLException {

-               OwnIdentity id = new OwnIdentity(insertURI, requestURI, new 
Date(), new Date(0), "Restore in prgress...", "false");
-               db.store(id);
+               OwnIdentity id;
+               
+               try {
+                       Identity old = wot.getIdentityByURI(requestURI);
+                       
+                       // We already have fetched this identity as a 
stranger's one. We need to update the database.
+                       id = new OwnIdentity(insertURI, requestURI, 
old.getLastChange(), new Date(), old.getNickName(), old.doesPublishTrustList() 
? "true" : "false");
+                       id.setContexts(old.getContexts(), db); 
+                       id.setProps(old.getProps(), db); 
+                       
+                       // Update all received trusts
+                       ObjectSet<Trust> receivedTrusts = db.queryByExample(new 
Trust(null, old, 0));
+                       while(receivedTrusts.hasNext()) {
+                               Trust receivedTrust = receivedTrusts.next();
+                               receivedTrust.setTrustee(id);
+                               db.store(receivedTrust);
+                       }
+                       
+                       // Update all received scores
+                       ObjectSet<Score> scores = db.queryByExample(new 
Score(null, old, 0, 0, 0));
+                       while(scores.hasNext()) {
+                               Score score = scores.next();
+                               score.setTarget(id);
+                               db.store(score);
+                       }
+
+                       // Initialize the trust tree
+                       Score score = new Score(id, id, 100, 0, 100);  
+                       db.store(score);
+                       
+                       // Store the new identity
+                       db.store(id);
+                       
+                       // Update all given trusts
+                       ObjectSet<Trust> givenTrusts = db.queryByExample(new 
Trust(old, null, 0));
+                       while(givenTrusts.hasNext()) {
+                               Trust givenTrust = givenTrusts.next();
+                               givenTrust.setTruster(id);
+                               wot.setTrust(givenTrust);
+                               db.delete(givenTrust);
+                       }
+                       
+                       // Remove the old identity
+                       db.delete(old);
+                       
+               } catch (UnknownIdentityException e) {
+                       id = new OwnIdentity(insertURI, requestURI, new Date(), 
new Date(0), "Restore in progress...", "false");
+                       
+                       // Initialize the trust tree
+                       Score score = new Score(id, id, 100, 0, 100);  
+                       db.store(score);
+                       
+                       // Store the new identity
+                       db.store(id);
+               }
+               
                db.commit();

                fetcher.fetch(id);
@@ -427,7 +481,7 @@
                        sfs.putAppend("Rank", "null");
                }

-               Iterator<String> contexts = identity.getContexts();
+               Iterator<String> contexts = identity.getContextsIterator();
                for(int i = 1 ; contexts.hasNext() ; i++) 
sfs.putAppend("Context"+i, contexts.next());

                return sfs;


Reply via email to