1 package org.paneris.melati.site.model; 2 3 import org.paneris.melati.site.model.generated.UploadedFileTableBase; 4 import org.melati.poem.DefinitionSource; 5 import org.melati.poem.Database; 6 import org.melati.poem.PoemException; 7 import org.melati.poem.Persistent; 8 import org.melati.poem.AccessPoemException; 9 import org.melati.poem.InitialisationPoemException; 10 import org.melati.poem.ValidationPoemException; 11 import org.melati.poem.PoemThread; 12 import org.melati.poem.User; 13 import java.sql.Date; 14 15 /** 16 * Melati POEM generated, programmer modifiable stub 17 * for a <code>UploadedFileTable</code> object. 18 * <p> 19 * Description: 20 * A file uploaded by a user. 21 * </p> 22 * 23 * 24 * <table> 25 * <tr><th colspan='3'> 26 * Field summary for SQL table <code>UploadedFile</code> 27 * </th></tr> 28 * <tr><th>Name</th><th>Type</th><th>Description</th></tr> 29 * <tr><td> type </td><td> UploadedFileType </td><td> The type of this file 30 * </td></tr> 31 * <tr><td> description </td><td> String </td><td> A description of the file 32 * </td></tr> 33 * <tr><td> size </td><td> Integer </td><td> The size of this file </td></tr> 34 * <tr><td> when </td><td> Date </td><td> The date on which this file was 35 * uploaded </td></tr> 36 * <tr><td> uploadedby </td><td> User </td><td> The user who uploaded this 37 * file </td></tr> 38 * <tr><td> deleted </td><td> Boolean </td><td> Whether this file been 39 * deleted or not </td></tr> 40 * </table> 41 * 42 * @generator org.melati.poem.prepro.TableDef#generateTableMainJava 43 */ 44 public class UploadedFileTable extends UploadedFileTableBase { 45 46 /** 47 * Constructor. 48 * 49 * @generator org.melati.poem.prepro.TableDef#generateTableMainJava 50 * @param database the POEM database we are using 51 * @param name the name of this <code>Table</code> 52 * @param definitionSource which definition is being used 53 * @throws PoemException if anything goes wrong 54 */ 55 public UploadedFileTable( 56 Database database, String name, 57 DefinitionSource definitionSource) throws PoemException { 58 super(database, name, definitionSource); 59 } 60 61 // programmer's domain-specific code here 62 63 public void create(Persistent persistent) 64 throws AccessPoemException, ValidationPoemException, 65 InitialisationPoemException { 66 67 Integer userTroid; 68 try{ 69 User user = (User)PoemThread.accessToken(); 70 userTroid = user.troid(); 71 } catch (ClassCastException e){ // When access token is root ie not a real user 72 userTroid = new Integer(1); 73 } 74 75 /* 76 Need canUpload capability? 77 78 if (!user.canUploadTo(account)) 79 throw new AccessPoemException(PoemThread.accessToken(), 80 getDatabase().administerCapability()); 81 */ 82 83 /* 84 File testfile = new File( ((UploadedFile)persistent).getPath_unsafe() ); 85 long size = testfile.length(); 86 String sizeString = null; 87 if ((size / KILOBYTE) >= 1) { 88 if ((size / MEGABYTE) >= 1) { 89 if ((size / GIGABYTE) >= 1) 90 sizeString = (size / GIGABYTE) + " Gb"; 91 else 92 sizeString = (size / MEGABYTE) + " Mb"; 93 } else { 94 sizeString = (size / KILOBYTE) + " Kb"; 95 } 96 } else { 97 sizeString = size + " bytes"; 98 } 99 */ 100 persistent.setRaw("uploadedby", userTroid); 101 persistent.setRaw("when", new Date(new java.util.Date().getTime())); 102 persistent.setRaw("deleted", Boolean.FALSE); 103 104 super.create(persistent); 105 } 106 107 /** 108 * @return the relative url 109 */ 110 public String getRelativeUrl() { 111 return "/"; 112 } 113 114 }