
com.adobe.aemds.guide.common.GuideField Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2014 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.aemds.guide.common;
import com.adobe.aemds.guide.utils.GuideConstants;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.day.cq.wcm.foundation.forms.FormsHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import javax.jcr.RepositoryException;
import java.util.Map;
/**
* GuideField is a base class from which every AEM form field component inherits from.
* It abstracts the basic properties of a field. Few are:
*
* - Hiding the title of the field
*
- Short Description of the field
*
- Long description
*
* This provides basic getter's and setter's required to create/initialize an AEM Form Field Component.
*
* @since AEM 6.0
*/
abstract public class GuideField extends GuideNode{
protected String helpQuestionMarkContent = "";
private String displayNoneStyle = ";display:none";
public Boolean getIsRequired(){
return FormsHelper.isRequired(super.getResource());
}
public Boolean getIsReadOnly(){
return FormsHelper.isReadOnly(slingRequest, super.getResource());
}
/**
* Returns id for field label association, for accessibility
* @return String Id for field label association
* @throws RepositoryException
*/
public String getLabelForId() throws RepositoryException {
return getId() + "_widget";
}
/**
* Returns the value of autofill field keyword
* @return String representing autofill field keyword value
*/
public String getAutofillFieldKeyword() {
return resourceProps.get("autofillFieldKeyword", "off");
}
/**
* Returns the value of autocomplete
* @return Boolean representing autocomplete value
*/
public Boolean getAutocomplete() {
return resourceProps.get("autocomplete", false);
}
public boolean getIsOldFieldLayout(){
String fieldLayout = resourceProps.get("fieldLayout", "");
// If there is old field renderer set(usually set when Inherit from Adaptive Form Configuration option is selected)
// then we still need to use path, resource type rendition, done to solve CQ-47888
if(fieldLayout != null && fieldLayout.length() > 0 && !fieldLayout.equals(GuideConstants.DEFAULT_OLD_FIELD_RENDERER)){
return checkIfPathIsAbsolute(fieldLayout);
}
// if no layout is present in the property, then it means we need to use the 6.1 CF1 approach(resource type)
return false;
}
/***
* Returns the field layout configured during authoring.
* If field layout is specified at field level, it takes priority, else field layout
* configured in adaptive form container is honored.
*
* @return path of the field layout
*/
public String getFieldLayout(){
String fieldLayout = resourceProps.get("fieldLayout", "");
// for fields the default layout should result in field taking up the layout of its parent
if(fieldLayout != null && fieldLayout.length() > 0 && fieldLayout.indexOf(GuideConstants.DEFAULT_FIELD_LAYOUT_NAME)<0) {
return fieldLayout;
} else {
// Get the guide container
Resource guideContainerResource = getResource().getResourceResolver().getResource(GuideUtils.getGuideContainerPath(slingRequest, getResource()));
return GuideUtils.getFieldLayout(guideContainerResource, "fieldLayout", GuideConstants.DEFAULT_FIELD_RENDERER);
}
}
/***
* Returns the name of the field layout
* @return name of the field layout configured
*/
public String getFieldLayoutName(){
// get the name of the field layout from the component name selected
String fieldLayout = this.getFieldLayout(),
fieldLayoutName = GuideConstants.DEFAULT_FIELD_LAYOUT_NAME;
// field layout would always exist, still adding null check
if(fieldLayout != null && fieldLayout.length() > 0){
// this will also have extension in it(eg .jsp)
fieldLayoutName = StringUtils.substringAfterLast(fieldLayout, "/");
// lets remove the extension from the file name too
// we always expect the name of the jsp file to be same as the sling folder name
fieldLayoutName = StringUtils.substringBeforeLast(fieldLayoutName, ".jsp");
}
return fieldLayoutName;
}
/**
* Returns if the current field's title is hidden or not
* @return boolean indicating if title is hidden or not
*/
public boolean getHideTitle(){
return resourceProps.get("hideTitle", false);
}
/**
* Returns the short description of the field configured in the authoring dialog
* @return string representing the short description of the field
*/
public String getShortDescription() {
return externalize(resourceProps.get("shortDescription", ""));
}
/**
* Returns the long description of the field configured in authoring dialog
* @return String representing the long description
*/
public String getLongDescription() {
return externalize(resourceProps.get("longDescription", ""));
}
/**
* Returns the visibility of the short description as specified in the Authoring Dialog
* @return true if "Always show short Description" is selected otherwise false
*/
public Boolean getDescriptionVisibility() {
return resourceProps.get("shortVisible", false);
}
/**
* Returns the initial value of the field set in the authoring dialog
* @return String representing the default value
*/
public String getValue(){
return resourceProps.get("_value", String.class);
}
/**
* Returns the string containing the inline style specified for the caption
* @return inline style of the caption
*/
public String getCaptionInlineStyles() {
if (this.getHideTitle()) {
return getInlineStyles(GuideConstants.GUIDE_STYLE_CAPTION) + displayNoneStyle;
} else {
return getInlineStyles(GuideConstants.GUIDE_STYLE_CAPTION);
}
}
/**
* Returns the string containing the inline style specified for the widget
* @return inline style of the widget
*/
public String getWidgetInlineStyles() {
return getInlineStyles(GuideConstants.GUIDE_STYLE_WIDGET);
}
/**
* Sets the help indicator content for the field. By default the indicator is represented as a "?", but can be
* changed through this API.
* @param helpQuestionMarkContent string representing the help question mark content
*/
public void setHelpIndicatorContent(String helpQuestionMarkContent){
this.helpQuestionMarkContent = ((helpQuestionMarkContent != null) ? helpQuestionMarkContent : "");
}
/**
* Returns the help content for field
* @return question mark content of the help
*/
public String getHelpIndicatorContent(){
return this.helpQuestionMarkContent;
}
/**
* Returns the string containing the inline style specified for the short description
* @return inline style of the short description
*/
public String getShortDescriptionInlineStyles() {
if (this.getDescriptionVisibility()) {
return getInlineStyles(GuideConstants.GUIDE_STYLE_SHORTDESCRIPTION);
} else {
return getInlineStyles(GuideConstants.GUIDE_STYLE_SHORTDESCRIPTION) + displayNoneStyle;
}
}
/***
* Returns the placeholder text set in the authoring dialog
* @return place holder text set for the field
*/
public String getPlaceholderText() {
return(resourceProps.get("placeholderText",""));
}
/**
* Returns the error simulation string to be shown in Authoring for styling purposes
* @return error simulation string for a given field
*/
public String getErrorSimulatorString () {
String mandatoryMessageAttribute = null;
if(isMandatory()) {
String mandatoryMessage = resourceProps.get("mandatoryMessage", String.class);
if(mandatoryMessage != null && !mandatoryMessage.isEmpty()){
mandatoryMessageAttribute=mandatoryMessage;
} else {
mandatoryMessageAttribute="There is an error in this field !";
}
} else {
String validateExpMessage = resourceProps.get("validateExpMessage", String.class);
if(validateExpMessage != null && !validateExpMessage.isEmpty()){
mandatoryMessageAttribute=validateExpMessage;
}
}
if(mandatoryMessageAttribute != null){
String validatePictureClauseMessage = resourceProps.get("validatePictureClauseMessage", String.class);
if(validatePictureClauseMessage != null){
mandatoryMessageAttribute=validatePictureClauseMessage;
}
}
return mandatoryMessageAttribute;
}
/**
* Return if the field is marked as required.
* @return true if field is configured as mandatory, false otherwise
*/
public boolean isMandatory () {
return resourceProps.get("mandatory", false);
}
/**
* Gives the authoring configuration of the guide field component
* @return authoring config of guide field component
* @pad.exclude Exclude from Published API.
*/
@Override
public Map getAuthoringConfig(){
Map authoringConfig = super.getAuthoringConfig();
Boolean hideTitle = getHideTitle();
if(hideTitle){
authoringConfig.put(GuideConstants.GUIDE_FIELD_HIDE_TITLE,hideTitle);
}
GuideUtils.setMasterAuthoringConfig(authoringConfig, resourceProps);
return authoringConfig;
}
}