Custom facets per request

Custom facets per request

It is possible to adapt facets to a particular service call (GET) by passing the facetsParamsparameter in the request. This parameter can be retrieved from the parameter map available in the facet processing methods :

  • void modify(IObjectReadOnly object, CTSurfer surfer, EditableObjectFace defaultFace, Map<String, String> params) of com.noheto.extensions.interfaces.services.AbstractObjectFaceService

  • Collection<String> getFacesNames(IObjectReadOnly object, CTSurfer surfer, Map<String, String> params).

simple mode

In this mode, you simply pass an ID. In the map, there is just a key with null mapped to it.

Example of a parameter in the URI:

facetsParams=foo

Example of testing the variable in the map :

if ( params.containsKey("foo") ) { // customs facets }

advanced syntax

Use a JSON object, by example

{foo: 1, bar: xxx}

In this case, each key/value will be added to the map

Example of a parameter in the URI:

facetsParams=%7Bfoo%3A%201%2C%20bar%3A%20xxx%7D

Example of testing the variable in the map:

if ( params.containsKey("foo") ) { // customs facets for foo String value = params.get("foo"); // use value } if ( params.containsKey("bar") ) { // customs facets for bar String value = params.get("bar"); // use value }

Multiple values

Multiple values are supported.

By example facetsParams=baz&facetsParams=%7Btest%3A%20true%2C%20foo%3A%20xxx%2C%20bar%3A%20null%7D is equivalent to pass the following JSON:

{ test: true, foo: xxx, bar: null, baz: null }

Example of implémentation - Case of imgly declination

The product uses this setting for Digital Templates.

see com.wedia.packaged.digitaltemplates.extensions.services.ImglyDeclinationObjectFaceService in the plugin PACKAGED_Digital_Templates

This class customizes facets for imgly template declinations based on the presence of imgly_declination.

public class ImglyDeclinationObjectFaceService extends AbstractObjectFaceService { private static final Logger LOGGER = LoggingService.getLogger(LOGGER_NAME); public static final String IMGLY_DECLINATION_FACETS_PARAMETER_NAME = "imgly_declination"; private static final String LANGS_FIELD_NAME = "langs"; public static final Set<String> EDITABLE_FIELD_SET = Set.of(LANGS_FIELD_NAME); /** * Determines which object types trigger this facet. * * <p>Returns true if structure has tag damobject</p> */ @Override public boolean applyTo(IObjectStructureReadOnly type) { return isDamObject(type); } private boolean isDamObject(IObjectStructureReadOnly type) { try { IObjectTableReadOnly table = CTObjectTable.load(type.getObjectType().toString()); return table.hasTag("damobject"); } catch (Throwable e) { LOGGER.warn("Can't load table for " + type, e); } return false; } /** * Modifies facets for the specified instance, in the cas of parameters contains the imgly declination creation parameter. * * <ul> * <li>set all fields non editable</li> * <li>makes closed list fields editable</li> * </ul> */ @Override public void modify(IObjectReadOnly instance, CTSurfer surfer, EditableObjectFace facets, Map<String, String> parameters) { if ( imglyDeclinationCreation(parameters) ) { final boolean logDebug = LOGGER.isDebugEnabled(); if ( logDebug ) { LOGGER.debug("IMGLY DECLINATION DEFAULT FACETS - instance: " + instance); } Map<Integer, ? extends EditablePropertyFace> facetProperties = facets.getFaces(); Set<String> editableFields = logDebug ? new HashSet<>(EDITABLE_FIELD_SET) : Collections.emptySet(); IObjectStructureReadOnly structure = instance.getCTObjectStructure(); for(Map.Entry<Integer, ? extends EditablePropertyFace> facetProperty : facetProperties.entrySet()) { // fixme: what about already mandatory fields? IObjectField field = structure.getField(facetProperty.getKey()); String fieldname = field.getName().toString(); boolean fieldIsEditable = EDITABLE_FIELD_SET.contains(fieldname); facetProperty.getValue().setEditable(fieldIsEditable); if (LOGGER.isTraceEnabled()) { LOGGER.trace("IMGLY DECLINATION DEFAULT FACETS - field: " + field + " editable: " + fieldIsEditable); } editableFields.remove(fieldname); } if ( logDebug ) { LOGGER.debug("IMGLY DECLINATION DEFAULT FACETS - missing field(s): " + editableFields); } } } private boolean imglyDeclinationCreation(Map<String, String> parameters) { return parameters!=null && parameters.containsKey(IMGLY_DECLINATION_FACETS_PARAMETER_NAME); } }

See also in the same package, the ImglyTemplateObjectFaceService class.

This class customizes facets for imgly template based on the presence of imgly_template.