Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

  • Blama
    replied
    Hi all,

    this is my code, if somebody needs it:
    Code:
    import java.util.Arrays;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    import com.isomorphic.criteria.DefaultOperators;
    import com.isomorphic.criteria.criterion.SimpleCriterion;
    import com.isomorphic.datasource.DSRequest;
    import com.isomorphic.datasource.DSResponse;
    import com.isomorphic.datasource.DataSource;
    import com.isomorphic.datasource.Validator;
    import com.isomorphic.log.Logger;
    import com.lmscompany.lms.server.I18n;
    import com.lmscompany.lms.shared.type.DatasourceFieldEnum;
    
    /**
     * Replacement for the framework's hadRelatedRecord validator, as this does not support a operationId attribute and therefore will use a slow
     * fetch.<br>
     * See <a href=
     * "https://forums.smartclient.com/forum/smart-gwt-technical-q-a/258391-enhancement-hasrelatedrecord-validator-support-for-the-operationid-attribute-and-general-type-servercustom-questions">this
     * thread</a>.
     * 
     * @author ST
     */
    public class ValidatorHasRelatedRecord {
        Logger log = new Logger(ValidatorHasRelatedRecord.class.getName());
    
        public boolean condition(Object value, Validator validator, String fieldName, Map<Object, Object> record, DataSource ds,
                HttpServletRequest httpServletRequest) throws Exception {
    
            log.info("Validating for field \"" + fieldName + "\", value \"" + (value == null ? "" : value.toString()) + "\"");
    
            if (validator.getProperty("dataSource") == null || validator.getProperty("dataSource").toString().isEmpty()) {
                throw new Exception("Validator must define a \"dataSource\" attribute.");
            }
            String dataSource = validator.getProperty("dataSource").toString();
    
            if (value == null)
                return true;
    
            DSRequest pkRequest = new DSRequest(dataSource, DataSource.OP_FETCH);
            pkRequest.addToCriteria(new SimpleCriterion(DatasourceFieldEnum.GENERAL__ID.getValue(), DefaultOperators.Equals, value));
    
            // Important to reduce joins and SELECTed fields.
            pkRequest.setOutputs(Arrays.asList(DatasourceFieldEnum.GENERAL__ID.getValue()));
    
            DSResponse pkResponse = pkRequest.execute();
            if (pkResponse.getTotalRows() == 0) {
                validator.addErrorMessageVariable("errorMessage", String.format(I18n.getString("lookupValueNotFound", httpServletRequest), value));
                return false;
            } else if (pkResponse.getTotalRows() >= 2) {
                validator.addErrorMessageVariable("errorMessage",
                        String.format(I18n.getString("tooManyLookupValuesFound", httpServletRequest), value));
                return false;
            }
            return true;
        }
    }
    Best regards
    Blama

    Leave a comment:


  • Blama
    replied
    Hi Isomorphic,

    it seems to work as expected. Amazing.
    W.r.t the docs amendment: Validator is already there in the docs above the list as mandatory attribute. The one in the list are optional attributes, so this is already documented.

    While I still think the operationId-attribute for the hasRelatedRecord-validator is a good idea, this now has a very low priority for me.

    Best regards
    Blama

    Leave a comment:


  • Blama
    replied
    Hi Isomorphic,

    this seems to exist already?!
    Looking at existing code I saw this signature:
    Code:
        public boolean condition(Object value, Validator validator, String fieldName, Map<Object, Object> record, DataSource ds,
                HttpServletRequest httpServletRequest) throws Exception
    While the general serverObject-docs don't list the Validator from above code (can you add it here?), it is available and has the Property-getters I was looking for.
    If this is working as expected my enhancement suggestion here gets minor for me.

    Best regards
    Blama

    Leave a comment:


  • Enhancement: hasRelatedRecord-validator support for the operationId-attribute and general type:serverCustom questions

    Hi Isomorphic,

    as written here support for the operationId-attribute for the hasRelatedRecord-validator and not only isUnique would really help me.
    If this is not possible, I think I could somehow build this myself. Can you make all the attributes from the validator available somewhere in Java code, perhaps in ValidationContext? Or is it already there?
    Then I could use ValidatorType.SERVERCUSTOM and build the validation myself.

    Of course I could somehow parse the .ds.xml myself and get the important parts via XPath, but I do think this would make sense as general feature.

    Best regards
    Blama
Working...
X