Author: xor
Date: 2008-11-06 18:24:32 +0000 (Thu, 06 Nov 2008)
New Revision: 23358

Modified:
   trunk/plugins/WoT/Score.java
   trunk/plugins/WoT/WoT.java
Log:
Provide a getIdentitiesByScore() function, needed for Freetalk.

Modified: trunk/plugins/WoT/Score.java
===================================================================
--- trunk/plugins/WoT/Score.java        2008-11-06 18:17:47 UTC (rev 23357)
+++ trunk/plugins/WoT/Score.java        2008-11-06 18:24:32 UTC (rev 23358)
@@ -13,6 +13,7 @@

 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
+import com.db4o.query.Constraint;
 import com.db4o.query.Query;

 /**
@@ -70,34 +71,29 @@
        /**
         * Gets Identities matching a specified score criteria.
         * 
+        * @param db A reference to the database
         * @param owner requestURI of the owner of the trust tree
         * @param select Score criteria, can be '+', '0' or '-'
-        * @param db A reference to the database
         * @return an {@link ObjectSet} containing Identities that match the 
criteria
         * @throws InvalidParameterException if the criteria is not recognised
-        * @throws MalformedURLException if the supplied requestURI of the 
treeOwner is not a valid {@link FreenetURI}
-        * @throws UnknownIdentityException if the supplied treeOwner is not in 
the database
-        * @throws DuplicateIdentityException if the supplied treeOwner exists 
more than once in the database (should never happen)
         */
        @SuppressWarnings("unchecked")
-       public static ObjectSet<Score> getIdentitiesByScore (String owner, 
String select, ObjectContainer db) throws InvalidParameterException, 
MalformedURLException, UnknownIdentityException, DuplicateIdentityException {
+       public static ObjectSet<Score> getIdentitiesByScore (ObjectContainer 
db, OwnIdentity treeOwner, int select) throws InvalidParameterException {
+               if(treeOwner == null)
+                       throw new IllegalArgumentException();

-               OwnIdentity treeOwner = OwnIdentity.getByURI(db, owner);
-               
                Query query = db.query();
                query.constrain(Score.class);
                query.descend("treeOwner").constrain(treeOwner);
+       
+               // TODO: we should decide whether identities with score 0 
should be returned if select>0

-               if(select.equals("+")) {
+               if(select > 0)
                        query.descend("score").constrain(new 
Integer(0)).greater();
-               }
-               else if(select.equals("0")) {
-                       query.descend("score").constrain(new Integer(0));
-               }
-               else if(select.equals("-")) {
+               else if(select < 0 )
                        query.descend("score").constrain(new 
Integer(0)).smaller();
-               }
-               else throw new InvalidParameterException("Unhandled select 
value ("+select+")");
+               else 
+                       query.descend("score").constrain(new Integer(0));

                return query.execute();
        }

Modified: trunk/plugins/WoT/WoT.java
===================================================================
--- trunk/plugins/WoT/WoT.java  2008-11-06 18:17:47 UTC (rev 23357)
+++ trunk/plugins/WoT/WoT.java  2008-11-06 18:24:32 UTC (rev 23358)
@@ -9,7 +9,10 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.MalformedURLException;
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map.Entry;

 import javax.xml.parsers.ParserConfigurationException;
@@ -531,7 +534,25 @@
                return sfs;
        }

-       // TODO refactor this in order to make it available not only from FCP
+       // TODO: javadoc
+       public List<Identity> getIdentitiesByScore(OwnIdentity treeOwner, int 
select, String context) throws InvalidParameterException
+       {
+               ObjectSet<Score> result = Score.getIdentitiesByScore(db, 
treeOwner, select);
+               // TODO: decide whether the tradeoff of using too much memory 
for the ArrayList is worth the speedup of not having a linked
+               // list which allocates lots of pieces of memory for its nodes.
+               ArrayList<Identity> identities = new 
ArrayList<Identity>(result.size()); 
+               boolean getAll = context.equals("all");
+               
+               while(result.hasNext()) {
+                       Identity identity = result.next().getTarget();
+                       // TODO: Maybe there is a way to do this through SODA
+                       if(getAll || identity.hasContext(context))
+                               identities.add(identity);
+               }
+               
+               return identities;
+       }
+
        private SimpleFieldSet handleGetIdentitiesByScore(SimpleFieldSet 
params) throws InvalidParameterException, MalformedURLException, 
UnknownIdentityException, DuplicateIdentityException {

                SimpleFieldSet sfs = new SimpleFieldSet(false);
@@ -539,12 +560,25 @@
                if(params.get("TreeOwner") == null || params.get("Select") == 
null || params.get("Context") == null) throw new 
InvalidParameterException("Missing mandatory parameter");

                sfs.putAppend("Message", "Identities");
+               
+               OwnIdentity treeOwner = OwnIdentity.getByURI(db, 
params.get("TreeOwner"));
+               
+               String selectString = params.get("Select").trim();
+               int select = 0; // TODO: decide about the default value
+               
+               if(selectString.equals("+")) select = 1;
+               else if(selectString.equals("-")) select = -1;
+               else if(selectString.equals("0")) select = 0;
+               else throw new InvalidParameterException("Unhandled select 
value ("+select+")");
+               
+               ObjectSet<Score> result = Score.getIdentitiesByScore(db, 
treeOwner, select);
+               String context = params.get("Context");
+               boolean getAll = context.equals("all");

-               ObjectSet<Score> result = 
Score.getIdentitiesByScore(params.get("TreeOwner"), 
params.get("Select").trim(), db);
                for(int i = 1 ; result.hasNext() ; i++) {
                        Score score = result.next();
-                       // Maybe there is a way to do this through SODA
-                       if(score.getTarget().hasContext(params.get("Context")) 
|| params.get("Context").equals("all"))
+                       // TODO: Maybe there is a way to do this through SODA
+                       if(getAll || score.getTarget().hasContext(context))
                                sfs.putAppend("Identity"+i, 
score.getTarget().getRequestURI().toString());
                }
                return sfs;


Reply via email to