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:fr.ortolang.diffusion.core.CoreServiceBean.java

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void deleteMetadataObject(String wskey, String path, String name, boolean recursive)
        throws CoreServiceException, InvalidPathException, WorkspaceReadOnlyException, PathNotFoundException {
    LOGGER.log(Level.FINE, "deleting metadataobject into workspace [" + wskey + "] for path [" + path
            + "] with name [" + name + "]");
    try {//from   w  w w.  ja  va 2  s .  c  o m
        PathBuilder npath = PathBuilder.fromPath(path);
        PathBuilder ppath = npath.clone().parent();

        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 delete meta data object in workspace with key ["
                    + wskey + "] because it is read only");
        }
        ws.setKey(wskey);
        LOGGER.log(Level.FINEST, "workspace loaded");

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

        String current = resolveWorkspacePath(wskey, Workspace.HEAD, npath.build());
        MetadataElement cmdelement = loadMetadataElement(name, current);
        if (cmdelement == null) {
            throw new CoreServiceException("a metadata object with name [" + name
                    + "] does not exists for at path [" + npath.build() + "]");
        }
        OrtolangObjectIdentifier cidentifier = registry.lookup(cmdelement.getKey());
        checkObjectType(cidentifier, MetadataObject.OBJECT_TYPE);
        MetadataObject cmeta = em.find(MetadataObject.class, cidentifier.getId());
        if (cmeta == null) {
            throw new CoreServiceException(
                    "unable to load metadata with id [" + cidentifier.getId() + "] from storage");
        }

        Collection parent = null;
        CollectionElement element = null;
        String tkey;
        if (npath.isRoot()) {
            tkey = ws.getHead();
        } else {
            parent = loadCollectionAtPath(ws.getHead(), ppath, ws.getClock());
            LOGGER.log(Level.FINEST, "parent collection loaded for path " + ppath.build());
            element = parent.findElementByName(npath.part());
            if (element == null) {
                throw new PathNotFoundException(npath.build());
            }
            LOGGER.log(Level.FINEST, "collection element found for name " + npath.part());
            tkey = element.getKey();
        }

        OrtolangObjectIdentifier tidentifier = registry.lookup(tkey);
        if (!tidentifier.getType().equals(Link.OBJECT_TYPE)
                && !tidentifier.getType().equals(Collection.OBJECT_TYPE)
                && !tidentifier.getType().equals(DataObject.OBJECT_TYPE)) {
            throw new CoreServiceException("metadata target can only be a Link, a DataObject or a Collection.");
        }

        MetadataElement mdelement;
        switch (tidentifier.getType()) {
        case Collection.OBJECT_TYPE:
            Collection collection = em.find(Collection.class, tidentifier.getId());
            if (collection == null) {
                throw new CoreServiceException(
                        "unable to load collection with id [" + tidentifier.getId() + "] from storage");
            }
            collection.setKey(tkey);
            if (collection.findMetadataByName(name) == null) {
                throw new CoreServiceException("a metadata object with name [" + name
                        + "] does not exists for collection at path [" + npath.build() + "]");
            }
            if (collection.getClock() < ws.getClock()) {
                Collection clone = cloneCollection(ws.getHead(), collection, ws.getClock());
                tkey = clone.getKey();
                if (parent != null) {
                    parent.removeElement(element);
                    parent.addElement(new CollectionElement(Collection.OBJECT_TYPE, clone.getName(),
                            System.currentTimeMillis(), 0, Collection.MIME_TYPE, clone.getKey()));
                }
                collection = clone;
            }
            mdelement = collection.findMetadataByName(name);
            if (recursive) {
                LOGGER.log(Level.FINE, "Removing children metadata");
                purgeChildrenMetadata(collection, wskey, path, name);
            }
            collection.removeMetadata(mdelement);
            em.merge(collection);
            break;
        case DataObject.OBJECT_TYPE:
            DataObject object = em.find(DataObject.class, tidentifier.getId());
            if (object == null) {
                throw new CoreServiceException(
                        "unable to load object with id [" + tidentifier.getId() + "] from storage");
            }
            object.setKey(tkey);
            if (object.findMetadataByName(name) == null) {
                throw new CoreServiceException("a metadata object with name [" + name
                        + "] does not exists for object at path [" + npath.build() + "]");
            }
            if (object.getClock() < ws.getClock()) {
                DataObject clone = cloneDataObject(ws.getHead(), object, ws.getClock());
                tkey = clone.getKey();
                if (parent == null) {
                    throw new CoreServiceException("An object should have a parent");
                }
                parent.removeElement(element);
                parent.addElement(new CollectionElement(DataObject.OBJECT_TYPE, clone.getName(),
                        System.currentTimeMillis(), clone.getSize(), clone.getMimeType(), clone.getKey()));
                object = clone;
            }
            mdelement = object.findMetadataByName(name);
            object.removeMetadata(mdelement);
            em.merge(object);
            break;
        case Link.OBJECT_TYPE:
            Link link = em.find(Link.class, tidentifier.getId());
            if (link == null) {
                throw new CoreServiceException(
                        "unable to load link with id [" + tidentifier.getId() + "] from storage");
            }
            link.setKey(tkey);
            if (link.findMetadataByName(name) == null) {
                throw new CoreServiceException("a metadata object with name [" + name
                        + "] does not exists for link at path [" + npath.build() + "]");
            }
            if (link.getClock() < ws.getClock()) {
                Link clone = cloneLink(ws.getHead(), link, ws.getClock());
                tkey = clone.getKey();
                if (parent == null) {
                    throw new CoreServiceException("A link should have a parent");
                }
                parent.removeElement(element);
                parent.addElement(new CollectionElement(Link.OBJECT_TYPE, clone.getName(),
                        System.currentTimeMillis(), 0, Link.MIME_TYPE, clone.getKey()));
                link = clone;
            }
            mdelement = link.findMetadataByName(name);
            link.removeMetadata(mdelement);
            em.merge(link);
            break;
        default:
            throw new CoreServiceException(
                    "Metadata target should be a Metadata Source not a " + tidentifier.getType());
        }

        if (mdelement == null) {
            throw new CoreServiceException("unable to find metadata object into workspace [" + wskey
                    + "] for path [" + npath.build() + "] and name [" + name + "]");
        }

        registry.delete(mdelement.getKey());
        indexing.remove(mdelement.getKey());

        indexing.index(tkey);

        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", mdelement.getKey()).addArgument("tkey", tkey)
                .addArgument("path", npath.build()).addArgument("name", name);
        notification.throwEvent(wskey, caller, Workspace.OBJECT_TYPE,
                OrtolangEvent.buildEventType(CoreService.SERVICE_NAME, MetadataObject.OBJECT_TYPE, "delete"),
                argsBuilder.build());
    } catch (KeyLockedException | KeyNotFoundException | RegistryServiceException | NotificationServiceException
            | AuthorisationServiceException | MembershipServiceException | CloneException
            | IndexingServiceException | OrtolangException e) {
        ctx.setRollbackOnly();
        LOGGER.log(Level.SEVERE, "unexpected error occurred during metadata deletion", e);
        throw new CoreServiceException(
                "unable to delete metadata into workspace [" + wskey + "] for path [" + path + "]", e);
    }
}

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

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public String createMetadataFormat(String name, String description, String schema, String form,
        boolean validationNeeded, boolean indexable) throws CoreServiceException {
    LOGGER.log(Level.FINE, "creating metadataformat with name [" + name + "]");
    try {//from  w  w w  .  ja  v a 2s  .  c  om
        MetadataFormat newmdf = new MetadataFormat();
        newmdf.setName(name);
        newmdf.setDescription(description);
        newmdf.setForm(form);
        newmdf.setValidationNeeded(validationNeeded);
        newmdf.setIndexable(indexable);
        if (schema != null && schema.length() > 0) {
            newmdf.setSize(binarystore.size(schema));
            newmdf.setMimeType(binarystore.type(schema));
            newmdf.setSchema(schema);
        } else {
            newmdf.setSize(0);
            newmdf.setMimeType("application/octet-stream");
            newmdf.setSchema("");
        }
        MetadataFormat mdf = getMetadataFormat(name);
        if (mdf != null) {
            if (mdf.equals(newmdf)) {
                LOGGER.log(Level.INFO, "Already imported metadata format: " + mdf.getId());
                return mdf.getId();
            }
            LOGGER.log(Level.FINE, "metadata format version already exists, creating new version");
            newmdf.setSerial(mdf.getSerial() + 1);
        }
        newmdf.setId(name + ":" + newmdf.getSerial());
        em.persist(newmdf);
        return newmdf.getId();
    } catch (BinaryStoreServiceException | DataNotFoundException e) {
        LOGGER.log(Level.SEVERE, "unexpected error during create metadata format", e);
        throw new CoreServiceException("unexpected error during create metadata format", e);
    }
}

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

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Set<String> systemListWorkspaceKeys(String wskey) throws CoreServiceException, KeyNotFoundException {
    LOGGER.log(Level.FINE, "#SYSTEM# listing workspace keys [" + wskey + "]");
    Set<String> keys = new HashSet<String>();
    try {/*  w w  w  .  j av a 2  s  .  c  om*/
        OrtolangObjectIdentifier identifier = registry.lookup(wskey);
        checkObjectType(identifier, Workspace.OBJECT_TYPE);
        Workspace workspace = em.find(Workspace.class, identifier.getId());
        if (workspace == null) {
            throw new CoreServiceException(
                    "unable to load workspace with id [" + identifier.getId() + "] from storage");
        }
        keys = systemListCollectionKeys(workspace.getHead(), keys);
        for (SnapshotElement snapshot : workspace.getSnapshots()) {
            keys = systemListCollectionKeys(snapshot.getKey(), keys);
        }

        return keys;
    } catch (RegistryServiceException e) {
        LOGGER.log(Level.SEVERE, "unexpected error occurred while listing workspace keys", e);
        throw new CoreServiceException("unable to list keys for workspace with key [" + wskey + "]", e);
    }
}

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

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void systemSetWorkspaceReadOnly(String wskey, boolean readonly)
        throws CoreServiceException, KeyNotFoundException, NotificationServiceException {
    LOGGER.log(Level.FINE, "#SYSTEM# setting workspace [" + wskey + "] read only to [" + readonly + "]");
    try {/*from  ww  w .ja v a  2s .  c om*/
        String caller = membership.getProfileKeyForConnectedIdentifier();
        OrtolangObjectIdentifier identifier = registry.lookup(wskey);
        checkObjectType(identifier, Workspace.OBJECT_TYPE);

        Workspace workspace = em.find(Workspace.class, identifier.getId());
        if (workspace == null) {
            throw new CoreServiceException(
                    "unable to load workspace with id [" + identifier.getId() + "] from storage");
        }
        workspace.setReadOnly(readonly);
        em.merge(workspace);

        registry.update(wskey);

        ArgumentsBuilder argsBuilder = new ArgumentsBuilder(1).addArgument("ws-alias", workspace.getAlias());
        notification.throwEvent(wskey, caller, Workspace.OBJECT_TYPE, OrtolangEvent
                .buildEventType(CoreService.SERVICE_NAME, Workspace.OBJECT_TYPE, readonly ? "lock" : "unlock"),
                argsBuilder.build());
    } catch (KeyLockedException | RegistryServiceException e) {
        ctx.setRollbackOnly();
        LOGGER.log(Level.SEVERE,
                "unexpected error occurred while setting workspace read only mode to [" + readonly + "]", e);
        throw new CoreServiceException(
                "unable to set workspace with key [" + wskey + "] read only mode to  [" + readonly + "]", e);
    }
}

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

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void systemUpdateWorkspace(String wskey, String alias, boolean changed, String head, String members,
        String privileged, boolean readOnly, String type)
        throws CoreServiceException, KeyNotFoundException, NotificationServiceException {
    LOGGER.log(Level.FINE, "#SYSTEM# updating workspace [" + wskey + "]");
    try {//from   www  .  j  a  v a  2 s . c  o m
        String caller = membership.getProfileKeyForConnectedIdentifier();
        OrtolangObjectIdentifier identifier = registry.lookup(wskey);
        checkObjectType(identifier, Workspace.OBJECT_TYPE);

        Workspace workspace = em.find(Workspace.class, identifier.getId());
        if (workspace == null) {
            throw new CoreServiceException(
                    "unable to load workspace with id [" + identifier.getId() + "] from storage");
        }
        workspace.setAlias(alias);
        workspace.setChanged(changed);
        workspace.setHead(head);
        workspace.setMembers(members);
        workspace.setPrivileged(privileged);
        workspace.setReadOnly(readOnly);
        workspace.setType(type);
        em.merge(workspace);

        registry.update(wskey);

        ArgumentsBuilder argsBuilder = new ArgumentsBuilder(1).addArgument("ws-alias", workspace.getAlias());
        notification.throwEvent(wskey, caller, Workspace.OBJECT_TYPE,
                OrtolangEvent.buildEventType(CoreService.SERVICE_NAME, Workspace.OBJECT_TYPE, "update"),
                argsBuilder.build());
    } catch (KeyLockedException | RegistryServiceException e) {
        ctx.setRollbackOnly();
        LOGGER.log(Level.SEVERE, "unexpected error occurred while setting workspace alias to [" + alias + "]",
                e);
        throw new CoreServiceException(
                "unable to set workspace with key [" + wskey + "] alias to  [" + alias + "]", e);
    }
}

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

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void systemCreateMetadata(String tkey, String name, String hash, String filename)
        throws KeyNotFoundException, CoreServiceException, MetadataFormatException, DataNotFoundException,
        BinaryStoreServiceException, KeyAlreadyExistsException, IdentifierAlreadyRegisteredException,
        RegistryServiceException, AuthorisationServiceException, IndexingServiceException {
    LOGGER.log(Level.FINE, "#SYSTEM# create metadata for key [" + tkey + "]");
    if (!name.startsWith("system-")) {
        throw new CoreServiceException("only system metadata can be added this way.");
    }/*from  ww  w .j a v a  2s.  c  om*/
    OrtolangObjectIdentifier identifier = registry.lookup(tkey);
    if (!identifier.getService().equals(SERVICE_NAME)) {
        throw new CoreServiceException("metadata target can only be a Link, a DataObject or a Collection.");
    }
    OrtolangObject object;
    switch (identifier.getType()) {
    case DataObject.OBJECT_TYPE:
        object = em.find(DataObject.class, identifier.getId());
        break;
    case Link.OBJECT_TYPE:
        object = em.find(Link.class, identifier.getId());
        break;
    case Collection.OBJECT_TYPE:
        object = em.find(Collection.class, identifier.getId());
        break;
    default:
        throw new CoreServiceException("metadata target can only be a Link, a DataObject or a Collection.");
    }
    MetadataElement metadataElement = ((MetadataSource) object).findMetadataByName(name);
    if (metadataElement != null) {
        ((MetadataSource) object).removeMetadata(metadataElement);
    }
    MetadataObject meta = new MetadataObject();
    meta.setId(UUID.randomUUID().toString());
    meta.setName(name);

    MetadataFormat format = getMetadataFormat(name);
    if (format == null) {
        LOGGER.log(Level.SEVERE, "Unable to find a metadata format for name: " + name);
        throw new CoreServiceException("unknown metadata format for name: " + name);
    }

    if (hash != null && hash.length() > 0) {
        if (format.isValidationNeeded()) {
            validateMetadata(hash, format);
        }
        meta.setSize(binarystore.size(hash));
        if (filename != null) {
            meta.setContentType(binarystore.type(hash, filename));
        } else {
            meta.setContentType(binarystore.type(hash));
        }
        meta.setStream(hash);
    } else {
        meta.setSize(0);
        meta.setContentType("application/octet-stream");
        meta.setStream("");
    }

    meta.setFormat(format.getId());
    meta.setTarget(tkey);
    meta.setKey(UUID.randomUUID().toString());
    em.persist(meta);

    try {
        String wskey = registry.getProperty(tkey, WORKSPACE_REGISTRY_PROPERTY_KEY);
        Properties properties = new Properties();
        properties.put(WORKSPACE_REGISTRY_PROPERTY_KEY, wskey);
        registry.register(meta.getKey(), meta.getObjectIdentifier(), "system", properties);
    } catch (PropertyNotFoundException e) {
        registry.register(meta.getKey(), meta.getObjectIdentifier(), "system");
    }

    registry.refresh(tkey);
    indexing.index(tkey);
    authorisation.clonePolicy(meta.getKey(), tkey);

    ((MetadataSource) object).addMetadata(new MetadataElement(name, meta.getKey()));
    em.merge(object);
}

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

@TransactionAttribute(TransactionAttributeType.REQUIRED)
private Collection readCollectionAtPath(String root, PathBuilder path) throws PathNotFoundException,
        CoreServiceException, RegistryServiceException, KeyNotFoundException, InvalidPathException {
    LOGGER.log(Level.FINE, "Reading tree from root [" + root + "] to path [" + path.build() + "]");
    String[] parts = path.buildParts();
    Collection node;/*from w ww  .ja  va2  s.co  m*/
    PathBuilder current = PathBuilder.newInstance();

    OrtolangObjectIdentifier ridentifier = registry.lookup(root);
    checkObjectType(ridentifier, Collection.OBJECT_TYPE);
    node = em.find(Collection.class, ridentifier.getId());
    if (node == null) {
        throw new CoreServiceException(
                "unable to load root collection with id [" + ridentifier.getId() + "] from storage");
    }
    node.setKey(root);
    if (!node.isRoot()) {
        throw new CoreServiceException("root collection [" + root + "] is not flagged as a root collection");
    }

    for (int i = 0; i < parts.length; i++) {
        current.path(parts[i]);
        CollectionElement element = node.findElementByName(parts[i]);
        if (element == null) {
            throw new PathNotFoundException(path.build());
        }
        OrtolangObjectIdentifier cidentifier = registry.lookup(element.getKey());
        checkObjectType(cidentifier, Collection.OBJECT_TYPE);
        node = em.find(Collection.class, cidentifier.getId());
        if (node == null) {
            throw new CoreServiceException(
                    "unable to load collection with id [" + cidentifier.getId() + "] from storage");
        }
        node.setKey(element.getKey());
        if (node.isRoot()) {
            LOGGER.log(Level.SEVERE, "WRONG ROOT FLAG found for collection key [" + element.getKey()
                    + "] at path [" + current.build() + "] with root [" + root + "]");
            throw new CoreServiceException("Internal Problem : collection [" + parts[i]
                    + "] is a root collection but is not a root node");
        }
    }

    return node;
}

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

@TransactionAttribute(TransactionAttributeType.REQUIRED)
private Collection loadCollectionAtPath(String root, PathBuilder path, int clock) throws CoreServiceException,
        RegistryServiceException, KeyNotFoundException, InvalidPathException, PathNotFoundException {
    LOGGER.log(Level.FINE,// w w w .ja  va2s .  c o  m
            "Loading tree from root [" + root + "] to path [" + path.build() + "] with clock [" + clock + "]");
    try {
        String[] parts = path.buildParts();
        Collection parent;
        Collection leaf;
        PathBuilder current = PathBuilder.newInstance();

        OrtolangObjectIdentifier ridentifier = registry.lookup(root);
        checkObjectType(ridentifier, Collection.OBJECT_TYPE);
        leaf = em.find(Collection.class, ridentifier.getId());
        if (leaf == null) {
            throw new CoreServiceException(
                    "unable to load root collection with id [" + ridentifier.getId() + "] from storage");
        }
        leaf.setKey(root);
        if (!leaf.isRoot()) {
            throw new CoreServiceException(
                    "root collection [" + root + "] is not flagged as a root collection");
        }
        if (leaf.getClock() < clock) {
            LOGGER.log(Level.SEVERE, "WRONG CLOCK found for root collection key [ " + root + "]");
            throw new CoreServiceException("root collection [" + root + "] clock is not good");
        }

        for (int i = 0; i < parts.length; i++) {
            parent = leaf;
            current.path(parts[i]);
            CollectionElement element = parent.findElementByName(parts[i]);
            if (element == null) {
                throw new PathNotFoundException(path.build());
            }
            OrtolangObjectIdentifier cidentifier = registry.lookup(element.getKey());
            checkObjectType(cidentifier, Collection.OBJECT_TYPE);
            leaf = em.find(Collection.class, cidentifier.getId());
            if (leaf == null) {
                throw new CoreServiceException(
                        "unable to load collection with id [" + cidentifier.getId() + "] from storage");
            }
            leaf.setKey(element.getKey());
            if (leaf.isRoot()) {
                LOGGER.log(Level.SEVERE, "WRONG ROOT FLAG found for collection key [" + element.getKey()
                        + "] at path [" + current.build() + "] with root [" + root + "]");
                throw new CoreServiceException("Internal Problem : collection [" + parts[i]
                        + "] is a root collection but is not a root node");
            }
            if (leaf.getClock() < clock) {
                Collection clone = cloneCollection(root, leaf, clock);
                parent.removeElement(element);
                CollectionElement celement = new CollectionElement(Collection.OBJECT_TYPE, clone.getName(),
                        System.currentTimeMillis(), 0, Collection.MIME_TYPE, clone.getKey());
                parent.addElement(celement);
                registry.update(parent.getKey());
                leaf = clone;
            }
        }

        return leaf;
    } catch (KeyLockedException | CloneException e) {
        ctx.setRollbackOnly();
        throw new CoreServiceException("unexpected error during loading collection at path " + path.build(), e);
    }
}

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

@TransactionAttribute(TransactionAttributeType.REQUIRED)
private Collection cloneCollection(String root, Collection origin, int clock) throws CloneException {
    LOGGER.log(Level.FINE, "cloning collection for origin [" + origin.getKey() + "]");
    try {//from w w w  . ja  va 2s  .c om
        String key = UUID.randomUUID().toString();

        Collection clone = new Collection();
        clone.setId(UUID.randomUUID().toString());
        clone.setName(origin.getName());
        clone.setRoot(origin.isRoot());
        clone.setClock(clock);
        Set<CollectionElement> elements = new HashSet<CollectionElement>();
        elements.addAll(origin.getElements());
        clone.setElements(elements);
        Set<MetadataElement> metadatas = new HashSet<MetadataElement>();
        for (MetadataElement mde : origin.getMetadatas()) {
            MetadataObject mdclone = cloneMetadataObject(root, mde.getKey(), key);
            metadatas.add(new MetadataElement(mde.getName(), mdclone.getKey()));
        }
        clone.setMetadatas(metadatas);
        clone.setKey(key);
        em.persist(clone);

        registry.register(key, clone.getObjectIdentifier(), origin.getKey(), true);
        indexing.index(key);
        authorisation.clonePolicy(key, root);

        return clone;
    } catch (RegistryServiceException | KeyAlreadyExistsException | IdentifierAlreadyRegisteredException
            | KeyNotFoundException | AuthorisationServiceException | IndexingServiceException e) {
        throw new CloneException("unable to clone collection with origin key [" + origin + "]", e);
    }
}

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

@TransactionAttribute(TransactionAttributeType.REQUIRED)
private DataObject cloneDataObject(String root, DataObject origin, int clock) throws CloneException {
    LOGGER.log(Level.FINE, "cloning object for origin [" + origin.getKey() + "]");
    try {/*  www . j  a  v  a  2 s  . c  om*/
        String key = UUID.randomUUID().toString();

        DataObject clone = new DataObject();
        clone.setId(UUID.randomUUID().toString());
        clone.setName(origin.getName());
        clone.setSize(origin.getSize());
        clone.setMimeType(origin.getMimeType());
        clone.setStream(origin.getStream());
        clone.setClock(clock);
        Set<MetadataElement> metadatas = new HashSet<MetadataElement>();
        for (MetadataElement mde : origin.getMetadatas()) {
            MetadataObject mdclone = cloneMetadataObject(root, mde.getKey(), key);
            metadatas.add(new MetadataElement(mde.getName(), mdclone.getKey()));
        }
        clone.setMetadatas(metadatas);
        clone.setKey(key);
        em.persist(clone);

        registry.register(key, clone.getObjectIdentifier(), origin.getKey(), true);
        indexing.index(key);
        authorisation.clonePolicy(key, root);

        return clone;
    } catch (RegistryServiceException | IdentifierAlreadyRegisteredException | AuthorisationServiceException
            | KeyAlreadyExistsException | KeyNotFoundException | IndexingServiceException e) {
        throw new CloneException("unable to clone object with origin [" + origin + "]", e);
    }
}