1 25 26 29 package net.killingar.forum.comics; 30 31 import net.killingar.forum.internal.Comic; 32 import net.killingar.forum.internal.Strip; 33 import net.killingar.forum.internal.managers.ComicManager; 34 35 import java.io.Writer ; 36 import java.sql.Connection ; 37 import java.sql.ResultSet ; 38 import java.sql.Statement ; 39 40 public class ComicsVerifier 41 { 42 45 public static void verifyAll(ComicManager cmgr, Writer out) throws Exception 46 { 47 Comic comics[] = cmgr.getComics(); 48 for (int i = 0; i < comics.length; i++) 49 { 50 verify(cmgr, comics[i], out); 51 } 52 53 out.flush(); 54 } 55 56 59 public static void verify(ComicManager cmgr, Comic comic, Writer out) throws Exception 60 { 61 out.write("- "+comic.getName()+" ("+comic.getId()+") -\n"); 62 89 90 Strip first = cmgr.getStrip(comic.getFirstID()); 93 Strip latest = cmgr.getStrip(comic.getLatestID()); 94 if (first == null) 95 out.write("broken first strip (non-existant) on comic "+comic.getName()+" (id: "+comic.getId()+")\n"); 96 else if (first.getParentID() != comic.getId()) 97 out.write("broken first strip (links to other comic) on comic "+comic.getName()+" (id: "+comic.getId()+")\n"); 98 99 if (latest == null) 100 out.write("broken latest strip (non-existant) on comic "+comic.getName()+" (id: "+comic.getId()+")\n"); 101 else if (latest.getParentID() != comic.getId()) 102 out.write("broken latest strip (links to other comic) on comic "+comic.getName()+" (id: "+comic.getId()+")\n"); 103 104 Strip[] archive = cmgr.getStrips(comic.getId()); 107 boolean[] checked = new boolean[archive.length]; 108 Strip current = first; 109 while (current != null) 110 { 111 for (int i = 0; i < archive.length; i++) 113 { 114 if (archive[i].getId() == current.getId()) 115 { 116 checked[i] = true; 117 break; 118 } 119 } 120 121 Strip c = cmgr.getStrip(current.getNextID()); 123 if (c == null) 124 { 125 if (current.getNextID() != 0) 126 out.write("broken next id for strip id: "+current.getId()+"\n"); 127 128 break; 129 } 130 131 if (c.getPrevID() != current.getId()) 132 out.write("strip id "+c.getId()+" incorrectly links back to "+c.getPrevID()+" when it should link to "+current.getId()+"\n"); 133 134 current = c; 135 } 136 137 for (int i = 0; i < archive.length; i++) 139 { 140 if (!checked[i]) 141 { 142 boolean duplicate = false; 143 for (int j = 0; j < archive.length; j++) 144 { 145 if (archive[j].getUrl().equals(archive[i].getUrl())) 146 { 147 duplicate = true; 148 break; 149 } 150 } 151 152 out.write("strip "+archive[i].getId()+" unlinked"+(duplicate? " (duplicate)" : "")+"\n"); 153 } 154 } 155 156 157 current = latest; 161 while (current != null) 162 { 163 Strip c = cmgr.getStrip(current.getPrevID()); 165 if (c == null) 166 { 167 if (current.getPrevID() != 0) 168 out.write("broken prev id for strip id: "+current.getId()+"\n"); 169 170 break; 171 } 172 173 if (c.getNextID() != current.getId()) 174 { 175 out.write("strip id "+c.getId()+" incorrectly links forward to "+c.getPrevID()+" when it should link to "+current.getId()+"\n"); 176 continue; 177 } 178 179 if (c.getUrl().equals(current.getUrl())) 180 { 181 out.write("removed duplicate strip\n"); 182 cmgr.removeStrip(current.getId()); 183 } 184 185 current = c; 186 } 187 188 189 for (int i = 0; i < archive.length; i++) 195 { 196 boolean localUrlOK = archive[i].localURL == null? false: URLExists(archive[i].localURL); 198 199 if (!localUrlOK && archive[i].localURL != null) 200 { 201 archive[i].localURL = null; 202 cmgr.changeStrip(archive[i]); 203 out.write("strip id "+archive[i].getId()+": removed broken local url\n"); 204 } 205 206 } 209 210 Connection connection = net.killingar.forum.internal.managers.AbstractManager.getNewConnection(); 214 Statement statement = connection.createStatement(); 215 ResultSet result = statement.executeQuery("select User, Position from ComicPositions where Comic = "+comic.getId()); 216 while (result.next()) 217 { 218 Strip s = cmgr.getStrip(result.getLong(2)); 219 if (s == null) 220 { 221 out.write("comic position broken for user "+result.getLong(1)+"\n"); 222 statement.executeUpdate("delete from ComicPositions where Comic = "+comic.getId()+" && Position = "+result.getLong(2)); 223 } 224 } 225 226 net.killingar.forum.internal.managers.AbstractManager.closeAll(connection, statement, result); 227 } 228 229 static boolean URLExists(String url) 230 { 231 try 232 { 233 java.net.HttpURLConnection con = (java.net.HttpURLConnection )new java.net.URL (url).openConnection(); 234 con.connect(); 235 236 if (con.getResponseCode() != java.net.HttpURLConnection.HTTP_OK) 237 return false; 238 239 con.disconnect(); 240 } 241 catch (Exception e) 242 { 243 return false; 244 } 245 246 return true; 247 } 248 } 249 | Popular Tags |