Example usage for javax.ejb EJBTransactionRolledbackException getCause

List of usage examples for javax.ejb EJBTransactionRolledbackException getCause

Introduction

In this page you can find the example usage for javax.ejb EJBTransactionRolledbackException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.gss_project.gss.server.ejb.ExternalAPIBean.java

@Override
 public void updateFile(Long userId, Long fileId, String name, String tagSet, Date modificationDate,
         Boolean versioned, Boolean readForAll, Set<Permission> permissions)
         throws DuplicateNameException, ObjectNotFoundException, InsufficientPermissionsException {
     if (userId == null)
         throw new ObjectNotFoundException("No user specified");
     if (fileId == null)
         throw new ObjectNotFoundException("No file specified");
     FileHeader file = dao.getEntityById(FileHeader.class, fileId);
     final Folder parent = file.getFolder();
     if (parent == null)
         throw new ObjectNotFoundException("The specified file has no parent folder");

     User user = dao.getEntityById(User.class, userId);
     // Check permissions for modifying the file metadata.
     if ((name != null || tagSet != null || modificationDate != null || versioned != null)
             && !file.hasWritePermission(user))
         throw new InsufficientPermissionsException(
                 "User " + user.getId() + " cannot update file " + file.getName() + "(" + file.getId() + ")");
     // Check permissions for making file public.
     if (readForAll != null && !user.equals(file.getOwner()))
         throw new InsufficientPermissionsException("Only the owner can make a file public or not public");
     // Check permissions for modifying the ACL.
     if (permissions != null && !permissions.isEmpty() && !file.hasModifyACLPermission(user))
         throw new InsufficientPermissionsException("User " + user.getId()
                 + " cannot update the permissions on file " + file.getName() + "(" + file.getId() + ")");

     if (name != null) {
         // Do plain check for file already exists.
         // Extreme concurrency case should be caught by constraint violation later.
         if (dao.existsFolderOrFile(parent.getId(), name))
             throw new DuplicateNameException("A file or folder with the name '" + name + "' already exists");
         file.setName(name);// w  ww . j ava  2 s .co m
     }

     if (modificationDate != null)
         file.getAuditInfo().setModificationDate(modificationDate);
     else
         file.getAuditInfo().setModificationDate(new Date());
     file.getAuditInfo().setModifiedBy(user);

     List<FileTag> tags = file.getFileTags();
     if (tagSet != null) {
         Iterator<FileTag> i = tags.iterator();
         while (i.hasNext()) {
             FileTag tag = i.next();
             i.remove();
             tag.setFile(null);
             user.removeTag(tag);
             dao.delete(tag);
         }
         dao.flush();
         StringTokenizer st = new StringTokenizer(tagSet, ",");
         while (st.hasMoreTokens())
             new FileTag(user, file, st.nextToken().trim());
     }
     if (versioned != null && !file.isVersioned() == versioned) {
         if (file.isVersioned())
             removeOldVersions(userId, fileId);
         file.setVersioned(versioned);
     }
     if (readForAll != null && user.equals(file.getOwner()))
         file.setReadForAll(readForAll);
     if (permissions != null && !permissions.isEmpty())
         setFilePermissions(file, permissions);

     /*
      * Force constraint violation to manifest itself here.
      * This should cover extreme concurrency cases that the simple check
      * above hasn't caught.
      */
     try {
         dao.flush();
     } catch (EJBTransactionRolledbackException e) {
         Throwable cause = e.getCause();
         if (cause instanceof PersistenceException && cause.getCause() instanceof ConstraintViolationException)
             throw new DuplicateNameException("A file or folder with the name '" + name + "' already exists");
         throw e;
     }

     touchParentFolders(parent, user, new Date());

     // Re-index the file if it was modified.
     if (name != null || tagSet != null || (permissions != null && !permissions.isEmpty()) || readForAll != null)
         indexFile(fileId, false);
 }