Skip to content

Commit

Permalink
Merge pull request #4 from thojava/add-video-to-teaser-component
Browse files Browse the repository at this point in the history
add video to teaser component
  • Loading branch information
thojava authored Nov 22, 2023
2 parents dbd2523 + a993385 commit 79d078d
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 27 deletions.
91 changes: 72 additions & 19 deletions core/src/main/java/nz/co/manawabay/core/models/Teaser.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package nz.co.manawabay.core.models;

import com.adobe.cq.wcm.core.components.commons.link.Link;
import com.adobe.cq.wcm.core.components.models.ListItem;
import nz.co.manawabay.core.internal.resource.CoreResourceWrapper;
import com.adobe.cq.wcm.core.components.models.Image;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.components.Component;
import com.adobe.cq.wcm.core.components.models.ListItem;
import com.day.cq.commons.DownloadResource;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.DamConstants;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.components.Component;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import nz.co.manawabay.core.internal.resource.CoreResourceWrapper;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.models.annotations.injectorspecific.*;
import org.apache.sling.models.annotations.via.ResourceSuperType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.List;

import java.util.*;


@Model(adaptables = SlingHttpServletRequest.class,
adapters = com.adobe.cq.wcm.core.components.models.Teaser.class,
resourceType = "manawabay/components/teaser",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Slf4j
public class Teaser implements com.adobe.cq.wcm.core.components.models.Teaser {

//LOGGER
private static final Logger LOGGER = LoggerFactory.getLogger(Teaser.class);
public static final String PN_IMAGE_DELEGATE = "imageDelegate";
public static final String PN_PAGE_PUBLISHDATE = "publishDate";

Expand All @@ -56,29 +56,47 @@ public class Teaser implements com.adobe.cq.wcm.core.components.models.Teaser {
@ScriptVariable
protected Component component;

@SlingObject
private ResourceResolver resourceResolver;

@Self
@Via(type = ResourceSuperType.class)
private com.adobe.cq.wcm.core.components.models.Teaser teaser;

@OSGiService
private MimeTypeService mimeTypeService;

private Resource iconResource;

private Resource brandImageResource;

@ValueMapValue
private Calendar publishDate;

@ValueMapValue
protected boolean showIcon;

@ValueMapValue
protected boolean showPublishDate;

@ValueMapValue
protected boolean hideTitle;

@ValueMapValue
protected boolean showDescription;

@ValueMapValue
protected boolean hideImage;

@ValueMapValue
protected boolean showBrandImage;

@ValueMapValue
private String publishDateFormatString;

@ValueMapValue
private String videoFileReference;

public Calendar getPublishDate() {
if (publishDate == null) {
publishDate = pageProperties.get(PN_PAGE_PUBLISHDATE, Calendar.class);
Expand All @@ -89,18 +107,23 @@ public Calendar getPublishDate() {
public boolean getShowPublishDate() {
return showPublishDate;
}

public boolean getShowIcon() {
return showIcon;
}

public boolean getHideTitle() {
return hideTitle;
}

public boolean getHideImage() {
return hideImage;
}

public boolean getShowDescription() {
return showDescription;
}

public boolean getShowBrandImage() {
return showBrandImage;
}
Expand All @@ -109,6 +132,9 @@ public String getPublishDateFormatString() {
return publishDateFormatString;
}

@Getter
private String videoUrl;

@JsonIgnore
public Resource getIconImage(@NotNull Page page) {
if (this.iconResource == null) {
Expand All @@ -131,7 +157,7 @@ public Resource getIconResource() {
if (iconResource != null && component != null) {
String delegateResourceType = component.getProperties().get(PN_IMAGE_DELEGATE, String.class);
if (StringUtils.isEmpty(delegateResourceType)) {
LOGGER.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" +
log.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" +
" the {} component; its value has to point to the resource type of a teaser component.", component.getPath());
} else {
ValueMap valueMap = iconResource.getValueMap();
Expand All @@ -158,7 +184,7 @@ public Resource getBrandImageResource() {
if (brandImageResource != null && component != null) {
String delegateResourceType = component.getProperties().get(PN_IMAGE_DELEGATE, String.class);
if (StringUtils.isEmpty(delegateResourceType)) {
LOGGER.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" +
log.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" +
" the {} component; its value has to point to the resource type of a teaser component.", component.getPath());
} else {
ValueMap valueMap = brandImageResource.getValueMap();
Expand All @@ -184,14 +210,15 @@ public Resource getBrandImageResource() {
public static @Nullable Resource getPageIcon(@NotNull Page page) {
return page.getContentResource(NN_PAGE_ICON_IMAGE);
}

public static @Nullable Resource getPageBrandImage(@NotNull Page page) {
return page.getContentResource(NN_PAGE_BRANDIMAGE_IMAGE);
}

@PostConstruct
protected void init(){
protected void init() {

LOGGER.info("Teaser init");
log.info("Teaser init");

this.showPublishDate = this.resource.getValueMap().get(nz.co.manawabay.core.models.List.PN_SHOW_PUBLISH_DATE, Boolean.FALSE);
this.publishDateFormatString = this.resource.getValueMap().get(nz.co.manawabay.core.models.List.PN_PUBLISHED_DATE_FORMAT_STRING, nz.co.manawabay.core.models.List.PUBLISHED_DATE_FORMAT_DEFAULT);
Expand All @@ -217,6 +244,32 @@ protected void init(){
if (this.brandImageResource == null) {
this.brandImageResource = this.currentPage.getContentResource(NN_PAGE_BRANDIMAGE_IMAGE);
}

if (StringUtils.isNotBlank(videoFileReference)) {
initVideoResource();
}
}

private void initVideoResource() {
Resource downloadResource = resourceResolver.getResource(videoFileReference);

if (downloadResource != null) {
Asset downloadAsset = downloadResource.adaptTo(Asset.class);
String filename = downloadAsset.getName();

String format = downloadAsset.getMetadataValue(DamConstants.DC_FORMAT);
String extension = mimeTypeService.getExtension(format);

if (StringUtils.isEmpty(extension)) {
extension = FilenameUtils.getExtension(filename);
}

videoUrl = getDownloadUrl(downloadResource, extension);
}
}

private String getDownloadUrl(Resource resource, String extension) {
return resource.getPath() + ".coredownload." + extension;
}

@Override
Expand Down Expand Up @@ -254,7 +307,7 @@ public String getPretitle() {

@Override
public String getTitle() {
return ! this.hideTitle ? teaser.getTitle() : null;
return !this.hideTitle ? teaser.getTitle() : null;
}

@Override
Expand Down
12 changes: 11 additions & 1 deletion core/src/test/java/nz/co/manawabay/core/models/TeaserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.day.cq.commons.jcr.JcrConstants;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import nz.co.manawabay.core.utils.ModelUtils;
import nz.co.manawabay.core.testcontext.AppAemContext;
import nz.co.manawabay.core.utils.ModelUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
Expand All @@ -25,6 +25,7 @@ class TeaserTest {
static final String CONF_ROOT = "/conf";
static final String PNG_IMAGE_BINARY_NAME = "Adobe_Systems_logo_and_wordmark.png";
static final String PNG_ASSET_PATH = "/content/dam/core/images/" + PNG_IMAGE_BINARY_NAME;
static final String VIDEO_BINARY_NAME = "test_video.mp4";
static final String CONTEXT_PATH = "/core";
static final String TEST_ROOT_PAGE = "/content/teasers";
static final String TEST_ROOT_PAGE_GRID = "/jcr:content/root/responsivegrid";
Expand All @@ -46,6 +47,7 @@ class TeaserTest {
static final String TEASER_13 = TEST_ROOT_PAGE + TEST_ROOT_PAGE_GRID + "/teaser-13";
static final String TEASER_14 = TEST_ROOT_PAGE + TEST_ROOT_PAGE_GRID + "/teaser-14";
static final String TEASER_15 = TEST_ROOT_PAGE + TEST_ROOT_PAGE_GRID + "/teaser-15";
static final String TEASER_16 = TEST_ROOT_PAGE + TEST_ROOT_PAGE_GRID + "/teaser-16";

final AemContext context = AppAemContext.newAemContext();

Expand All @@ -63,6 +65,8 @@ void internalSetup() {
context.load().json(testBase + AppAemContext.TEST_APPS_JSON, AppAemContext.TEST_APPS_ROOT);
context.load().json(testBase + AppAemContext.TEST_CONF_JSON, CONF_ROOT);
context.load().binaryFile("/image/" + PNG_IMAGE_BINARY_NAME, PNG_ASSET_PATH + "/jcr:content/renditions/original");
context.load().json(testBase + AppAemContext.TEST_CONTENT_DAM_VIDEO_JSON, "/content/dam/core/videos");
context.load().binaryFile("/video/" + VIDEO_BINARY_NAME, "/content/dam/core/videos/jcr:content/renditions/original");
}

@Test
Expand Down Expand Up @@ -239,6 +243,12 @@ void testTeaserWithLinkToAsset() {
assertEquals(CONTEXT_PATH + "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png", actions.get(0).getPath());
}

@Test
void testTeaserWithVideoAsset() {
Teaser teaser = getTeaserUnderTest(TEASER_16);
assertEquals("/content/dam/core/videos/sample_video.mp4.coredownload.mp4", teaser.getVideoUrl());
}

Teaser getTeaserUnderTest(String resourcePath, Object... properties) {
ModelUtils.enableDataLayer(context, true);
MockSlingHttpServletRequest request = context.request();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
*/
package nz.co.manawabay.core.testcontext;

import static com.adobe.cq.wcm.core.components.testing.mock.ContextPlugins.CORE_COMPONENTS;
import static org.apache.sling.testing.mock.caconfig.ContextPlugins.CACONFIG;

import org.apache.sling.testing.mock.sling.ResourceResolverType;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextBuilder;
import io.wcm.testing.mock.aem.junit5.AemContextCallback;
import org.apache.sling.testing.mock.sling.ResourceResolverType;

import static com.adobe.cq.wcm.core.components.testing.mock.ContextPlugins.CORE_COMPONENTS;
import static org.apache.sling.testing.mock.caconfig.ContextPlugins.CACONFIG;

/**
* Sets up {@link AemContext} for unit tests in this application.
Expand All @@ -33,6 +32,7 @@ public final class AppAemContext {
public static final String TEST_CONTENT_JSON = "/test-content.json";
public static final String TEST_TAGS_JSON = "/test-tags.json";
public static final String TEST_CONTENT_DAM_JSON = "/test-content-dam.json";
public static final String TEST_CONTENT_DAM_VIDEO_JSON = "/test-content-dam-video.json";
public static final String TEST_APPS_JSON = "/test-apps.json";
public static final String TEST_CONF_JSON = "/test-conf.json";

Expand Down
37 changes: 37 additions & 0 deletions core/src/test/resources/teaser/test-content-dam-video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"sample_video.mp4": {
"jcr:primaryType": "dam:Asset",
"jcr:mixinTypes": [
"mix:referenceable"
],
"jcr:content": {
"jcr:primaryType": "dam:AssetContent",
"dam:assetState": "processed",
"renditions": {
"jcr:primaryType": "nt:folder",
"original": {
"jcr:primaryType": "nt:file",
"jcr:content": {
"jcr:primaryType": "oak:Resource",
"jcr:mimeType": "video/mp4",
":jcr:data": 27795
}
}
}
},
"related": {
"jcr:primaryType": "nt:unstructured"
},
"metadata": {
"jcr:primaryType": "nt:unstructured",
"jcr:mixinTypes": [
"cq:Taggable"
],
"tiff:ImageLength": 2000,
"tiff:ImageWidth": 2000,
"dc:format": "video/mp4",
"dam:sha1": "1385f0808b7ef1600b6c39244917ef1df2ce6652",
"dam:size": 3114374
}
}
}
6 changes: 6 additions & 0 deletions core/src/test/resources/teaser/test-content.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@
"text": "CTA linking to an asset"
}
}
},
"teaser-16": {
"jcr:primaryType" : "nt:unstructured",
"jcr:title" : "Teaser",
"sling:resourceType" : "manawabay/components/teaser",
"videoFileReference": "/content/dam/core/videos/sample_video.mp4"
}
}
}
Expand Down
Binary file added core/src/test/resources/video/test_video.mp4
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,35 @@
jcr:title="Asset"
sling:resourceType="core/wcm/components/include/imagedelegate"
path="core/wcm/components/image/v3/image/cq:dialog/content/items/tabs/items/asset"/>
<video
jcr:primaryType="nt:unstructured"
jcr:title="Video"
sling:resourceType="granite/ui/components/coral/foundation/container"
margin="{Boolean}true">
<items jcr:primaryType="nt:unstructured">
<columns
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"
margin="{Boolean}true">
<items jcr:primaryType="nt:unstructured">
<column
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<file
jcr:primaryType="nt:unstructured"
sling:resourceType="cq/gui/components/authoring/dialog/fileupload"
allowUpload="false"
fieldLabel="Video"
fileReferenceParameter="./videoFileReference"
mimeTypes="[video/*]"
name="./videoFile"/>
</items>
</column>
</items>
</columns>
</items>
</video>
<styletab
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/include"
Expand Down
Loading

0 comments on commit 79d078d

Please sign in to comment.