Author: batosai
Date: 2008-08-14 18:58:26 +0000 (Thu, 14 Aug 2008)
New Revision: 21859

Modified:
   trunk/apps/WoT/src/plugins/WoT/Identity.java
   trunk/apps/WoT/src/plugins/WoT/WoT.java
   trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
Log:
End of refactoring. We don't store blank Score objects for unknown identities 
anymore.

Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-08-14 18:13:40 UTC 
(rev 21858)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-08-14 18:58:26 UTC 
(rev 21859)
@@ -67,7 +67,7 @@
        public Trust getTrust(Identity truster, ObjectContainer db) throws 
InvalidParameterException {
                ObjectSet<Trust> trust = db.queryByExample(new Trust(truster, 
this, 0, null));
                if(trust.hasNext()) return trust.next();
-               else throw new InvalidParameterException("Identity 
"+truster.getRequestURI().toString()+" does not trust 
"+this.getRequestURI().toString());
+               else return null;
        }

        /**

Modified: trunk/apps/WoT/src/plugins/WoT/WoT.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-08-14 18:13:40 UTC (rev 
21858)
+++ trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-08-14 18:58:26 UTC (rev 
21859)
@@ -100,42 +100,35 @@
                int newCapacity = 0;
                boolean hasNegativeTrust = false;

-               // We get the score of the identity, or create one if it 
doesn't exist 
-               ObjectSet<Score> scoreResult = db.queryByExample(new 
Score(treeOwner, identity, 0, 0, 0));
-               if(scoreResult.hasNext()) {
-                       score = scoreResult.next();
-                       oldCapacity = score.getCapacity();
-               }
-               else score = new Score(treeOwner, identity, 0, -1, 0);

                // We get the trust given by the tree owner (because if it is 
negative, the identity has no capacity)
-               ObjectSet<Trust> trustResult = db.queryByExample(new 
Trust(treeOwner, identity, 0));
-               if(trustResult.hasNext()) {
-                       Trust trust = trustResult.next();
-                       if(trust.getValue() <= 0) hasNegativeTrust = true;
-               }
+               Trust trust = identity.getTrust(treeOwner, db);
+               if(trust != null && trust.getValue() <= 0) hasNegativeTrust = 
true;

                // We get get all trust received by the identity
-               ObjectSet<Trust> trusters = db.queryByExample(new Trust(null, 
identity, 0));
+               ObjectSet<Trust> trusters = getTrusters(identity);
                while(trusters.hasNext()) {

                        Trust trusterTrust = trusters.next();
-                                       
-                       Score trusterScore; 
-                       
-                       // We get the score of each identity that gives trust 
to this one 
-                       ObjectSet<Score> trustersScore = db.queryByExample(new 
Score(treeOwner, trusterTrust.getTruster(), 0, 0, 0));
-                       if(trustersScore.hasNext()) trusterScore = 
trustersScore.next();
-                       else trusterScore = new Score(treeOwner, 
trusterTrust.getTruster(), 0, -1, 0); // This can only happen when you have 
multiple primary identities
-                       
-                       if(trusterScore.getCapacity() > 0) { // If the truster 
has capacity
-                               newScore += trusterScore.getCapacity() * 
trusterTrust.getValue() / 100; // We update the trustee's score
-                               if(newRank == -1 || trusterScore.getRank() < 
newRank) // We store the lowest truster's rank (but not -1) 
-                                       newRank = trusterScore.getRank();
+                                                               
+                       // We get the score of each identity that gives trust 
to this one
+                       try {
+                               Score trusterScore = 
trusterTrust.getTruster().getScore(treeOwner, db);
+                               if(trusterScore.getCapacity() > 0) { // If the 
truster has capacity                             
+                                       newScore += trusterScore.getCapacity() 
* trusterTrust.getValue() / 100; // We update the trustee's score
+                                       if(newRank == -1 || 
trusterScore.getRank() < newRank) // We store the lowest truster's rank (but 
not -1) 
+                                               newRank = 
trusterScore.getRank();
+                               }
                        }
+                       catch(NotInTrustTreeException e) {
+                               // The truster is not in the trust tree
+                               continue;
+                       }
+
                }

-               if(newRank != -1) newRank++; // We don't want to give the 
trustee the same rank as the truster, right ?
+               if(newRank == -1) return; // None of the trusters was in the 
trust tree, this identity isn't either 
+               else newRank++; // We don't want to give the trustee the same 
rank as the truster, right ?
                if((newScore > 0) && !hasNegativeTrust) {
                        if(newRank >= capacities.length) {
                                newCapacity = capacities[capacities.length - 1];
@@ -145,6 +138,15 @@
                        }
                }

+               // We get the score of the identity, or create one if it 
doesn't exist 
+               try {
+                       score = identity.getScore(treeOwner, db);
+               } catch (NotInTrustTreeException e) {
+                       score = new Score(treeOwner, identity, 0, -1, 0);
+               }
+               
+               oldCapacity = score.getCapacity();
+               
                if(score.getRank() != 0) { // We don't want our own identites' 
rank to be changed either
                        score.setRank(newRank);
                        score.setCapacity(newCapacity);
@@ -155,10 +157,9 @@
                // We don't commit here because this method is going to be 
called *many* times while parsing a trust list.

                if(oldCapacity != newCapacity) { // If the capacity has 
changed, we have to update all identities this one trusts 
-                       ObjectSet<Trust> trustees = db.queryByExample(new 
Trust(identity, null, 0));
+                       ObjectSet<Trust> trustees = getTrustees(identity);
                        while(trustees.hasNext()) {
-                               Trust trustee = trustees.next();
-                               updateScore(treeOwner,trustee.getTrustee());
+                               updateScore(treeOwner, 
trustees.next().getTrustee());
                        }
                }
        }
@@ -233,5 +234,21 @@
                // Same problem here
                return query.execute();
        }
+       
+       public ObjectSet<Trust> getTrusters (String identity) throws 
Db4oIOException, DatabaseClosedException, MalformedURLException, 
InvalidParameterException, UnknownIdentityException { 
+               return getTrusters(identity);
+       }
+       
+       private ObjectSet<Trust> getTrusters (Identity identity) throws 
Db4oIOException, DatabaseClosedException, InvalidParameterException {
+               return db.queryByExample(new Trust(null, identity, 0));
+       }
+
+       public ObjectSet<Trust> getTrustees (String identity) throws 
Db4oIOException, DatabaseClosedException, MalformedURLException, 
InvalidParameterException, UnknownIdentityException { 
+               return getTrustees(identity);
+       }
+
+       private ObjectSet<Trust> getTrustees (Identity identity) throws 
Db4oIOException, DatabaseClosedException, InvalidParameterException { 
+               return db.queryByExample(new Trust(identity, null, 0));
+       }
 }


Modified: trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-08-14 18:13:40 UTC 
(rev 21858)
+++ trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-08-14 18:58:26 UTC 
(rev 21859)
@@ -358,7 +358,7 @@

                sfs.putAppend("Message", "Identities");

-               ObjectSet<Trust> result = db.queryByExample(new Trust(null, 
wot.getIdentityByURI(params.get("Identity")),0));
+               ObjectSet<Trust> result = 
wot.getTrusters(params.get("Identity"));

                for(int i = 1 ; result.hasNext() ; i++) {
                        Trust trust = result.next();
@@ -377,7 +377,7 @@

                sfs.putAppend("Message", "Identities");

-               ObjectSet<Trust> result = db.queryByExample(new 
Trust(wot.getIdentityByURI(params.get("Identity")), null, 0));
+               ObjectSet<Trust> result = 
wot.getTrustees(params.get("Identity"));

                for(int i = 1 ; result.hasNext() ; i++) {
                        Trust trust = result.next();


Reply via email to