Search in sources :

Example 16 with RetryingTransactionCallback

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

the class BaseDetailsBean method reject.

/**
 * Event handler called to handle the approve step of the simple workflow
 *
 * @param event The event that was triggered
 */
public void reject(ActionEvent event) {
    UIActionLink link = (UIActionLink) event.getComponent();
    Map<String, String> params = link.getParameterMap();
    String id = params.get("id");
    if (id == null || id.length() == 0) {
        throw new AlfrescoRuntimeException("reject called without an id");
    }
    final NodeRef docNodeRef = new NodeRef(Repository.getStoreRef(), id);
    try {
        RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(FacesContext.getCurrentInstance());
        RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {

            public Object execute() throws Throwable {
                // call the service to perform the reject
                WorkflowUtil.reject(docNodeRef, getNodeService(), getCopyService());
                return null;
            }
        };
        txnHelper.doInTransaction(callback);
        // if this was called via the node details dialog we need to reset the node
        if (getNode() != null) {
            getNode().reset();
        }
        // also make sure the UI will get refreshed
        UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
    } catch (Throwable e) {
        // rollback the transaction
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_WORKFLOW_REJECT), e.getMessage()), e);
        ReportedException.throwIfNecessary(e);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) UIActionLink(org.alfresco.web.ui.common.component.UIActionLink) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 17 with RetryingTransactionCallback

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

the class AdminNodeBrowseBean method submitSearch.

/**
 * Action to submit search
 *
 * @return next action
 */
public String submitSearch() {
    long start = System.currentTimeMillis();
    RetryingTransactionCallback<String> searchCallback = new RetryingTransactionCallback<String>() {

        public String execute() throws Throwable {
            if (queryLanguage.equals("noderef")) {
                // ensure node exists
                NodeRef nodeRef = new NodeRef(query);
                boolean exists = getNodeService().exists(nodeRef);
                if (!exists) {
                    throw new AlfrescoRuntimeException("Node " + nodeRef + " does not exist.");
                }
                setNodeRef(nodeRef);
                return "node";
            } else if (queryLanguage.equals("selectnodes")) {
                List<NodeRef> nodes = getSearchService().selectNodes(getNodeRef(), query, null, getNamespaceService(), false);
                searchResults = new SearchResults(nodes);
                return "search";
            }
            // perform search
            searchResults = new SearchResults(getSearchService().query(getNodeRef().getStoreRef(), queryLanguage, query));
            return "search";
        }
    };
    try {
        String result = getTransactionService().getRetryingTransactionHelper().doInTransaction(searchCallback, true);
        this.searchElapsedTime = System.currentTimeMillis() - start;
        return result;
    } catch (Throwable e) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage message = new FacesMessage();
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        message.setDetail("Search failed due to: " + e.toString());
        context.addMessage("searchForm:query", message);
        return "error";
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) FacesContext(javax.faces.context.FacesContext) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List) FacesMessage(javax.faces.application.FacesMessage)

Example 18 with RetryingTransactionCallback

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

the class ImportDialog method performImport.

/**
 * Performs the import operation using the current state of the bean
 *
 * @return The outcome
 */
public String performImport(final FacesContext context, String outcome) {
    if (logger.isDebugEnabled())
        logger.debug("Called import for file: " + this.file);
    if (this.file != null && this.file.exists()) {
        // check the file actually has contents
        if (this.file.length() > 0) {
            try {
                RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(context);
                RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {

                    public Object execute() throws Throwable {
                        // first of all we need to add the uploaded ACP/ZIP file to the repository
                        NodeRef acpNodeRef = addFileToRepository(context);
                        // build the action params map based on the bean's current state
                        Map<String, Serializable> params = new HashMap<String, Serializable>(2, 1.0f);
                        params.put(ImporterActionExecuter.PARAM_DESTINATION_FOLDER, browseBean.getActionSpace().getNodeRef());
                        params.put(ImporterActionExecuter.PARAM_ENCODING, encoding);
                        // build the action to execute
                        Action action = getActionService().createAction(ImporterActionExecuter.NAME, params);
                        if (action instanceof ImporterActionExecuter) {
                            ((ImporterActionExecuter) action).setHighByteZip(highByteZip);
                        }
                        action.setExecuteAsynchronously(runInBackground);
                        // execute the action on the ACP file
                        getActionService().executeAction(action, acpNodeRef);
                        if (logger.isDebugEnabled()) {
                            logger.debug("Executed import action with action params of " + params);
                        }
                        return null;
                    }
                };
                txnHelper.doInTransaction(callback);
                // reset the bean
                reset();
            } catch (Throwable e) {
                if (e instanceof DuplicateChildNodeNameException) {
                    String name = ((DuplicateChildNodeNameException) e).getName();
                    String err_mess = MessageFormat.format(I18NUtil.getMessage(ERR_DUPLICATE_NAME), name);
                    Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR), err_mess), e);
                } else {
                    Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR), e.toString()), e);
                }
                outcome = null;
                ReportedException.throwIfNecessary(e);
            }
        } else {
            Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_EMPTY_FILE));
            outcome = null;
        }
    } else {
        Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_NO_FILE));
        outcome = null;
    }
    return outcome;
}
Also used : DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) Serializable(java.io.Serializable) Action(org.alfresco.service.cmr.action.Action) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) HashMap(java.util.HashMap) ImporterActionExecuter(org.alfresco.repo.action.executer.ImporterActionExecuter) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)

Example 19 with RetryingTransactionCallback

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

the class CreateCategoryDialog method finishCreate.

public String finishCreate() {
    String outcome = DEFAULT_OUTCOME;
    try {
        FacesContext context = FacesContext.getCurrentInstance();
        RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(context);
        RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {

            public Object execute() throws Throwable {
                // create category using categoryservice
                NodeRef ref;
                if (getCategoryRef() == null || getCategoryRef().getId().equals("null")) {
                    ref = getCategoryService().createRootCategory(Repository.getStoreRef(), ContentModel.ASPECT_GEN_CLASSIFIABLE, getName());
                } else {
                    ref = getCategoryService().createCategory(getCategoryRef(), getName());
                }
                // apply the titled aspect - for description
                Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>(1, 1.0f);
                titledProps.put(ContentModel.PROP_DESCRIPTION, getDescription());
                getNodeService().addAspect(ref, ContentModel.ASPECT_TITLED, titledProps);
                return null;
            }
        };
        txnHelper.doInTransaction(callback);
    } catch (Throwable err) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
        outcome = null;
        ReportedException.throwIfNecessary(err);
    }
    return outcome;
}
Also used : FacesContext(javax.faces.context.FacesContext) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName)

Example 20 with RetryingTransactionCallback

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

the class InvokeCommand method execute.

// ///////////////////////////////////////////////////////////////////////////
public void execute(final FacesContext facesContext, final String expression, final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    ResponseWriter writer = null;
    try {
        final int indexOfDot = expression.indexOf('.');
        final String variableName = expression.substring(0, indexOfDot);
        final String methodName = expression.substring(indexOfDot + 1);
        if (logger.isDebugEnabled())
            logger.debug("Invoking method represented by " + expression + " on variable " + variableName + " with method " + methodName);
        Object bean = null;
        if (Application.inPortalServer()) {
            // retrieve the managed bean, this is really weak but if the
            // request comes from a portal server the bean we need to get
            // is in the session with a prefix chosen by the portal vendor,
            // to cover this scenario we have to go through the names of
            // all the objects in the session to find the bean we want.
            String beanNameSuffix = "?" + variableName;
            Enumeration<?> enumNames = request.getSession().getAttributeNames();
            while (enumNames.hasMoreElements()) {
                String name = (String) enumNames.nextElement();
                if (name.endsWith(beanNameSuffix)) {
                    bean = request.getSession().getAttribute(name);
                    if (logger.isDebugEnabled())
                        logger.debug("Found bean " + bean + " in the session");
                    break;
                }
            }
        }
        // if we don't have the bean yet try and get it via the variable resolver
        if (bean == null) {
            VariableResolver vr = facesContext.getApplication().getVariableResolver();
            bean = vr.resolveVariable(facesContext, variableName);
            if (logger.isDebugEnabled())
                logger.debug("Created bean " + bean + " via the variable resolver");
        }
        final Method method = bean.getClass().getMethod(methodName);
        final String responseMimetype = (method.isAnnotationPresent(ResponseMimetype.class) ? method.getAnnotation(ResponseMimetype.class).value() : MimetypeMap.MIMETYPE_XML);
        if (logger.isDebugEnabled())
            logger.debug("invoking method " + method + " with repsonse mimetype  " + responseMimetype);
        writer = this.setupResponseWriter(responseMimetype, response, facesContext);
        // setup the transaction
        RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(FacesContext.getCurrentInstance());
        final Object beanFinal = bean;
        RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {

            public Object execute() throws Throwable {
                // invoke the method
                try {
                    method.invoke(beanFinal);
                    return null;
                }// Let's prevent RuntimeExceptions being wrapped twice by unwrapping InvocationTargetExceptions
                 catch (InvocationTargetException e) {
                    if (e.getCause() != null) {
                        throw e.getCause();
                    }
                    throw e;
                }
            }
        };
        txnHelper.doInTransaction(callback);
    } catch (EvaluationException e) {
        Throwable err = e.getCause();
        if (err == null) {
            logger.error("Failed to execute method " + expression + ": " + e.getMessage(), e);
            throw e;
        } else {
            logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
            if (err instanceof RuntimeException) {
                throw (RuntimeException) err;
            } else {
                throw new AlfrescoRuntimeException("Failed to execute method " + expression + ": " + err.getMessage(), err);
            }
        }
    } catch (RuntimeException err) {
        logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
        throw err;
    } catch (Exception err) {
        logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
        throw new AlfrescoRuntimeException("Failed to execute method " + expression + ": " + err.getMessage(), err);
    }
    // force the output back to the client
    writer.close();
}
Also used : RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) Method(java.lang.reflect.Method) EvaluationException(javax.faces.el.EvaluationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) EvaluationException(javax.faces.el.EvaluationException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ResponseWriter(javax.faces.context.ResponseWriter) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) VariableResolver(javax.faces.el.VariableResolver)

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