Skip to content

Instantly share code, notes, and snippets.

@kuckmc01
Last active May 19, 2020 14:35
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 kuckmc01/6d61d050ce46b147dba7aef62acd4286 to your computer and use it in GitHub Desktop.
Save kuckmc01/6d61d050ce46b147dba7aef62acd4286 to your computer and use it in GitHub Desktop.
package datasources.staticstyles.v1;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.designer.Designer;
import org.apache.commons.collections.iterators.TransformIterator;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.StreamSupport;
/**
* This datasource sling model provides style classes for static templates as static templates are not compatible with the ootb style system
*/
@Model(
adaptables = {SlingHttpServletRequest.class},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class StaticStylesDatasourceModel {
private static final Logger log = LoggerFactory.getLogger(StaticStylesDatasourceModel.class);
public static final String NN_STATIC_STYLES = "staticStyles";
public static final String PN_STATIC_STYLE_LABEL = "staticStyleLabel";
public static final String PN_STATIC_STYLE_CLASS = "staticStyleClass";
public static final String VALUE = "value";
public static final String TEXT = "text";
@Self
private SlingHttpServletRequest request;
@PostConstruct
public void init() {
final Map<String, String> stylesMap = new LinkedHashMap<>();
try{
Resource suffixResource = request.getRequestPathInfo().getSuffixResource();
if(suffixResource != null){
//This is getting the values of the static style system through the ootb Design API
final Designer designer = request.getResourceResolver().adaptTo(Designer.class);
Optional.ofNullable(designer)
.map(designer1 -> designer.getStyle(suffixResource))
.map(style -> request.getResourceResolver().getResource(style.getPath()))
.map(designResource -> designResource.getChild(NN_STATIC_STYLES))
.map(Resource::getChildren)
.map(iterable -> StreamSupport.stream(iterable.spliterator(), false))
.ifPresent(s -> {
s.map(Resource::getValueMap).forEach(
valueMap -> {
stylesMap.put(
valueMap.get(PN_STATIC_STYLE_LABEL, String.class),
valueMap.get(PN_STATIC_STYLE_CLASS, String.class));
}
);
});
}
}catch (Exception e){
log.error("Problem with getting the datasource for the static styles tab", e);
}
if(!stylesMap.isEmpty()){
DataSource dataSource = new SimpleDataSource(new TransformIterator(stylesMap.keySet().iterator(), transformer -> {
final String key = (String) transformer;
ValueMap valueMap = new ValueMapDecorator(new HashMap<>());
valueMap.put(TEXT, key);
valueMap.put(VALUE, stylesMap.get(key));
return new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), JcrConstants.NT_UNSTRUCTURED, valueMap);
}));
request.setAttribute(DataSource.class.getName(), dataSource);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment