View Javadoc

1   package net.sf.cantina.system;
2   
3   import net.sf.cantina.DataSource;
4   import net.sf.cantina.Document;
5   import net.sf.cantina.Realm;
6   import net.sf.cantina.User;
7   import net.sf.cantina.exceptions.ForbiddenOperationException;
8   import net.sf.cantina.exceptions.DuplicateKeyException;
9   import net.sf.cantina.exceptions.ObjectNotFoundException;
10  import net.sf.hibernate.NonUniqueObjectException;
11  import net.sf.hibernate.JDBCException;
12  
13  /***
14   * @author Stephane JAIS
15   */
16  public class CreateDocumentTransaction
17  implements Transaction
18  {
19    private String documentId;
20    private String realmName;
21    private User user;
22    private String contentType;
23    public CreateDocumentTransaction(String documentId,
24                                     String realmName,
25                                     String contentType,
26                                     User u)
27    throws Exception
28    {
29      this.user = u;
30      this.realmName = realmName;
31      this.documentId = documentId;
32      this.contentType = contentType;
33    }
34  
35    public void execute() throws DuplicateKeyException, Exception
36    {
37      // check the realm for existance
38      Realm realm = DataSource.getInstance().loadRealm(realmName);
39      if (!realm.accepts(user))
40        throw new ForbiddenOperationException();
41      DataSource.getInstance().createDocument(documentId);
42      Document newInstance = DataSource.getInstance().loadDocument(documentId);
43      newInstance.setRealm(realm);
44      newInstance.setContentType(contentType);
45      DataSource.getInstance().saveDocument(newInstance);
46    }
47  }