Blog Post

...

Alfresco basic content operations in Java

The post is dedicated to the task of file (content) operations (create, search, copy, aspect management) in Alfresco Java. As an example is taken some production case which demonstrates how to implement all these operations.

First of all the simplest way to search for a file or subfolder in some folder is to use org.alfresco.service.cmr.model.FileFolderService.searchSimple(folderNodeRef, fileOrFolderName).

File or folder can be renamed by this service as well org.alfresco.service.cmr.model.FileFolderService.rename(FileOrFolderNodeRef, newName).

File or folder can also be copied or moved to another folder by means of methods org.alfresco.service.cmr.model.FileFolderService.copy(FileOrFolderNodeRef, targetParentRef, newName) and org.alfresco.service.cmr.model.FileFolderService.move(FileOrFolderNodeRef, targetParentRef, newName) accordingly.

In my previous article there was a production task to create an Alfresco action that copies an uploaded content to the target document library locations. As a summary code that combines all described content operations an Alfresco action is presented. The name of this action is copycontent, it’s important to specify this id within spring context for current bean in order Alfresco folder rule could trigger this action upon upload events. Here is Spring excerpt:

<bean id="copycontent" class="alfresco.extension.de.CopyContentAction"
	parent="action-executer">
	<property name="nodeService" ref="nodeService" />
	<property name="searchService" ref="SearchService" />
	<property name="fileFolderService" ref="FileFolderService" />
	<property name="siteService" ref="siteService" />		
</bean>

And copy content action should extend org.alfresco.repo.action.executerActionExecuterAbstractBase:

package alfresco.extension.de;

public class CopyContentAction extends ActionExecuterAbstractBase {

    @Override
    protected void executeImpl(Action actionArg, NodeRef nr) {
        if ((nodeService.getType(nr).equals(ContentModel.TYPE_CONTENT)
                && isParentCentralizedFolder(nr)) {
            List<NodeRef> remoteFolders = getTargetFolderPaths(nr);
            for (NodeRef remoteFolder : remoteFolders) {
                String fileOrFolderName = nodeService.getProperty(nr, ContentModel.PROP_NAME).toString();
                if (!nodeService.hasAspect(remoteFolder, Constants.ASPECT_CENTRALIZED)) {
                    // if target remote folder is not centralized - skip. 
                    continue;
                }
                if (fileFolderService.searchSimple(remoteFolder, fileOrFolderName) != null) {
                    // if file or folder with current name exists - skip. 
                    continue;
                }
                try {
                    fileFolderService.copy(nr, remoteFolder, null);
                } catch (FileExistsException e) {
                    log.e();
                } catch (FileNotFoundException e) {
                    log.e();
                }
            }
        }
    }
    
    private boolean isParentCentralizedFolder(NodeRef nr) {
        NodeRef parent = nodeService.getPrimaryParent(nr).getParentRef();
        if (parent == null) {
            return false;
        }
        return nodeService.getType(parent).equals(ContentModel.TYPE_FOLDER) && nodeService.hasAspect(parent, Constants.ASPECT_CENTRALIZED);
    }
}

So this how Alfresco folder action implemented including basic content operations.

Comments (0)

Tags: alfresco


0 comments

Leave a Comment