Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 JBoss Inc
   *
   * 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 org.kie.workbench.common.services.datamodel.backend.server;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.drools.workbench.models.datamodel.oracle.Annotation;
 import  org.drools.workbench.models.datamodel.oracle.DataType;
 import  org.drools.workbench.models.datamodel.oracle.MethodInfo;
 import  org.drools.workbench.models.datamodel.oracle.ModelField;
 import  org.drools.workbench.models.datamodel.oracle.PackageDataModelOracle;
 import  org.drools.workbench.models.datamodel.oracle.ProjectDataModelOracle;
 import  org.drools.workbench.models.datamodel.oracle.TypeSource;
 import  org.kie.workbench.common.services.datamodel.model.LazyModelField;
 import  org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload;
 import  org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleIncrementalPayload;

Utilities to query ProjectDMO content
 
 public class DataModelOracleUtilities {

    
Convenience method to get an array of all fully qualified class names available in a project

Parameters:
oracle The DMO representing a project
Returns:
 
     public static String[] getFactTypes(final ProjectDataModelOracle oracle) {
 
         List<StringpackageNames = oracle.getProjectPackageNames();
 
         final Map<String, ModelField[]> modelFields = oracle.getProjectModelFields();
         final List<Stringtypes = new ArrayList<String>();
         for (String type : modelFields.keySet()) {
             int beginIndex = type.lastIndexOf('.');
 
             if (beginIndex < 0) {
                 types.add(type);
             } else {
                 String substring = type.substring(0, beginIndex);
                 if (packageNames.contains(substring)) {
                     types.add(type);
                 }
             }
         }
         Collections.sort(types);
 
         return types.toArray(new String[types.size()]);
     }

    
Convenience method to get an array of field names for a type in a project

Parameters:
oracle The DMO representing a project
fullyQualifiedClassName The FQCN of the type
Returns:
 
     public static String[] getFieldNamesfinal ProjectDataModelOracle oracle,
                                           final String fullyQualifiedClassName ) {
         final ModelField[] modelFields = oracle.getProjectModelFields().get( fullyQualifiedClassName );
         if ( modelFields == null ) {
             return new String[ 0 ];
         }
 
         final String[] fieldNames = new StringmodelFields.length ];
         for ( int i = 0; i < modelFields.length; i++ ) {
             fieldNamesi ] = modelFieldsi ].getName();
         }
         return fieldNames;
     }

    
Convenience method to get the fully qualified class name of the super type of another type in a project

Parameters:
oracle The DMO representing a project
fullyQualifiedClassName The FQCN of the type
Returns:
 
     public static String getSuperTypefinal ProjectDataModelOracle oracle,
                                        final String fullyQualifiedClassName ) {
         List<StringsuperTypes = oracle.getProjectSuperTypes().get(fullyQualifiedClassName);
        if (superTypes != null && superTypes.size() > 0) {
            return superTypes.get(0);
        } else {
            return null;
        }
    }

    
Convenience method to get a set of annotations on a type in a project

Parameters:
oracle The DMO representing a project
fullyQualifiedClassName The FQCN of the type
Returns:
    public static Set<Annotation> getTypeAnnotationsfinal ProjectDataModelOracle oracle,
                                                      final String fullyQualifiedClassName ) {
        final Map<StringSet<Annotation>> typeAnnotations = oracle.getProjectTypeAnnotations();
        if ( !typeAnnotations.containsKey( fullyQualifiedClassName ) ) {
            return .;
        }
        return typeAnnotations.get( fullyQualifiedClassName );
    }

    
Convenience method to get all field annotations on a type in a project

Parameters:
oracle The DMO representing a project
fullyQualifiedClassName The FQCN of the type
Returns:
    public static Map<StringSet<Annotation>> getTypeFieldsAnnotationsfinal ProjectDataModelOracle oracle,
                                                                         final String fullyQualifiedClassName ) {
        final Map<StringMap<StringSet<Annotation>>> typeFieldsAnnotations = oracle.getProjectTypeFieldsAnnotations();
        if ( !typeFieldsAnnotations.containsKey( fullyQualifiedClassName ) ) {
            return .;
        }
        return typeFieldsAnnotations.get( fullyQualifiedClassName );
    }

    
Convenience method to get the fully qualified class name of a field on a type in a project

Parameters:
oracle The DMO representing a project
fullyQualifiedClassName The FQCN of the type
fieldName The field Name
Returns:
    public static String getFieldClassNamefinal ProjectDataModelOracle oracle,
                                            final String fullyQualifiedClassName,
                                            final String fieldName ) {
        final ModelField field = getFieldoracle,
                                           fullyQualifiedClassName,
                                           fieldName );
        return field == null ? null : field.getClassName();
    }
    private static ModelField getFieldfinal ProjectDataModelOracle oracle,
                                        final String fullyQualifiedClassName,
                                        final String fieldName ) {
        final String shortName = getFactNameFromTypeoracle,
                                                      fullyQualifiedClassName );
        final ModelField[] fields = oracle.getProjectModelFields().get( shortName );
        if ( fields == null ) {
            return null;
        }
        for ( ModelField modelField : fields ) {
            if ( modelField.getName().equals( fieldName ) ) {
                return modelField;
            }
        }
        return null;
    }
    private static String getFactNameFromTypefinal ProjectDataModelOracle oracle,
                                               final String fullyQualifiedClassName ) {
        if ( fullyQualifiedClassName == null ) {
            return null;
        }
        if ( oracle.getProjectModelFields().containsKey( fullyQualifiedClassName ) ) {
            return fullyQualifiedClassName;
        }
        for ( Map.Entry<String, ModelField[]> entry : oracle.getProjectModelFields().entrySet() ) {
            for ( ModelField mf : entry.getValue() ) {
                if ( DataType.TYPE_THIS.equals( mf.getName() ) && fullyQualifiedClassName.equalsmf.getClassName() ) ) {
                    return entry.getKey();
                }
            }
        }
        return null;
    }

    
Convenience method to get the generic type of a field on a type in a project

Parameters:
oracle The DMO representing a project
fullyQualifiedClassName The FQCN of the type
fieldName The field Name
Returns:
    public static String getParametricFieldTypefinal ProjectDataModelOracle oracle,
                                                 final String fullyQualifiedClassName,
                                                 final String fieldName ) {
        final String qualifiedFactFieldName = fullyQualifiedClassName + "#" + fieldName;
        return oracle.getProjectFieldParametersType().get( qualifiedFactFieldName );
    }

    
Convenience method to get the source of a type in a project

Parameters:
oracle The DMO representing a project
fullyQualifiedClassName The FQCN of the type
Returns:
    public static TypeSource getTypeSourcefinal ProjectDataModelOracle oracle,
                                            final String fullyQualifiedClassName ) {
        return oracle.getProjectTypeSources().get( fullyQualifiedClassName );
    }
    public static void populateDataModelfinal PackageDataModelOracle oracle,
                                          final PackageDataModelOracleBaselinePayload dataModel,
                                          final Set<StringusedFullyQualifiedClassNames ) {
        dataModel.setProjectName( oracle.getProjectName() );
        dataModel.setPackageName( oracle.getPackageName() );
        dataModel.setModelFields( setupModelFieldsusedFullyQualifiedClassNames,
                                                    oracle.getProjectModelFields(),
                                                    oracle.getPackageGlobals() ) );
        dataModel.setFieldParametersType( filterFieldParametersTypesusedFullyQualifiedClassNames,
                                                                      oracle.getProjectFieldParametersType() ) );
        dataModel.setEventTypes( filterEventTypesusedFullyQualifiedClassNames,
                                                   oracle.getProjectEventTypes() ) );
        dataModel.setTypeSources( filterTypeSourcesusedFullyQualifiedClassNames,
                                                     oracle.getProjectTypeSources() ) );
        dataModel.setSuperTypes( filterSuperTypesusedFullyQualifiedClassNames,
                                                   oracle.getProjectSuperTypes() ) );
        dataModel.setTypeAnnotations( filterTypeAnnotationsusedFullyQualifiedClassNames,
                                                             oracle.getProjectTypeAnnotations() ) );
        dataModel.setTypeFieldsAnnotations( filterTypeFieldsAnnotationsusedFullyQualifiedClassNames,
                                                                         oracle.getProjectTypeFieldsAnnotations() ) );
        dataModel.setJavaEnumDefinitions( oracle.getProjectJavaEnumDefinitions() );
        dataModel.setWorkbenchEnumDefinitions( oracle.getPackageWorkbenchDefinitions() );
        dataModel.setMethodInformation( filterMethodInformationusedFullyQualifiedClassNames,
                                                                 oracle.getProjectMethodInformation() ) );
        dataModel.setCollectionTypes( filterCollectionTypesusedFullyQualifiedClassNames,
                                                             oracle.getProjectCollectionTypes() ) );
        dataModel.setDslConditionSentences( oracle.getPackageDslConditionSentences() );
        dataModel.setDslActionSentences( oracle.getPackageDslActionSentences() );
        dataModel.setGlobalTypes( oracle.getPackageGlobals() );
        dataModel.setPackageNames( oracle.getProjectPackageNames() );
    }
    public static void populateDataModelfinal PackageDataModelOracle oracle,
                                          final PackageDataModelOracleIncrementalPayload dataModel,
                                          final String usedFullyQualifiedClassName ) {
        final Set<StringusedFullyQualifiedClassNames = new HashSet<String>();
        usedFullyQualifiedClassNames.addusedFullyQualifiedClassName );
        dataModel.setModelFields( filterModelFieldsusedFullyQualifiedClassNames,
                                                     oracle.getProjectModelFields() ) );
        dataModel.setFieldParametersType( filterFieldParametersTypesusedFullyQualifiedClassNames,
                                                                      oracle.getProjectFieldParametersType() ) );
        dataModel.setEventTypes( filterEventTypesusedFullyQualifiedClassNames,
                                                   oracle.getProjectEventTypes() ) );
        dataModel.setTypeSources( filterTypeSourcesusedFullyQualifiedClassNames,
                                                     oracle.getProjectTypeSources() ) );
        dataModel.setSuperTypes( filterSuperTypesusedFullyQualifiedClassNames,
                                                   oracle.getProjectSuperTypes() ) );
        dataModel.setTypeAnnotations( filterTypeAnnotationsusedFullyQualifiedClassNames,
                                                             oracle.getProjectTypeAnnotations() ) );
        dataModel.setTypeFieldsAnnotations( filterTypeFieldsAnnotationsusedFullyQualifiedClassNames,
                                                                         oracle.getProjectTypeFieldsAnnotations() ) );
        dataModel.setMethodInformation( filterMethodInformationusedFullyQualifiedClassNames,
                                                                 oracle.getProjectMethodInformation() ) );
        dataModel.setCollectionTypes( filterCollectionTypesusedFullyQualifiedClassNames,
                                                             oracle.getProjectCollectionTypes() ) );
    }
    //Setup Model Fields for lazy loading client-side
    private static Map<String, ModelField[]> setupModelFieldsfinal Set<StringusedFullyQualifiedClassNames,
                                                               final Map<String, ModelField[]> projectModelFields,
                                                               final Map<StringStringpackageGlobals ) {
        final Map<String, ModelField[]> scopedModelFields = new HashMap<String, ModelField[]>();
        for ( Map.Entry<String, ModelField[]> e : projectModelFields.entrySet() ) {
            final String mfQualifiedType = e.getKey();
            if ( usedFullyQualifiedClassNames.containsmfQualifiedType ) ) {
                scopedModelFields.put( mfQualifiedType,
                                       e.getValue() );
            } else if ( packageGlobals.containsValuemfQualifiedType ) ) {
                scopedModelFields.put( mfQualifiedType,
                                       e.getValue() );
            } else {
                scopedModelFields.put( mfQualifiedType,
                                       makeLazyProxyModelFielde.getValue() ) );
            }
        }
        return scopedModelFields;
    }
    //AsyncPackageDataModelOracle.getFactNameFromType() uses THIS to determine the simple Type from a FQCN.
    //Therefore ensure we provide this minimal information for every Type in the DMO to prevent getFactNameFromType()
    //needing a callback to the server which makes things more complicated than really needed.
    private static ModelField[] makeLazyProxyModelFieldfinal ModelField[] modelFields ) {
        for ( ModelField modelField : modelFields ) {
            if ( DataType.TYPE_THIS.equals( modelField.getName() ) ) {
                final ModelField[] result = new ModelField[ 1 ];
                //LazyModelField is a place-holder to tell AsyncPackageDataModelOracle that it needs to load more information
                result[ 0 ] = new LazyModelField( modelField.getName(),
                                                  modelField.getClassName(),
                                                  modelField.getClassType(),
                                                  modelField.getOrigin(),
                                                  modelField.getAccessorsAndMutators(),
                                                  modelField.getType() );
                return result;
            }
        }
        return null;
    }
    //Filter Model Fields by the types used
    private static Map<String, ModelField[]> filterModelFieldsfinal Set<StringusedFullyQualifiedClassNames,
                                                                final Map<String, ModelField[]> projectModelFields ) {
        final Map<String, ModelField[]> scopedModelFields = new HashMap<String, ModelField[]>();
        for ( Map.Entry<String, ModelField[]> e : projectModelFields.entrySet() ) {
            final String mfQualifiedType = e.getKey();
            final ModelField[] mfModelFields = e.getValue();
            if ( isTypeUsedmfQualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedModelFields.put( mfQualifiedType,
                                       mfModelFields );
            }
        }
        return scopedModelFields;
    }
    //Filter Collection Types by the types used
    private static Map<StringBooleanfilterCollectionTypesfinal Set<StringusedFullyQualifiedClassNames,
                                                               final Map<StringBooleanprojectCollectionTypes ) {
        final Map<StringBooleanscopedCollectionTypes = new HashMap<StringBoolean>();
        for ( Map.Entry<StringBooleane : projectCollectionTypes.entrySet() ) {
            final String collectionQualifiedType = e.getKey();
            if ( isTypeUsedcollectionQualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedCollectionTypes.putcollectionQualifiedType,
                                           e.getValue() );
            }
        }
        return scopedCollectionTypes;
    }
    //Filter Event Types by the types used
    private static Map<StringBooleanfilterEventTypesfinal Set<StringusedFullyQualifiedClassNames,
                                                          final Map<StringBooleanprojectEventTypes ) {
        final Map<StringBooleanscopedEventTypes = new HashMap<StringBoolean>();
        for ( Map.Entry<StringBooleane : projectEventTypes.entrySet() ) {
            final String eventQualifiedType = e.getKey();
            if ( isTypeUsedeventQualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedEventTypes.puteventQualifiedType,
                                      e.getValue() );
            }
        }
        return scopedEventTypes;
    }
    //Filter TypeSource by the types used
    private static Map<String, TypeSource> filterTypeSourcesfinal Set<StringusedFullyQualifiedClassNames,
                                                              final Map<String, TypeSource> projectTypeSources ) {
        final Map<String, TypeSource> scopedTypeSources = new HashMap<String, TypeSource>();
        for ( Map.Entry<String, TypeSource> e : projectTypeSources.entrySet() ) {
            final String typeQualifiedType = e.getKey();
            if ( isTypeUsedtypeQualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedTypeSources.put( typeQualifiedType,
                                       e.getValue() );
            }
        }
        return scopedTypeSources;
    }
    //Filter Super Types by the types used
    private static Map<StringList<String>> filterSuperTypesfinal Set<StringusedFullyQualifiedClassNames,
                                                               final Map<StringList<String>> projectSuperTypes ) {
        final Map<StringList<String>> scopedSuperTypes = new HashMap<StringList<String>>();
        for ( Map.Entry<StringList<String>> e : projectSuperTypes.entrySet() ) {
            final String typeQualifiedType = e.getKey();
            final List<StringsuperTypeQualifiedTypes = e.getValue();
            if ( isTypeUsedtypeQualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedSuperTypes.puttypeQualifiedType,
                                      superTypeQualifiedTypes );
            }
        }
        return scopedSuperTypes;
    }
    //Filter Type Annotations by the types used
    private static Map<StringSet<Annotation>> filterTypeAnnotationsfinal Set<StringusedFullyQualifiedClassNames,
                                                                       final Map<StringSet<Annotation>> projectTypeAnnotations ) {
        final Map<StringSet<Annotation>> scopedTypeAnnotations = new HashMap<StringSet<Annotation>>();
        for ( Map.Entry<StringSet<Annotation>> e : projectTypeAnnotations.entrySet() ) {
            final String typeAnnotationQualifiedType = e.getKey();
            if ( isTypeUsedtypeAnnotationQualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedTypeAnnotations.put( typeAnnotationQualifiedType,
                                           e.getValue() );
            }
        }
        return scopedTypeAnnotations;
    }
    //Filter Type Fields Annotations by the types used
    private static Map<StringMap<StringSet<Annotation>>> filterTypeFieldsAnnotationsfinal Set<StringusedFullyQualifiedClassNames,
                                                                                          final Map<StringMap<StringSet<Annotation>>> projectTypeFieldsAnnotations ) {
        final Map<StringMap<StringSet<Annotation>>> scopedTypeFieldsAnnotations = new HashMap<StringMap<StringSet<Annotation>>>();
        for ( Map.Entry<StringMap<StringSet<Annotation>>> e : projectTypeFieldsAnnotations.entrySet() ) {
            final String typeAnnotationQualifiedType = e.getKey();
            if ( isTypeUsedtypeAnnotationQualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedTypeFieldsAnnotations.put( typeAnnotationQualifiedType,
                                                 e.getValue() );
            }
        }
        return scopedTypeFieldsAnnotations;
    }
    //Filter Method Information (used by ActionCallXXX and ExpressionBuilder) by the types used
    private static Map<StringList<MethodInfo>> filterMethodInformationfinal Set<StringusedFullyQualifiedClassNames,
                                                                          final Map<StringList<MethodInfo>> projectMethodInformation ) {
        final Map<StringList<MethodInfo>> scopedMethodInformation = new HashMap<StringList<MethodInfo>>();
        for ( Map.Entry<StringList<MethodInfo>> e : projectMethodInformation.entrySet() ) {
            final String miQualifiedType = e.getKey();
            if ( isTypeUsedmiQualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedMethodInformation.put( miQualifiedType,
                                             e.getValue() );
            }
        }
        return scopedMethodInformation;
    }
    //Filter Field Parameter Types by the types used
    private static Map<StringStringfilterFieldParametersTypesfinal Set<StringusedFullyQualifiedClassNames,
                                                                   final Map<StringStringprojectFieldParametersTypes ) {
        final Map<StringStringscopedFieldParametersType = new HashMap<StringString>();
        for ( Map.Entry<StringStringe : projectFieldParametersTypes.entrySet() ) {
            final String fieldName = e.getKey();
            final String fieldType = e.getValue();
            final String fFieldName_QualifiedType = getQualifiedTypeFromEncodedFieldNamefieldName );
            if ( isTypeUsedfFieldName_QualifiedType,
                             usedFullyQualifiedClassNames ) ) {
                scopedFieldParametersType.putfieldName,
                                               fieldType );
            }
        }
        return scopedFieldParametersType;
    }
    private static String getQualifiedTypeFromEncodedFieldNamefinal String encodedFieldName ) {
        String typeName = encodedFieldName;
        int hashIndex = typeName.lastIndexOf"#" );
        if ( hashIndex != -1 ) {
            typeName = typeName.substring( 0,
                                           hashIndex );
        }
        return typeName;
    }
    private static boolean isTypeUsedfinal String fullyQualifiedClassName,
                                       final Set<StringusedFullyQualifiedClassNames ) {
        return usedFullyQualifiedClassNames.containsfullyQualifiedClassName.replaceAll"\\$",
                                                                                          "." ) );
    }
New to GrepCode? Check out our FAQ X