UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
Node currentNode = uiExplorer.getCurrentNode();
try {
File googleDriveFile = null;
String googleDriveFileID = null;
if(!currentNode.isNodeType(GoogleDocsConstants.GOOGLE_DRIVE_NODE_TYPE) || !currentNode.hasProperty(GoogleDocsConstants.GOOGLE_DRIVE_NODE_PROPERTY)) {
// insert doc in google drive
googleDriveFile = googleDriveService.addFileInGoogleDrive(currentNode);
googleDriveFileID = googleDriveFile.getId();
// share the document with the main Google account
googleDriveService.shareFileWithMasterAccount(googleDriveFileID);
// add google drive mixin
currentNode.addMixin(GoogleDocsConstants.GOOGLE_DRIVE_NODE_TYPE);
currentNode.setProperty(GoogleDocsConstants.GOOGLE_DRIVE_NODE_PROPERTY, googleDriveFileID);
currentNode.save();
} else {
// get the Google Drive file id
googleDriveFileID = currentNode.getProperty(GoogleDocsConstants.GOOGLE_DRIVE_NODE_PROPERTY).getString();
// get the Google Drive file
googleDriveFile = googleDriveService.getFile(googleDriveFileID);
}
// share the document with the contributor
String userId = ConversationState.getCurrent().getIdentity().getUserId();
User user = organizationService.getUserHandler().findUserByName(userId);
if(user == null) {
throw new GoogleDocsException("UIActionBar.msg.googledocs.user-not-found", "User " + userId + " not found");
}
String userEmail = user.getEmail();
if(userEmail == null || userEmail.isEmpty()) {
throw new GoogleDocsException("UIActionBar.msg.googledocs.no-user-email", "Email of user " + userId + " is empty");
}
googleDriveService.shareFileWith(googleDriveFileID, userEmail);
event.getRequestContext().getJavascriptManager().addCustomizedOnLoadScript("ajaxRedirect('" + googleDriveFile.getAlternateLink() + "');");
} catch (GoogleDocsException gde) {
log.error("Error while adding the document " + currentNode.getPath() + " in Google Drive - Cause : " + gde.getMessage(), gde);
org.exoplatform.wcm.webui.Utils.createPopupMessage(uiExplorer,
gde.getMessageKey(),