1
\$\begingroup\$

Can someone help me to create a generic method that validates common fields/variables comes from multiple objects, if that is possible?

The below code validates some bunch of variables/fields from a schema object, so here the requirement is I have two different schema objects which has common Fields/variables, Is it possible to validate these variables in more generic way. In below code there is two events FI & Profile (Note: Future can be more events like these) these are two different schema objects but has same fields/variables of same Object "Types".

public class RequestValidation {

    public void validateRequest(EventRequest eventRequest) {
            //FI Event


            FIEventProcessorImpl fiEventProcessor = new FIEventProcessorImpl();
            FIEventSchema fiEvent = fiEventProcessor.getFiEventSchema(eventRequest);

            //Party data Validation
            String partyFName = null;
            String partyLName = null;
            String partyEmail = null;
            String partyId = null;
            if (null != fiEvent.getParty()) {
                partyId = fiEvent.getParty().getPartyId();
                if (null != fiEvent.getParty().getPartyName()
                        && null != fiEvent.getParty().getPartyName().getName()) {
                    partyFName = fiEvent.getParty().getPartyName().getName().getGivenName();
                    partyLName = fiEvent.getParty().getPartyName().getName().getSurname();
                    if (null == partyFName && null == partyLName) {
                        Map<String, Object> nameMap = fiEvent.getParty().getPartyName().getName()
                                .getAdditionalProperties();
                        if (!nameMap.isEmpty()) {
                            partyFName = getAdditionalProperty(nameMap, "first_name");
                            partyLName = getAdditionalProperty(nameMap, "last_name");
                        }
                    }
                }
                if (null != fiEvent.getParty().getPartyEmail()) {
                    partyEmail = fiEvent.getParty().getPartyEmail().getEmailAddress();
                    if(null == partyEmail || partyEmail.isEmpty()){
                        //Logging missing field value logic
                    }
                }
            }
            if (null == partyFName || partyFName.isEmpty() || null == partyLName || partyLName.isEmpty()
                    || null == partyId || partyId.isEmpty()) {
                //Logging missing field value logic
            }

            //Application Context data Validation
            String score = null;
            String clientId = null;
            if(null != fiEvent.getApplicationContext() && null != fiEvent.getApplicationContext().getContextItems()){
                List<ContextItem> contextItems = fiEvent.getApplicationContext().getContextItems();
                if(null != contextItems && !contextItems.isEmpty()){
                    socialScore = getContextValue(contextItems, "SCORE");
                    clientFp = getContextValue(contextItems, "CLIENT_ID");
                }
            }
            if (null == score || score.isEmpty() || null == clientId || clientId.isEmpty()) {
                //Logging missing field value logic
            }


            //ProfileChange Event
            ProfileProcessorImpl eventProcessor = new ProfileProcessorImpl();
            ProfileEventSchema profileevent = eventProcessor.getProfileEventSchema(eventRequest);

            //Party data Validation
            if (null != profileevent.getParty()) {
                partyId = profileevent.getParty().getPartyId();
                if (null != profileevent.getParty().getPartyName()
                        && null != profileevent.getParty().getPartyName().getName()) {
                    partyFName = profileevent.getParty().getPartyName().getName().getGivenName();
                    partyLName = profileevent.getParty().getPartyName().getName().getSurname();
                    if (null == partyFName && null == partyLName) {
                        Map<String, Object> nameMap = profileevent.getParty().getPartyName().getName()
                                .getAdditionalProperties();
                        if (!nameMap.isEmpty()) {
                            partyFName = getAdditionalProperty(nameMap, "first_name");
                            partyLName = getAdditionalProperty(nameMap, "last_name");
                        }
                    }
                }
                if (null != profileevent.getParty().getPartyEmail()) {
                    partyEmail = profileevent.getParty().getPartyEmail().getEmailAddress();
                    if(null == partyEmail || partyEmail.isEmpty()){
                    //Logging missing field value logic
                    }
                }
            }
            if (null == partyFName || partyFName.isEmpty() || null == partyLName || partyLName.isEmpty()
                    || null == partyId || partyId.isEmpty()) {
                //Logging missing field value logic
            }

            //Application Context data Validation
            if(null != profileevent.getApplicationContext() && null != profileevent.getApplicationContext().getContextItems()){
                List<ContextItem> contextItems = profileevent.getApplicationContext().getContextItems();
                if(null != contextItems && !contextItems.isEmpty()){
                    socialScore = getContextValue(contextItems, "SCORE");
                    clientFp = getContextValue(contextItems, "CLIENT_ID");
                }
            }
            if (null == score || score.isEmpty() || null == clientId || clientId.isEmpty()) {
                //Logging missing field value logic
            }

    }


    private String getContextValue(List<ContextItem> contextItems, String key) {
        String value = null;
        for(ContextItem contextItem : contextItems){
            if(contextItem.getKey().equalsIgnoreCase(key)){
                value = contextItem.getValue();
            }
        }
        return value.toString();
    }

    private String getAdditionalProperty(Map<String, ?> map, String key) {
        Object value = map.get(key);
        return value == null ? null : value.toString();
    }
}
\$\endgroup\$
0
\$\begingroup\$

It looks like both of the events are validating the same party and context objects. If those are the same type you could make a method to validate the profile, and a method to validate the context object. If the objects are different but you have control over the classes you could make both party objects implement an interface with getters for all of the fields being retrieved. If the objects being retrieved are different you can continue with the same pattern. In my opinion this method would be much easier to read if it was broken up to smaller methods that validate one thing, such as the name of the party.

\$\endgroup\$
  • \$\begingroup\$ Thank you for the suggestion, this helps! \$\endgroup\$ – Krishna K Apr 12 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.