Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
SIX OVAL - http://code.google.com/p/six-oval/ Copyright (C) 2010 National Institute of Advanced Industrial Science and Technology (AIST) Registration Number: H22PRO-1124 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package jp.go.aist.six.oval.core.repository.morphia;
 
 import java.util.List;
 import java.util.Map;
A query builder for OVAL repository.

Author(s):
Akihito Nakamura, AIST
Version:
$Id: OvalQueryBuilder.java 3114 2013-04-30 02:25:21Z nakamura5akihito@gmail.com $
 
 public class OvalQueryBuilder
     extends CommonBuilder
     implements QueryBuilderFactory
 {
 
 //    /**
 //     * Logger.
 //     */
 //    private static final Logger  _LOG_ = LoggerFactory.getLogger( OvalQueryBuilder.class );
 


    
Constructor.
 
     protected OvalQueryBuilder()
     {
     }
 
 
 
     //*********************************************************************
     //  implements QueryBuilderFactory
     //*********************************************************************
 
     public QueryBuilder newBuilder(
                     final Class<?> type
                     )
     {
         if (DefinitionType.class.isAssignableFromtype )) {
             return .;
         } else if (DefinitionsElement.class.isAssignableFromtype )) {
             return .;
         } else if (OvalSystemCharacteristics.class.isAssignableFromtype )) {
             return .;
         } else if (OvalResults.class.isAssignableFromtype )) {
             return .;
         }
 
         return .;
     }
 
 
 
    ///////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////
//    /**
//     * A factory method.
//     */
//    public static QueryBuilder getInstance(
//                    final Class<?> type
//                    )
//    {
//        if (DefinitionType.class.isAssignableFrom( type )) {
//            return DefinitionBuilder.INSTANCE;
//        } else if (DefinitionsElement.class.isAssignableFrom( type )) {
//            return DefinitionsElementBuilder.INSTANCE;
//        } else if (OvalSystemCharacteristics.class.isAssignableFrom( type )) {
//            return OvalSystemCharacteristicsBuilder.INSTANCE;
//        } else if (OvalResults.class.isAssignableFrom( type )) {
//            return OvalResultsBuilder.INSTANCE;
//        }
//
//        return CommonBuilder.INSTANCE;
//    }
    ///////////////////////////////////////////////////////////////////////
    //  Handler variations
    ///////////////////////////////////////////////////////////////////////
    protected static class BooleanHandler
    implements Handler
    {
        public static final BooleanHandler  INSTANCE = new BooleanHandler();
        public BooleanHandler()
        {
        }
        public void build(
                        final Query<?> query,
                        final String field,
                        final String value
                        )
        {
            if (_isEmptyvalue )) {
                return;
            }
            char  operator = value.charAt( 0 );
            if (operator == '!') {
                Boolean  bool_value = Boolean.valueOfvalue.substring( 1 ) );
                query.filterfield + " !="bool_value );
            } else {
                Boolean  bool_value = Boolean.valueOfvalue );
                query.filterfieldbool_value );
            }
        }
    }
    //Boolean
    protected static class OvalEnumerationListHandler
    implements Handler
    {
        private final Class<? extends OvalEnumeration>  _type;
        public OvalEnumerationListHandler(
                        final Class<? extends OvalEnumerationtype
                        )
        {
             = type;
        }
        public void build(
                        final Query<?> query,
                        final String field,
                        final String value
                        )
        {
            if (_isEmptyvalue )) {
                return;
            }
            if (_isListvalue )) {
                String[]  value_elem = _asListvalue );
                List<OvalEnumeration>  list = new ArrayList<OvalEnumeration>();
                for (String  v : value_elem) {
                    list.add( OvalEnumerationHelper.fromValuev ) );
                }
                query.filterfield + " in"list );
            } else {
                query.filterfield, OvalEnumerationHelper.fromValuevalue ) );
            }
        }
    }
    //OvalEnumerationList
    ///////////////////////////////////////////////////////////////////////
    //  QueryBuilder variations
    ///////////////////////////////////////////////////////////////////////


    
oval-def:{definition,test,object,state,variable}
    public static class DefinitionsElementBuilder
    extends CommonBuilder
    {
        public static final DefinitionsElementBuilder  INSTANCE = new DefinitionsElementBuilder();
        protected static Map<StringString_createFieldMapping()
        {
            Map<StringString>  mapping = new HashMap<StringString>();
            mapping.put..,          "oval_id" );
            mapping.put..,     "oval_version" );
            mapping.put..,  "deprecated" );
            mapping.put..,        null );
            //NOTE: TYPE parameter is handled by the Repository implementation class.
            mapping.put..,      "_oval_family" );
            mapping.put..,   "_oval_component" );
            mapping.put..,      "_oval_generator.schema_version" );
            //override:
            mapping.put..,            "comment" );
            return mapping;
        }
        private static final Map<StringString>  _FIELDS_ = _createFieldMapping();
        protected static Map<StringHandler_createHandlers()
        {
            Map<StringHandler>  mapping = CommonBuilder._createHandlerMapping();
            //common//
            mapping.put..,            . );
//          mapping.put( CommonQueryParams.Key.SEARCH_TERMS,            SearchTermsHandler.INSTANCE ); //slower
            //definitions element//
            mapping.put..,          . );
            mapping.put..,     . );
            // TODO:
            mapping.put..,  . );
            mapping.put..,        . );
            mapping.put..,      . );
            mapping.put..,      new OvalEnumerationListHandlerFamily.class ) );
            mapping.put..,   new OvalEnumerationListHandlerComponentType.class ) );
            return mapping;
        }
        private static final Map<StringHandler>  _HANDLERS_ = _createHandlers();
        public DefinitionsElementBuilder()
        {
        }
        @Override
        protected Map<StringHandler_handlerMapping()
        {
            return ;
        }
        @Override
        protected Map<StringString_fieldMapping()
        {
            return ;
        }
    }
    //DefinitionsElement



    
oval-def:definition
    public static class DefinitionBuilder
    {
        public static final DefinitionBuilder  INSTANCE = new DefinitionBuilder();
        protected static Map<StringString_createFieldMapping()
        {
            Map<StringString>  mapping = DefinitionsElementBuilder._createFieldMapping();
            mapping.put..,    "class" );
            mapping.put..,              "metadata.affected.family" );   //override
            mapping.put..,            "metadata.affected.platform" );
            mapping.put..,             "metadata.affected.product" );
            mapping.put..,          "metadata.reference.source" );
            mapping.put..,              "metadata.reference.ref_id" );
            mapping.put..,                 "metadata.reference.ref_id" );
            // override
            mapping.put..,            "metadata.title,metadata.description" );
            return mapping;
        }
        private static final Map<StringString>  _FIELDS_ = _createFieldMapping();
        protected static Map<StringHandler_createHandlerMapping()
        {
            Map<StringHandler>  mapping = DefinitionsElementBuilder._createHandlers();
            //override//
            mapping.put..,      new OvalEnumerationListHandlerFamilyEnumeration.class ) );
//          mapping.put( CommonQueryParams.Key.SEARCH_TERMS,            SearchTermHandler.INSTANCE );
            //def:definition//
            mapping.put..,    new OvalEnumerationListHandlerClassEnumeration.class ) );
            mapping.put..,            . );
            mapping.put..,             . );
            mapping.put..,          . );
            mapping.put..,              . );
            mapping.put..,                 . );
            return mapping;
        }
        private static final Map<StringHandler>  _HANDLERS_ = _createHandlerMapping();
        public DefinitionBuilder()
        {
        }
        @Override
        protected Map<StringHandler_handlerMapping()
        {
            return ;
        }
        @Override
        protected Map<StringString_fieldMapping()
        {
            return ;
        }
    }
    //Definition



    
oval-sc:oval_system_characteristics
    public static class OvalSystemCharacteristicsBuilder
    extends CommonBuilder
    {
        public static final OvalSystemCharacteristicsBuilder  INSTANCE = new OvalSystemCharacteristicsBuilder();
        protected static Map<StringString_createFieldMapping()
        {
            Map<StringString>  mapping = new HashMap<StringString>();
            mapping.put..,         "system_info.primary_host_name" );
            mapping.put..,           "system_info.os_name" );
            mapping.put..,   "system_info.os_version" );
            mapping.put..,           "system_info.interfaces.interface.ip_address" );
            mapping.put..,          "system_info.interfaces.interface.mac_address" );
            return mapping;
        }
        private static final Map<StringString>  _FIELDS_ = _createFieldMapping();
        protected static Map<StringHandler_createHandlerMapping()
        {
            Map<StringHandler>  mapping = CommonBuilder._createHandlerMapping();
            //override
//          mapping.put( CommonQueryParams.Key.SEARCH_TERMS,            SearchTermsHandler2.INSTANCE );
            //sc//
            mapping.put..,         . );
            mapping.put..,           . );
//            mapping.put( OvalSystemCharacteristicsQueryParams.Key.OS_VERSION,   FilterHandler2.INSTANCE );
            mapping.put..,           . );
            mapping.put..,          . );
            return mapping;
        }
        private static final Map<StringHandler>  _HANDLERS_ = _createHandlerMapping();
        public OvalSystemCharacteristicsBuilder()
        {
        }
        @Override
        protected Map<StringHandler_handlerMapping()
        {
            return ;
        }
        @Override
        protected Map<StringString_fieldMapping()
        {
            return ;
        }
    }
    //OvalSystemCharacteristics



    
oval-res:oval_results
    public static class OvalResultsBuilder
    extends CommonBuilder
    {
        public static final OvalResultsBuilder  INSTANCE = new OvalResultsBuilder();
        protected static Map<StringString_createFieldMapping()
        {
            Map<StringString>  mapping = OvalSystemCharacteristicsBuilder._createFieldMapping();
            for (String  key : mapping.keySet()) {
                String  field = mapping.getkey );
                field = "results.system.oval_system_characteristics." + field;
                mapping.putkeyfield );
            }
            mapping.put..,         "results.system.definitions.definition.oval_id" );
            mapping.put..,    "results.system.definitions.definition" );
            return mapping;
        }
        private static final Map<StringString>  _FIELDS_ = _createFieldMapping();
        protected static Map<StringHandler_createHandlerMapping()
        {
            Handler  definition_true_handler = new Handler()
            {
                public void build(
                                final Query<?> query,
                                final String field,
                                final String value  //def's oval_id
                                )
                {
                    if (_isEmptyvalue )) {
                        return;
                    }
                    if (_isListvalue )) {
                        String[]  value_elem = _asListvalue );
                        int  num_value_elem = value_elem.length;
                        Criteria[]  criteria = new Criteria[num_value_elem];
                        for (int  i = 0; i < num_value_elemi++) {
                            jp.go.aist.six.oval.model.results.DefinitionType  result_def =
                                            new jp.go.aist.six.oval.model.results.DefinitionType();
                            result_def.setOvalIdvalue_elem[i] );
                            result_def.setResult. );
                            criteria[i] = query.criteriafield ).hasThisElementresult_def );
                        }
                        query.orcriteria );
                    } else {
                        jp.go.aist.six.oval.model.results.DefinitionType  result_def =
                                        new jp.go.aist.six.oval.model.results.DefinitionType();
                        result_def.setDefinitionIdvalue );
                        result_def.setResult. );
                        query.filterfield + " elem"result_def );
                    }
                }
            };
            Map<StringHandler>  mapping = OvalSystemCharacteristicsBuilder._createHandlerMapping();
            mapping.put..,    definition_true_handler );
            mapping.put..,         . );
            return mapping;
        }
        private static final Map<StringHandler>  _HANDLERS_ = _createHandlerMapping();
        public OvalResultsBuilder()
        {
        }
        @Override
        protected Map<StringHandler_handlerMapping()
        {
            return ;
        }
        @Override
        protected Map<StringString_fieldMapping()
        {
            return ;
        }
    }
    //OvalResults
//
New to GrepCode? Check out our FAQ X