1 | |
package org.paneris.bibliomania; |
2 | |
|
3 | |
import java.io.BufferedWriter; |
4 | |
import java.io.ByteArrayInputStream; |
5 | |
import java.io.File; |
6 | |
import java.io.FileInputStream; |
7 | |
import java.io.FileWriter; |
8 | |
import java.io.IOException; |
9 | |
import java.io.InputStream; |
10 | |
import java.io.Writer; |
11 | |
import java.util.Enumeration; |
12 | |
import java.util.Hashtable; |
13 | |
|
14 | |
import org.melati.Melati; |
15 | |
import org.melati.admin.AdminSpecialised; |
16 | |
import org.melati.poem.CachedSelection; |
17 | |
import org.melati.poem.Capability; |
18 | |
import org.melati.poem.PoemThread; |
19 | |
import org.melati.poem.Table; |
20 | |
import org.melati.template.MarkupLanguage; |
21 | |
import org.melati.poem.util.ArrayUtils; |
22 | |
import org.melati.poem.util.EmptyEnumeration; |
23 | |
import org.melati.poem.Treeable; |
24 | |
import org.paneris.bibliomania.fti.FTIException; |
25 | |
import org.paneris.bibliomania.fti.Text; |
26 | |
import org.paneris.bibliomania.generated.BookBase; |
27 | |
|
28 | 1663 | public class Book extends BookBase implements AdminSpecialised, Text { |
29 | |
|
30 | |
public String getFulltitle() { |
31 | 11 | String it = getFulltitleifdifferent(); |
32 | 11 | return it == null ? getTitle() : it; |
33 | |
} |
34 | |
|
35 | |
public File getSourceDir() { |
36 | 2 | return new File(getBibliomaniaDatabase().getContentRootDir(), getPath()); |
37 | |
} |
38 | |
|
39 | |
public class TooManyBooksException extends SequenceFTILimitsException { |
40 | |
|
41 | |
|
42 | |
|
43 | |
private static final long serialVersionUID = 1L; |
44 | |
|
45 | |
public TooManyBooksException(int seq, int limit) { |
46 | |
super(seq, limit); |
47 | |
} |
48 | |
|
49 | |
public Table getTable() { |
50 | |
return getBookTable(); |
51 | |
} |
52 | |
|
53 | |
public String getParentTable() { |
54 | |
return "author"; |
55 | |
} |
56 | |
} |
57 | |
|
58 | |
public void setAuthorsequence(Integer s) { |
59 | 0 | if (s != null && s.intValue() >= Chapter.ftiTextID_book_limit) |
60 | 0 | throw new TooManyBooksException( |
61 | |
s.intValue(), |
62 | |
Chapter.ftiTextID_book_limit); |
63 | 0 | super.setAuthorsequence(s); |
64 | 0 | } |
65 | |
|
66 | |
private void notifyChangedTextIDs() { |
67 | 0 | for (Enumeration c = getChapters(); c.hasMoreElements();) |
68 | 0 | ((Chapter)c.nextElement()).notifyChangedTextID(); |
69 | 0 | } |
70 | |
|
71 | |
public void setAuthor(Author author) { |
72 | 0 | if (!author.troid().equals(this.author)) { |
73 | 0 | super.setAuthor(author); |
74 | 0 | setAuthorsequence(author.firstFreeAuthorsequence()); |
75 | 0 | notifyChangedTextIDs(); |
76 | |
} |
77 | 0 | } |
78 | |
|
79 | 1663 | private CachedSelection<Chapter> chapters = null; |
80 | |
|
81 | |
public Enumeration getChapters() { |
82 | 11 | if (getTroid() == null) |
83 | 0 | return new EmptyEnumeration<Chapter>(); |
84 | |
else { |
85 | 11 | if (chapters == null) |
86 | 1 | chapters = |
87 | |
getBibliomaniaDatabase() |
88 | |
.getChapterTable() |
89 | |
.getBookColumn() |
90 | |
.cachedSelectionWhereEq(getTroid()); |
91 | 11 | return chapters.objects(); |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
public Enumeration getProductAssociations() { |
96 | 0 | return getProductAssociations( |
97 | |
getBibliomaniaDatabaseTables() |
98 | |
.getProductAssociationTable() |
99 | |
.getBookColumn()); |
100 | |
} |
101 | |
|
102 | |
public Enumeration getMembersSlowly() { |
103 | 0 | return getChapters(); |
104 | |
} |
105 | |
|
106 | |
public Chapter chapter(int sequence) { |
107 | 5 | getChapters(); |
108 | 5 | return (Chapter)chapters.nth(sequence); |
109 | |
} |
110 | |
|
111 | |
public Chapter getChapter0() { |
112 | 0 | getChapters(); |
113 | 0 | return (Chapter)chapters.firstObject(); |
114 | |
} |
115 | |
|
116 | |
public String getCha0JS() { |
117 | 0 | Boolean hnfp = getHasnofrontpage(); |
118 | 0 | if (hnfp != null && hnfp.booleanValue()) { |
119 | 0 | Chapter c0 = getChapter0(); |
120 | 0 | if (c0 != null) |
121 | 0 | return c0.troid().toString(); |
122 | |
} |
123 | |
|
124 | 0 | return "null"; |
125 | |
} |
126 | |
|
127 | |
public void setSection(Section section) { |
128 | 0 | if (!(section == null |
129 | |
? this.section == null |
130 | |
: section.troid().equals(this.section))) { |
131 | 0 | super.setSection(section); |
132 | 0 | notifyChangedTextIDs(); |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
public Capability getCanReadContent() { |
137 | 3 | Capability it = getOverridecanreadcontent(); |
138 | 3 | return it == null ? getSection().getDefaultcanreadcontent() : it; |
139 | |
} |
140 | |
|
141 | |
public String adminHandle(Melati melati, MarkupLanguage markupLanguage) |
142 | |
throws Exception { |
143 | 0 | return null; |
144 | |
} |
145 | |
|
146 | |
public String adminSpecialFacilities( |
147 | |
Melati melati, |
148 | |
MarkupLanguage markupLanguage) |
149 | |
throws Exception { |
150 | 0 | return getBibliomaniaDatabase().getWMTemplet("Book-specials"); |
151 | |
} |
152 | |
|
153 | |
public long ftiTextID() { |
154 | 0 | return ftiTextID_start() | ((1L << Chapter.ftiTextID_book_shift) - 1); |
155 | |
} |
156 | |
|
157 | |
public InputStream body() throws IOException { |
158 | 0 | if (Boolean.TRUE.equals(getHasnofrontpage())) |
159 | 0 | return new ByteArrayInputStream((getTitle() + |
160 | |
|
161 | |
" xyxyx xyxyx").getBytes()); |
162 | |
else { |
163 | 0 | File f = getFrontPageFile(); |
164 | 0 | if (f.exists()) { |
165 | 0 | InputStream it = new FileInputStream(f); |
166 | 0 | PoemThread.toTidy().add(it); |
167 | 0 | return it; |
168 | |
} else |
169 | 0 | return new ByteArrayInputStream(new byte[0]); |
170 | |
} |
171 | |
} |
172 | |
|
173 | |
public InputStream bodyForFragment() throws IOException { |
174 | 0 | return body(); |
175 | |
} |
176 | |
|
177 | |
public long ftiTextID_start() { |
178 | 0 | return Chapter.ftiTextID( |
179 | |
getSectionTroid().intValue(), |
180 | |
getAuthorTroid().intValue(), |
181 | |
getAuthorsequence().intValue(), |
182 | |
0); |
183 | |
} |
184 | |
|
185 | |
public long ftiTextID_limit() { |
186 | 0 | return ftiTextID_start() + (1L << Chapter.ftiTextID_book_shift); |
187 | |
} |
188 | |
|
189 | |
public Unit getParentUnit() { |
190 | 2 | return getAuthor(); |
191 | |
} |
192 | |
|
193 | |
public String getDisplayName() { |
194 | 0 | return getFulltitle(); |
195 | |
} |
196 | |
|
197 | |
public void appendCacheSubpath(StringBuffer buffer) { |
198 | 9 | getAuthor().appendCacheSubpath(buffer); |
199 | 9 | buffer.append('/'); |
200 | 9 | buffer.append(troid()); |
201 | 9 | } |
202 | |
|
203 | |
public void readKeyDotTxt() { |
204 | 1 | org.paneris.bibliomania.population.KeyDotTxtFilter.populate(this); |
205 | 1 | } |
206 | |
|
207 | |
public static final String frontPageFileName = "index.html"; |
208 | |
public static final String frontPageSourceFileName = "index.wm"; |
209 | |
|
210 | |
protected Hashtable<String, Boolean> speciallyTreatedInCache() { |
211 | 1 | Hashtable<String, Boolean> them = new Hashtable<String, Boolean>(); |
212 | 1 | for (Enumeration c = getChapters(); c.hasMoreElements();) |
213 | 5 | them.put(((Chapter)c.nextElement()).getFilename(), Boolean.TRUE); |
214 | 1 | them.put(frontPageSourceFileName, Boolean.TRUE); |
215 | 1 | them.put(frontPageFileName, Boolean.TRUE); |
216 | 1 | return them; |
217 | |
} |
218 | |
|
219 | |
public File getFrontPageFile() { |
220 | 1 | return new File(getCachePath(), "index.html"); |
221 | |
} |
222 | |
|
223 | |
public boolean hasTOC() { |
224 | 1 | File toc = new File(getSourceDir(), "index.wm"); |
225 | 1 | return toc.exists(); |
226 | |
} |
227 | |
public void index() throws IOException { |
228 | |
try { |
229 | 0 | getBibliomaniaDatabase().infoFTI().index(this); |
230 | 0 | } catch (IOException e) { |
231 | 0 | throw e; |
232 | 0 | } catch (Exception e) { |
233 | 0 | throw new FTIException(e); |
234 | 0 | } |
235 | 0 | } |
236 | |
|
237 | |
public void encache() throws Exception { |
238 | 1 | super.encache(); |
239 | 1 | File whole = new File(getCachePath(), "whole.html"); |
240 | |
|
241 | |
{ |
242 | 1 | Writer w = new BufferedWriter(new FileWriter(whole)); |
243 | |
try { |
244 | 1 | getBibliomaniaDatabase().interpolateTemplateToFile("Whole.wm", whole, this); |
245 | |
} finally { |
246 | 0 | try { |
247 | 1 | w.close(); |
248 | 0 | } catch (Exception e) { |
249 | 1 | } |
250 | 0 | } |
251 | |
} |
252 | |
|
253 | 1 | Boolean hnfp = getHasnofrontpage(); |
254 | 1 | if (hnfp != null && !hnfp.booleanValue()) { |
255 | 1 | getBibliomaniaDatabase().interpolateTemplateToFile( |
256 | |
"read/Book.wm", |
257 | |
getFrontPageFile(), |
258 | |
this); |
259 | |
} |
260 | 1 | } |
261 | |
|
262 | |
public SectionGroup getReadArea() { |
263 | 11 | return getSection().getReadArea(); |
264 | |
} |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | 1663 | public Treeable[] children = null; |
270 | |
|
271 | |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
272 | |
public Treeable[] getChildren() { |
273 | 0 | if (children == null) { |
274 | 0 | Object[] kids = ArrayUtils.arrayOf((Enumeration<Object>)getChapters()); |
275 | 0 | children = new Treeable[kids.length]; |
276 | 0 | for (int i=0; i<kids.length; i++) { |
277 | 0 | children[i] = (Treeable)kids[i]; |
278 | |
} |
279 | |
} |
280 | 0 | return children; |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
public String getName() { |
286 | 0 | return getDisplayName(); |
287 | |
} |
288 | |
} |