Search in sources :

Example 26 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project acs-community-packaging by Alfresco.

the class CreateDiscussionDialog method createTopic.

// ------------------------------------------------------------------------------
// Helper methods
/**
 * Creates a topic for the node with the given id
 *
 * @param id The id of the node to discuss
 */
protected void createTopic(final String id) {
    RetryingTransactionCallback<NodeRef> createTopicCallback = new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Throwable {
            NodeRef forumNodeRef = null;
            discussingNodeRef = new NodeRef(Repository.getStoreRef(), id);
            if (getNodeService().hasAspect(discussingNodeRef, ForumModel.ASPECT_DISCUSSABLE)) {
                throw new AlfrescoRuntimeException("createDiscussion called for an object that already has a discussion!");
            }
            // Add the discussable aspect
            getNodeService().addAspect(discussingNodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
            // The discussion aspect create the necessary child
            List<ChildAssociationRef> destChildren = getNodeService().getChildAssocs(discussingNodeRef, ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL);
            // Take the first one
            if (destChildren.size() == 0) {
                // Drop the aspect and recreate it.  This should not happen, but just in case ...
                getNodeService().removeAspect(discussingNodeRef, ForumModel.ASPECT_DISCUSSABLE);
                getNodeService().addAspect(discussingNodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
                // The discussion aspect create the necessary child
                destChildren = getNodeService().getChildAssocs(discussingNodeRef, ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL);
            }
            if (destChildren.size() == 0) {
                throw new AlfrescoRuntimeException("The discussable aspect behaviour is not creating a topic");
            } else {
                // We just take the first one
                ChildAssociationRef discussionAssoc = destChildren.get(0);
                forumNodeRef = discussionAssoc.getChildRef();
            }
            if (logger.isDebugEnabled())
                logger.debug("created forum for content: " + discussingNodeRef.toString());
            return forumNodeRef;
        }
    };
    FacesContext context = FacesContext.getCurrentInstance();
    NodeRef forumNodeRef = null;
    try {
        forumNodeRef = getTransactionService().getRetryingTransactionHelper().doInTransaction(createTopicCallback, false);
    } catch (InvalidNodeRefException refErr) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object[] { id }));
        throw new AbortProcessingException("Invalid node reference");
    } catch (Throwable e) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_GENERIC), e.getMessage()), e);
        ReportedException.throwIfNecessary(e);
    }
    // finally setup the context for the forum we just created
    if (forumNodeRef != null) {
        this.browseBean.clickSpace(forumNodeRef);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) FacesContext(javax.faces.context.FacesContext) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) AbortProcessingException(javax.faces.event.AbortProcessingException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 27 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project acs-community-packaging by Alfresco.

the class CheckinCheckoutDialog method updateFileOK.

/**
 * Action called upon completion of the Update File page
 */
public String updateFileOK(final FacesContext context, String outcome) {
    // NOTE: for update the document node _is_ the working document!
    final Node node = property.getDocument();
    if (node != null && this.getFileName() != null) {
        try {
            RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(FacesContext.getCurrentInstance());
            RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {

                public Object execute() throws Throwable {
                    if (logger.isDebugEnabled())
                        logger.debug("Trying to update content node Id: " + node.getId());
                    // get an updating writer that we can use to modify the content on the current node
                    ContentWriter writer = property.getContentService().getWriter(node.getNodeRef(), ContentModel.PROP_CONTENT, true);
                    // also update the mime type in case a different type of file is uploaded
                    String mimeType = Repository.getMimeTypeForFileName(context, property.getFileName());
                    writer.setMimetype(mimeType);
                    writer.putContent(property.getFile());
                    return null;
                }
            };
            txnHelper.doInTransaction(callback);
            // clear action context
            property.setDocument(null);
            resetState();
            outcome = AlfrescoNavigationHandler.CLOSE_DIALOG_OUTCOME;
        } catch (Throwable err) {
            Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_UPDATE) + err.getMessage(), err);
            ReportedException.throwIfNecessary(err);
        }
    } else {
        logger.warn("WARNING: updateFileOK called without a current Document!");
    }
    return outcome;
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) Node(org.alfresco.web.bean.repository.Node)

Example 28 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project acs-community-packaging by Alfresco.

the class DownloadContentServlet method doGet.

/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
protected void doGet(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
    if (logger.isDebugEnabled()) {
        String queryString = req.getQueryString();
        logger.debug("Authenticating (GET) request to URL: " + req.getRequestURI() + ((queryString != null && queryString.length() > 0) ? ("?" + queryString) : ""));
    }
    AuthenticationStatus status = servletAuthenticate(req, res);
    if (status == AuthenticationStatus.Failure) {
        return;
    }
    ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
    TransactionService transactionService = serviceRegistry.getTransactionService();
    RetryingTransactionCallback<Void> processCallback = new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            processDownloadRequest(req, res, true, true);
            return null;
        }
    };
    transactionService.getRetryingTransactionHelper().doInTransaction(processCallback, true);
}
Also used : TransactionService(org.alfresco.service.transaction.TransactionService) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) ServiceRegistry(org.alfresco.service.ServiceRegistry)

Example 29 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project acs-community-packaging by Alfresco.

the class DownloadContentServlet method doHead.

/* (non-Javadoc)
    * @see javax.servlet.http.HttpServlet#doHead(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
@Override
protected void doHead(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
    if (logger.isDebugEnabled()) {
        String queryString = req.getQueryString();
        logger.debug("Authenticating (HEAD) request to URL: " + req.getRequestURI() + ((queryString != null && queryString.length() > 0) ? ("?" + queryString) : ""));
    }
    AuthenticationStatus status = servletAuthenticate(req, res);
    if (status == AuthenticationStatus.Failure) {
        return;
    }
    ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
    TransactionService transactionService = serviceRegistry.getTransactionService();
    RetryingTransactionCallback<Void> processCallback = new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            processDownloadRequest(req, res, true, false);
            return null;
        }
    };
    transactionService.getRetryingTransactionHelper().doInTransaction(processCallback, true);
}
Also used : TransactionService(org.alfresco.service.transaction.TransactionService) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) ServiceRegistry(org.alfresco.service.ServiceRegistry)

Example 30 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project records-management by Alfresco.

the class UnfiledRecordFolderChildrenRelation method create.

@Override
@WebApiDescription(title = "Create one (or more) nodes as children of a unfiled record folder identified by 'unfiledRecordFolderId'")
public List<UnfiledRecordFolderChild> create(String unfiledRecordFolderId, final List<UnfiledRecordFolderChild> nodeInfos, Parameters parameters) {
    checkNotBlank("unfiledRecordFolderId", unfiledRecordFolderId);
    mandatory("nodeInfos", nodeInfos);
    mandatory("parameters", parameters);
    NodeRef parentNodeRef = apiUtils.lookupAndValidateNodeType(unfiledRecordFolderId, RecordsManagementModel.TYPE_UNFILED_RECORD_FOLDER);
    // Create the children
    RetryingTransactionCallback<List<NodeRef>> callback = new RetryingTransactionCallback<List<NodeRef>>() {

        public List<NodeRef> execute() {
            List<NodeRef> createdNodes = new LinkedList<>();
            for (UnfiledRecordFolderChild nodeInfo : nodeInfos) {
                NodeRef nodeParent;
                if (StringUtils.isNoneBlank(nodeInfo.getRelativePath())) {
                    nodeParent = apiUtils.lookupAndValidateRelativePath(parentNodeRef, nodeInfo.getRelativePath(), RecordsManagementModel.TYPE_UNFILED_RECORD_FOLDER);
                } else {
                    nodeParent = parentNodeRef;
                }
                NodeRef newNodeRef = apiUtils.createRMNode(nodeParent, nodeInfo, parameters);
                createdNodes.add(newNodeRef);
            }
            return createdNodes;
        }
    };
    List<NodeRef> createdNodes = transactionService.getRetryingTransactionHelper().doInTransaction(callback, false, true);
    // Get the nodes info
    List<UnfiledRecordFolderChild> result = new LinkedList<>();
    Map<String, UserInfo> mapUserInfo = new HashMap<>();
    for (NodeRef newNodeRef : createdNodes) {
        FileInfo info = fileFolderService.getFileInfo(newNodeRef);
        apiUtils.postActivity(info, parentNodeRef, ActivityType.FILE_ADDED);
        result.add(nodesModelFactory.createUnfiledRecordFolderChild(info, parameters, mapUserInfo, false));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) UserInfo(org.alfresco.rest.api.model.UserInfo) LinkedList(java.util.LinkedList) UnfiledRecordFolderChild(org.alfresco.rm.rest.api.model.UnfiledRecordFolderChild) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) AbstractList(java.util.AbstractList) LinkedList(java.util.LinkedList) List(java.util.List) WebApiDescription(org.alfresco.rest.framework.WebApiDescription)

Aggregations

RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)78 NodeRef (org.alfresco.service.cmr.repository.NodeRef)44 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)20 FileInfo (org.alfresco.service.cmr.model.FileInfo)20 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)16 HashMap (java.util.HashMap)15 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)15 FacesContext (javax.faces.context.FacesContext)12 QName (org.alfresco.service.namespace.QName)11 Serializable (java.io.Serializable)10 List (java.util.List)9 ArrayList (java.util.ArrayList)8 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)8 IOException (java.io.IOException)7 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)7 Test (org.junit.Test)7 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)7 AuthenticationUtil (org.alfresco.repo.security.authentication.AuthenticationUtil)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 AbstractList (java.util.AbstractList)5