0
|
1 package nabble.model;
|
|
2
|
|
3 import fschmidt.db.DbDatabase;
|
|
4 import fschmidt.db.DbUtils;
|
|
5 import org.slf4j.Logger;
|
|
6 import org.slf4j.LoggerFactory;
|
|
7
|
|
8 import java.net.InetAddress;
|
|
9 import java.net.UnknownHostException;
|
|
10 import java.sql.Connection;
|
|
11 import java.sql.ResultSet;
|
|
12 import java.sql.SQLException;
|
|
13 import java.sql.Statement;
|
|
14 import java.sql.PreparedStatement;
|
|
15 import java.util.List;
|
|
16
|
|
17
|
|
18 final class Anonymous extends PersonImpl {
|
|
19
|
|
20 private static final Logger logger = LoggerFactory.getLogger(Anonymous.class);
|
|
21
|
|
22 private final String cookie;
|
|
23 private String name;
|
|
24 private final SiteImpl site;
|
|
25
|
|
26 static String newCookie(Site site) {
|
|
27 return "a" + nextId(site);
|
|
28 }
|
|
29
|
|
30 Anonymous(SiteImpl site,String cookie, String name) {
|
|
31 if( site == null )
|
|
32 throw new NullPointerException("site is null");
|
|
33 this.site = site;
|
|
34 this.cookie = cookie;
|
|
35 this.name = name;
|
|
36 }
|
|
37
|
|
38 public Site getSite() {
|
|
39 return site;
|
|
40 }
|
|
41
|
|
42 String getCookie() {
|
|
43 return cookie;
|
|
44 }
|
|
45
|
|
46 public String getName() {
|
|
47 return name;
|
|
48 }
|
|
49
|
|
50 public void setName(String name) {
|
|
51 this.name = name;
|
|
52 }
|
|
53
|
|
54 public Node newChildNode(Node.Kind kind,String subject,String message,Message.Format msgFmt,Node parent) throws ModelException {
|
|
55 if( name==null )
|
|
56 throw new ModelException.RequiredName();
|
|
57 return NodeImpl.newChildNode(kind,this,subject,message,msgFmt,(NodeImpl)parent);
|
|
58 }
|
|
59
|
|
60 public String getSearchId() {
|
|
61 return cookie;
|
|
62 }
|
|
63
|
|
64 static final char SEPERATOR = '~';
|
|
65
|
|
66 public String getIdString() {
|
|
67 String s = cookie + SEPERATOR;
|
|
68 if( name != null )
|
|
69 s += name;
|
|
70 return s;
|
|
71 }
|
|
72
|
|
73 public Message getSignature() {
|
|
74 return null;
|
|
75 }
|
|
76
|
|
77 public boolean equals(Object obj) {
|
|
78 if( !(obj instanceof Anonymous) )
|
|
79 return false;
|
|
80 Anonymous anon = (Anonymous)obj;
|
|
81 return anon.cookie.equals(cookie) && anon.site.equals(site);
|
|
82 }
|
|
83
|
|
84 public int hashCode() {
|
|
85 return cookie.hashCode();
|
|
86 }
|
|
87
|
|
88 public String toString() {
|
|
89 return "anonymous-" + cookie;
|
|
90 }
|
|
91
|
|
92 private static long nextId(Site site) {
|
|
93 try {
|
|
94 Connection con = site.getDb().getConnection();
|
|
95 Statement stmt = con.createStatement();
|
|
96 try {
|
|
97 ResultSet rs = stmt.executeQuery(
|
|
98 "select nextval('cookie_seq') as id"
|
|
99 );
|
|
100 rs.next();
|
|
101 return rs.getLong("id");
|
|
102 } finally {
|
|
103 stmt.close();
|
|
104 con.close();
|
|
105 }
|
|
106 } catch(SQLException e) {
|
|
107 throw new RuntimeException(e);
|
|
108 }
|
|
109 }
|
|
110
|
|
111
|
|
112 public NodeIterator<? extends Node> getNodesByDateDesc(String cnd) {
|
|
113 return new CursorNodeIterator( site.siteKey,
|
|
114 "select * from node where cookie = ?" +
|
|
115 (cnd == null?"":" and "+cnd) +
|
|
116 " order by when_created desc"
|
|
117 ,
|
|
118 new DbParamSetter() {
|
|
119 public void setParams(PreparedStatement stmt) throws SQLException {
|
|
120 stmt.setString( 1, cookie );
|
|
121 }
|
|
122 }
|
|
123 );
|
|
124 }
|
|
125
|
|
126 public int getNodeCount(String cnd) {
|
|
127 try {
|
|
128 Connection con = site.getDb().getConnection();
|
|
129 PreparedStatement stmt = con.prepareStatement(
|
|
130 "select count(*) as n from node where cookie = ?" +
|
|
131 (cnd == null? "" : " and " + cnd)
|
|
132 );
|
|
133 stmt.setString( 1, cookie );
|
|
134 ResultSet rs = stmt.executeQuery();
|
|
135 rs.next();
|
|
136 int nodeCount = rs.getInt("n");
|
|
137 stmt.close();
|
|
138 con.close();
|
|
139 return nodeCount;
|
|
140 } catch(SQLException e) {
|
|
141 throw new RuntimeException(e);
|
|
142 }
|
|
143 }
|
|
144
|
|
145 public int deleteNodes() {
|
|
146 DbDatabase db = site.getDb();
|
|
147 List<NodeImpl> nodes = new CursorNodeIterator( site.siteKey,
|
|
148 "select *"
|
|
149 +" from node"
|
|
150 +" where cookie = ?"
|
|
151 ,
|
|
152 new DbParamSetter() {
|
|
153 public void setParams(PreparedStatement stmt) throws SQLException {
|
|
154 stmt.setString( 1, getCookie() );
|
|
155 }
|
|
156 }
|
|
157 ).asList();
|
|
158 int n = 0;
|
|
159 for( NodeImpl node : nodes ) {
|
|
160 if( node.getSite().equals(site) ) {
|
|
161 db.beginTransaction();
|
|
162 try {
|
|
163 DbUtils.getGoodCopy(node).deleteMessageOrNode();
|
|
164 db.commitTransaction();
|
|
165 n++;
|
|
166 } finally {
|
|
167 db.endTransaction();
|
|
168 }
|
|
169 }
|
|
170 }
|
|
171 return n;
|
|
172 }
|
|
173
|
|
174 }
|