Local vs
Bundle Java Class
The Java
use-class can be installed in two ways:
1.
Local install
2.
OSGI bundle Install
Local install
The
Java source file is placed alongside the HTL file, in the same repository
folder. The source is automatically compiled on demand.
Benefit
of this approach: No separate compilation or packaging step is required.
Bundle install
The
Java class must be compiled and deployed within an OSGi bundle using the
standard AEM bundle deployment mechanism.
In our demo
we have used both ways
Local install is used for below
components:
1.
cta-link
2.
customer-story
3.
product-box
4.
product-card
Bundle install is used for below components:
1.
banner
2.
banner-accordion
data-sly-use attribute initialize a Java
class, where that class should be installed as part of an OSGi bundle. Its
fully-qualified class name must be used as follows:
For Local java file:
data-sly-use.ctaLink="CTALink"
For OSGi uploaded java
file:
data-sly-use.helper="com.eclerx.aem.demo.core.util.BannerHelper"
Where CTALink and BannerHelper
are the class name not
the file names.
Bundle Install Code
Example
Here,
For Banner Component we have written java class (BannerHelper.java) and deployed that as bundle in OSGI and using
data-sly-use attribute in banner.html we leveraged that.
Banner.html
Here,
com.eclerx.aem.demo.core.util.BannerHelper
is used, where BannerHelper is a
class Name.
BannerHelper.java
package
com.eclerx.aem.demo.core.util;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.sightly.WCMUsePojo;
public class BannerHelper extends WCMUsePojo {
protected final Logger log =
LoggerFactory.getLogger(BannerHelper.class);
private String ctaPath1;
private String ctaPath2;
private String accordion1CtaPath1;
private String accordion1CtaPath2;
private String accordion1CtaPath3;
private String accordion2CtaPath1;
private String accordion2CtaPath2;
private String accordion2CtaPath3;
private String accordion3CtaPath1;
private String accordion3CtaPath2;
private String accordion3CtaPath3;
private String[] accordionHeader1;
private String[] accordionHeader2;
private String[] accordionHeader3;
private String accordionHeader1Part1;
private String accordionHeader1Part2;
private String accordionHeader2Part1;
private String accordionHeader2Part2;
private String accordionHeader3Part1;
private String accordionHeader3Part2;
public String getCtaPath1() {
return ctaPath1;
}
public String getCtaPath2() {
return ctaPath2;
}
public String getAccordion1CtaPath1() {
return accordion1CtaPath1;
}
public String getAccordion1CtaPath2() {
return accordion1CtaPath2;
}
public String getAccordion1CtaPath3() {
return accordion1CtaPath3;
}
public String getAccordion2CtaPath1() {
return accordion2CtaPath1;
}
public String getAccordion2CtaPath2() {
return accordion2CtaPath2;
}
public String getAccordion2CtaPath3() {
return accordion2CtaPath3;
}
public String getAccordion3CtaPath1() {
return accordion3CtaPath1;
}
public String getAccordion3CtaPath2() {
return accordion3CtaPath2;
}
public String getAccordion3CtaPath3() {
return accordion3CtaPath3;
}
public String getAccordionHeader1Part1() {
return accordionHeader1Part1;
}
public String getAccordionHeader1Part2() {
return accordionHeader1Part2;
}
public String getAccordionHeader2Part1() {
return accordionHeader2Part1;
}
public String getAccordionHeader2Part2() {
return accordionHeader2Part2;
}
public String getAccordionHeader3Part1() {
return accordionHeader3Part1;
}
public String getAccordionHeader3Part2() {
return accordionHeader3Part2;
}
@Override
public void activate() throws Exception {
log.info("*** Entered Activate of
BannerHelper");
ResourceResolver resourceResolver = getResourceResolver();
/*
Banner Component CTA Links*/
ctaPath1 = CtaUtil.resolveURI(getProperties().get("ctaPath1",""), resourceResolver);
ctaPath2 = CtaUtil.resolveURI(getProperties().get("ctaPath2",""), resourceResolver);
/*
Banner Accordion Component CTA Links*/
accordion1CtaPath1 = CtaUtil.resolveURI(getProperties().get("accordion1CtaPath1",""), resourceResolver);
accordion1CtaPath2 = CtaUtil.resolveURI(getProperties().get("accordion1CtaPath2",""), resourceResolver);
accordion1CtaPath3 = CtaUtil.resolveURI(getProperties().get("accordion1CtaPath3",""), resourceResolver);
accordion2CtaPath1 = CtaUtil.resolveURI(getProperties().get("accordion2CtaPath1",""), resourceResolver);
accordion2CtaPath2 = CtaUtil.resolveURI(getProperties().get("accordion2CtaPath2",""), resourceResolver);
accordion2CtaPath3 = CtaUtil.resolveURI(getProperties().get("accordion2CtaPath3",""), resourceResolver);
accordion3CtaPath1 = CtaUtil.resolveURI(getProperties().get("accordion3CtaPath1",""), resourceResolver);
accordion3CtaPath2 = CtaUtil.resolveURI(getProperties().get("accordion3CtaPath2",""), resourceResolver);
accordion3CtaPath3 = CtaUtil.resolveURI(getProperties().get("accordion3CtaPath3",""), resourceResolver);
//Splitting
Accordion Header Text 1
if(!getProperties().get("accordionHeader1","").isEmpty()) {
accordionHeader1 = getProperties().get("accordionHeader1","").split("&");
accordionHeader1Part1 = accordionHeader1[0];
accordionHeader1Part2 = accordionHeader1[1];
}
//Splitting
Accordion Header Text 2
if(!getProperties().get("accordionHeader2","").isEmpty()) {
accordionHeader2 = getProperties().get("accordionHeader2","").split("&");
accordionHeader2Part1 = accordionHeader2[0];
accordionHeader2Part2 = accordionHeader2[1];
}
//Splitting
Accordion Header Text 3
if(!getProperties().get("accordionHeader3","").isEmpty()) {
accordionHeader3 = getProperties().get("accordionHeader3","").split("&");
accordionHeader3Part1 = accordionHeader3[0];
accordionHeader3Part2 = accordionHeader3[1];
}
}
}
Local Install Code Example
Here,
For customer-story Component we have
written java class (CustomerStory.java)
and placed that file in our component itself and using data-sly-use attribute
in customer-story.html we leveraged
that.
CustomerStory.java
package
apps.aemdemo.components.content.customer_story;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.sling.api.resource.Resource;
import
org.apache.sling.api.resource.ResourceResolver;
import
org.apache.sling.api.resource.ValueMap;
import com.day.cq.wcm.api.Page;
import com.adobe.cq.sightly.WCMUsePojo;
public class CustomerStory extends WCMUsePojo {
protected final Logger log = LoggerFactory.getLogger(CustomerStory.class);
private Boolean hasContent;
private String ctaPath;
private String bgImage;
public String getBgImage() {
return bgImage;
}
public Boolean getHasContent(){
return hasContent;
}
public String getCtaPath() {
return ctaPath;
}
@Override
public void activate() throws Exception {
ctaPath =
resolveURI(getProperties().get("ctaPath",""));
log.info("Customer Story ctapath : "+ctaPath);
bgImage = getProperties().get("fileReference")==null ? "" : getProperties().get("fileReference").toString();
log.info("Customer Story background image : "+bgImage);
if(getProperties().get("fileReference")!=null || getProperties().get("subhead")!=null || getProperties().get("title")!=null || getProperties().get("text")!=null) {
hasContent = true;
}
else {
hasContent = false;
}
log.info("Customer Story hasContent : "+hasContent);
}
private String resolveURI(String ctaPath) {
ResourceResolver resourceResolver =
getResourceResolver();
if (!ctaPath.isEmpty() && ctaPath.startsWith("/")) {
Resource resource =
resourceResolver.getResource(ctaPath);
if (resource != null) {
ValueMap map =
resource.adaptTo(ValueMap.class);
if (map.get("jcr:primaryType", String.class).equalsIgnoreCase("cq:Page")) {
ctaPath += ".html";
}
}
}
return ctaPath;
}
}
No comments:
Post a Comment