Mercurial Hosting > nabble
view src/nabble/model/Anonymous.java @ 60:36b0e32246d0
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 12 Jun 2022 19:20:41 -0600 |
parents | 18cf4872fd7f |
children |
line wrap: on
line source
package nabble.model; import fschmidt.db.DbDatabase; import fschmidt.db.DbUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; 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; } }