| 1 | |
package org.paneris.bibliomania; |
| 2 | |
|
| 3 | |
import java.io.BufferedOutputStream; |
| 4 | |
import java.io.BufferedWriter; |
| 5 | |
import java.io.File; |
| 6 | |
import java.io.FileOutputStream; |
| 7 | |
import java.io.FileWriter; |
| 8 | |
import java.io.OutputStream; |
| 9 | |
import java.io.Writer; |
| 10 | |
import java.sql.Timestamp; |
| 11 | |
import java.util.Enumeration; |
| 12 | |
import java.util.Hashtable; |
| 13 | |
import java.util.Vector; |
| 14 | |
|
| 15 | |
import org.melati.poem.CachedSelection; |
| 16 | |
import org.melati.poem.Column; |
| 17 | |
import org.melati.poem.util.EmptyEnumeration; |
| 18 | |
import org.melati.poem.util.MappedEnumeration; |
| 19 | |
import org.melati.util.IoUtils; |
| 20 | |
import org.melati.util.MelatiRuntimeException; |
| 21 | |
import org.paneris.bibliomania.generated.UnitBase; |
| 22 | |
|
| 23 | |
public abstract class Unit extends UnitBase { |
| 24 | |
|
| 25 | |
public BibliomaniaDatabase getBibliomaniaDatabase () { |
| 26 | 40 | return (BibliomaniaDatabase)getDatabase(); |
| 27 | |
} |
| 28 | |
|
| 29 | 3382 | public Unit() {} |
| 30 | |
|
| 31 | |
public abstract long ftiTextID_start(); |
| 32 | |
public abstract long ftiTextID_limit(); |
| 33 | |
public abstract Unit getParentUnit(); |
| 34 | |
public abstract String getName(); |
| 35 | |
|
| 36 | |
public abstract String getMetatag_description(); |
| 37 | |
public abstract String getMetatag_keywords(); |
| 38 | |
public abstract void appendCacheSubpath(StringBuffer buffer); |
| 39 | |
|
| 40 | |
public Enumeration getAncestors() { |
| 41 | 0 | Vector v = new Vector(); |
| 42 | 0 | Unit p = getParentUnit(); |
| 43 | 0 | while ( p!= null) { |
| 44 | 0 | v.addElement(p); |
| 45 | 0 | p = p.getParentUnit(); |
| 46 | |
} |
| 47 | 0 | return v.elements(); |
| 48 | |
} |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 1691 | private CachedSelection productAssociations = null; |
| 62 | |
protected Enumeration getProductAssociations(Column c) { |
| 63 | 1 | if (getTroid() == null) |
| 64 | 0 | return new EmptyEnumeration(); |
| 65 | |
else { |
| 66 | 1 | if (productAssociations == null) |
| 67 | 1 | productAssociations = |
| 68 | |
c.cachedSelectionWhereEq(getTroid()); |
| 69 | 1 | return productAssociations.objects(); |
| 70 | |
} |
| 71 | |
} |
| 72 | |
public abstract Enumeration getProductAssociations(); |
| 73 | |
|
| 74 | |
public Enumeration getRelatedProducts() { |
| 75 | 1 | return new MappedEnumeration(getProductAssociations()) { |
| 76 | |
public Object mapped(Object productAssociation) { |
| 77 | 0 | return ((ProductAssociation)productAssociation).getProduct(); |
| 78 | |
} |
| 79 | |
}; |
| 80 | |
} |
| 81 | |
|
| 82 | |
public static class NoSourceDirException extends MelatiRuntimeException { |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
private static final long serialVersionUID = 1L; |
| 87 | |
public String description; |
| 88 | |
public File dir; |
| 89 | |
|
| 90 | |
public NoSourceDirException(String description, File dir) { |
| 91 | |
this.description = description; |
| 92 | |
this.dir = dir; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public String getMessage() { |
| 96 | |
return |
| 97 | |
"Can't access the source directory " + dir + " for " + description + |
| 98 | |
"---either it doesn't exist or it doesn't have the right permissions"; |
| 99 | |
} |
| 100 | |
} |
| 101 | |
|
| 102 | |
public void appendCachePath(StringBuffer buffer) { |
| 103 | 9 | buffer.append(getBibliomaniaDatabase().getCachedContentRootDir()); |
| 104 | 9 | appendCacheSubpath(buffer); |
| 105 | 9 | } |
| 106 | |
|
| 107 | |
public String getCachePath() { |
| 108 | 7 | StringBuffer buffer = new StringBuffer(); |
| 109 | 7 | appendCachePath(buffer); |
| 110 | 7 | return buffer.toString(); |
| 111 | |
} |
| 112 | |
|
| 113 | |
public String getBoardName() { |
| 114 | 0 | String n = getPath(); |
| 115 | 0 | if (n.endsWith("/")) |
| 116 | 0 | n = n.substring(0, n.length() - 1); |
| 117 | 0 | if (n.startsWith("/")) |
| 118 | 0 | n = n.substring(1, n.length()); |
| 119 | 0 | return n.replace('/','.'); |
| 120 | |
|
| 121 | |
} |
| 122 | |
|
| 123 | |
protected Hashtable speciallyTreatedInCache() { |
| 124 | 0 | return null; |
| 125 | |
} |
| 126 | |
|
| 127 | |
protected void writeLock() { |
| 128 | 122 | super.writeLock(); |
| 129 | 122 | lastencached = null; |
| 130 | 122 | } |
| 131 | |
|
| 132 | |
public abstract Enumeration<Unit> getMembersSlowly(); |
| 133 | |
|
| 134 | |
public abstract String getDisplayName(); |
| 135 | |
|
| 136 | |
public void readKeyDotTxt() throws Exception { |
| 137 | 0 | } |
| 138 | |
|
| 139 | |
public void paginate() throws Exception { |
| 140 | 1 | } |
| 141 | |
|
| 142 | |
public void index() throws Exception { |
| 143 | 0 | } |
| 144 | |
|
| 145 | |
public void encache() throws Exception { |
| 146 | 1 | assertCanWrite(); |
| 147 | |
|
| 148 | 1 | File sourceDir = new File(getBibliomaniaDatabase().getContentRootDir(), |
| 149 | |
getPath()); |
| 150 | 1 | String[] sources = sourceDir.list(); |
| 151 | 1 | if (sources == null) |
| 152 | 0 | sources = new String[0]; |
| 153 | |
|
| 154 | |
|
| 155 | 1 | File cachePath = new File(getCachePath()); |
| 156 | 1 | cachePath.mkdirs(); |
| 157 | |
|
| 158 | 1 | Hashtable speci = speciallyTreatedInCache(); |
| 159 | |
|
| 160 | 9 | for (int s = 0; s < sources.length; ++s) { |
| 161 | 8 | String source = sources[s]; |
| 162 | 8 | File from = new File(sourceDir, source); |
| 163 | |
|
| 164 | 8 | if (!from.isDirectory() && |
| 165 | |
(speci == null || !speci.containsKey(source))) { |
| 166 | |
File to; |
| 167 | 2 | if (source.endsWith(".wm")) { |
| 168 | 0 | to = |
| 169 | |
new File(cachePath, |
| 170 | |
source.substring(0, source.length() - 3) + ".html"); |
| 171 | 0 | OutputStream w = new BufferedOutputStream(new FileOutputStream(to)); |
| 172 | |
|
| 173 | |
try { |
| 174 | 0 | getBibliomaniaDatabase().writeContentHeader(w, this, null); |
| 175 | 0 | getBibliomaniaDatabase().interpolateAsTemplate(from, w, this); |
| 176 | 0 | getBibliomaniaDatabase().writeContentFooter(w, this, null); |
| 177 | |
} |
| 178 | |
finally { |
| 179 | 0 | try { w.close(); } catch (Exception e) {} |
| 180 | 0 | } |
| 181 | 0 | } |
| 182 | |
else |
| 183 | 2 | IoUtils.copy(from, 16384, to = new File(cachePath, source)); |
| 184 | |
|
| 185 | 2 | BibliomaniaDatabase.notifyNewContentFile(to); |
| 186 | |
} |
| 187 | |
} |
| 188 | |
|
| 189 | 1 | File defaul = new File(cachePath, "index.html"); |
| 190 | 1 | if (!defaul.exists()) { |
| 191 | 0 | Writer w = new BufferedWriter(new FileWriter(defaul)); |
| 192 | |
try { |
| 193 | 0 | getBibliomaniaDatabase().interpolateTemplateToFile( |
| 194 | |
"read/Default.wm", defaul, this); |
| 195 | |
} |
| 196 | |
finally { |
| 197 | 0 | try { w.close(); } catch (Exception e) {} |
| 198 | 0 | } |
| 199 | |
} |
| 200 | |
|
| 201 | 1 | File redirec = new File(cachePath, "contents.html"); |
| 202 | |
|
| 203 | |
{ |
| 204 | 1 | Writer w = new BufferedWriter(new FileWriter(redirec)); |
| 205 | |
try { |
| 206 | 1 | getBibliomaniaDatabase().interpolateTemplateToFile( |
| 207 | |
"Contents.wm", redirec, this); |
| 208 | |
} |
| 209 | |
finally { |
| 210 | 1 | try { w.close(); } catch (Exception e) {} |
| 211 | 0 | } |
| 212 | |
} |
| 213 | |
|
| 214 | 1 | setLastencached(new Timestamp(System.currentTimeMillis())); |
| 215 | 1 | } |
| 216 | |
|
| 217 | |
public String getURLSubpath() { |
| 218 | 0 | return getBibliomaniaDatabase().getBib().urlPrefixed( |
| 219 | |
"", null, null, this, null); |
| 220 | |
} |
| 221 | |
|
| 222 | |
public String getURLSubpathReally() { |
| 223 | 0 | return getBibliomaniaDatabase().getBib().urlPrefixedReally( |
| 224 | |
"", null, null, this, null); |
| 225 | |
} |
| 226 | |
|
| 227 | |
public abstract SectionGroup getReadArea(); |
| 228 | |
} |
| 229 | |
|