1 package org.paneris.bibliomania;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.Enumeration;
9 import java.util.Hashtable;
10
11 import org.melati.Melati;
12 import org.melati.admin.AdminSpecialised;
13 import org.melati.poem.CachedSelection;
14 import org.melati.poem.PoemThread;
15 import org.melati.poem.Table;
16 import org.melati.poem.util.ArrayUtils;
17 import org.melati.poem.util.EmptyEnumeration;
18 import org.melati.poem.Treeable;
19 import org.melati.template.MarkupLanguage;
20 import org.paneris.bibliomania.fti.FTIException;
21 import org.paneris.bibliomania.fti.Text;
22 import org.paneris.bibliomania.generated.AuthorBase;
23
24 public class Author extends AuthorBase implements AdminSpecialised, Text {
25
26 public Author() {
27 }
28
29 public Author(String displayname) {
30 this.displayname = displayname;
31 }
32
33 public String getLongname() {
34 String it = getLongnameifdifferent();
35 return it == null ? getDisplayname() : it;
36 }
37
38 public String getSortOrShortName() {
39 String it = getSortname();
40 return it == null ? getDisplayname() : it;
41 }
42
43 int firstFreeAuthorsequence() {
44 return getBibliomaniaDatabase()
45 .getBookTable()
46 .getAuthorsequenceColumn()
47 .firstFree("author = " + troid());
48 }
49
50 public Enumeration getBooks() {
51 return getBibliomaniaDatabase()
52 .getBookTable()
53 .getAuthorColumn()
54 .cachedSelectionWhereEq(this.troid()).objects();
55 }
56
57 public Enumeration getBooksOrderedBySection() {
58 return getBibliomaniaDatabase().getBookTable().selection(
59 "author=" + this.troid(),
60 "section",
61 false);
62 }
63
64 public Enumeration getMembersSlowly() {
65 return getBooks();
66 }
67
68 public Enumeration getProductAssociations() {
69 return getProductAssociations(
70 getBibliomaniaDatabaseTables()
71 .getProductAssociationTable()
72 .getAuthorColumn());
73 }
74
75 private Hashtable booksInSection = null;
76
77 public Enumeration booksInSection(Section sectionP) {
78 if (booksInSection == null)
79 booksInSection = new Hashtable();
80
81 CachedSelection books = (CachedSelection)booksInSection.get(sectionP);
82
83 if (books == null)
84 booksInSection.put(
85 sectionP,
86 books =
87 new CachedSelection(
88 getBibliomaniaDatabase().getBookTable(),
89 "author = " + troid() + " AND section = " + sectionP.troid(),
90 null));
91 System.err.println(books.toString());
92 return books.objects();
93 }
94
95 private Hashtable<SectionGroup, CachedSelection<Book>> booksInSectionGroup = null;
96
97 public Enumeration<Book> booksInSectionGroup(SectionGroup sectionGroup) {
98 if (booksInSectionGroup == null)
99 booksInSectionGroup = new Hashtable<SectionGroup, CachedSelection<Book>>();
100
101 CachedSelection<Book> books = booksInSectionGroup.get(sectionGroup);
102
103 if (books == null)
104 booksInSectionGroup.put(
105 sectionGroup,
106 books =
107 new CachedSelection(
108 getBibliomaniaDatabase().getBookTable(),
109 getBibliomaniaDatabase().getBookTable().getAuthorColumn().fullQuotedName() +
110 " = "
111 + troid()
112 + " AND " +
113 getBibliomaniaDatabase().getSectionTable().troidColumn().fullQuotedName() +
114 " = " +
115 getBibliomaniaDatabase().getBookTable().getSectionColumn().fullQuotedName() +
116 " AND " +
117 getBibliomaniaDatabase().getSectionTable().getGroupColumn().fullQuotedName() +
118 " = "
119 + sectionGroup.troid(),
120 null,
121 new Table[] { getBibliomaniaDatabase().getSectionTable()}));
122
123 return books.objects();
124 }
125
126 public long ftiTextID() {
127 return ftiTextID_start() | ((1L << Chapter.ftiTextID_author_shift) - 1);
128 }
129
130 public InputStream body() throws IOException {
131 File f = getFrontPageFile();
132 if (f.exists()) {
133 InputStream it = new FileInputStream(f);
134 PoemThread.toTidy().add(it);
135 return it;
136 } else
137 return new ByteArrayInputStream(new byte[0]);
138 }
139
140 public InputStream bodyForFragment() throws IOException {
141 return body();
142 }
143
144 public long ftiTextID_start() {
145 Integer sectionTroid = getSectionTroid();
146 return Chapter.ftiTextID(
147 sectionTroid == null ? 0 : sectionTroid.intValue(),
148 troid().intValue(),
149 0,
150 0);
151 }
152
153 public long ftiTextID_limit() {
154 return ftiTextID_start() + (1L << Chapter.ftiTextID_author_shift);
155 }
156
157 public Unit getParentUnit() {
158 return getSection();
159 }
160
161 public void appendCacheSubpath(StringBuffer buffer) {
162 buffer.append("/a/");
163 buffer.append(troid());
164 }
165
166 public String getDisplayName() {
167 return getLongname();
168 }
169
170 public static final String frontPageFileName = "index.html";
171
172 public File getFrontPageFile() {
173 return new File(getCachePath(), frontPageFileName);
174 }
175
176 public void index() throws IOException {
177 try {
178 getBibliomaniaDatabase().infoFTI().index(this);
179 } catch (IOException e) {
180 throw e;
181 } catch (Exception e) {
182 throw new FTIException(e);
183 }
184 }
185
186 public void encache() throws Exception {
187 super.encache();
188
189 Boolean nonstandardL = getNonstandard();
190 if (nonstandardL == null || !nonstandardL.booleanValue())
191 getBibliomaniaDatabase().interpolateTemplateToFile(
192 "read/Author.wm",
193 getFrontPageFile(),
194 this);
195 }
196
197 public String getImageURL() {
198 String imagefilenameL = getImagefilename();
199 return imagefilenameL == null
200 ? null
201 : getBibliomaniaDatabase().getContentStaticRootURL()
202 + new File(getPath() + "/" + imagefilenameL).toString();
203 }
204
205 private CachedSelection links = null;
206
207 public Enumeration getLinks() {
208 if (getTroid() == null)
209 return new EmptyEnumeration();
210 else {
211 links =
212 getBibliomaniaDatabase()
213 .getAuthorWebSiteTable()
214 .getAuthorColumn()
215 .cachedSelectionWhereEq(getTroid());
216
217 return links.objects();
218 }
219 }
220
221 public Enumeration getCurrentLinks() {
222 getBibliomaniaDatabase().getAuthorWebSiteTable().ensure(
223 this,
224 "http://en.wikipedia.org/wiki/"+getDisplayName().replace(' ','_'),
225 getDisplayName() + " on WikiPedia",
226 "");
227 Enumeration<Object> l = getLinks();
228 while(l.hasMoreElements()) {
229 AuthorWebSite aws = (AuthorWebSite)l.nextElement();
230 if (!aws.isStillLive())
231 aws.delete();
232 }
233 return getLinks();
234 }
235 public String adminHandle(Melati melati, MarkupLanguage markupLanguage)
236 throws Exception {
237 return null;
238 }
239
240 public String adminSpecialFacilities(
241 Melati melati,
242 MarkupLanguage markupLanguage)
243 throws Exception {
244
245 return getBibliomaniaDatabase().getWMTemplet("Author-specials");
246 }
247
248 public SectionGroup getReadArea() {
249 Section sectionL = getSection();
250 return sectionL != null
251 ? sectionL.getReadArea()
252 : getBibliomaniaDatabase().getReadSectionGroup();
253 }
254
255
256
257
258
259 public Treeable[] children = null;
260
261 public Treeable[] getChildren() {
262 if (children == null) {
263 Object[] kids = ArrayUtils.arrayOf(getBooks());
264 children = new Treeable[kids.length];
265 for (int i=0; i<kids.length; i++) {
266 children[i] = (Treeable)kids[i];
267 }
268 }
269 return children;
270 }
271
272
273
274 public String getName() {
275 return getDisplayName();
276 }
277
278 }