KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > comics > ComicsVerifier


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Abstracts automatic verification of the comics.
28  */

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 JavaDoc;
36 import java.sql.Connection JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.Statement JavaDoc;
39
40 public class ComicsVerifier
41 {
42     /**
43      * Verify all the comics
44      */

45     public static void verifyAll(ComicManager cmgr, Writer JavaDoc out) throws Exception JavaDoc
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     /**
57      * Verify a specific strip. This function does not return until all the threads have finished.
58      */

59     public static void verify(ComicManager cmgr, Comic comic, Writer JavaDoc out) throws Exception JavaDoc
60     {
61         out.write("- "+comic.getName()+" ("+comic.getId()+") -\n");
62         /*
63         verify procedude:
64
65     1)
66     check that the first and last ids of the comic link to valid strips
67
68         2)
69         get all the strips of the comic
70         traverse the linked list
71           mark all strips traversed
72             print all strips that point to invalid strip ids
73         traverse the archive
74           print all non-marked strips
75
76         3)
77         traverse the list in reverse
78           delete all strips that are identical to the next
79
80         4)
81         traverse the archive
82             if local path is broken but not the url, set local path to null
83           print all strips with broken urls
84             print all strips with broken local paths
85
86         5)
87         verify that all comicpositions of all users are valid
88         */

89
90         // 1)
91
// check that the first and last ids of the comic link to valid strips
92
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         // 2)
105
// get all strips
106
Strip[] archive = cmgr.getStrips(comic.getId());
107         boolean[] checked = new boolean[archive.length];
108         Strip current = first;
109         while (current != null)
110         {
111             // find this strip in the archive and mark it
112
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             // get next
122
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         // print all non-marked strips
138
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         // 3)
158
// traverse the list in reverse
159
// delete all strips that are identical to the next
160
current = latest;
161         while (current != null)
162         {
163             // get prev
164
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         // 4)
190
// traverse the archive
191
// if local path is broken but not the url, set local path to null
192
// print all strips with broken urls
193
// print all strips with broken local paths
194
for (int i = 0; i < archive.length; i++)
195         {
196             //boolean urlOK = URLExists(archive[i].URL);
197
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             //if (!urlOK)
207
//out.write("strip id "+archive[i].getId()+" has broken url '"+archive[i].URL+"'\n");
208
}
209
210         // FIXME
211
// 5)
212
// verify that all comicpositions of all users are valid
213
Connection JavaDoc connection = net.killingar.forum.internal.managers.AbstractManager.getNewConnection();
214         Statement JavaDoc statement = connection.createStatement();
215         ResultSet JavaDoc 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 JavaDoc url)
230     {
231         try
232         {
233             java.net.HttpURLConnection JavaDoc con = (java.net.HttpURLConnection JavaDoc)new java.net.URL JavaDoc(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 JavaDoc e)
242         {
243             return false;
244         }
245
246         return true;
247     }
248 }
249
Popular Tags