View Javadoc

1   package org.paneris.messageboard.model;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.sql.ResultSet;
6   
7   import org.paneris.jal.model.DDField;
8   import org.paneris.jal.model.DDRecord;
9   
10  /**
11   * <p>An item on the messageboard.</p>
12   */
13  
14  public class Attachment extends DDRecord {
15  
16    private static final long serialVersionUID = 1L;
17      Integer board = new Integer(0);
18      byte[] data = null;
19  
20  /**
21   * Constructor to build a DDRecord given a string describing the table and
22   * a java.sql.resultSet.
23   */
24      public Attachment(String db, ResultSet rs) throws Exception {
25        super(db,"attachments",rs);
26      }
27  
28  /**
29   * Constructor to build an empty DDRecord
30   *
31   * @throws Exception if something fails
32   */
33      public Attachment(String db) throws Exception {
34        super(db,"attachments");
35      }
36  
37  /**
38   * Constructor to build a DDRecord given a string or int describing 
39   * the table and an record id int.  
40   * If you don't have a record id, send it in as null.
41   *
42   * @exception    java.lang.Exception If somthing fails
43   */
44      public Attachment(String db, Integer record) throws Exception {
45          super(db,"attachments",record);
46      }
47  
48  /**
49   * Set data.
50   */
51      public void setData(byte[] data_) {
52        data = data_;
53      }
54  
55  /**
56   * Set the board.
57   */
58      public void setBoard(Integer b) {
59        board = b;
60      }
61  
62  /**
63   * get the size.
64   */
65      public String getSize() {
66           Integer size = new Integer(getFieldValue("size"));
67           int s = size.intValue();
68          if (s<1024) {
69              return new String(s+" Bytes");
70          }
71          if (s<1048576) {
72              return new String((s/1024)+" K");
73          } else {
74              return new String((s/1048576)+" MB");
75          }
76      }
77  
78  
79    public String getAttachmentPath() throws Exception {
80      DDRecord boardRec = new DDRecord(database,"messageboards",board);
81      return boardRec.getFieldValue("attachmentpath").trim();
82    }
83  
84  /**
85   * Write the file as well as the record.
86   *
87   * @throws Exception if somthing fails
88   */
89      public synchronized void write() throws Exception {
90      if (data != null) {
91      String path = getAttachmentPath();
92      String extension = null;
93      File outputDir = new File(path);
94          boolean missing = !outputDir.exists();
95          if (missing) {
96             throw new RuntimeException(
97               "Attachment directory (" +path+") does not exist.");
98          }
99  
100     String filename = getFieldValue("filename").trim();
101 
102     if (filename.length() == 0) {
103       filename = "att";
104       DDField ct = (DDField) get("type");
105       DDRecord ctype = ct.getLookup();
106       DDField ext = (DDField) ctype.get("extension");
107       if (ext != null && ext.getValue() != null)
108         filename += ext.getValue().toString().trim();
109     }
110         // get rid of spaces!
111         filename = filename.replace(' ','_');
112     filename = path + "/" + filename;
113     int filenumber = 0;
114     String testFilename = new String(filename);
115     File outputFile = new File(testFilename);
116     boolean found = outputFile.exists();
117     if (found) { // dump the extension
118       if (testFilename.lastIndexOf(".") != -1) {
119         extension = new String(filename.substring(filename.lastIndexOf(".")+1,
120                                                   filename.length()));
121         filename = filename.substring(0,filename.lastIndexOf("."));
122       }
123     }
124     while (found) {
125       filenumber++;
126       testFilename = new String(filename);
127       String filenumberString = "" + filenumber;
128       for (int i=0; i < (8 - filenumberString.length()); i++) {
129         testFilename += "0";
130       }
131       testFilename += filenumber;
132       if (extension != null) {
133         testFilename += "." + extension;
134       }
135       outputFile = new File(testFilename);
136       found = outputFile.exists();
137     }
138     filename = testFilename.trim();
139     setFieldValue("filename",filename.substring(path.length() + 1));
140     FileOutputStream os = new FileOutputStream(outputFile);
141     os.write(data);
142     os.close();
143     ((DDField)get("size")).setValue(new Integer(data.length));
144     }
145     super.write();
146      }
147 
148 }
149