Sesat > Docs + Support > Architecture Overview > Design Proposals > New design proposal for SearchCommand and AbstractSearchCommand > SearchCommandParameter code example

EXAMPLE implementation of a more dynamic Parameter handling for SearchCommands.

interface SearchCommandParameter{

        enum PARAMETER_ORIGIN {REQUEST,USER,CONFIGURATION};

        enum PARAMETER_TYPE {PLAIN,NAVIGATION_MAP,TAB};

        String getName();

        boolean isActiveParameter();

        PARAMETER_ORIGIN getParametersOrigin();

        String getParameter();
}

class BaseSearchCommandParameter implements SearchCommandParameter{

        private final String name;
        private final PARAMETER_ORIGIN[] lookupOrder;
        private PARAMETER_ORIGIN origin = null;

        public BaseSearchCommandParameter(
                final String name,
                final PARAMETER_ORIGIN[] lookupOrder){

            this.name = name;
            this.lookupOrder = Arrays.copyOf(lookupOrder, 0);
        }

        public String getName(){
            return name;
        }

        public boolean isActiveParameter() {
            return true;
        }

        public String getParameter() {

            String result = null;
            if(isActiveParameter()){

                for(PARAMETER_ORIGIN origin: lookupOrder){

                    if(null != this.origin){
                        // shortcut to the origin found last time.
                        origin = this.origin;
                    }
                    switch(origin){

                        case REQUEST:
                            final StringDataObject sdo = context.getDataModel().getParameters().getValue(name);

                            if(null != sdo){
                                result = sdo.getString();
                            }
                            break;
                        
                        case USER:
                            result = context.getUser().getUser().getUserPropertiesMap().get(name);
                            break;

                        case CONFIGURATION:
                            final PropertyDescriptor[] properties = Introspector.getBeanInfo(getSearchConfiguration().getClass()).getPropertyDescriptors();
                            for(PropertyDescriptor property : properties){
                                if (name.equals(property.getName()){

                                    if( null != property.getReadMethod() ){
                                        result = (String)invoke(property.getReadMethod(), getSearchConfiguration(), new Object[0]);
                                        break;
                                    }
                                }
                            }
                    }
                    if(null != result){
                        this.origin = origin;
                        break;
                    }
                }
            }
            return result;

        }

        public PARAMETER_ORIGIN getParametersOrigin() {

            if(null == origin){
                getParameter();
            }
            return origin;
        }

}

class NavigationSearchCommandParameter extends BaseSearchCommandParameter{

        private final String navigationMapKey;

        public NavigationSearchCommandParameter(
                final String name,
                final String navigationMapKey,
                final PARAMETER_ORIGIN[] lookupOrder){

            super(name, lookupOrder);
            this.navigationMapKey = navigationMapKey;
        }

        @Override
        public boolean isActiveParameter() {

            final boolean navMapExists = null != context.getDataModel().getNavigation()
                    && null != context.getDataModel().getNavigation().getConfiguration();

            final Nav nav = navMapExists
                    ? context.getDataModel().getNavigation().getConfiguration().getNavMap().get(navigationMapKey)
                    : null;

            return null != nav && getSearchConfiguration().getId().equals(nav.getCommandName());
        }


}
 © 2007-2009 Schibsted ASA
Contact us