Integrating UGC on a JSP Web site
Integrate UGC with Audience Manager Contacts, or with your custom contacts stored in your custom user management system, from a JSP Web site.
Procedure
- First, access your UGC installation CD-ROM and navigate to the folder Content Delivery\resources\samples\ugc\java.
You see a file called changeProfile.jsp.
- Open the file in a plain-text editor. This file is an updated version of the sample changeProfile.jsp page that SDL Tridion ships with. UGC code has been added to it.
- Next, open your own JSP Web page that your visitors use to log in and to change their profile, in a plain-text editor.
- Copy the following import statement from changeProfile.jsp and insert it at the top of your page:
<%@ page import="com.tridion.ambientdata.claimstore.ClaimStore, com.tridion.ambientdata.web.WebContext, com.tridion.broker.StorageException, com.tridion.storage.StorageManagerFactory, com.tridion.storage.ugc.UGCTypeMapping, com.tridion.storage.ugc.UGCUser com.tridion.storage.ugc.dao.UserDAO, com.tridion.ugc.odata.claimprocessor.BasePostClaimProcessor, org.codehaus.jettison.json.JSONException, org.codehaus.jettison.json.JSONObject, java.util.List" %> - Add the following static method from changeProfile.jsp to the page. This method is set up to integrate with Audience Manager. To integrate it with your custom external user management system, check the comments marked CUSTOM.
/** * Utility method to synchronize UGC user with a Contact. * * @param contact The contact to synchronize with. * @throws JSONException If the UGCUser externalId could not be created. * @throws StorageException On any error during database access. */ static void syncUGCUserWithContact(Contact contact) throws JSONException, StorageException { JSONObject externalIdJSON = new JSONObject(); for (Object field : contact.getDetails().getIdentificationFields().values()) { ExtendedDetail extendedDetail = (ExtendedDetail) field; externalIdJSON.put(extendedDetail.getFieldName(), extendedDetail.getValue()); } ClaimStore claimStore = WebContext.getCurrentClaimStore(); UserDAO userDAO = (UserDAO) StorageManagerFactory.getDefaultDAO(UGCTypeMapping.UGC_USER.getConfigurationName()); List <UGCUser> ugcUsers = userDAO.findByExternalId(externalIdJSON.toString()); if (ugcUsers != null && !ugcUsers.isEmpty()) { // Get first 'Contact-linked' UGC user. UGCUser ugcUser = ugcUsers.remove(0); // CUSTOM: To integrate with your custom external user management system, // remove the following line, which integrates with Audience Manager. claimStore.put(BasePostClaimProcessor.USER_CLAIM, ugcUser.getId()); // CUSTOM: To integrate with your custom external user management system, // uncomment the following line, where "userId" is a String that matches the ID column // in the UGC Users table: // claimStore.put(URI.create("taf:claim:contentdelivery:webservice:user"), userId); // // INSERT CODE HERE if remaining users need to be aggregated // } else { // Else: Current user claim is leading. if (claimStore.contains(BasePostClaimProcessor.USER_CLAIM)) { String userId = claimStore.get(BasePostClaimProcessor.USER_CLAIM, String.class); UGCUser user = userDAO.findByPrimaryKey(userId); if (user != null) { // Link current tracked UGC user to Contact. user.setExternalId(externalIdJSON.toString()); } else { // Create a new 'Contact-linked' UGC user. user = new UGCUser(userId, null, null, externalIdJSON.toString()); } userDAO.store(user); } } } - In your Web page, find the places in your code where the user has successfully logged in, and where the user has changed his or her profile. In this location, make the call to the method you just added, catching any exceptions thrown:
// Sync UGC User try { syncUGCUserWithContact(contact); } catch (StorageException e) { addToErrorMessage(errorBuffer, "Unable to set current Contact as current UGC user. " + e.getMessage()); } catch (JSONException e) { addToErrorMessage(errorBuffer, "Problem reading Contact's identification fields. " + e.getMessage()); }Save and close your JSP Web page.