Mercurial Hosting > nabble
diff src/nabble/model/Anonymous.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children | 18cf4872fd7f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/model/Anonymous.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,174 @@ +package nabble.model; + +import fschmidt.db.DbDatabase; +import fschmidt.db.DbUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.PreparedStatement; +import java.util.List; + + +final class Anonymous extends PersonImpl { + + private static final Logger logger = LoggerFactory.getLogger(Anonymous.class); + + private final String cookie; + private String name; + private final SiteImpl site; + + static String newCookie(Site site) { + return "a" + nextId(site); + } + + Anonymous(SiteImpl site,String cookie, String name) { + if( site == null ) + throw new NullPointerException("site is null"); + this.site = site; + this.cookie = cookie; + this.name = name; + } + + public Site getSite() { + return site; + } + + String getCookie() { + return cookie; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Node newChildNode(Node.Kind kind,String subject,String message,Message.Format msgFmt,Node parent) throws ModelException { + if( name==null ) + throw new ModelException.RequiredName(); + return NodeImpl.newChildNode(kind,this,subject,message,msgFmt,(NodeImpl)parent); + } + + public String getSearchId() { + return cookie; + } + + static final char SEPERATOR = '~'; + + public String getIdString() { + String s = cookie + SEPERATOR; + if( name != null ) + s += name; + return s; + } + + public Message getSignature() { + return null; + } + + public boolean equals(Object obj) { + if( !(obj instanceof Anonymous) ) + return false; + Anonymous anon = (Anonymous)obj; + return anon.cookie.equals(cookie) && anon.site.equals(site); + } + + public int hashCode() { + return cookie.hashCode(); + } + + public String toString() { + return "anonymous-" + cookie; + } + + private static long nextId(Site site) { + try { + Connection con = site.getDb().getConnection(); + Statement stmt = con.createStatement(); + try { + ResultSet rs = stmt.executeQuery( + "select nextval('cookie_seq') as id" + ); + rs.next(); + return rs.getLong("id"); + } finally { + stmt.close(); + con.close(); + } + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + + + public NodeIterator<? extends Node> getNodesByDateDesc(String cnd) { + return new CursorNodeIterator( site.siteKey, + "select * from node where cookie = ?" + + (cnd == null?"":" and "+cnd) + + " order by when_created desc" + , + new DbParamSetter() { + public void setParams(PreparedStatement stmt) throws SQLException { + stmt.setString( 1, cookie ); + } + } + ); + } + + public int getNodeCount(String cnd) { + try { + Connection con = site.getDb().getConnection(); + PreparedStatement stmt = con.prepareStatement( + "select count(*) as n from node where cookie = ?" + + (cnd == null? "" : " and " + cnd) + ); + stmt.setString( 1, cookie ); + ResultSet rs = stmt.executeQuery(); + rs.next(); + int nodeCount = rs.getInt("n"); + stmt.close(); + con.close(); + return nodeCount; + } catch(SQLException e) { + throw new RuntimeException(e); + } + } + + public int deleteNodes() { + DbDatabase db = site.getDb(); + List<NodeImpl> nodes = new CursorNodeIterator( site.siteKey, + "select *" + +" from node" + +" where cookie = ?" + , + new DbParamSetter() { + public void setParams(PreparedStatement stmt) throws SQLException { + stmt.setString( 1, getCookie() ); + } + } + ).asList(); + int n = 0; + for( NodeImpl node : nodes ) { + if( node.getSite().equals(site) ) { + db.beginTransaction(); + try { + DbUtils.getGoodCopy(node).deleteMessageOrNode(); + db.commitTransaction(); + n++; + } finally { + db.endTransaction(); + } + } + } + return n; + } + +}