GoogleDoc integration

- sharing email for a person is now first taken from the googleusername property on the person object, if thisn't set it falls back to the user's email
  - if google rejects the email (with a server error) when setting the permissions on a document, this no longer rolls back the transaction, but logs the error instead
  - unit tests 
  


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@20061 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2010-04-30 13:35:52 +00:00
parent 5c87d5eb87
commit 3a8e5b4cfe
4 changed files with 106 additions and 43 deletions

View File

@@ -192,6 +192,7 @@ public interface ContentModel
static final QName PROP_COMPANYFAX = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "companyfax"); static final QName PROP_COMPANYFAX = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "companyfax");
static final QName PROP_COMPANYEMAIL = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "companyemail"); static final QName PROP_COMPANYEMAIL = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "companyemail");
static final QName PROP_SKYPE = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "skype"); static final QName PROP_SKYPE = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "skype");
static final QName PROP_GOOGLEUSERNAME = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "googleusername");
static final QName PROP_INSTANTMSG = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "instantmsg"); static final QName PROP_INSTANTMSG = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "instantmsg");
static final QName PROP_SIZE_CURRENT = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "sizeCurrent"); static final QName PROP_SIZE_CURRENT = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "sizeCurrent");

View File

@@ -68,13 +68,14 @@ import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException; import com.google.gdata.util.ServiceException;
/** /**
* * Google docs integration service implementation
*/ */
public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
{ {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(GoogleDocsServiceImpl.class); private static Log logger = LogFactory.getLog(GoogleDocsServiceImpl.class);
/** Google document types */
public static final String TYPE_DOCUMENT = "document"; public static final String TYPE_DOCUMENT = "document";
public static final String TYPE_SPREADSHEET = "spreadsheet"; public static final String TYPE_SPREADSHEET = "spreadsheet";
public static final String TYPE_PRESENTATION = "presentation"; public static final String TYPE_PRESENTATION = "presentation";
@@ -99,6 +100,7 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
private String username; private String username;
private String password; private String password;
/** Permission map */
private Map<String, String> permissionMap; private Map<String, String> permissionMap;
/** /**
@@ -157,7 +159,7 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
this.permissionService = permissionService; this.permissionService = permissionService;
} }
/* /**
* @param ownableService ownable service * @param ownableService ownable service
*/ */
public void setOwnableService(OwnableService ownableService) public void setOwnableService(OwnableService ownableService)
@@ -340,10 +342,11 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
} }
/** /**
* Set a google permission on a specified resource
* *
* @param nodeRef * @param nodeRef node reference
* @param resourceId * @param resource document resource
* @param permissionContext * @param permissionContext permission context
*/ */
private void setGoogleResourcePermissions(NodeRef nodeRef, DocumentListEntry resource, GoogleDocsPermissionContext permissionContext) private void setGoogleResourcePermissions(NodeRef nodeRef, DocumentListEntry resource, GoogleDocsPermissionContext permissionContext)
{ {
@@ -387,11 +390,12 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
} }
/** /**
* Set a google permission on a specified resource
* *
* @param resourceId * @param resource document resource
* @param authorityType * @param authorityType authority type
* @param authorityName * @param authorityName authority name
* @param role * @param role role
*/ */
private void setGoogleResourcePermission(DocumentListEntry resource, AuthorityType authorityType, String authorityName, String role) private void setGoogleResourcePermission(DocumentListEntry resource, AuthorityType authorityType, String authorityName, String role)
{ {
@@ -415,25 +419,34 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
} }
/** /**
* Gets the users email used to identify their google account.
* *
* @param userName * @param userName user name
* @return * @return String google account email, null if none
*/ */
private String getUserEMail(String userName) private String getUserEMail(String userName)
{ {
String email = null; String email = null;
NodeRef personNodeRef = personService.getPerson(userName); NodeRef personNodeRef = personService.getPerson(userName);
if (personNodeRef != null) if (personNodeRef != null)
{
// First see if the google user information has been set
email = (String)nodeService.getProperty(personNodeRef, ContentModel.PROP_GOOGLEUSERNAME);
// If no google user information then default back to the user's email
if (email == null || email.length() == 0)
{ {
email = (String) nodeService.getProperty(personNodeRef, ContentModel.PROP_EMAIL); email = (String) nodeService.getProperty(personNodeRef, ContentModel.PROP_EMAIL);
} }
}
return email; return email;
} }
/** /**
* Gets the nodes parent folder google resource.
* *
* @param nodeRef * @param nodeRef node reference
* @return * @return DocumentList Entry folder resource
*/ */
private DocumentListEntry getParentFolder(NodeRef nodeRef) private DocumentListEntry getParentFolder(NodeRef nodeRef)
{ {
@@ -636,12 +649,13 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
} }
/** /**
* Create a google document
* *
* @param name * @param name document name
* @param mimetype * @param mimetype mime type
* @param parentFolder * @param parentFolder parent folder resource
* @param is * @param is input stream for content
* @return * @return DocumentListEntry resource for created document
*/ */
private DocumentListEntry createGoogleDocument(String name, String mimetype, DocumentListEntry parentFolder, InputStream is) private DocumentListEntry createGoogleDocument(String name, String mimetype, DocumentListEntry parentFolder, InputStream is)
{ {
@@ -697,10 +711,11 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
} }
/** /**
* Updates the content of a google document
* *
* @param docResourceId * @param document document resource
* @param mimeType * @param mimeType mimetype
* @param is * @param is input stream
*/ */
private void updateGoogleDocContent(DocumentListEntry document, String mimeType, InputStream is) private void updateGoogleDocContent(DocumentListEntry document, String mimeType, InputStream is)
{ {
@@ -775,6 +790,12 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
ParameterCheck.mandatory("email", email); ParameterCheck.mandatory("email", email);
ParameterCheck.mandatory("role", role); ParameterCheck.mandatory("role", role);
// Log details of failure
if (logger.isDebugEnabled() == true)
{
logger.debug("Setting the role " + role + " on the google resource " + resource.getResourceId() + " for email " + email + ".");
}
try try
{ {
AclRole aclRole = new AclRole(role); AclRole aclRole = new AclRole(role);
@@ -801,6 +822,7 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
} }
} }
// Set the permission details
if (aclEntry == null) if (aclEntry == null)
{ {
aclEntry = new AclEntry(); aclEntry = new AclEntry();
@@ -808,6 +830,15 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
aclEntry.setScope(scope); aclEntry.setScope(scope);
googleDocumentService.insert(aclFeedLinkURL, aclEntry); googleDocumentService.insert(aclFeedLinkURL, aclEntry);
} }
else
{
// Log details of failure
if (logger.isDebugEnabled() == true)
{
logger.debug("Unable to the role " + role + " on the google resource " + resource.getResourceId() + " for email " + email + "." +
" This user already has a role on this document.");
}
}
// TODO for now we will not 'update' the permissions if they have already been set .... // TODO for now we will not 'update' the permissions if they have already been set ....
// //
@@ -823,7 +854,18 @@ public class GoogleDocsServiceImpl implements GoogleDocsService, GoogleDocsModel
} }
catch (ServiceException e) catch (ServiceException e)
{ {
throw new AlfrescoRuntimeException("Unable to set premissions on google document", e); // Ignore this exception since we don't want to roll back the entire transaction because
// a single users permissions can not be set.
// It seems the google API will return a server exception if the email does not correspond to
// a google account, so catching this exception in this indiscriminate way is the best thing to
// do for now.
// Log details of failure
if (logger.isDebugEnabled() == true)
{
logger.debug("Unable to the role " + role + " on the google resource " + resource.getResourceId() + " for email " + email + "." +
" Check that this is a valid google account.");
}
} }
catch (IOException e) catch (IOException e)
{ {

View File

@@ -70,6 +70,10 @@ public class GoogleDocumentServiceTest extends TestCase implements GoogleDocsMod
private static final String USER_TWO = "GoogleDocUserTwo"; private static final String USER_TWO = "GoogleDocUserTwo";
private static final String USER_THREE = "GoogleDocUserThree"; private static final String USER_THREE = "GoogleDocUserThree";
private static final String USER_FOUR = "GoogleDocUserFour"; private static final String USER_FOUR = "GoogleDocUserFour";
private static final String USER_FIVE = "GoogleDocUserFive";
private static final String USER_SIX = "GoogleDocUserSix";
private static final String USER_SEVEN = "GoogleDocUserSeven";
//private static final String EMAIL_DOMAIN = "@alfresco.com"; //private static final String EMAIL_DOMAIN = "@alfresco.com";
private NodeRef folder = null; private NodeRef folder = null;
@@ -112,10 +116,13 @@ public class GoogleDocumentServiceTest extends TestCase implements GoogleDocsMod
googleDocsService = (GoogleDocsService)childContext.getBean("googleDocsService"); googleDocsService = (GoogleDocsService)childContext.getBean("googleDocsService");
// Create the test users // Create the test users
createUser(USER_ONE, "rwetherall@alfresco.com"); createUser(USER_ONE, "rwetherall@alfresco.com", null);
createUser(USER_TWO, "rwetherall@activiti.com"); createUser(USER_TWO, "admin@alfresco.com", "rwetherall@activiti.com");
createUser(USER_THREE, "roy.wetherall@alfresco.com"); createUser(USER_THREE, "roy.wetherall@alfresco.com", null);
createUser(USER_FOUR, "roy.wetherall@activiti.com"); createUser(USER_FOUR, "roy.wetherall@activiti.com", null);
createUser(USER_FIVE, "admin@alfresco.com", "admin@alfresco.com");
createUser(USER_SIX, "admin@alfresco.com", null);
createUser(USER_SEVEN, null, null);
// Authenticate as user one // Authenticate as user one
AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE); AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
@@ -130,13 +137,17 @@ public class GoogleDocumentServiceTest extends TestCase implements GoogleDocsMod
siteService.setMembership(id, USER_TWO, SiteServiceImpl.SITE_COLLABORATOR); siteService.setMembership(id, USER_TWO, SiteServiceImpl.SITE_COLLABORATOR);
siteService.setMembership(id, USER_THREE, SiteServiceImpl.SITE_CONTRIBUTOR); siteService.setMembership(id, USER_THREE, SiteServiceImpl.SITE_CONTRIBUTOR);
siteService.setMembership(id, USER_FOUR, SiteServiceImpl.SITE_CONSUMER); siteService.setMembership(id, USER_FOUR, SiteServiceImpl.SITE_CONSUMER);
siteService.setMembership(id, USER_FIVE, SiteServiceImpl.SITE_COLLABORATOR);
siteService.setMembership(id, USER_SIX, SiteServiceImpl.SITE_COLLABORATOR);
siteService.setMembership(id, USER_SEVEN, SiteServiceImpl.SITE_COLLABORATOR);
// Create a folder in our site container // Create a folder in our site container
folder = fileFolderService.create(container, "myfolder", ContentModel.TYPE_FOLDER).getNodeRef(); folder = fileFolderService.create(container, "myfolder", ContentModel.TYPE_FOLDER).getNodeRef();
// Create test documents // Create test documents
nodeRefDoc = createTestDocument("mydoc.docx", "alfresco/subsystems/googledocs/default/test.docx", MimetypeMap.MIMETYPE_WORD); nodeRefDoc = createTestDocument("mydoc.docx", "alfresco/subsystems/googledocs/default/test.docx", MimetypeMap.MIMETYPE_WORD);
nodeRefSpread = createTestDocument("mydoc.xlsx", "alfresco/subsystems/googledocs/default/test.xlsx", MimetypeMap.MIMETYPE_EXCEL); //nodeRefSpread = createTestDocument("mydoc.xls", "alfresco/subsystems/googledocs/default/testBook.xls", MimetypeMap.MIMETYPE_EXCEL);
nodeRefSpread = createTestDocument("mydoc2.docx", "alfresco/subsystems/googledocs/default/test.docx", MimetypeMap.MIMETYPE_WORD);
// Create an empty content node (simulate creation of a new google doc in UI) // Create an empty content node (simulate creation of a new google doc in UI)
nodeRef2 = fileFolderService.create(folder, "mygoogledoc.doc", ContentModel.TYPE_CONTENT).getNodeRef(); nodeRef2 = fileFolderService.create(folder, "mygoogledoc.doc", ContentModel.TYPE_CONTENT).getNodeRef();
@@ -154,7 +165,7 @@ public class GoogleDocumentServiceTest extends TestCase implements GoogleDocsMod
return nodeRef; return nodeRef;
} }
private void createUser(String userName, String email) private void createUser(String userName, String email, String googleEmail)
{ {
if (authenticationService.authenticationExists(userName) == false) if (authenticationService.authenticationExists(userName) == false)
{ {
@@ -164,9 +175,18 @@ public class GoogleDocumentServiceTest extends TestCase implements GoogleDocsMod
ppOne.put(ContentModel.PROP_USERNAME, userName); ppOne.put(ContentModel.PROP_USERNAME, userName);
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName"); ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
ppOne.put(ContentModel.PROP_LASTNAME, "lastName"); ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
ppOne.put(ContentModel.PROP_EMAIL, email);
ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle"); ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
if (email != null)
{
ppOne.put(ContentModel.PROP_EMAIL, email);
}
if (googleEmail != null)
{
ppOne.put(ContentModel.PROP_GOOGLEUSERNAME, googleEmail);
}
personService.createPerson(ppOne); personService.createPerson(ppOne);
} }
} }
@@ -211,18 +231,18 @@ public class GoogleDocumentServiceTest extends TestCase implements GoogleDocsMod
String downloadFile = downloadFile(googleDocsService.getGoogleDocContent(nodeRefDoc), ".doc"); String downloadFile = downloadFile(googleDocsService.getGoogleDocContent(nodeRefDoc), ".doc");
System.out.println("Download file: " + downloadFile); System.out.println("Download file: " + downloadFile);
// googleDocsService.upload(nodeRefSpread, GoogleDocsPermissionContext.SHARE_WRITE); googleDocsService.createGoogleDoc(nodeRefSpread, GoogleDocsPermissionContext.SHARE_WRITE);
//
// assertTrue(nodeService.hasAspect(nodeRefSpread, ASPECT_GOOGLERESOURCE)); assertTrue(nodeService.hasAspect(nodeRefSpread, ASPECT_GOOGLERESOURCE));
// assertNotNull(nodeService.getProperty(nodeRefSpread, PROP_URL)); assertNotNull(nodeService.getProperty(nodeRefSpread, PROP_URL));
// assertNotNull(nodeService.getProperty(nodeRefSpread, PROP_RESOURCE_ID)); assertNotNull(nodeService.getProperty(nodeRefSpread, PROP_RESOURCE_ID));
// assertNotNull(nodeService.getProperty(nodeRefSpread, PROP_RESOURCE_TYPE)); assertNotNull(nodeService.getProperty(nodeRefSpread, PROP_RESOURCE_TYPE));
//
// System.out.println("Google doc URL: " + nodeService.getProperty(nodeRefSpread, PROP_URL)); System.out.println("Google doc URL: " + nodeService.getProperty(nodeRefSpread, PROP_URL));
// System.out.println("Google doc type: " + nodeService.getProperty(nodeRefSpread, PROP_RESOURCE_TYPE)); System.out.println("Google doc type: " + nodeService.getProperty(nodeRefSpread, PROP_RESOURCE_TYPE));
// System.out.println("Google doc id: " + nodeService.getProperty(nodeRefSpread, PROP_RESOURCE_ID)); System.out.println("Google doc id: " + nodeService.getProperty(nodeRefSpread, PROP_RESOURCE_ID));
// downloadFile = downloadFile(googleDocsService.download(nodeRefSpread), ".xls"); // // downloadFile = downloadFile(googleDocsService.download(nodeRefSpread), ".xls");
// System.out.println("Download file: " + downloadFile); // // System.out.println("Download file: " + downloadFile);
} }
} }