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.PhraseEngineBean.java

/**
 * {@inheritDoc}/*from   www  .  jav  a 2s.c  o m*/
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void assignPhrase(int category, long assignmentOwner, long nodeId, long nodeMandator, String phraseKey,
        long phraseMandator, long pos, boolean checkPositioning)
        throws FxNotFoundException, FxNoAccessException {
    assignPhrase(category, assignmentOwner, nodeId, nodeMandator, loadPhrase(phraseKey, phraseMandator).getId(),
            phraseMandator, pos, checkPositioning);
}

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

/**
 * {@inheritDoc}/*ww w .j  ava2s . c o m*/
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void assignPhrase(long assignmentOwner, long nodeId, long nodeMandator, String phraseKey,
        long phraseMandator, long pos, boolean checkPositioning)
        throws FxNotFoundException, FxNoAccessException {
    assignPhrase(assignmentOwner, nodeId, nodeMandator, loadPhrase(phraseKey, phraseMandator).getId(),
            phraseMandator, pos, checkPositioning);
}

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

/**
 * {@inheritDoc}/*from w ww.jav a  2s . c o  m*/
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void assignPhrases(long position, long assignmentOwner, long nodeId, long nodeMandator,
        FxPhrase[] phrases) throws FxApplicationException {
    assignPhrases(FxPhraseCategorySelection.CATEGORY_DEFAULT, position, assignmentOwner, nodeId, nodeMandator,
            phrases);
}

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

/**
 * {@inheritDoc}//from  ww  w  .  j av  a 2s .  co m
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void assignPhrases(int category, long position, long assignmentOwner, long nodeId, long nodeMandator,
        FxPhrase[] phrases) throws FxApplicationException {
    if (phrases == null || phrases.length == 0)
        return;
    checkMandatorAccess(assignmentOwner, FxContext.getUserTicket());
    Connection con = null;
    PreparedStatement ps = null;
    try {
        // Obtain a database connection
        con = Database.getDbConnection();
        //check categories
        ps = con.prepareStatement("SELECT ID FROM " + TBL_PHRASE_TREE + " WHERE ID=? AND MANDATOR=? AND CAT=?");
        ps.setLong(1, nodeId);
        ps.setLong(2, nodeMandator);
        ps.setInt(3, category);
        ResultSet rs = ps.executeQuery();
        if (rs == null || !(rs.next()))
            throw new FxNotFoundException("ex.phrases.node.notFound.id", nodeId, nodeMandator);
        ps.close();
        long startPhraseId = -1, startPhraseMandator = -1;
        ps = con.prepareStatement("SELECT PHRASEID,PMANDATOR FROM " + TBL_PHRASE_MAP
                + " WHERE MANDATOR=? AND NODEID=? AND NODEMANDATOR=? AND POS>=? AND CAT=? AND DIRECT=TRUE ORDER BY POS ASC");
        ps.setLong(1, assignmentOwner);
        ps.setLong(2, nodeId);
        ps.setLong(3, nodeMandator);
        ps.setLong(4, position);
        ps.setInt(5, category);
        rs = ps.executeQuery();
        while (rs != null && rs.next()) {
            long pid = rs.getLong(1);
            long mid = rs.getLong(2);
            if (!phrasesContains(phrases, pid, mid)) {
                startPhraseId = pid;
                startPhraseMandator = mid;
                break;
            }
        }
        ps.close();
        boolean useMaxPos = startPhraseId == -1 || startPhraseMandator == -1;
        //remove all contained phrases
        ps = con.prepareStatement("DELETE FROM " + TBL_PHRASE_MAP
                + " WHERE MANDATOR=? AND NODEID=? AND NODEMANDATOR=? AND PHRASEID=? AND PMANDATOR=? AND CAT=? AND DIRECT=TRUE");
        ps.setLong(1, assignmentOwner);
        ps.setLong(2, nodeId);
        ps.setLong(3, nodeMandator);
        ps.setInt(6, category);
        for (FxPhrase rm : phrases) {
            ps.setLong(4, rm.getId());
            ps.setLong(5, rm.getMandator());
            ps.addBatch();
        }
        ps.executeBatch();
        ps.close();
        //close gaps and reposition
        updatePhrasePosition(con, category, assignmentOwner, nodeId, nodeMandator, -1, -1, 0, true);
        int insertPos = -1;
        if (!useMaxPos) {
            ps = con.prepareStatement("SELECT POS FROM " + TBL_PHRASE_MAP
                    + " WHERE MANDATOR=? AND NODEID=? AND NODEMANDATOR=? AND PHRASEID=? AND PMANDATOR=? AND CAT=? AND DIRECT=?");
            ps.setLong(1, assignmentOwner);
            ps.setLong(2, nodeId);
            ps.setLong(3, nodeMandator);
            ps.setLong(4, startPhraseId);
            ps.setLong(5, startPhraseMandator);
            ps.setInt(6, category);
            ps.setBoolean(7, true);
            rs = ps.executeQuery();
            if (rs != null && rs.next())
                insertPos = rs.getInt(1);
            ps.close();
        }
        if (insertPos == -1)
            useMaxPos = true;
        if (!useMaxPos) {
            //make room for the phrases to insert
            ps = con.prepareStatement("UPDATE " + TBL_PHRASE_MAP + " SET POS=POS+" + (phrases.length)
                    + " WHERE MANDATOR=? AND NODEID=? AND NODEMANDATOR=? AND POS>? AND CAT=? AND DIRECT=?");
            ps.setLong(1, assignmentOwner);
            ps.setLong(2, nodeId);
            ps.setLong(3, nodeMandator);
            ps.setLong(4, insertPos);
            ps.setInt(5, category);
            ps.setBoolean(6, true);
            ps.executeUpdate();
            ps.close();
        } else {
            ps = con.prepareStatement("SELECT MAX(POS) FROM " + TBL_PHRASE_MAP
                    + " WHERE MANDATOR=? AND NODEID=? AND NODEMANDATOR=? AND CAT=? AND DIRECT=?");
            ps.setLong(1, assignmentOwner);
            ps.setLong(2, nodeId);
            ps.setLong(3, nodeMandator);
            ps.setInt(4, category);
            ps.setBoolean(5, true);
            rs = ps.executeQuery();
            if (rs != null && rs.next())
                insertPos = rs.getInt(1);
            else
                insertPos = 1; //fallback: first entry
            ps.close();
        }
        ps = con.prepareStatement("INSERT INTO " + TBL_PHRASE_MAP
                + "(MANDATOR,CAT,NODEID,NODEMANDATOR,PHRASEID,PMANDATOR,POS,DIRECT)VALUES(?,?,?,?,?,?,?,?)");
        ps.setLong(1, assignmentOwner);
        ps.setInt(2, category);
        ps.setLong(3, nodeId);
        ps.setLong(4, nodeMandator);
        ps.setBoolean(8, true);
        for (FxPhrase phrase : phrases) {
            ps.setLong(5, phrase.getId());
            ps.setLong(6, phrase.getMandator());
            ps.setLong(7, ++insertPos);
            ps.addBatch();
        }
        ps.executeBatch();
        if (phrases.length > 10)
            rebuildPhraseChildMapping(con, assignmentOwner, category, -1, -1);
        else {
            for (FxPhrase phrase : phrases) {
                rebuildPhraseChildMapping(con, assignmentOwner, category, phrase.getId(), phrase.getMandator());
            }
        }
    } catch (SQLException exc) {
        EJBUtils.rollback(ctx);
        throw new FxDbException(LOG, exc, "ex.db.sqlError", exc.getMessage()).asRuntimeException();
    } finally {
        Database.closeObjects(PhraseEngineBean.class, con, ps);
    }
}

From source file:fr.ortolang.diffusion.core.CoreServiceBean.java

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void deleteElements(String wskey, List<String> sources, boolean force) throws InvalidPathException,
        CoreServiceException, PathNotFoundException, AccessDeniedException, KeyNotFoundException,
        WorkspaceReadOnlyException, CollectionNotEmptyException, RegistryServiceException {
    if (!sources.isEmpty()) {
        try {/*from ww w .  j  a v a2  s . com*/
            String ppath = PathBuilder.fromPath(sources.get(0)).parent().build();
            for (String source : sources) {
                String sppath = PathBuilder.fromPath(source).parent().build();
                if (!ppath.equals(sppath)) {
                    throw new InvalidPathException("unable to delete elements from different collections");
                }
            }
            String parentKey = resolveWorkspacePath(wskey, "head", ppath);
            OrtolangObjectIdentifier identifier = registry.lookup(parentKey);
            checkObjectType(identifier, Collection.OBJECT_TYPE);
            Collection collection = readCollection(parentKey);

            for (String source : sources) {
                CollectionElement collectionElement = collection
                        .findElementByName(PathBuilder.fromPath(source).part());
                switch (collectionElement.getType()) {
                case DataObject.OBJECT_TYPE:
                    deleteDataObject(wskey, source);
                    break;
                case Collection.OBJECT_TYPE:
                    deleteCollection(wskey, source, force);
                }
            }
        } catch (InvalidPathException | CoreServiceException | PathNotFoundException | AccessDeniedException
                | KeyNotFoundException | WorkspaceReadOnlyException | RegistryServiceException e) {
            ctx.setRollbackOnly();
            LOGGER.log(Level.SEVERE, "unexpected error while moving workspace elements", e);
            throw e;
        } catch (CollectionNotEmptyException e) {
            ctx.setRollbackOnly();
            throw e;
        }
    }
}

From source file:com.stratelia.webactiv.kmelia.control.ejb.KmeliaBmEJB.java

/**
 * HEAD Delete a publication If this publication is in the basket or in the DZ, it's deleted from
 * the database Else it only send to the basket.
 *
 * @param pubPK the id of the publication to delete
 * @see com.stratelia.webactiv.kmelia.model.TopicDetail
 *//* w w  w . j av  a  2  s .c o m*/
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void deletePublication(PublicationPK pubPK) {
    // if the publication is in the basket or in the DZ
    // this publication is deleted from the database
    SilverTrace.info("kmelia", "KmeliaBmEJB.deletePublication()", "root.MSG_GEN_ENTER_METHOD");

    try {
        // remove form content
        removeXMLContentOfPublication(pubPK);
        // delete all reading controls associated to this publication
        deleteAllReadingControlsByPublication(pubPK);
        // delete all links
        publicationBm.removeAllFather(pubPK);
        // delete the publication
        publicationBm.removePublication(pubPK);
        // delete reference to contentManager
        deleteSilverContent(pubPK);

        removeExternalElementsOfPublications(pubPK);

    } catch (Exception e) {
        throw new KmeliaRuntimeException("KmeliaBmEJB.deletePublication()", ERROR,
                "kmelia.EX_IMPOSSIBLE_DE_SUPPRIMER_LA_PUBLICATION", e);
    }

    SilverTrace.info("kmelia", "KmeliaBmEJB.deletePublication()", "root.MSG_GEN_EXIT_METHOD");
}

From source file:fr.ortolang.diffusion.core.CoreServiceBean.java

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Group addMember(String wskey, String member) throws CoreServiceException, AccessDeniedException,
        KeyNotFoundException, MembershipServiceException, NotificationServiceException {
    String caller = membership.getProfileKeyForConnectedIdentifier();
    Workspace workspace = readWorkspace(wskey);
    Group group = membership.addMemberInGroup(workspace.getMembers(), member);
    ArgumentsBuilder argsBuilder = new ArgumentsBuilder().addArgument("ws-alias", workspace.getAlias())
            .addArgument("member", member);
    notification.throwEvent(wskey, caller, Workspace.OBJECT_TYPE, OrtolangEvent.buildEventType(
            CoreService.SERVICE_NAME, Workspace.OBJECT_TYPE, "notify-added-member"), argsBuilder.build());
    return group;
}

From source file:fr.ortolang.diffusion.core.CoreServiceBean.java

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Collection moveCollection(String wskey, String source, String destination)
        throws CoreServiceException, KeyNotFoundException, InvalidPathException, AccessDeniedException,
        PathNotFoundException, PathAlreadyExistsException, WorkspaceReadOnlyException {
    LOGGER.log(Level.FINE, "moving collection into workspace [" + wskey + "] from path [" + source
            + "] to path [" + destination + "]");
    try {//  w  w  w .ja  v  a  2 s .c  o  m
        PathBuilder spath = PathBuilder.fromPath(source);
        if (spath.isRoot()) {
            throw new InvalidPathException("unable to move the root collection");
        }
        PathBuilder sppath = spath.clone().parent();

        PathBuilder dpath = PathBuilder.fromPath(destination);
        if (dpath.isRoot()) {
            throw new InvalidPathException("unable to move to the root collection");
        }
        PathBuilder dppath = dpath.clone().parent();

        if (dpath.equals(spath)) {
            throw new InvalidPathException("unable to move into the same path");
        }
        if (spath.isParent(dpath)) {
            throw new InvalidPathException("unable to move into a children of this path");
        }

        String caller = membership.getProfileKeyForConnectedIdentifier();
        List<String> subjects = membership.getConnectedIdentifierSubjects();
        authorisation.checkAuthentified(subjects);
        LOGGER.log(Level.FINEST, "user [" + caller + "] is authentified");

        OrtolangObjectIdentifier wsidentifier = registry.lookup(wskey);
        checkObjectType(wsidentifier, Workspace.OBJECT_TYPE);
        LOGGER.log(Level.FINEST, "workspace with key [" + wskey + "] exists");

        Workspace ws = em.find(Workspace.class, wsidentifier.getId());
        if (ws == null) {
            throw new CoreServiceException(
                    "unable to load workspace with id [" + wsidentifier.getId() + "] from storage");
        }
        if (applyReadOnly(caller, subjects, ws)) {
            throw new WorkspaceReadOnlyException(
                    "unable to move collection in workspace with key [" + wskey + "] because it is read only");
        }
        ws.setKey(wskey);
        LOGGER.log(Level.FINEST, "workspace loaded");

        authorisation.checkPermission(ws.getHead(), subjects, "update");
        LOGGER.log(Level.FINEST,
                "user [" + caller + "] has 'update' permission on the head collection of this workspace");

        Collection sparent = loadCollectionAtPath(ws.getHead(), sppath, ws.getClock());
        CollectionElement selement = sparent.findElementByName(spath.part());
        if (selement == null) {
            throw new PathNotFoundException(spath.build());
        }
        LOGGER.log(Level.FINEST,
                "source collection element found for path " + spath.build() + ", key: " + selement.getKey());

        OrtolangObjectIdentifier scidentifier = registry.lookup(selement.getKey());
        checkObjectType(scidentifier, Collection.OBJECT_TYPE);
        Collection scollection = em.find(Collection.class, scidentifier.getId());
        if (scollection == null) {
            throw new CoreServiceException(
                    "unable to load source collection with id [" + scidentifier.getId() + "] from storage");
        }
        scollection.setKey(selement.getKey());
        LOGGER.log(Level.FINEST, "source collection exists and loaded from storage");

        Collection dparent = loadCollectionAtPath(ws.getHead(), dppath, ws.getClock());
        if (dparent.containsElementName(dpath.part())) {
            throw new PathAlreadyExistsException(dpath.build());
        }

        sparent.removeElement(selement);
        em.merge(sparent);
        registry.update(sparent.getKey());
        LOGGER.log(Level.FINEST, "parent [" + sparent.getKey() + "] has been updated");

        LOGGER.log(Level.FINEST, "destination element does not exists, ok for creating it");
        if (!dpath.part().equals(spath.part())) {
            if (scollection.getClock() < ws.getClock()) {
                scollection = cloneCollection(ws.getHead(), scollection, ws.getClock());
            }
            scollection.setName(dpath.part());
            em.merge(scollection);
            registry.update(scollection.getKey());
        }
        dparent.addElement(new CollectionElement(Collection.OBJECT_TYPE, scollection.getName(),
                System.currentTimeMillis(), 0, Collection.MIME_TYPE, scollection.getKey()));
        em.merge(dparent);
        registry.update(dparent.getKey());
        LOGGER.log(Level.FINEST, "collection [" + scollection.getKey() + "] added to destination parent ["
                + dparent.getKey() + "]");

        ws.setChanged(true);
        em.merge(ws);
        registry.update(ws.getKey());
        LOGGER.log(Level.FINEST, "workspace set changed");

        ArgumentsBuilder argsBuilder = new ArgumentsBuilder(5).addArgument("ws-alias", ws.getAlias())
                .addArgument("key", scollection.getKey()).addArgument("okey", selement.getKey())
                .addArgument("src-path", spath.build()).addArgument("dest-path", dpath.build());
        notification.throwEvent(wskey, caller, Workspace.OBJECT_TYPE,
                OrtolangEvent.buildEventType(CoreService.SERVICE_NAME, Collection.OBJECT_TYPE, "move"),
                argsBuilder.build());

        return scollection;
    } catch (KeyLockedException | NotificationServiceException | RegistryServiceException
            | MembershipServiceException | AuthorisationServiceException | CloneException e) {
        ctx.setRollbackOnly();
        LOGGER.log(Level.SEVERE, "unexpected error while moving collection", e);
        throw new CoreServiceException("unable to move collection into workspace [" + wskey + "] from path ["
                + source + "] to path [" + destination + "]", e);
    }
}

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

@TransactionAttribute(TransactionAttributeType.REQUIRED)
@RolesAllowed("org.nightlabs.jfire.store.queryDeliveryNotes")
@Override/*from  w ww.j a v a2  s .  c  om*/
public List<DeliveryNoteID> getDeliveryNoteIDs(AnchorID vendorID, AnchorID customerID, AnchorID endCustomerID,
        long rangeBeginIdx, long rangeEndIdx) {
    PersistenceManager pm = createPersistenceManager();
    try {
        return new ArrayList<DeliveryNoteID>(DeliveryNote.getDeliveryNoteIDs(pm, vendorID, customerID,
                endCustomerID, rangeBeginIdx, rangeEndIdx));
    } finally {
        pm.close();
    }
}

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

@TransactionAttribute(TransactionAttributeType.REQUIRED)
@RolesAllowed("org.nightlabs.jfire.store.deliver")
@Override/*from  w  ww.jav a2s.  c o m*/
public Delivery getDelivery(DeliveryID deliveryID, String[] fetchGroups, int maxFetchDepth) {
    PersistenceManager pm = createPersistenceManager();
    try {
        pm.getFetchPlan().setMaxFetchDepth(maxFetchDepth);
        if (fetchGroups != null)
            pm.getFetchPlan().setGroups(fetchGroups);
        Delivery delivery = (Delivery) pm.getObjectById(deliveryID);
        return pm.detachCopy(delivery);
    } finally {
        pm.close();
    }
}