- Create a new test user, for example Harry Orwell (horwell), in addition to the test user you created in the previous tutorial.
- Log in to SkyVault Share as this new user.
-
Create two test sites. The first one should be a public site, the second should
be public moderated. For ease of reference, in this tutorial, it will be assumed
the sites are called Harry's Public Site (harrys-public-site) and Harry's
Moderated Site (harrys-moderated-site).
Note: Earlier versions of SkyVault may not allow you to create a site name containing an apostrophe. In this case the apostrophe can be left out without affecting the outcome in this tutorial.
You now have two sites to work with later in the tutorial. You will see how the API can be used. The first site you will join and leave. The second site you will request to join, then cancel your request.
- Create a new Android Application project called JoinTest, in the package com.alfresco.tutorials.jointest.
-
Replace your MainActivity class with the following code:
package com.alfresco.tutorials.jointest; import org.alfresco.mobile.android.api.exceptions.AlfrescoServiceException; import org.alfresco.mobile.android.api.exceptions.AlfrescoSessionException; import org.alfresco.mobile.android.api.model.Site; import org.alfresco.mobile.android.api.services.SiteService; import org.alfresco.mobile.android.api.session.RepositorySession; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Modify to suit your SkyVault server installation String url = "http://localhost:8080/alfresco"; String username = "admin"; String password = "admin"; new ConnectToRepo().execute(url, username, password); } class ConnectToRepo extends AsyncTask<String, Integer, String> { private static final String TAG = "ConnectToRepo"; @Override protected String doInBackground(String... params) { Log.d(TAG, "doInBackground"); Log.d(TAG, params[0] + ":" + params[1] + ":" + params[2]); String url = params[0]; String username = params[1]; String password = params[2]; try { // connect to on-premise repo RepositorySession session = RepositorySession.connect(url, username, password); if (session != null) { // Get site service SiteService siteService = session.getServiceRegistry() .getSiteService(); String siteName = "harrys-public-site"; try { // find the site with specified short name Site publicSite = siteService.getSite(siteName); if (publicSite != null) { // request to join PUBLIC site publicSite = siteService.joinSite(publicSite); // test you are a member Log.d(TAG, "Member (should be true): " + publicSite.isMember()); // but you are fickle so now leave publicSite = siteService.leaveSite(publicSite); // test you are a member again Log.d(TAG, "Member (should be false): " + publicSite.isMember()); } else { Log.d(TAG, "Site was not found!"); } } catch (AlfrescoServiceException e) { Log.e(TAG, "Error while working with site: " + siteName); Log.e(TAG, "Exception generated: " + e.toString()); Log.e(TAG, "With Error Code: " + e.getErrorCode()); } } else { Log.d(TAG, "No Session available!"); } } catch (AlfrescoSessionException e) { Log.e(TAG, "Failed to connect: " + e.toString()); } Log.d(TAG, "doInBackground Complete"); return "doInBackground Complete"; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Log.d(TAG, "onPostExecute"); Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show(); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); Log.d(TAG, "onProgressUpdate"); } } }
- Modify the URL variable to match the SkyVault installation you will test against. The username, and password variables need to reflect the administrator account for that SkyVault installation.
- Link the Mobile SDK for Android into the project (copy the JARs into the libs folder) as per previous tutorials.
-
Modify the AndroidManifest.xml file to add in permissions to
access the Internet and storage, as you have done in previous tutorials:
... <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <!-- Add only these two lines to assign required permissions --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application ...
- Run the application.
-
Check that the Logcat output is as expected. For the purposes of this tutorial,
example output is given below (with unnecessary trace messages removed):
03-18 14:25:31.223: D/ConnectToRepo(588): Member (should be true): true 03-18 14:25:31.283: D/ConnectToRepo(588): Member (should be false): false 03-18 14:25:31.283: D/ConnectToRepo(588): doInBackground Complete 03-18 14:25:31.283: D/ConnectToRepo(588): onPostExecute
- Feel free to experiment with the code. For example, try joining a site you are already a member of. This operation will be caught by an exception. Check that the returned error code is as you expected it to be.
-
Now you will test the moderated site. You will generate a join request to the
site, and then cancel this join request. Change the code for your ConnectToRepo
class to be as follows:
package com.alfresco.tutorials.jointest; import java.util.List; import org.alfresco.mobile.android.api.exceptions.AlfrescoServiceException; import org.alfresco.mobile.android.api.exceptions.AlfrescoSessionException; import org.alfresco.mobile.android.api.model.Site; import org.alfresco.mobile.android.api.services.SiteService; import org.alfresco.mobile.android.api.session.RepositorySession; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Modify to suit your SkyVault server installation String url = "http://localhost:8080/alfresco"; String username = "admin"; String password = "admin"; new ConnectToRepo().execute(url, username, password); } class ConnectToRepo extends AsyncTask<String, Integer, String> { private static final String TAG = "ConnectToRepo"; @Override protected String doInBackground(String... params) { Log.d(TAG, "doInBackground"); Log.d(TAG, params[0] + ":" + params[1] + ":" + params[2]); String url = params[0]; String username = params[1]; String password = params[2]; try { // connect to on-premise repo RepositorySession session = RepositorySession.connect(url, username, password); if (session != null) { // Get site service SiteService siteService = session.getServiceRegistry() .getSiteService(); String siteName = "harrys-public-site"; try { Log.d(TAG, "PART 1:"); // find the site with specified short name Site publicSite = siteService.getSite(siteName); if (publicSite != null) { // request to join PUBLIC site publicSite = siteService.joinSite(publicSite); // test you are a member Log.d(TAG, "Member (should be true): " + publicSite.isMember()); // but you are fickle so now leave publicSite = siteService.leaveSite(publicSite); // test you are a member again Log.d(TAG, "Member (should be false): " + publicSite.isMember()); } else { Log.d(TAG, "Site was not found!"); } Log.d(TAG, "PART 2:"); siteName = "harrys-moderated-site"; // find the site with specified short name Site moderatedSite = siteService.getSite(siteName); if (moderatedSite != null) { // Check you aren't already a member Log.d(TAG, "Member (should be false): " + moderatedSite.isMember()); // request to join PUBLIC MODERATED site, if already // a // member an exception will be thrown moderatedSite = siteService.joinSite(moderatedSite); // Now check for pending requests List<Site> pendingSites = siteService .getPendingSites(); if (!pendingSites.isEmpty()) { for (Site pendingSite : pendingSites) { Log.d(TAG, "pendingSite: " + pendingSite .getShortName()); } } else { Log.d(TAG, "There are no pending requests!"); } // Now we decide to cancel our join request moderatedSite = siteService .cancelRequestToJoinSite(moderatedSite); // Check for pending requests again pendingSites = null; pendingSites = siteService.getPendingSites(); if (!pendingSites.isEmpty()) { for (Site pendingSite : pendingSites) { Log.d(TAG, "pendingSite: " + pendingSite .getShortName()); } } else { Log.d(TAG, "There are no pending requests!"); } // Check still not a member moderatedSite = null; moderatedSite = siteService.getSite(siteName); Log.d(TAG, "Member (should be false): " + moderatedSite.isMember()); } else { Log.d(TAG, "Site was not found!"); } } catch (AlfrescoServiceException e) { Log.e(TAG, "Error while working with site: " + siteName); Log.e(TAG, "Exception generated: " + e.toString()); Log.e(TAG, "With Error Code: " + e.getErrorCode()); } } else { Log.d(TAG, "No Session available!"); } } catch (AlfrescoSessionException e) { Log.e(TAG, "Failed to connect: " + e.toString()); } Log.d(TAG, "doInBackground Complete"); return "doInBackground Complete"; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Log.d(TAG, "onPostExecute"); Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show(); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); Log.d(TAG, "onProgressUpdate"); } } }
In this case you locate harrys-moderated-site and then generate a join request. You then enumerate any pending requests, printing out the site name the request is for. You then cancel the join request, and again check pending requests to make sure the request has actually been cancelled. At a couple of points in the code membership is checked using the isMember() method, to ensure membership status is as expected.
-
Run the code and check that the Logcat output is as expected. For the purposes
of this tutorial some example output is given below (unnecessary debug output
has been removed):
03-18 15:02:01.943: D/ConnectToRepo(685): PART 1: 03-18 15:02:02.133: D/ConnectToRepo(685): Member (should be true): true 03-18 15:02:02.173: D/ConnectToRepo(685): Member (should be false): false 03-18 15:02:02.173: D/ConnectToRepo(685): PART 2: 03-18 15:02:02.203: D/ConnectToRepo(685): Member (should be false): false 03-18 15:02:02.752: D/ConnectToRepo(685): pendingSite: harrys-moderated-site 03-18 15:02:03.113: D/ConnectToRepo(685): There are no pending requests! 03-18 15:02:03.153: D/ConnectToRepo(685): Member (should be false): false 03-18 15:02:03.153: D/ConnectToRepo(685): doInBackground Complete 03-18 15:02:03.163: D/ConnectToRepo(685): onPostExecute
Note: You will also see the moderated site you created in the previous tutorial if you have not already deleted it.
You are here
Joining and leaving sites
This task describes how to join, leave and cancel a join request to a
site.
For this task you will need to create a sample project that you can modify. If you
are not sure how to do this please refer to previous tutorials.
In this task you will join and leave a site. Also you will request to join a
moderated site and then cancel this request.
At this stage you should have a good understanding of the most important site
service, site, and join request methods.
© 2017 TBS-LLC. All Rights Reserved. Follow @twitter