Example usage for javax.ejb TransactionAttributeType REQUIRED

List of usage examples for javax.ejb TransactionAttributeType REQUIRED

Introduction

In this page you can find the example usage for javax.ejb TransactionAttributeType REQUIRED.

Prototype

TransactionAttributeType REQUIRED

To view the source code for javax.ejb TransactionAttributeType REQUIRED.

Click Source Link

Document

If a client invokes the enterprise bean's method while the client is associated with a transaction context, the container invokes the enterprise bean's method in the client's transaction context.

Usage

From source file:com.flexive.ejb.beans.AccountEngineBean.java

/**
 * {@inheritDoc}//from ww w . ja  va  2s  .  c o  m
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void addRole(long accountId, long roleId) throws FxApplicationException {
    final List<Role> roles = new ArrayList<Role>();
    roles.addAll(getRoles(accountId, RoleLoadMode.ALL));
    roles.add(Role.getById(roleId));
    setRoles(accountId, roles);
}

From source file:com.flexive.ejb.beans.ScriptingEngineBean.java

/**
 * {@inheritDoc}// w ww  .  jav  a 2s. co  m
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void executeRunOnceScripts() throws FxApplicationException {
    final long start = System.currentTimeMillis();
    runOnce(SystemParameters.DIVISION_RUNONCE, "flexive", "fxresources", "[fleXive]");
    if (LOG.isInfoEnabled()) {
        LOG.info("Executed flexive run-once scripts in " + (System.currentTimeMillis() - start) + "ms");
    }
}

From source file:com.flexive.ejb.beans.AccountEngineBean.java

/**
 * {@inheritDoc}//from  www.j  a va2 s  .c  o m
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void addGroup(long accountId, long groupId) throws FxApplicationException {
    final List<UserGroup> groups = new ArrayList<UserGroup>();
    groups.addAll(getGroups(accountId));
    groups.add(CacheAdmin.getEnvironment().getUserGroup(groupId));
    setGroups(accountId, groups);
}

From source file:org.nightlabs.jfire.accounting.AccountingManagerBean.java

@TransactionAttribute(TransactionAttributeType.REQUIRED)
@RolesAllowed("org.nightlabs.jfire.accounting.queryLocalAccountantDelegates")
@Override//from  w w  w . j av  a2  s  .  c om
public Collection<LocalAccountantDelegate> getLocalAccountantDelegates(
        final Collection<LocalAccountantDelegateID> delegateIDs, final String[] fetchGroups,
        final int maxFetchDepth) {
    final PersistenceManager pm = createPersistenceManager();
    try {
        return NLJDOHelper.getDetachedObjectSet(pm, delegateIDs, LocalAccountantDelegate.class, fetchGroups,
                maxFetchDepth);
    } finally {
        pm.close();
    }
}

From source file:org.nightlabs.jfire.store.StoreManagerBean.java

@TransactionAttribute(TransactionAttributeType.REQUIRED)
@RolesAllowed("org.nightlabs.jfire.store.editDeliveryNote")
@Override//  w w w  .j  a  v  a  2s .  com
public DeliveryNote createDeliveryNote(Collection<ArticleID> articleIDs, String deliveryNoteIDPrefix,
        boolean get, String[] fetchGroups, int maxFetchDepth) throws DeliveryNoteEditException {
    PersistenceManager pm = createPersistenceManager();
    try {
        pm.getExtent(Article.class);
        User user = User.getUser(pm, getPrincipal());
        Trader trader = Trader.getTrader(pm);
        Store store = trader.getStore();

        List<Article> articles = new ArrayList<Article>(articleIDs.size());
        for (ArticleID articleID : articleIDs) {
            Article article = (Article) pm.getObjectById(articleID);
            Offer offer = article.getOffer();
            trader.validateOffer(offer);
            trader.acceptOfferImplicitely(offer);
            articles.add(article);
        }

        DeliveryNote deliveryNote = store.createDeliveryNote(user, articles, deliveryNoteIDPrefix);
        store.validateDeliveryNote(deliveryNote);

        if (get) {
            pm.getFetchPlan()
                    .setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS + FetchPlan.DETACH_UNLOAD_FIELDS);
            pm.getFetchPlan().setMaxFetchDepth(maxFetchDepth);
            if (fetchGroups != null)
                pm.getFetchPlan().setGroups(fetchGroups);

            return pm.detachCopy(deliveryNote);
        }
        return null;
    } finally {
        pm.close();
    }
}

From source file:com.flexive.ejb.beans.structure.TypeEngineBean.java

/**
 * {@inheritDoc}/*  w w  w.ja v a  2  s . c  o m*/
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void remove(long id) throws FxApplicationException {
    final UserTicket ticket = FxContext.getUserTicket();
    FxPermissionUtils.checkRole(ticket, Role.StructureManagement);

    FxType type = CacheAdmin.getEnvironment().getType(id);

    Connection con = null;
    PreparedStatement ps = null;
    StringBuilder sql = new StringBuilder(500);
    try {
        con = Database.getDbConnection();
        List<FxPropertyAssignment> allPropertyAssignments = new ArrayList<FxPropertyAssignment>(20);
        FxEnvironment env = CacheAdmin.getEnvironment();
        for (FxPropertyAssignment fxpa : env.getPropertyAssignments(true))
            if (fxpa.getAssignedType().getId() == id)
                allPropertyAssignments.add(fxpa);
        List<Long> rmStackProp = new ArrayList<Long>(allPropertyAssignments.size());
        List<FxPropertyAssignment> rmProp = new ArrayList<FxPropertyAssignment>(allPropertyAssignments.size());

        for (FxPropertyAssignment a : allPropertyAssignments)
            if (a.getBaseAssignmentId() == FxAssignment.NO_PARENT)
                rmStackProp.add(a.getId());
            else {
                //check if base is from the same type
                if (env.getAssignment(a.getBaseAssignmentId()).getAssignedType().getId() == id)
                    rmProp.add(a);
                else
                    rmStackProp.add(a.getId());
            }
        boolean found;
        while (rmProp.size() > 0) {
            found = false;
            for (FxPropertyAssignment a : rmProp)
                if (rmStackProp.contains(a.getBaseAssignmentId())) {
                    rmProp.remove(a);
                    rmStackProp.add(0, a.getId());
                    found = true;
                    break;
                }
            assert found : "Internal error: no property assignment found to be removed!";
        }
        //remove group assignments in the 'correct' order (ie not violating parentgroup references)
        ArrayList<Long> rmStack = new ArrayList<Long>(10);
        buildGroupAssignmentRemoveStack(type.getConnectedAssignments("/"), rmStack);
        rmStack.addAll(0, rmStackProp);

        sql.setLength(0);
        sql.append("DELETE FROM ").append(TBL_STRUCT_ASSIGNMENTS).append(ML).append(" WHERE ID=?");
        ps = con.prepareStatement(sql.toString());
        for (Long rmid : rmStack) {
            ps.setLong(1, rmid);
            ps.addBatch();
        }
        ps.executeBatch();
        ps.close();

        //prevent base-related constraint issues by setting the base to null prior to removal
        sql.setLength(0);
        sql.append("UPDATE ").append(TBL_STRUCT_ASSIGNMENTS).append(" SET BASE=NULL WHERE TYPEDEF=? AND ID=?");
        ps = con.prepareStatement(sql.toString());
        ps.setLong(1, type.getId());
        for (Long rmid : rmStack) {
            ps.setLong(2, rmid);
            ps.addBatch();
        }
        ps.executeBatch();
        ps.close();

        //remove property and group assignment option entries
        sql.setLength(0);
        for (FxPropertyAssignment pa : allPropertyAssignments) {
            if ( //exclude the "ID" property whose Id is "0" which is "NO_PARENT"
            !(pa.getProperty().getId() == FxAssignment.NO_PARENT)) {
                if (sql.length() == 0) {
                    sql.append(" WHERE ASSID IN(").append(pa.getId());
                } else
                    sql.append(',').append(pa.getId());
            }
        }
        if (sql.length() > 0) {
            sql.append(')');
            ps = con.prepareStatement("DELETE FROM " + TBL_STRUCT_PROPERTY_OPTIONS + sql.toString());
            ps.executeUpdate();
            ps.close();
        }
        sql.setLength(0);
        for (FxGroupAssignment ga : type.getAssignedGroups()) {
            if (ga.getBaseAssignmentId() == FxAssignment.NO_PARENT) {
                if (sql.length() == 0) {
                    sql.append(" WHERE ASSID IN(").append(ga.getId());
                } else
                    sql.append(',').append(ga.getId());
            }
        }
        if (sql.length() > 0) {
            sql.append(')');
            ps = con.prepareStatement("DELETE FROM " + TBL_STRUCT_GROUP_OPTIONS + sql.toString());
            ps.executeUpdate();
            ps.close();
        }

        // remove all type structure options
        storeTypeOptions(con, TBL_STRUCT_TYPES_OPTIONS, "ID", id, null, true);

        //remove all flat storage assignments for this type
        FxFlatStorageManager.getInstance().removeTypeMappings(con, type.getId());

        //remove the assignments
        sql.setLength(0);
        //clear parent key refs for removal to avoid referential integrity issues within the type itself
        //            sql.append("UPDATE ").append(TBL_STRUCT_ASSIGNMENTS).append(" SET PARENTGROUP=ID WHERE TYPEDEF=?");
        //            ps = con.prepareStatement(sql.toString());
        //            ps.setLong(1, type.getId());
        //            ps.executeUpdate();
        //            ps.close();
        //            sql.setLength(0);
        //            ps = con.prepareStatement(sql.toString());
        sql.append("DELETE FROM ").append(TBL_STRUCT_ASSIGNMENTS).append(" WHERE TYPEDEF=? AND ID=?");
        ps = con.prepareStatement(sql.toString());
        ps.setLong(1, type.getId());
        for (Long rmid : rmStack) {
            ps.setLong(2, rmid);
            ps.addBatch();
        }
        ps.executeBatch();
        ps.close();

        sql.setLength(0);
        sql.append("DELETE FROM ").append(TBL_STRUCT_TYPERELATIONS)
                .append(" WHERE TYPEDEF=? OR TYPESRC=? OR TYPEDST=?");
        ps = con.prepareStatement(sql.toString());
        ps.setLong(1, type.getId());
        ps.setLong(2, type.getId());
        ps.setLong(3, type.getId());
        ps.executeUpdate();
        ps.close();
        sql.setLength(0);
        sql.append("DELETE FROM ").append(TBL_STRUCT_TYPES).append(ML).append(" WHERE ID=?");
        ps = con.prepareStatement(sql.toString());
        ps.setLong(1, type.getId());
        ps.executeUpdate();
        ps.close();
        sql.setLength(0);
        sql.append("UPDATE ").append(TBL_STRUCT_PROPERTIES).append(" SET REFTYPE=NULL WHERE REFTYPE=?");
        ps = con.prepareStatement(sql.toString());
        ps.setLong(1, type.getId());
        ps.executeUpdate();
        ps.close();
        sql.setLength(0);
        sql.append("DELETE FROM ").append(TBL_STRUCT_TYPES).append(" WHERE ID=?");
        ps = con.prepareStatement(sql.toString());
        ps.setLong(1, type.getId());
        ps.executeUpdate();

        //remove eventually orphaned properties and groups
        FxStructureUtils.removeOrphanedProperties(con);
        FxStructureUtils.removeOrphanedGroups(con);

        StructureLoader.reload(con);
        htracker.track(type, "history.type.remove", type.getName(), type.getId());
    } catch (SQLException e) {
        if (StorageManager.isForeignKeyViolation(e)) {
            EJBUtils.rollback(ctx);
            throw new FxRemoveException(LOG, e, "ex.structure.type.inUse", type.getName());
        }
        EJBUtils.rollback(ctx);
        throw new FxRemoveException(LOG, e, "ex.db.sqlError", e.getMessage());
    } catch (FxCacheException e) {
        EJBUtils.rollback(ctx);
        throw new FxRemoveException(LOG, e, "ex.cache", e.getMessage());
    } catch (FxLoadException e) {
        EJBUtils.rollback(ctx);
        throw new FxRemoveException(e);
    } finally {
        Database.closeObjects(TypeEngineBean.class, con, ps);
    }
}

From source file:org.nightlabs.jfire.accounting.AccountingManagerBean.java

@TransactionAttribute(TransactionAttributeType.REQUIRED)
@RolesAllowed("org.nightlabs.jfire.accounting.queryLocalAccountantDelegates")
@Override// ww w. j  a  v  a2s.  c  o  m
public LocalAccountantDelegate getLocalAccountantDelegate(final LocalAccountantDelegateID delegateID,
        final String[] fetchGroups, final int maxFetchDepth) {
    final PersistenceManager pm = createPersistenceManager();
    try {
        pm.getFetchPlan().setMaxFetchDepth(maxFetchDepth);
        if (fetchGroups != null)
            pm.getFetchPlan().setGroups(fetchGroups);

        final LocalAccountantDelegate delegate = (LocalAccountantDelegate) pm.getObjectById(delegateID);
        return pm.detachCopy(delegate);
    } finally {
        pm.close();
    }
}

From source file:com.flexive.ejb.beans.ContentEngineBean.java

/**
 * {@inheritDoc}//  w  w w . j a v  a2  s.  c o  m
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public FxLock lock(FxLockType lockType, FxPK pk) throws FxLockException {
    Connection con = null;
    try {
        con = Database.getDbConnection();
        FxLock lock = StorageManager.getLockStorage().lock(con, lockType, pk);
        FxCachedContent cachedContent = CacheAdmin.getCachedContent(pk);
        if (cachedContent != null) {
            cachedContent.updateLock(lock);
            CacheAdmin.cacheContent(cachedContent);
        }
        return lock;
    } catch (SQLException e) {
        throw new FxLockException(e, "ex.db.sqlError", e.getMessage());
    } catch (FxNotFoundException e) {
        throw new FxLockException(e);
    } finally {
        Database.closeObjects(ContentEngineBean.class, con, null);
    }
}

From source file:org.nightlabs.jfire.accounting.AccountingManagerBean.java

@TransactionAttribute(TransactionAttributeType.REQUIRED)
@RolesAllowed("org.nightlabs.jfire.accounting.editLocalAccountantDelegate")
@Override//from   ww  w  .j ava2s .co  m
public LocalAccountantDelegate storeLocalAccountantDelegate(final LocalAccountantDelegate delegate,
        final boolean get, final String[] fetchGroups, final int maxFetchDepth) {
    final PersistenceManager pm = createPersistenceManager();
    try {
        return NLJDOHelper.storeJDO(pm, delegate, get, fetchGroups, maxFetchDepth);
    } finally {
        pm.close();
    }
}

From source file:com.flexive.ejb.beans.ScriptingEngineBean.java

/**
 * {@inheritDoc}/*  ww w.j  a  va  2 s.co  m*/
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void executeDropRunOnceScripts(Parameter<Boolean> param, String dropName) throws FxApplicationException {
    if (!FxSharedUtils.getDrops().contains(dropName))
        throw new FxInvalidParameterException("dropName", "ex.scripting.drop.notFound", dropName);
    runOnce(param, dropName, dropName + "Resources", dropName);
}