You are here

The basics of working with sites

This task describes how to create an application to demonstrate the basics of working with sites.
This task assumes you know how to create an SDK project. It also assumes you know how to use SkyVault Share to create sites.
In this task you will create a new project that demonstrates the basics of working with sites.
  1. Create a new Android project called, for example, SiteBasics, in the com.alfresco.tutorials.sitebasics package. If you are not sure how to do this see the tutorial How to create an SDK application.

    You should now have a basic SDK project that runs.

  2. Modify the code for your MainActivity class as follows:

                
    package com.alfresco.tutorials.sitebasics;
    
    import java.util.List;
    
    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();
    
                        // 1. Get pending requests for current user
                        List<Site> requests = siteService.getPendingSites();
                        
                        for (Site request : requests){
                        	
                        	Log.d(TAG, "Pending join requests to: "+request.getShortName());
                        	
                        }
                        
                        // 2. Get favorite sites for current user
                        List<Site> faves = siteService.getFavoriteSites();
                       
                        for (Site fave : faves){
                        	
                        	Log.d(TAG, "Favorite site: "+fave.getIdentifier());
                        	
                        }
                                            
                        // 3. Get all sites (including private!)
                        List<Site> sites = siteService.getAllSites();
    
                        // print out some info about the sites
                        int i = 0;
                        for (Site site : sites)
                        {
                          Log.d(TAG, "****  site number "+i+"  ****");
                          Log.d(TAG, "site title: " + site.getTitle());
                          Log.d(TAG, "site identifier: " + site.getIdentifier());
                          Log.d(TAG, "site description: " + site.getDescription());
                          Log.d(TAG, "site visibility: " + site.getVisibility());
                          Log.d(TAG, "site isMember?: " + site.isMember());
                          Log.d(TAG, "site isPendingMember?: " + site.isPendingMember());
                          Log.d(TAG, "site isFavorite?: " + site.isFavorite());
                          Log.d(TAG, "----------------------------------------------------");         
                          i++;		
                        }
                        Log.d(TAG, ">>> There were "+i+" sites!");
                                                      
                    }
                    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");
    
            }
        }
    }                        
                
              

    You will see that this code is based on the HelloRepo code, but the main code has been deleted and site related code added instead - the basic framework is however the same.

  3. Modify the previous code to reflect your SkyVault installation. You will need to change the URL, username, and password to reflect the system you are testing against. Don't forget you will also need to add the correct permissions to the Android Manifest as described in previous tutorials.

You will now create a few sites to allow you to test your code.

  1. Using SkyVault share, create several sites. You should include the following to get the best from the test program:
    1. As admin create (at least) the following:

      1. A public site.
      2. A public but moderated site.
      3. A private site.
    2. Add at least one site to your favourites, but not all of them.
    3. Create another test user, and log in as this user.
    4. As the test user create the following (then logout):

      1. A public but moderated site.
    5. Log in as admin again. Request to join the site created by the test user.
  2. Run the application (if you are not sure how to do this see previous tutorials).
  3. Check the Logcat output and confirm that it is as expected. By way of example, the following is from a sample run (with unnecessary information removed for clarity), your output will be somewhat different depending on the sites you created:

              
    
    03-18 12:31:00.258: D/ConnectToRepo(638): Pending join requests to: tonys-moderated-site
    03-18 12:31:00.438: D/ConnectToRepo(638): Favorite site: swsdp
    03-18 12:31:00.438: D/ConnectToRepo(638): Favorite site: moderated-site
    03-18 12:31:00.528: D/ConnectToRepo(638): ****  site number 0  ****
    03-18 12:31:00.528: D/ConnectToRepo(638): site title: Moderated Site
    03-18 12:31:00.528: D/ConnectToRepo(638): site identifier: moderated-site
    03-18 12:31:00.528: D/ConnectToRepo(638): site description: A moderated site.
    03-18 12:31:00.528: D/ConnectToRepo(638): site visibility: MODERATED
    03-18 12:31:00.528: D/ConnectToRepo(638): site isMember?: true
    03-18 12:31:00.528: D/ConnectToRepo(638): site isPendingMember?: false
    03-18 12:31:00.528: D/ConnectToRepo(638): site isFavorite?: true
    03-18 12:31:00.539: D/ConnectToRepo(638): ----------------------------------------------------
    03-18 12:31:00.539: D/ConnectToRepo(638): ****  site number 1  ****
    03-18 12:31:00.539: D/ConnectToRepo(638): site title: Private Site
    03-18 12:31:00.539: D/ConnectToRepo(638): site identifier: private-site
    03-18 12:31:00.539: D/ConnectToRepo(638): site description: A private site.
    03-18 12:31:00.539: D/ConnectToRepo(638): site visibility: PRIVATE
    03-18 12:31:00.539: D/ConnectToRepo(638): site isMember?: true
    03-18 12:31:00.548: D/ConnectToRepo(638): site isPendingMember?: false
    03-18 12:31:00.548: D/ConnectToRepo(638): site isFavorite?: false
    03-18 12:31:00.548: D/ConnectToRepo(638): ----------------------------------------------------
    03-18 12:31:00.548: D/ConnectToRepo(638): ****  site number 2  ****
    03-18 12:31:00.548: D/ConnectToRepo(638): site title: Public Site
    03-18 12:31:00.548: D/ConnectToRepo(638): site identifier: public-site
    03-18 12:31:00.548: D/ConnectToRepo(638): site description: A public site.
    03-18 12:31:00.548: D/ConnectToRepo(638): site visibility: PUBLIC
    03-18 12:31:00.548: D/ConnectToRepo(638): site isMember?: true
    03-18 12:31:00.548: D/ConnectToRepo(638): site isPendingMember?: false
    03-18 12:31:00.548: D/ConnectToRepo(638): site isFavorite?: false
    03-18 12:31:00.558: D/ConnectToRepo(638): ----------------------------------------------------
    03-18 12:31:00.558: D/ConnectToRepo(638): ****  site number 3  ****
    03-18 12:31:00.558: D/ConnectToRepo(638): site title: Sample: Web Site Design Project
    03-18 12:31:00.558: D/ConnectToRepo(638): site identifier: swsdp
    03-18 12:31:00.558: D/ConnectToRepo(638): site description: This is a Sample SkyVault Team site.
    03-18 12:31:00.558: D/ConnectToRepo(638): site visibility: PUBLIC
    03-18 12:31:00.558: D/ConnectToRepo(638): site isMember?: true
    03-18 12:31:00.558: D/ConnectToRepo(638): site isPendingMember?: false
    03-18 12:31:00.558: D/ConnectToRepo(638): site isFavorite?: true
    03-18 12:31:00.558: D/ConnectToRepo(638): ----------------------------------------------------
    03-18 12:31:00.558: D/ConnectToRepo(638): ****  site number 4  ****
    03-18 12:31:00.558: D/ConnectToRepo(638): site title: Tony's Moderated Site
    03-18 12:31:00.558: D/ConnectToRepo(638): site identifier: tonys-moderated-site
    03-18 12:31:00.568: D/ConnectToRepo(638): site description: Tony's moderated site
    03-18 12:31:00.568: D/ConnectToRepo(638): site visibility: MODERATED
    03-18 12:31:00.568: D/ConnectToRepo(638): site isMember?: false
    03-18 12:31:00.568: D/ConnectToRepo(638): site isPendingMember?: true
    03-18 12:31:00.568: D/ConnectToRepo(638): site isFavorite?: false
    03-18 12:31:00.568: D/ConnectToRepo(638): ----------------------------------------------------
    03-18 12:31:00.568: D/ConnectToRepo(638): >>> There were 5 sites!
    03-18 12:31:00.578: D/ConnectToRepo(638): doInBackground Complete
    03-18 12:31:00.578: D/ConnectToRepo(638): onPostExecute
    
              
            
You've see the basics of using the Site service and Site model APIs.