Skip to content
JM.PASCAL edited this page Feb 25, 2015 · 4 revisions

This section introduces the most commonly used parts of the Alfresco client API

##Connecting to Alfresco On Premise To do any work on Alfresco, you must first find your Alfresco server, and create a session with it.

Simple way to create an OnPremise Alfresco Session:

RepositorySession session = RepositorySession.connect("http://hostname:port/alfresco", "username", "password");

You can also also pass some settings during the creation. Below we define the cache folder associated where session can store some cache information.

HashMap<String, Serializable> settings = new HashMap<String, Serializable>(1);
settings.put(AlfrescoSession.CACHE_FOLDER, "Custom/Path/In/My/Device/" );
RepositorySession session = RepositorySession.connect("http://hostname:port/alfresco", "username", "password", settings);

####Retrieve Server information Now you have a session you can retrieve information from the repository.

RepositoryInfo repositoryInformation = session.getRepositoryInfo();
Log.d('RepositoryInfo', repositoryInformation.getName());
Log.d('RepositoryInfo', repositoryInformation.getDescription());
Log.d('RepositoryInfo', repositoryInformation.getEdition());
Log.d('RepositoryInfo', repositoryInformation.getVersion());
Log.d('RepositoryInfo', repositoryInformation.getMajorVersion());
Log.d('RepositoryInfo', repositoryInformation.getMinorVersion());
Log.d('RepositoryInfo', repositoryInformation.getMaintenanceVersion());
Log.d('RepositoryInfo', repositoryInformation.getBuildNumber());

##Services One of the most important part of the session object is the ServiceRegistry which contains all services available with the repository. Regarding Alfresco Server version the ServiceRegistry object might support different services.

####Retrieve all services

ServiceRegistry serviceRegistry= session.getServiceRegistry();

DocumentFolderService documentFolderService = serviceRegistry.getDocumentFolderService();
VersionService versionService= serviceRegistry.getVersionService();
SearchService searchService= serviceRegistry.getSearchService();
SiteService siteService= serviceRegistry.getSiteService();
ActivityStreamService activityStreamService = serviceRegistry.getActivityStreamService();
CommentService commentService = serviceRegistry.getCommentService();
PersonService personService = serviceRegistry.getPersonService();
TaggingService taggingService= serviceRegistry.getTaggingService();
RatingService  ratingService= serviceRegistry.getRatingService();
WorkflowService workflowService= serviceRegistry.getWorkflowService();

####Custom ServiceRegistry In some cases you will usefull to extend the ServiceRegistry to add your own custom services.

HashMap<String, Serializable> settings = new HashMap<String, Serializable>(1);
settings.put(AlfrescoSession.ONPREMISE_SERVICES_CLASSNAME, "custom.package.name.of.your.custom.service.registry.class.name" );
RepositorySession session = RepositorySession.connect("http://hostname:port/alfresco", "username", "password", settings);

##DocumentFolderService

####Create a folder

//Create Folder
Folder folder = docfolderservice.createFolder(parentFolder, "MyFolderName", null);

####Create a folder with properties
//Define properties
HashMap<String, Serializable> properties = new HashMap<String, Serializable>();
properties.put(ContentModel.PROP_TITLE, "MyFolderTitle");
properties.put(ContentModel.PROP_DESCRIPTION, "MyFolderDescription");

//Create Folder
Folder folder = docfolderservice.createFolder(parentFolder, "MyFolderName", properties);

####Create an empty document

//Create Document
Document doc = docfolderservice.createDocument(folder, "MyDocument.txt", null, null);

####Create a document with properties
//Define properties
HashMap<String, Serializable> properties = new HashMap<String, Serializable>();
properties.put(ContentModel.PROP_TITLE, "MyDocumentTitle");
properties.put(ContentModel.PROP_DESCRIPTION, "MyDocumentDescription");

//Define content
File myDocumentFile = new File('path/inside/my/device/to/the/file');
ContentFile contentFile = new ContentFileImpl(myDocumentFile);

//Create Document
Document doc = docfolderservice.createDocument(folder, "MyDocument.txt", properties, contentFile);

####Create a document with custom aspects

//Define properties
HashMap<String, Serializable> properties = new HashMap<String, Serializable>();
properties.put(ContentModel.PROP_TITLE, "MyDocumentTitle");
properties.put(ContentModel.PROP_DESCRIPTION, "MyDocumentDescription");

//Define aspects + properties associated to the aspect
List<String> aspects = new ArrayList<String>(1);
aspects.add("sc:productRelated");
properties.put("sc:product", "My Product Name");
properties.put("sc:version", "My Product Version");

//Define content
File myDocumentFile = new File('path/inside/my/device/to/the/file');
ContentFile contentFile = new ContentFileImpl(myDocumentFile);

//Create Document
Document doc = docfolderservice.createDocument(folder, "MyDocument.txt", properties, contentFile, aspects);

####Create an empty document with a specific type

//Create Document
Document doc = docfolderservice.createDocument(folder, "MyDocument.txt", properties, null, null, "sc:whitepaper");

####Retrieve a document by its identifier & custom properties

//Create Document
Document customDoc = docfolderservice.getNodeByIdentifier("YourIdentifierHere");
Boolean hasAspect = customDoc.hasAspect("sc:productRelated");
String prop1 = customDoc.getProperty("sc:product").getValue();
String prop2 = customDoc.getProperty("sc:version").getValue();

##WorkflowService

####Quick Start an Adhoc Process

WorkflowService workflowService = alfsession.getServiceRegistry().getWorkflowService();

// Process Definition
ProcessDefinition def = workflowService.getProcessDefinition(WorkflowModel.KEY_ADHOC_ACTIVITI);

// Assignee
Person user = alfsession.getServiceRegistry().getPersonService().getPerson(alfsession.getPersonIdentifier());
List<Person> users = new ArrayList<Person>();
users.add(user);

// Start Process : Prepare Variables
Map<String, Serializable> variables = new HashMap<String, Serializable>();
variables.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, DESCRIPTION);

// START THE PROCESS
Process adhocProcess = workflowService.startProcess(def, users, variables, null);

####Start an Adhoc Process with variables

WorkflowService workflowService = alfsession.getServiceRegistry().getWorkflowService();

// Start Process : Prepare Variables
Map<String, Serializable> variables = new HashMap<String, Serializable>();

// Process Definition
ProcessDefinition def = workflowService.getProcessDefinition(WorkflowModel.KEY_ADHOC_ACTIVITI);

// Assignee
Person user = alfsession.getServiceRegistry().getPersonService().getPerson(alfsession.getPersonIdentifier());
List<Person> users = new ArrayList<Person>();
users.add(user);

// Items - Attachments
Document doc = (Document) alfsession.getServiceRegistry().getDocumentFolderService().getChildByPath("My/Custom/Path/To/MyDoc.txt");
List<Document> docs = new ArrayList<Document>();
docs.add(doc);

// Due date
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR, 2000);
variables.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, DateUtils.format(calendar));

// Priority
variables.put(WorkflowModel.PROP_WORKFLOW_PRIORITY, WorkflowModel.PRIORITY_HIGH);

// Description
variables.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "Please Review !");

// Notification
variables.put(WorkflowModel.PROP_SEND_EMAIL_NOTIFICATIONS, "true");

// START THE PROCESS
Process adhocProcess = workflowService.startProcess(def, users, variables, docs);

####Start a Pooled Process with variables

WorkflowService workflowService = alfsession.getServiceRegistry().getWorkflowService();

// Start Process : Prepare Variables
Map<String, Serializable> variables = new HashMap<String, Serializable>();

// Process Definition
ProcessDefinition def = workflowService.getProcessDefinition(WorkflowModel.KEY_POOLED_REVIEW_ACTIVITI);

// Assignees
variables.put(WorkflowModel.ASSOC_GROUP_ASSIGNEE, "GROUP_workflow");
variables.put(WorkflowModel.PROP_REQUIRED_APPROVE_PERCENT, 50);

// Items - Attachments
Document doc = (Document) alfsession.getServiceRegistry().getDocumentFolderService().getChildByPath("My/Custom/Path/To/MyDoc.txt");
List<Document> docs = new ArrayList<Document>();
docs.add(doc);

// Description
variables.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "Please Review !");

// START THE PROCESS
Process adhocProcess = workflowService.startProcess(def, null, variables, docs);

####List Tasks with Filters

WorkflowService workflowService = alfsession.getServiceRegistry().getWorkflowService();

// Search unassigned task
ListingContext lc = new ListingContext();
ListingFilter lf = new ListingFilter();
lf.addFilter(WorkflowService.FILTER_KEY_STATUS, WorkflowService.FILTER_STATUS_ACTIVE);
lf.addFilter(WorkflowService.FILTER_KEY_ASSIGNEE, WorkflowService.FILTER_ASSIGNEE_UNASSIGNED);
lc.setFilter(lf);
PagingResult<Task> pagingTasks = workflowService.getTasks(lc);

####Complete Review Task

//Retrieve a task
List<Task> tasks = workflowService.getTasks();
Task taskInProgress = tasks.get(0);

// Prepare Variable to complete task
Map<String, Serializable> variables = new HashMap<String, Serializable>();
variables.put(WorkflowModel.PROP_COMMENT, ''This is a comment');
variables.put(WorkflowModel.PROP_REVIEW_OUTCOME, WorkflowModel.TRANSITION_APPROVE);

// Close Active Task
Task taskCompleted = workflowService.completeTask(taskInProgress, variables);
Clone this wiki locally