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 mighe 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 EmptyEnumeration.it;
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 
133   public String getWMTemplet(String templetName) {
134     return "org/paneris/bibliomania/template/webmacro/templets/html/" + templetName + ".wm";
135   }
136   public abstract Enumeration getMembersSlowly();
137 
138   public abstract String getDisplayName();
139 
140   public void readKeyDotTxt() throws Exception {
141   }
142 
143   public void paginate() throws Exception {
144   }
145 
146   public void index() throws Exception {
147   }
148 
149   public void encache() throws Exception {
150     assertCanWrite();
151 
152     File sourceDir = new File(getBibliomaniaDatabase().getContentRootDir(),
153                               getPath());
154     String[] sources = sourceDir.list();
155     if (sources == null)
156       sources = new String[0];
157     // rather than throw new NoSourceDirException(toString(), sourceDir);
158 
159     File cachePath = new File(getCachePath());
160     cachePath.mkdirs();
161 
162     Hashtable speci = speciallyTreatedInCache();
163 
164     for (int s = 0; s < sources.length; ++s) {
165       String source = sources[s];
166       File from = new File(sourceDir, source);
167 
168       if (!from.isDirectory() &&
169           (speci == null || !speci.containsKey(source))) {
170         File to;
171         if (source.endsWith(".wm")) {
172           to =
173               new File(cachePath,
174                        source.substring(0, source.length() - 3) + ".html");
175           OutputStream w = new BufferedOutputStream(new FileOutputStream(to));
176 
177           try {
178             getBibliomaniaDatabase().writeContentHeader(w, this, null);
179             getBibliomaniaDatabase().macroexpand(from, w, this);
180             getBibliomaniaDatabase().writeContentFooter(w, this, null);
181           }
182           finally {
183             try { w.close(); } catch (Exception e) {}
184           }
185         }
186         else
187           IoUtils.copy(from, 16384, to = new File(cachePath, source));
188 
189         BibliomaniaDatabase.notifyNewContentFile(to);
190       }
191     }
192 
193     File defaul = new File(cachePath, "index.html");
194     if (!defaul.exists()) {
195       Writer w = new BufferedWriter(new FileWriter(defaul));
196       try {
197         getBibliomaniaDatabase().templateExpandNamed(
198             "read/Default.wm", defaul, this);
199       }
200       finally {
201         try { w.close(); } catch (Exception e) {}
202       }
203     }
204 
205     File redirec = new File(cachePath, "contents.html");
206 
207     {
208       Writer w = new BufferedWriter(new FileWriter(redirec));
209       try {
210         getBibliomaniaDatabase().templateExpandNamed(
211             "Contents.wm", redirec, this);
212       }
213       finally {
214         try { w.close(); } catch (Exception e) {}
215       }
216     }
217 
218     setLastencached(new Timestamp(System.currentTimeMillis()));
219   }
220 
221   public String getURLSubpath() {
222     return getBibliomaniaDatabase().getBib().urlPrefixed(
223                "", null, null, this, null);
224   }
225 
226   public String getURLSubpathReally() {
227     return getBibliomaniaDatabase().getBib().urlPrefixedReally(
228                "", null, null, this, null);
229   }
230 
231   public abstract SectionGroup getReadArea();
232 }
233