You are here

Retrieving and adding version labels to the node detail view

This task shows you how to retrieve versions of a document and then display the version label for each version in the node detail view.
You will retrieve the versions of a document and display the version label for each version.

Modifying the header file

You need to modify the header file DocumentTableViewController.h in order to add support for versions.
  1. Load DocumentTableViewController.h into Xcode. You need to include the SkyVault Version Service as this will be used to retrieve versions. Add the following import statement:

                  
    #import "AlfrescoVersionService.h"     
                  
                

    This includes the version service which you will be using to retrieve versions for the selected node.

  2. You need a property to store the retrieved SkyVaultVersionService instance in. Below the other properties in the file add the following:

    
    @property (strong, nonatomic) SkyVaultVersionService *versionService;
    
                

    This creates a property that can be used from other code that references the version service.

  3. You next need a property to store the retrieved versions in. The versions can be stored in an array. Below the other properties in the file add the following:

    
    @property (strong, nonatomic) NSArray *versions;
    
                

    This creates a property that can be used to reference an array of document versions.

  4. You will also be recording the version label of the latest document. This will be used in section 0, the summary section, of the node detail table view. This will be stored in a string property, so you need to declare that in the header too, after the property added in the previous step:

                  
    @property (strong, nonatomic) NSString *latestVersion;              
                  
                
  5. You will also be creating a method to fetch versions, so you could create the prototype for this method now. After the prototype for -(void)getTags, you can add the following:

                  
    - (void)getVersions;              
                  
                

    DocumentTableViewController.h will now contain the following code:

                  
    #import <UIKit/UIKit.h>
    
    #import "AlfrescoSession.h"
    #import "AlfrescoNode.h"
    #import "AlfrescoDocumentFolderService.h"
    #import "AlfrescoCommentService.h" 
    #import "AlfrescoTaggingService.h"
    #import "AlfrescoVersionService.h"
    
    @interface DocumentTableViewController : UITableViewController
    
    @property (strong, nonatomic) SkyVaultNode *node;
    @property (strong, nonatomic) id<AlfrescoSession> session;
    @property (strong, nonatomic) SkyVaultDocumentFolderService *documentFolderService;
    @property (strong, nonatomic) SkyVaultCommentService *commentService;
    @property (strong, nonatomic) NSArray *comments;
    @property (strong, nonatomic) SkyVaultTaggingService *taggingService;
    @property (strong, nonatomic) NSArray *tags;
    @property (strong, nonatomic) SkyVaultVersionService *versionService;
    @property (strong, nonatomic) NSArray *versions;
    @property (strong, nonatomic) NSString *latestVersion;
    
    
    - (id)initWithSession:(id<AlfrescoSession>)createdSession andNode:(AlfrescoNode *)nodeToBeDisplayed;
    - (void)getComments;
    - (void)getTags;
    - (void)getVersions;
    
    @end
                  
                  
                
You have now prepared the DocumentTableViewController.h header file to support versions.

Adding a method to retrieve versions

In this task you will add a method to retrieve versions.
  1. Load DocumentTableViewController.m into Xcode. You will need to include a couple of header files, one to include SkyVaultVersionService and one for SkyVaultDocument, as these will be used in the method to retrieve versions:

                  
    #import "AlfrescoVersionService.h"              
    #import "AlfrescoDocument.h"              
                  
                

    This includes the version service and document classes which you will be using to retrieve versions for the selected node.

  2. Now add the method to retrieve the versions just after the - (void) getTags method:

    
    - (void) getVersions
    {
        if(nil != self.session && self.node != nil)
        {
            // get the document versions using the SkyVaultVersionService
            self.versionService = [[AlfrescoVersionService alloc] initWithSession:self.session];
            __weak DocumentTableViewController *weakSelf = self;
            
            SkyVaultDocument *document = (AlfrescoDocument *)self.node;
            
            [self.versionService retrieveAllVersionsOfDocument:document completionBlock:^(NSArray *array, NSError *error){
                    if (nil == array)
                    {
                        NSLog(@"ERROR: No versions array returned: %@", error);
                    }
                    else
                    {
                        weakSelf.versions = [NSArray arrayWithArray:array];
                    
                        // get latest version               
                        SkyVaultDocument *doc = [weakSelf.versions objectAtIndex:0];
                        weakSelf.latestVersion = [NSString stringWithString:doc.versionLabel];
                    
                    }
                    [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
                    [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
                }];
        }
    }
                

    This code returns an array of document versions via the completion block. Using __weak to create a weak reference to self is recommended practice for code inside completion blocks. As the versions are associated with section 4 in the node detail table view, this section of the table view needs to be redrawn when the completion block completes so that the labels for retrieved versions are displayed. You also update information for the latest version label in section 0, the summary section. Note that latest version is always the first object in the array of retrieved versions.

You have added the method to retrieve and display versions. There are however a few more changes that need to be made before this code can be built and run.

Further additions and changes to the code

In this task you will add a few more changes to the code to get a working application.
  1. The first change you need to make is to add code to call the method to retrieve the versions. Navigate to the method viewDidLoad and add the following method call after the call to getTags:

                  
    [self getVersions];
                  
                
  2. You also need to change the number of rows that will be allocated in the table view for the versions returned. The number of rows in the section will now depend on the number of versions returned. Navigate to the method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and change the case statement for the versions section as follows:

    
        case 4: // Versions section
            numRows = [self.versions count];
            break;
    
                

    This will set the number of rows in this section to the number of versions retrieved.

  3. You also need to ensure that the latest version label is set in section 0 of the table view, the summary section. Navigate to the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method implementation. In the section 0 if statement, modify the case 2 statement which corresponds to the row that holds the latest version label:

                  
                case 2: // Latest version label
                    cell.textLabel.text = self.latestVersion;
                    break;              
                  
                

    Section 0 code should now look like the following:

                  
            // SECTION 0 - Summary
            
            if (indexPath.section == 0)
            {
                
                switch (indexPath.row)
                {
                    case 0:
                        cell.textLabel.text = self.node.name;
                        break;
                        
                    case 1:
                        if (self.node.isDocument == TRUE)
                        {
                            cell.textLabel.text = @"isDocument == TRUE";
                        }
                        else if (self.node.isFolder == TRUE)
                        {
                            cell.textLabel.text = @"isFolder == TRUE";
                        }
                        else
                        {
                            cell.textLabel.text = @"Not document or folder.";
                        }
                        break;
                        
                    case 2: // Latest version label
                        cell.textLabel.text = self.latestVersion;
                        break;
                        
                    default:
                        break;
                }
                
            }
                  
                  
                

    The cell will be set to the latest version label for the document.

  4. You now need to set the table cells in the versions section to the version label values. Navigate to the tableView:cellForRowAtIndexPath: method if not located there. Find the if statement that corresponds to section 4 of the the table view, the versions section. Change the code as follows:

                  
            // SECTION 4 - Versions
            
            if (indexPath.section == 4) 
            {
                SkyVaultDocument *document = [self.versions objectAtIndex:indexPath.row];
                cell.textLabel.text = document.versionLabel;
            }
                  
                

    This code retrieves the version from the versions array that corresponds to the row being drawn and then sets the cell's text to be that of the version's label.

  5. Build and run your code. Your application should show something similar to the following when a node is selected. Note the versions are listed in the Versions section. Note also the latest version is correctly displayed in the Summary section.

You have added the final code snippets to ensure a working application. This concludes the HelloRepo tutorial series.

In this tutorial you added code to retrieve and display versions for a node.