Author: batosai
Date: 2008-09-02 20:26:53 +0000 (Tue, 02 Sep 2008)
New Revision: 22360

Modified:
   trunk/apps/WoT/src/plugins/WoT/Identity.java
Log:
First step of a big refactor. It breaks everything, don't try to compile ;)

Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-09-02 19:41:13 UTC 
(rev 22359)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-09-02 20:26:53 UTC 
(rev 22360)
@@ -25,31 +25,31 @@
  */
 public class Identity {

-       String requestURI;
-       long edition;
-       Date lastChange;
-       String nickName;
-       boolean publishTrustList;
-       HashMap<String, String> props;
-       ArrayList<String> contexts;
+       private byte[] id;
+       private FreenetURI requestURI;
+       
+       private Date lastChange;
+       private String nickName;
+       private boolean publishTrustList;
+       private HashMap<String, String> props;
+       private ArrayList<String> contexts;

-       /**
-        * Creates an identity from given parameters.
-        * @param requestURI A string representing a USK or a SSK
-        * @param lastChange Date of the last update
-        * @param nickName A short name representing this identity 
-        * @param publishTrustList Whether this identity publishes its 
trustList or not
-        * @throws InvalidParameterException if a parameter doesn't pass checks 
-        */
-       public Identity (String requestURI, Date lastChange, String nickName, 
String publishTrustList) throws InvalidParameterException {
+       
+       public Identity (FreenetURI requestURI, String nickName, String 
publishTrustList, String context) throws InvalidParameterException {

                setRequestURI(requestURI);
-               setLastChange(lastChange);
                setNickName(nickName);
                setPublishTrustList(publishTrustList);
                props = new HashMap<String, String>();
                contexts = new ArrayList<String>();
+               contexts.add(context);
+               id = getRequestURI().getRoutingKey();
        }
+
+       public Identity (String requestURI, Date lastChange, String nickName, 
String publishTrustList, String context) throws InvalidParameterException, 
MalformedURLException {
+               this(new FreenetURI(requestURI), nickName, publishTrustList, 
context);
+       }
+

        @SuppressWarnings("unchecked")
        public static Identity getByURI (ObjectContainer db, FreenetURI uri) 
throws UnknownIdentityException, DuplicateIdentityException {
@@ -78,230 +78,113 @@
         * @throws  
         */
        public Score getScore(OwnIdentity treeOwner, ObjectContainer db) throws 
NotInTrustTreeException, DuplicateScoreException {
-
+               //TODO query on routing key
                ObjectSet<Score> score = db.queryByExample(new Score(treeOwner, 
this, 0, 0, 0));
                if(score.size() == 0) throw new 
NotInTrustTreeException(this.getRequestURI().toString()+" is not in that trust 
tree");
                else if(score.size() > 1) throw new 
DuplicateScoreException(this.getRequestURI().toString()+" ("+ getNickName() +") 
has "+score.size()+" scores in "+treeOwner.getNickName()+"'s trust tree"); 
                else return score.next();
-                
        }

        public Trust getTrust(Identity truster, ObjectContainer db) throws 
InvalidParameterException {
+               //TODO query on routing key
                ObjectSet<Trust> trust = db.queryByExample(new Trust(truster, 
this, 0, null));
                if(trust.hasNext()) return trust.next();
                else return null;
        }

-       /**
-        * @return The request URI to fetch this identity
-        */
-       public FreenetURI getRequestURI() {
-               
-               FreenetURI key;
-               try {
-                       key = new FreenetURI("USK@" + requestURI + 
"/WoT/"+edition);
-               } catch (MalformedURLException e) {
-                       // Can't happen, the key is tested on creation
-                       System.out.println(e.getMessage());
-                       return null;
-               }
-               return key.setMetaString(new String [] {"identity.xml"});
+       public void setRequestURI(FreenetURI requestURI) throws 
InvalidParameterException {
+               if(requestURI.getKeyType().equals("SSK")) requestURI = 
requestURI.setKeyType("USK");
+               if(!requestURI.getKeyType().equals("USK")) throw new 
InvalidParameterException("Key type not supported");
+               this.requestURI = 
requestURI.setKeyType("USK").setDocName("WoT");
+               updated();
        }

-       /**
-        * @param requestURI
-        * @throws InvalidParameterException if this isn't a valid FreenetURI
-        */
-       public void setRequestURI(String requestURI) throws 
InvalidParameterException {
-
-               FreenetURI key;
-               try {
-                       key = new FreenetURI(requestURI).setDocName("WoT");
-               } catch (MalformedURLException e) {
-                       throw new InvalidParameterException("Invalid key : 
"+e.getMessage());
-               }
-               setRequestURI(key);
-       }
-               
-       /**
-        * @param key
-        * @throws InvalidParameterException if this isn't a SSK or a USK
-        */
-       public void setRequestURI(FreenetURI key) throws 
InvalidParameterException {
-               
-               if(key.getKeyType().equals("SSK")) key = key.setKeyType("USK");
-               if(!key.getKeyType().equals("USK")) throw new 
InvalidParameterException("Key type not supported");
-               
-               setEdition(key.getSuggestedEdition());
-
-               this.requestURI = 
key.toString().substring(key.toString().indexOf("@") + 1, 
key.toString().indexOf("/"));
-       }
-
-       /**
-        * @return Current edition of this identity
-        */
-       public long getEdition() {
-               return edition;
-       }
-
-       /**
-        * @param edition
-        */
-       public void setEdition(long edition) {
-               this.edition = edition;
-       }
-
-       /**
-        * @return lastChange Date of the Identity's last update
-        */
-       public Date getLastChange() {
-               return lastChange;
-       }
-
-       /**
-        * @param lastChange Date of the Identity's last update
-        */
-       public void setLastChange(Date lastChange) {
-               this.lastChange = lastChange;
-               
-       }
-       
-       /**
-        * @return The nickname of this identity 
-        */
-       public String getNickName() {
-               return nickName;
-       }
-
-       /**
-        * @param nickName
-        * @throws InvalidParameterException if the nickname is blank or too 
long
-        */
        public void setNickName(String nickName) throws 
InvalidParameterException {
                String nick = nickName.trim();
                if(nick.length() == 0) throw new 
InvalidParameterException("Blank nickName");
                if(nick.length() > 50) throw new 
InvalidParameterException("NickName is too long (50 chars max)");
                this.nickName = nick;
+               updated();
        }

-       /**
-        * 
-        * @return Whether this identity publishes its trustList or not
-        */
-       public boolean doesPublishTrustList() {
-               return publishTrustList;
+       public void setPublishTrustList(boolean publishTrustList) {
+               this.publishTrustList = publishTrustList;
+               updated();
        }

-       /**
-        * Sets publishTrustList from a string (as given by forms and FCP)
-        * @param publishTrustList
-        */
        public void setPublishTrustList(String publishTrustList) {
                setPublishTrustList(publishTrustList.equals("true"));
        }

-       /**
-        * @param publishTrustList
-        */
-       public void setPublishTrustList(boolean publishTrustList) {
-               this.publishTrustList = publishTrustList;
-       }
-
-       /**
-        * Creates or updates a property
-        * @param key
-        * @param value
-        * @param db
-        * @throws InvalidParameterException if the key or value is blank
-        */
        public void setProp(String key, String value, ObjectContainer db) 
throws InvalidParameterException {
                if(key.trim().length() == 0 || value.trim().length() == 0) 
throw new InvalidParameterException("Blank key or value in this property");
                props.put(key.trim(), value.trim());
                db.store(props);
+               updated();
        }

-       /**
-        * Gets the property value or null if the property doesn't exist
-        * @param key
-        * @return
-        * @throws InvalidParameterException 
-        */
-       public String getProp(String key) throws InvalidParameterException {
-               if(!props.containsKey(key)) throw new 
InvalidParameterException("Property '"+key+"' isn't set on this identity");
-               return props.get(key);
-       }
-       
-       public String getPropsAsString() {
-               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
-        * @param db
-        * @throws InvalidParameterException 
-        */
        public void removeProp(String key, ObjectContainer db) throws 
InvalidParameterException {
                if(!props.containsKey(key)) throw new 
InvalidParameterException("Property '"+key+"' isn't set on this identity");
                props.remove(key.trim());
                db.store(props);
+               updated();
        }

-       /**
-        * Adds a context to this identity.
-        * @param context
-        * @param db
-        * @throws InvalidParameterException if the context is blank
-        */
        public void addContext(String context, ObjectContainer db) throws 
InvalidParameterException {
                String newContext = context.trim();
                if(newContext.length() == 0) throw new 
InvalidParameterException("Blank context");
                if(!contexts.contains(newContext)) contexts.add(newContext);
                db.store(contexts);
+               updated();
        }

-       /**
-        * Check if this identity is usable for a particular context or not.
-        * @param context
-        * @return
-        */
-       public boolean hasContext(String context) {
-               return contexts.contains(context.trim());
+       public void removeContext(String context, ObjectContainer db) throws 
InvalidParameterException {
+               if(contexts.size() == 1) throw new 
InvalidParameterException("Only one context left");
+               contexts.remove(context);
+               db.store(contexts);
+               updated();
        }

+       public Iterator<String> getContextsIterator() {
+               return contexts.iterator();
+       }
+       
+       private void updated() {
+               lastChange = new Date();
+       }
+
+       public FreenetURI getRequestURI() {
+               return requestURI;
+       }
+
+       public Date getLastChange() {
+               return lastChange;
+       }
+
+       public String getNickName() {
+               return nickName;
+       }
+
+       public boolean doesPublishTrustList() {
+               return publishTrustList;
+       }
+
        public String getContextsAsString() {
                return contexts.toString();
        }

-       public ArrayList<String> getContexts() {
-               return contexts;
+       public String getProp(String key) throws InvalidParameterException {
+               if(!props.containsKey(key)) throw new 
InvalidParameterException("Property '"+key+"' isn't set on this identity");
+               return props.get(key);
        }

-       public void setContexts(ArrayList<String> newContexts, ObjectContainer 
db) {
-               contexts = newContexts;
+       public String getPropsAsString() {
+               return props.toString();
        }

-       /**
-        * Removes a context from this identity
-        * @param context
-        * @param db
-        * @throws InvalidParameterException 
-        */
-       public void removeContext(String context, ObjectContainer db) throws 
InvalidParameterException {
-               if(contexts.size() == 1) throw new 
InvalidParameterException("Only one context left");
-               contexts.remove(context);
-               db.store(contexts);
+       public boolean hasContext(String context) {
+               return contexts.contains(context.trim());
        }

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


Reply via email to