View Javadoc

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        return (BibliomaniaDatabase)getDatabase();
27    }
28  
29    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        Vector v = new Vector();
42        Unit p = getParentUnit();
43        while ( p!= null) {
44            v.addElement(p);
45            p = p.getParentUnit();
46        }
47        return v.elements();
48    }
49    /*
50  
51  This only sort of works, ie it does not stop deleted 
52  rows from being returned by normal select.
53  If we really want to do this we do actually have to set the flags in the records.
54  In which case might as well do it in SQL outside.
55    public Boolean getDeleted() {
56      return super.getDeleted().booleanValue() || 
57              (getParentUnit() != null && getParentUnit().getDeleted().booleanValue()) 
58             ? new Boolean(true) : new Boolean(false);
59    }
60    */
61    private CachedSelection productAssociations = null;
62    protected Enumeration getProductAssociations(Column c) {
63      if (getTroid() == null)
64        return new EmptyEnumeration();
65      else {
66        if (productAssociations == null)
67          productAssociations =
68              c.cachedSelectionWhereEq(getTroid());
69        return productAssociations.objects();
70      }
71    }
72    public abstract Enumeration getProductAssociations();
73    
74    public Enumeration getRelatedProducts() {
75      return new MappedEnumeration(getProductAssociations()) {
76            public Object mapped(Object productAssociation) {
77                 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     buffer.append(getBibliomaniaDatabase().getCachedContentRootDir());
104     appendCacheSubpath(buffer);
105   }
106 
107   public String getCachePath() {
108     StringBuffer buffer = new StringBuffer();
109     appendCachePath(buffer);
110     return buffer.toString();
111   }
112 
113   public String getBoardName() {
114     String n = getPath();
115     if (n.endsWith("/"))
116       n = n.substring(0, n.length() - 1);
117     if (n.startsWith("/"))
118       n = n.substring(1, n.length());
119     return n.replace('/','.'); 
120       
121   }
122 
123   protected Hashtable speciallyTreatedInCache() {
124     return null;
125   }
126 
127   protected void writeLock() {
128     super.writeLock();
129     lastencached = null;
130   }
131 
132   public abstract Enumeration<Unit> getMembersSlowly();
133 
134   public abstract String getDisplayName();
135 
136   public void readKeyDotTxt() throws Exception {
137   }
138 
139   public void paginate() throws Exception {
140   }
141 
142   public void index() throws Exception {
143   }
144 
145   public void encache() throws Exception {
146     assertCanWrite();
147 
148     File sourceDir = new File(getBibliomaniaDatabase().getContentRootDir(),
149                               getPath());
150     String[] sources = sourceDir.list();
151     if (sources == null)
152       sources = new String[0];
153     // rather than throw new NoSourceDirException(toString(), sourceDir);
154 
155     File cachePath = new File(getCachePath());
156     cachePath.mkdirs();
157 
158     Hashtable speci = speciallyTreatedInCache();
159 
160     for (int s = 0; s < sources.length; ++s) {
161       String source = sources[s];
162       File from = new File(sourceDir, source);
163 
164       if (!from.isDirectory() &&
165           (speci == null || !speci.containsKey(source))) {
166         File to;
167         if (source.endsWith(".wm")) {
168           to =
169               new File(cachePath,
170                        source.substring(0, source.length() - 3) + ".html");
171           OutputStream w = new BufferedOutputStream(new FileOutputStream(to));
172 
173           try {
174             getBibliomaniaDatabase().writeContentHeader(w, this, null);
175             getBibliomaniaDatabase().interpolateAsTemplate(from, w, this);
176             getBibliomaniaDatabase().writeContentFooter(w, this, null);
177           }
178           finally {
179             try { w.close(); } catch (Exception e) {}
180           }
181         }
182         else
183           IoUtils.copy(from, 16384, to = new File(cachePath, source));
184 
185         BibliomaniaDatabase.notifyNewContentFile(to);
186       }
187     }
188 
189     File defaul = new File(cachePath, "index.html");
190     if (!defaul.exists()) {
191       Writer w = new BufferedWriter(new FileWriter(defaul));
192       try {
193         getBibliomaniaDatabase().interpolateTemplateToFile(
194             "read/Default.wm", defaul, this);
195       }
196       finally {
197         try { w.close(); } catch (Exception e) {}
198       }
199     }
200 
201     File redirec = new File(cachePath, "contents.html");
202 
203     {
204       Writer w = new BufferedWriter(new FileWriter(redirec));
205       try {
206         getBibliomaniaDatabase().interpolateTemplateToFile(
207             "Contents.wm", redirec, this);
208       }
209       finally {
210         try { w.close(); } catch (Exception e) {}
211       }
212     }
213 
214     setLastencached(new Timestamp(System.currentTimeMillis()));
215   }
216 
217   public String getURLSubpath() {
218     return getBibliomaniaDatabase().getBib().urlPrefixed(
219                "", null, null, this, null);
220   }
221 
222   public String getURLSubpathReally() {
223     return getBibliomaniaDatabase().getBib().urlPrefixedReally(
224                "", null, null, this, null);
225   }
226 
227   public abstract SectionGroup getReadArea();
228 }
229