use of org.alfresco.repo.transaction.RetryingTransactionHelper 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;
}
use of org.alfresco.repo.transaction.RetryingTransactionHelper in project acs-community-packaging by Alfresco.
the class WorkspaceClipboardItem method paste.
/**
* @see org.alfresco.web.bean.clipboard.ClipboardItem#paste(javax.faces.context.FacesContext, java.lang.String, int)
*/
public boolean paste(final FacesContext fc, String viewId, final int action) {
final ServiceRegistry serviceRegistry = getServiceRegistry();
final RetryingTransactionHelper retryingTransactionHelper = serviceRegistry.getTransactionService().getRetryingTransactionHelper();
if (super.canCopyToViewId(viewId) || WORKSPACE_PASTE_VIEW_ID.equals(viewId) || FORUMS_PASTE_VIEW_ID.equals(viewId) || FORUM_PASTE_VIEW_ID.equals(viewId)) {
NavigationBean navigator = (NavigationBean) FacesHelper.getManagedBean(fc, NavigationBean.BEAN_NAME);
final NodeRef destRef = new NodeRef(Repository.getStoreRef(), navigator.getCurrentNodeId());
final DictionaryService dd = serviceRegistry.getDictionaryService();
final NodeService nodeService = serviceRegistry.getNodeService();
final FileFolderService fileFolderService = serviceRegistry.getFileFolderService();
final CopyService copyService = serviceRegistry.getCopyService();
final MultilingualContentService multilingualContentService = serviceRegistry.getMultilingualContentService();
final boolean isPrimaryParent;
final ChildAssociationRef assocRef;
if (getParent() == null) {
assocRef = nodeService.getPrimaryParent(getNodeRef());
isPrimaryParent = true;
} else {
NodeRef parentNodeRef = getParent();
List<ChildAssociationRef> assocList = nodeService.getParentAssocs(getNodeRef());
ChildAssociationRef foundRef = null;
if (assocList != null) {
for (ChildAssociationRef assocListEntry : assocList) {
if (parentNodeRef.equals(assocListEntry.getParentRef())) {
foundRef = assocListEntry;
break;
}
}
}
assocRef = foundRef;
isPrimaryParent = parentNodeRef.equals(nodeService.getPrimaryParent(getNodeRef()).getParentRef());
}
// initial name to attempt the copy of the item with
String name = getName();
String translationPrefix = "";
if (action == UIClipboardShelfItem.ACTION_PASTE_LINK) {
// copy as link was specifically requested by the user
String linkTo = Application.getMessage(fc, MSG_LINK_TO);
name = linkTo + ' ' + name;
}
// Loop until we find a target name that doesn't exist
for (; ; ) {
try {
final String currentTranslationPrefix = translationPrefix;
final String currentName = name;
// attempt each copy/paste in its own transaction
retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
if (getMode() == ClipboardStatus.COPY) {
if (action == UIClipboardShelfItem.ACTION_PASTE_LINK) {
// LINK operation
if (logger.isDebugEnabled())
logger.debug("Attempting to link node ID: " + getNodeRef() + " into node: " + destRef.toString());
// create the node using the nodeService (can only use FileFolderService for content)
if (checkExists(currentName + LINK_NODE_EXTENSION, destRef) == false) {
Map<QName, Serializable> props = new HashMap<QName, Serializable>(2, 1.0f);
String newName = currentName + LINK_NODE_EXTENSION;
props.put(ContentModel.PROP_NAME, newName);
props.put(ContentModel.PROP_LINK_DESTINATION, getNodeRef());
if (dd.isSubClass(getType(), ContentModel.TYPE_CONTENT)) {
// create File Link node
ChildAssociationRef childRef = nodeService.createNode(destRef, ContentModel.ASSOC_CONTAINS, QName.createQName(assocRef.getQName().getNamespaceURI(), newName), ApplicationModel.TYPE_FILELINK, props);
// apply the titled aspect - title and description
Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>(2, 1.0f);
titledProps.put(ContentModel.PROP_TITLE, currentName);
titledProps.put(ContentModel.PROP_DESCRIPTION, currentName);
nodeService.addAspect(childRef.getChildRef(), ContentModel.ASPECT_TITLED, titledProps);
} else {
// create Folder link node
ChildAssociationRef childRef = nodeService.createNode(destRef, ContentModel.ASSOC_CONTAINS, assocRef.getQName(), ApplicationModel.TYPE_FOLDERLINK, props);
// apply the uifacets aspect - icon, title and description props
Map<QName, Serializable> uiFacetsProps = new HashMap<QName, Serializable>(4, 1.0f);
uiFacetsProps.put(ApplicationModel.PROP_ICON, "space-icon-link");
uiFacetsProps.put(ContentModel.PROP_TITLE, currentName);
uiFacetsProps.put(ContentModel.PROP_DESCRIPTION, currentName);
nodeService.addAspect(childRef.getChildRef(), ApplicationModel.ASPECT_UIFACETS, uiFacetsProps);
}
}
} else {
// COPY operation
if (logger.isDebugEnabled())
logger.debug("Attempting to copy node: " + getNodeRef() + " into node ID: " + destRef.toString());
// first check that we are not attempting to copy a duplicate into the same parent
if (destRef.equals(assocRef.getParentRef()) && currentName.equals(getName())) {
// manually change the name if this occurs
throw new FileExistsException(destRef, currentName);
}
if (dd.isSubClass(getType(), ContentModel.TYPE_CONTENT) || dd.isSubClass(getType(), ContentModel.TYPE_FOLDER)) {
// copy the file/folder
fileFolderService.copy(getNodeRef(), destRef, currentName);
} else if (dd.isSubClass(getType(), ContentModel.TYPE_MULTILINGUAL_CONTAINER)) {
// copy the mlContainer and its translations
multilingualContentService.copyTranslationContainer(getNodeRef(), destRef, currentTranslationPrefix);
} else {
// copy the node
if (checkExists(currentName, destRef) == false) {
copyService.copyAndRename(getNodeRef(), destRef, ContentModel.ASSOC_CONTAINS, assocRef.getQName(), true);
}
}
}
} else {
// MOVE operation
if (logger.isDebugEnabled())
logger.debug("Attempting to move node: " + getNodeRef() + " into node ID: " + destRef.toString());
if (dd.isSubClass(getType(), ContentModel.TYPE_CONTENT) || dd.isSubClass(getType(), ContentModel.TYPE_FOLDER)) {
// move the file/folder
fileFolderService.moveFrom(getNodeRef(), getParent(), destRef, currentName);
} else if (dd.isSubClass(getType(), ContentModel.TYPE_MULTILINGUAL_CONTAINER)) {
// copy the mlContainer and its translations
multilingualContentService.moveTranslationContainer(getNodeRef(), destRef);
} else {
if (isPrimaryParent) {
// move the node
nodeService.moveNode(getNodeRef(), destRef, ContentModel.ASSOC_CONTAINS, assocRef.getQName());
} else {
nodeService.removeChild(getParent(), getNodeRef());
nodeService.addChild(destRef, getNodeRef(), assocRef.getTypeQName(), assocRef.getQName());
}
}
}
return null;
}
});
// We got here without error, so no need to loop with a new name
break;
} catch (FileExistsException fileExistsErr) {
// If mode is COPY, have another go around the loop with a new name
if (getMode() == ClipboardStatus.COPY) {
String copyOf = Application.getMessage(fc, MSG_COPY_OF);
name = copyOf + ' ' + name;
translationPrefix = copyOf + ' ' + translationPrefix;
} else {
// we should not rename an item when it is being moved - so exit
throw fileExistsErr;
}
}
}
return true;
} else {
return false;
}
}
use of org.alfresco.repo.transaction.RetryingTransactionHelper 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();
}
use of org.alfresco.repo.transaction.RetryingTransactionHelper in project acs-community-packaging by Alfresco.
the class DocumentDetailsDialog method unlock.
/**
* Action Handler to unlock a locked document
*/
public void unlock(final ActionEvent event) {
final FacesContext fc = FacesContext.getCurrentInstance();
try {
RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(FacesContext.getCurrentInstance());
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {
public Object execute() throws Throwable {
getLockService().unlock(getNode().getNodeRef());
String msg = Application.getMessage(fc, MSG_SUCCESS_UNLOCK);
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
String formId = Utils.getParentForm(fc, event.getComponent()).getClientId(fc);
fc.addMessage(formId + ':' + getPropertiesPanelId(), facesMsg);
getNode().reset();
return null;
}
};
txnHelper.doInTransaction(callback);
} catch (Throwable e) {
Utils.addErrorMessage(MessageFormat.format(Application.getMessage(fc, Repository.ERROR_GENERIC), e.getMessage()), e);
ReportedException.throwIfNecessary(e);
}
}
use of org.alfresco.repo.transaction.RetryingTransactionHelper in project acs-community-packaging by Alfresco.
the class DocumentDetailsDialog method applyInlineEditable.
/**
* Applies the inlineeditable aspect to the current document
*/
public String applyInlineEditable() {
try {
RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(FacesContext.getCurrentInstance());
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {
public Object execute() throws Throwable {
// add the inlineeditable aspect to the node
Map<QName, Serializable> props = new HashMap<QName, Serializable>(1, 1.0f);
String contentType = null;
ContentData contentData = (ContentData) getDocument().getProperties().get(ContentModel.PROP_CONTENT);
if (contentData != null) {
contentType = contentData.getMimetype();
}
if (contentType != null) {
// set the property to true by default if the filetype is a known content type
if (MimetypeMap.MIMETYPE_HTML.equals(contentType) || MimetypeMap.MIMETYPE_TEXT_PLAIN.equals(contentType) || MimetypeMap.MIMETYPE_XML.equals(contentType) || MimetypeMap.MIMETYPE_TEXT_CSS.equals(contentType) || MimetypeMap.MIMETYPE_JAVASCRIPT.equals(contentType)) {
props.put(ApplicationModel.PROP_EDITINLINE, true);
}
}
getNodeService().addAspect(getDocument().getNodeRef(), ApplicationModel.ASPECT_INLINEEDITABLE, props);
return null;
}
};
txnHelper.doInTransaction(callback);
// reset the state of the current document
getDocument().reset();
} catch (Throwable e) {
Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_ASPECT_INLINEEDITABLE), e.getMessage()), e);
ReportedException.throwIfNecessary(e);
}
// force recreation of the details view - this means the properties sheet component will reinit
return OUTCOME_RETURN;
}
Aggregations