Android SDK : Cookbook

Document created by resplin Employee on Jun 6, 2015
Version 1Show Document
  • View in full screen mode

Obsolete Pages{{Obsolete}}

The official documentation is at: http://docs.alfresco.com



AndroidCookbook
This page is the central page for examples for the Android SDK API. Feel free to add your examples to this. 


Session


Creation


Simple way to create an OnPremise Alfresco Session:



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



Create an OnPremise Alfresco Session + define the cache folder:



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


Log Server information:



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


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();




Override ServiceRegistry


Create an OnPremise Alfresco Session + define a custom ServiceRegistry :



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);

Attachments

    Outcomes