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