Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:17
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save floriankraft/ce24d2c57309aaa2643d to your computer and use it in GitHub Desktop.
Extending an AEM social component and factory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.companyname.components.product.review; | |
import com.adobe.cq.social.commons.client.api.AbstractSocialComponentFactory; | |
import com.adobe.cq.social.commons.client.api.ClientUtilities; | |
import com.adobe.cq.social.commons.client.api.ClientUtilityFactory; | |
import com.adobe.cq.social.commons.client.api.QueryRequestInfo; | |
import com.adobe.cq.social.commons.client.api.SocialComponent; | |
import com.adobe.cq.social.commons.client.api.SocialComponentFactory; | |
import com.adobe.cq.social.commons.client.api.User; | |
import com.adobe.cq.social.commons.comments.api.CommentCollectionConfiguration; | |
import com.adobe.cq.social.commons.comments.listing.CommentSocialComponentListProviderManager; | |
import com.adobe.cq.social.review.client.api.AbstractReview; | |
import com.adobe.cq.social.review.client.api.ReviewSocialComponent; | |
import com.adobe.cq.social.ugcbase.SocialUtils; | |
import com.adobe.granite.xss.XSSAPI; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.jcr.RepositoryException; | |
import javax.jcr.Value; | |
import org.apache.commons.lang3.StringUtils; | |
import org.apache.felix.scr.annotations.Activate; | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Deactivate; | |
import org.apache.felix.scr.annotations.Reference; | |
import org.apache.felix.scr.annotations.Service; | |
import org.apache.jackrabbit.api.security.user.Authorizable; | |
import org.apache.jackrabbit.api.security.user.UserManager; | |
import org.apache.sling.api.resource.LoginException; | |
import org.apache.sling.api.resource.Resource; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.api.resource.ResourceResolverFactory; | |
import org.apache.sling.api.SlingHttpServletRequest; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* This factory implements the AEM {@link SocialComponentFactory}, which provides instances of | |
* {@link ProductReviewComponent}.<br/> | |
* <br/> | |
* When AEM tries to find a SocialComponentFactory for the current resource (social component), it makes use of | |
* {@link com.adobe.cq.social.commons.client.api.SocialComponentFactoryManager}. | |
* The manager will deliver this factory, if: | |
* <ul> | |
* <li>The resourceType of the current resource equals the return value of getSupportedResourceType() in this | |
* factory (i.e. "social/reviews/components/hbs/reviews/review").</li> | |
* <li>The getPriority() method of this factory delivers a value higher than the priority of any similar factory | |
* (in most cases a value greater than 0 will do the trick).</li> | |
* </ul> | |
* | |
* The same can be achieved "manually", by referencing the SocialComponentFactoryManager in the OSGi context and calling | |
* getSocialComponentFactory(...) with a social component resource.<br/> | |
* <br/> | |
* More information are on the | |
* <a href="http://docs.adobe.com/docs/en/aem/6-0/develop/social-communities/scf/server-customize.html">Server-side Customization</a> | |
* page of the AEM 6.0 knowledge base. | |
*/ | |
@Component(name = "ProductReviewComponentFactory") | |
@Service | |
public class ProductReviewComponentFactory extends AbstractSocialComponentFactory implements SocialComponentFactory { | |
private static final Logger LOG = LoggerFactory.getLogger(ProductReviewComponentFactory.class); | |
private ResourceResolver adminResourceResolver = null; | |
@Reference | |
private ResourceResolverFactory resolverFactory = null; | |
@Reference | |
private XSSAPI xssapi = null; | |
@Reference | |
private SocialUtils socialUtils = null; | |
@Reference | |
private ClientUtilityFactory clientUtilityFactory = null; | |
@Reference | |
private CommentSocialComponentListProviderManager commentListProviderManager = null; | |
public ProductReviewComponentFactory() { | |
super(); | |
} | |
@Activate | |
protected void activate(final Map<String, Object> props) { | |
initAdminResourceResolver(); | |
} | |
@Deactivate | |
protected void deactivate() { | |
if (this.adminResourceResolver != null) { | |
this.adminResourceResolver.close(); | |
} | |
} | |
@Override | |
public SocialComponent getSocialComponent(Resource resource) { | |
SocialComponent socialComponent = null; | |
bindClientUtilFactory(this.clientUtilityFactory); | |
try { | |
socialComponent = new ProductReviewComponent(resource, this.getClientUtilities(resource.getResourceResolver()), commentListProviderManager, getAdminResourceResolver()); | |
} catch (RepositoryException e) { | |
LOG.error("An error occurred while trying to create ProductReviewComponent object.", e); | |
} | |
return socialComponent; | |
} | |
@Override | |
public SocialComponent getSocialComponent(Resource resource, SlingHttpServletRequest request) { | |
SocialComponent socialComponent = null; | |
bindClientUtilFactory(this.clientUtilityFactory); | |
try { | |
socialComponent = new ProductReviewComponent(resource, this.getClientUtilities(request), this.getQueryRequestInfo(request), commentListProviderManager, getAdminResourceResolver()); | |
} catch (RepositoryException e) { | |
LOG.error("An error occurred while trying to create ProductReviewComponent object.", e); | |
} | |
return socialComponent; | |
} | |
@Override | |
public SocialComponent getSocialComponent(Resource resource, ClientUtilities clientUtilities, QueryRequestInfo queryRequestInfo) { | |
SocialComponent socialComponent = null; | |
try { | |
socialComponent = new ProductReviewComponent(resource, clientUtilities, queryRequestInfo, commentListProviderManager, getAdminResourceResolver()); | |
} catch (RepositoryException e) { | |
LOG.error("An error occurred while trying to create ProductReviewComponent object.", e); | |
} | |
return socialComponent; | |
} | |
@Override | |
public int getPriority() { | |
return 10; | |
} | |
@Override | |
public String getSupportedResourceType() { | |
return "social/reviews/components/hbs/reviews/review"; | |
} | |
private ResourceResolver getAdminResourceResolver() { | |
if (this.adminResourceResolver == null) { | |
initAdminResourceResolver(); | |
} | |
return this.adminResourceResolver; | |
} | |
private void initAdminResourceResolver() { | |
if (this.adminResourceResolver == null) { | |
Map<String, Object> params = new HashMap<String, Object>(); | |
params.put(ResourceResolverFactory.SUBSERVICE, "createSocialComponentService"); | |
try { | |
this.adminResourceResolver = resolverFactory.getServiceResourceResolver(params); | |
} catch (LoginException e) { | |
LOG.error("An error occurred while trying to get ResourceResolver.", e); | |
} | |
} | |
} | |
} | |
/** | |
* This class implements the AEM {@link ReviewSocialComponent} which qualifies it to represent a review component, that | |
* can be provided by a {@link com.adobe.cq.social.commons.client.api.SocialComponentFactory}. By extending | |
* {@link AbstractReview}, this class inherits the behaviour of the default AEM review component.<br/> | |
* <br/> | |
* Every public get-method of this class is called by the handlebars engine, which will add the return values to the | |
* JSON representation of this component. | |
*/ | |
public class ProductReviewComponent extends AbstractReview<CommentCollectionConfiguration> implements ReviewSocialComponent<CommentCollectionConfiguration> { | |
private static final Logger LOG = LoggerFactory.getLogger(ProductReviewComponent.class); | |
private static final String USER_UNKNOWN = "Unknown"; | |
private ResourceResolver adminResourceResolver = null; | |
public ProductReviewComponent(Resource resource, ClientUtilities clientUtils, CommentSocialComponentListProviderManager listProviderManager, ResourceResolver resourceResolver) throws RepositoryException { | |
super(resource, clientUtils, listProviderManager); | |
this.adminResourceResolver = resourceResolver; | |
} | |
public ProductReviewComponent(Resource resource, ClientUtilities clientUtils, QueryRequestInfo queryInfo, CommentSocialComponentListProviderManager listProviderManager, ResourceResolver resourceResolver) throws RepositoryException { | |
super(resource, clientUtils, queryInfo, listProviderManager); | |
this.adminResourceResolver = resourceResolver; | |
} | |
public String getUserName() { | |
String userName = USER_UNKNOWN; | |
User reviewAuthor = super.getAuthor(); | |
if (reviewAuthor != null) { | |
String authorPath = reviewAuthor.getId().toString(); | |
String authorId = StringUtils.removeStart(authorPath, "/social/authors/"); | |
userName = getUserNameByUserId(authorId); | |
} | |
return userName; | |
} | |
private String getUserNameByUserId(String authorizableId) { | |
String userName = USER_UNKNOWN; | |
if (this.adminResourceResolver != null) { | |
try { | |
UserManager userManager = this.adminResourceResolver.adaptTo(UserManager.class); | |
Authorizable authorizable = userManager.getAuthorizable(authorizableId); | |
Value[] firstNameVal = authorizable.getProperty("./profile/givenName"); | |
Value[] lastNameVal = authorizable.getProperty("./profile/familyName"); | |
if (firstNameVal != null) { | |
userName = firstNameVal[0].getString(); | |
} | |
if (lastNameVal != null) { | |
userName += " " + lastNameVal[0].getString(); | |
} | |
} catch (RepositoryException e) { | |
LOG.error("An error occurred while trying to extract the user name for id: " + authorizableId); | |
} | |
} | |
return userName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment