Modifying the header file
-
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.
-
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.
-
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.
-
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;
-
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
Adding a method to retrieve versions
-
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.
-
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.
Further additions and changes to the code
-
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];
-
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.
-
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.
-
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.
-
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.
In this tutorial you added code to retrieve and display versions for a node.