All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.aemds.guide.common.GuideValidationResult Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2015 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.GuideSubmitErrorCause;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;


/**
 * Class representing the result of server side validation performed for an adaptive form. This class provides APIs
 * to access the list of errors in the submitted adaptive form
 *
 * @since AEM 6.1
 */
public class GuideValidationResult {
    private GuideSubmitErrorCause errorCausedBy;
    //Marking it private. This should be set/get via respective setter/getter
    private List guideErrorList;

    private static Logger logger = LoggerFactory.getLogger(GuideValidationResult.class);
    /**
     * @pad.exclude
     */
    public GuideValidationResult(){

    }
    /**
     *
     * @param guideErrorList
     * @pad.exclude
     * @deprecated from 6.3 use GuideValidationResult(List guideErrorList, GuideSubmitErrorCause errorCausedBy) instead.
     */
    public GuideValidationResult(List guideErrorList) {
        this(guideErrorList , GuideSubmitErrorCause.SERVER_SIDE_VALIDATION);
    }
    /**
     * @param guideErrorList
     * @pad.exclude
     */
    public GuideValidationResult(List guideErrorList, GuideSubmitErrorCause errorCausedBy) {
        this.guideErrorList = guideErrorList;
        this.errorCausedBy = errorCausedBy;
    }

    /**
     * Returns the list of error messages in the server side validation
     * @return List of error messages
     */
    public List getGuideErrorList() {
        return guideErrorList;
    }

    /**
    * This setter should be used when errors occurred due to server side validation.
    * @deprecated from 6.3 use setGuideErrorList(List guideErrorList, GuideSubmitErrorCause errorCausedBy) instead.
    * */
    public void setGuideErrorList(List guideErrorList) {
        setGuideErrorList(guideErrorList , GuideSubmitErrorCause.SERVER_SIDE_VALIDATION);
    }

    /**
     * This sets List of error and the cause of error
     * @pad.exclude
     * @param guideErrorList
     * @param errorCausedBy
     */
    public void setGuideErrorList(List guideErrorList, GuideSubmitErrorCause errorCausedBy) {
        this.guideErrorList = guideErrorList;
        this.errorCausedBy = errorCausedBy;
    }

    /**
     * @pad.exclude
     * @return The cause of error.
     */
    public GuideSubmitErrorCause getErrorCause(){
        return this.errorCausedBy;
    }

    /**
     * Identify if there are any server side validation errors
     * @return true if the list contains error, false otherwise.
     */
    public boolean hasErrors() {
        return (guideErrorList != null && guideErrorList.size() > 0);
    }

    /**
     * @pad.exclude
     * Generates error Json object which will be read by client.
     */
    public JSONObject getValidationPayload(){
        return getErrorJsonObject();
    }

    private JSONObject getErrorJsonObject() {
        //TODO generate this Json using Gson or Jackson.
        JSONObject errorJson = new JSONObject();
        try {
            errorJson.put(GuideConstants.KEY_ERROR_CAUSED_BY, this.errorCausedBy);
            errorJson.put(GuideConstants.KEY_ERRORS, getErrors(this.guideErrorList));
        } catch (JSONException e) {
            logger.error("Error while writing JSON.", e);
        }
        return errorJson;
    }

    /**
     * Converts List of GuideErrors into JSONArray format.
     * @param errorList
     *
     * @return {@link org.apache.sling.commons.json.JSONArray}, Array of GuideError in JSONObject form. SOMExp would be the key and Error Message would be the value.
     */
    private JSONArray getErrors(List errorList){
        JSONArray errors = null;
        if(errorList != null){
            errors = new JSONArray();
            for(GuideError guideError : errorList) {
                JSONObject object = new JSONObject();
                try {
                    object.putOpt(GuideConstants.KEY_SOM_EXPRESSION , guideError.getSomExpression());
                    object.putOpt(GuideConstants.KEY_ERROR_MESSAGE , guideError.getErrorMessage());
                } catch (JSONException e) {
                    logger.error("Error while converting into JSON.", e);
                }
                errors.put(object);
            }
        }
        return  errors;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy