Skip to content

Instantly share code, notes, and snippets.

@odaBio
Created October 23, 2021 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odaBio/3cad00f7b5014b69abbe0b5aa1501053 to your computer and use it in GitHub Desktop.
Save odaBio/3cad00f7b5014b69abbe0b5aa1501053 to your computer and use it in GitHub Desktop.
package com.biomerieux.adobe.core.models.impl;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.Self;
import javax.annotation.PostConstruct;
import com.biomerieux.adobe.core.models.ImageView;
import com.adobe.cq.wcm.core.components.models.Image;
@Model(
adaptables = {SlingHttpServletRequest.class},
adapters = {ImageView.class},
resourceType = {ImageViewImpl.RESOURCE_TYPE},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class ImageViewImpl implements ImageView {
protected static final String RESOURCE_TYPE = "onebmx/components/content/imageview";
@OSGiService
private ModelFactory modelFactory;
@Self
private SlingHttpServletRequest request;
@ValueMapValue
private String title;
@ValueMapValue
private String url;
private Image image;
@PostConstruct
private void init() {
image = modelFactory.getModelFromWrappedRequest(request,
request.getResource(),
Image.class);
}
@Override
public String getTitle() {
return title;
}
@Override
public String getUrl() {
return url;
}
/**
* @return the Image Sling Model of this resource, or null if the resource cannot create a valid Image Sling Model.
*/
private Image getImage() {
return image;
}
@Override
public boolean isEmpty() {
final Image componentImage = getImage();
if (StringUtils.isBlank(title)) {
// Name is missing, but required
return true;
} else if (StringUtils.isBlank(url)) {
// At least one occupation is required
return true;
} else if (componentImage == null || StringUtils.isBlank(componentImage.getSrc())) {
// A valid image is required
return true;
} else {
// Everything is populated, so this component is not considered empty
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment