Documentation Center

Integrating UGC on a JSP Web site

Integrate User Generated Content with Audience Manager Contacts, or with your custom contacts stored in your custom user management system, from a JSP Web site.

Procedure

  1. Access the SDL Tridion installation media.
  2. Navigate to the folder Content Delivery\resources\samples\ugc\java.
  3. Open changeProfile.jsp the file in a plain-text editor. This file is an updated version of the sample changeProfile.jsp page that SDL Tridion ships with User Generated Content code has been added to it.
  4. Open your own JSP Web page that your visitors use to log in and to change their profile, in a plain-text editor.
  5. 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" %>
  6. 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 a User Generated Content 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' User Generated Content 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 User Generated Content 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 User Generated Content user to Contact.
    				user.setExternalId(externalIdJSON.toString());
    			} else {
    				// Create a new 'Contact-linked' User Generated Content user.
    				user = new UGCUser(userId, null, null, externalIdJSON.toString());
    			}
    			userDAO.store(user);
    		}
    	}
    }
  7. 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 User Generated Content User
    try {
    	syncUGCUserWithContact(contact);
    } catch (StorageException e) {
    	addToErrorMessage(errorBuffer, "Unable to set current Contact as current User Generated Content user. " + e.getMessage());
    } catch (JSONException e) {
    	addToErrorMessage(errorBuffer, "Problem reading Contact's identification fields. " + e.getMessage());
    }
  8. Save and close your JSP Web page.