Skip to content

Commit

Permalink
Add "properties" to CMSComponents for internal flags regarding component
Browse files Browse the repository at this point in the history
behaviour
  • Loading branch information
Florian Alpers committed Feb 6, 2025
1 parent 21e44f5 commit 193a719
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public CMSComponent createCMSComponent() {
}
JsfComponent jsfComponent = new JsfComponent(jsfLibraryPath, componentPath.getFileName().toString());
CMSComponent component = new CMSComponent(jsfComponent, this.name, this.description, ICONS_PATH + this.iconFileName,
this.templateFileName, scope, this.attributes, null);
this.templateFileName, scope, this.attributes, null, null);
for (CMSContentItemTemplate itemTemplate : contentItems) {
CMSContentItem item = itemTemplate.createCMSContentItem(component);
if (item != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
import java.util.Optional;
import java.util.stream.Collectors;

import jakarta.faces.component.UIComponent;
import jakarta.faces.component.html.HtmlPanelGroup;
import jakarta.faces.context.FacesContext;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -43,6 +39,9 @@
import io.goobi.viewer.model.jsf.DynamicContentBuilder;
import io.goobi.viewer.model.jsf.JsfComponent;
import io.goobi.viewer.model.security.user.User;
import jakarta.faces.component.UIComponent;
import jakarta.faces.component.html.HtmlPanelGroup;
import jakarta.faces.context.FacesContext;

public class CMSComponent implements Comparable<CMSComponent>, Serializable {

Expand All @@ -62,6 +61,8 @@ public class CMSComponent implements Comparable<CMSComponent>, Serializable {

private Integer order = null;

private final List<Property> properties;

private final Map<String, CMSComponentAttribute> attributes;

private final PersistentCMSComponent persistentComponent;
Expand All @@ -78,7 +79,8 @@ public class CMSComponent implements Comparable<CMSComponent>, Serializable {
*/
public CMSComponent(CMSComponent template, List<CMSContentItem> items) {
this(template.getJsfComponent(), template.getLabel(), template.getDescription(), template.getIconPath(), template.getTemplateFilename(),
template.getScope(), template.getAttributes(), template.getOrder(), Optional.ofNullable(template.getPersistentComponent()));
template.getScope(), template.getAttributes(), template.getProperties(), template.getOrder(),
Optional.ofNullable(template.getPersistentComponent()));
List<CMSContentItem> newItems = items.stream().map(CMSContentItem::new).toList();
this.contentItems.addAll(newItems);
}
Expand All @@ -93,6 +95,7 @@ public CMSComponent(CMSComponent template, Optional<PersistentCMSComponent> jpa)
template.getScope(),
CMSComponent.initializeAttributes(template.getAttributes(),
jpa.map(PersistentCMSComponent::getAttributes).orElse(Collections.emptyMap())),
template.getProperties(),
template.getOrder(),
jpa);
List<CMSContent> contentData = jpa.map(PersistentCMSComponent::getContentItems).orElse(Collections.emptyList());
Expand All @@ -113,8 +116,8 @@ public CMSComponent(CMSComponent template, Optional<PersistentCMSComponent> jpa)
* @param order
*/
public CMSComponent(JsfComponent jsfComponent, String label, String description, String iconPath, String templateFilename,
CMSComponentScope scope, Map<String, CMSComponentAttribute> attributes, Integer order) {
this(jsfComponent, label, description, iconPath, templateFilename, scope, attributes, order, Optional.empty());
CMSComponentScope scope, Map<String, CMSComponentAttribute> attributes, List<Property> properties, Integer order) {
this(jsfComponent, label, description, iconPath, templateFilename, scope, attributes, properties, order, Optional.empty());
}

/**
Expand All @@ -130,13 +133,15 @@ public CMSComponent(JsfComponent jsfComponent, String label, String description,
* @param jpa
*/
private CMSComponent(JsfComponent jsfComponent, String label, String description, String iconPath, String templateFilename,
CMSComponentScope scope, Map<String, CMSComponentAttribute> attributes, Integer order, Optional<PersistentCMSComponent> jpa) {
CMSComponentScope scope, Map<String, CMSComponentAttribute> attributes, List<Property> properties, Integer order,
Optional<PersistentCMSComponent> jpa) {
this.jsfComponent = jsfComponent;
this.label = label;
this.description = description;
this.iconPath = iconPath;
this.templateFilename = templateFilename;
this.attributes = attributes == null ? Collections.emptyMap() : attributes;
this.properties = properties == null ? Collections.emptyList() : properties;
this.scope = scope;
this.persistentComponent = jpa.orElse(null);
this.order = Optional.ofNullable(order).orElse(jpa.map(PersistentCMSComponent::getOrder).orElse(0));
Expand Down Expand Up @@ -543,6 +548,14 @@ public boolean isPaged() {
return this.contentItems.stream().map(CMSContentItem::getContent).anyMatch(PagedCMSContent.class::isInstance);
}

public List<Property> getProperties() {
return properties;
}

public boolean hasProperty(Property property) {
return this.properties.contains(property);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder(this.label == null ? "" : this.label);
Expand All @@ -555,4 +568,22 @@ public String toString() {
return sb.toString();
}

/**
* Additional properties that can be passed to the component to set certain behaviour
*/
public static enum Property {
/**
* Applicable to any search related content: Enforce faceting on page load
*/
FORCE_FACETING;

public static Property getProperty(String s) {
try {
return valueOf(s.toUpperCase());
} catch (IllegalArgumentException | NullPointerException e) {
return null;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -75,9 +75,15 @@ public CMSComponent read(Path templateFile) throws IOException, JDOMException {
attributes.put(attr.getName(), attr);
}

List<CMSComponent.Property> properties = XmlTools.evaluateToFirstString("properties", templateDoc.getRootElement(), null)
.map(s -> s.split("[;,\\s]+"))
.map(array -> Arrays.stream(array).map(CMSComponent.Property::getProperty).filter(p -> p != null).toList())
.orElse(Collections.emptyList());

String filename = FilenameUtils.getBaseName(templateFile.getFileName().toString());
CMSComponent component =
new CMSComponent(new JsfComponent(jsfComponentLibrary, jsfComponentName), label, desc, icon, filename, scope, attributes, null);
new CMSComponent(new JsfComponent(jsfComponentLibrary, jsfComponentName), label, desc, icon, filename, scope, attributes, properties,
null);

List<Element> contentElements = XmlTools.evaluateToElements("content/item", templateDoc.getRootElement(), null);

Expand Down Expand Up @@ -110,15 +116,6 @@ public CMSComponent read(Path templateFile) throws IOException, JDOMException {

}

private Option createOption(Element element) {
String label = element.getAttributeValue("label");
String value = element.getText();
if (StringUtils.isBlank(label)) {
label = value;
}
return new Option(value, label);
}

private static CMSContent createContentFromClassName(String className)
throws InstantiationException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.goobi.viewer.model.cms.itemfunctionality.SearchFunctionality;
import io.goobi.viewer.model.cms.pages.CMSPage;
import io.goobi.viewer.model.cms.pages.content.CMSComponent;
import io.goobi.viewer.model.cms.pages.content.CMSComponent.Property;
import io.goobi.viewer.model.cms.pages.content.CMSContent;
import io.goobi.viewer.model.cms.pages.content.PagedCMSContent;
import io.goobi.viewer.model.cms.widgets.embed.CMSSidebarElement;
Expand Down Expand Up @@ -314,6 +315,9 @@ public Search createSearch(List<SearchResultGroup> resultGroups, CMSComponent co
}

private boolean isUseFacetting(CMSComponent component) {
if (component.hasProperty(Property.FORCE_FACETING)) {
return true;
}
List<CMSSidebarElement> widgets =
Optional.ofNullable(component).map(CMSComponent::getOwningPage).map(CMSPage::getSidebarElements).orElse(Collections.emptyList());
for (CMSSidebarElement element : widgets) {
Expand Down

0 comments on commit 193a719

Please sign in to comment.