Search in sources :

Example 1 with MappingFilter

use of org.jaffa.soa.dataaccess.MappingFilter in project jaffa-framework by jaffa-projects.

the class ClassMetaDataHelper method showPropertiesRefactored.

/**
 * Show Properties.
 * @see #showProperties
 */
// Implementation NOTE: the primary difference between this and showProperties is
// that this calls addPropertyMetaDataRefactored instead of addPropertyMetaData.
// Also, some methods have been extracted.
void showPropertiesRefactored(String className, Class clazz, Object bean, boolean legacy, Writer out) throws Exception {
    String[] propertyNames = determinePropertyNames(className, clazz);
    if (propertyNames != null && propertyNames.length > 0) {
        GraphMapping mapping = null;
        try {
            mapping = clazz != null && GraphDataObject.class.isAssignableFrom(clazz) ? MappingFactory.getInstance(clazz) : null;
        } catch (InstantiationError ignore) {
        }
        StringBuilder buf = new StringBuilder();
        Object domainInstance = null;
        Object graphInstance = null;
        if (clazz != null && mapping != null) {
            try {
                domainInstance = mapping.getDomainClass().newInstance();
                graphInstance = clazz.newInstance();
                String[] filter = { "*" };
                MappingFilter mappingFilter = MappingFilter.getInstance(mapping, filter);
                DataTransformer.buildGraphFromDomain(domainInstance, graphInstance, mapping, mappingFilter, null, false);
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
        Object beanInstance = null;
        if (graphInstance == null) {
            try {
                FlexClass fc = FlexClass.instance(className);
                beanInstance = fc.newInstance();
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
        Map<String, String> foreignKeyMappings = buildForeignKeyMappings(clazz, propertyNames, mapping);
        for (String propertyName : propertyNames) {
            if (mapping != null && mapping.isForeignField(propertyName)) {
                /*
          For a foreign object, generate MetaData for all foreign-key fields.
          Consider the following scenario:
          - Graph has a foreign-object "deliveredPart : PartGraph"
          - The corresponding domain class has the foreign-key fields
               "deliveredPart : String" and "deliveredCage : String"
          - The foreign Graph has the key fields "part : String" and "cage : String"
          - MetaData will be generated for the fields -
               "deliveredPart.part: {...}" and "deliveredPart.cage: {...}"
          - If legacy==true, MetaData will continue to be generated for the field - "deliveredPart"
        */
                Class foreignGraph = BeanHelper.getPropertyType(clazz, propertyName);
                if (foreignGraph != null && GraphDataObject.class.isAssignableFrom(foreignGraph)) {
                    List<String> foreignKeys = mapping.getForeignKeys(propertyName);
                    Set<String> keys = MappingFactory.getInstance(foreignGraph).getKeyFields();
                    if (foreignKeys != null && keys != null && foreignKeys.size() == keys.size()) {
                        int i = 0;
                        for (String key : keys) {
                            String title = propertyName + '.' + key;
                            String foreignKey = foreignKeys.get(i++);
                            addPropertyMetaDataRefactored(className, foreignKey, bean, buf, title, mapping.getDomainClass(), graphInstance, beanInstance, foreignKeyMappings);
                        }
                    }
                }
                // For new code, do not generate MetaData for the propertyName
                if (!legacy) {
                    continue;
                }
            }
            addPropertyMetaDataRefactored(className, propertyName, bean, buf, propertyName, null, graphInstance, beanInstance, foreignKeyMappings);
        }
        if (buf.length() > 0) {
            buf.append('\n');
        }
        out.write(buf.toString());
    }
}
Also used : GraphDataObject(org.jaffa.soa.graph.GraphDataObject) MappingFilter(org.jaffa.soa.dataaccess.MappingFilter) FlexClass(org.jaffa.flexfields.FlexClass) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) JSONObject(net.sf.json.JSONObject) FlexClass(org.jaffa.flexfields.FlexClass) GraphMapping(org.jaffa.soa.dataaccess.GraphMapping)

Example 2 with MappingFilter

use of org.jaffa.soa.dataaccess.MappingFilter in project jaffa-framework by jaffa-projects.

the class ClassMetaDataHelper method showProperties.

/**
 * Show Properties.
 * @see #showPropertiesRefactored
 */
void showProperties(String className, Class clazz, Object bean, boolean legacy, Writer out) throws Exception {
    String[] propertyNames = determinePropertyNames(className, clazz);
    if (propertyNames != null && propertyNames.length > 0) {
        GraphMapping mapping = null;
        try {
            mapping = clazz != null && GraphDataObject.class.isAssignableFrom(clazz) ? MappingFactory.getInstance(clazz) : null;
        } catch (InstantiationError ignore) {
        }
        StringBuilder buf = new StringBuilder();
        Object domainInstance = null;
        Object graphInstance = null;
        if (clazz != null && mapping != null) {
            try {
                domainInstance = mapping.getDomainClass().newInstance();
                graphInstance = clazz.newInstance();
                String[] filter = { "*" };
                MappingFilter mappingFilter = MappingFilter.getInstance(mapping, filter);
                DataTransformer.buildGraphFromDomain(domainInstance, graphInstance, mapping, mappingFilter, null, false);
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
        Object beanInstance = null;
        if (graphInstance == null) {
            try {
                FlexClass fc = FlexClass.instance(className);
                beanInstance = fc.newInstance();
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
        // Map<String, String> foreignKeyMappings =
        // buildForeignKeyMappings(clazz, propertyNames, mapping);
        Map<String, String> foreignKeyMappings = new HashMap<String, String>();
        for (String propertyName : propertyNames) {
            if (mapping != null && mapping.isForeignField(propertyName)) {
                Class foreignGraph = BeanHelper.getPropertyType(clazz, propertyName);
                if (foreignGraph != null && GraphDataObject.class.isAssignableFrom(foreignGraph)) {
                    List<String> foreignKeys = mapping.getForeignKeys(propertyName);
                    Set<String> keys = MappingFactory.getInstance(foreignGraph).getKeyFields();
                    if (foreignKeys != null && keys != null && foreignKeys.size() == keys.size()) {
                        int i = 0;
                        for (String key : keys) {
                            String title = propertyName + '.' + key;
                            String foreignKey = foreignKeys.get(i++);
                            foreignKeyMappings.put(foreignKey, title);
                        }
                    }
                }
            }
        }
        for (String propertyName : propertyNames) {
            if (mapping != null && mapping.isForeignField(propertyName)) {
                /*
          For a foreign object, generate MetaData for all foreign-key fields.
          Consider the following scenario:
          - Graph has a foreign-object "deliveredPart : PartGraph"
          - The corresponding domain class has the foreign-key fields
               "deliveredPart : String" and "deliveredCage : String"
          - The foreign Graph has the key fields "part : String" and "cage : String"
          - MetaData will be generated for the fields -
               "deliveredPart.part: {...}" and "deliveredPart.cage: {...}"
          - If legacy==true, MetaData will continue to be generated for the field - "deliveredPart"
        */
                Class foreignGraph = BeanHelper.getPropertyType(clazz, propertyName);
                if (foreignGraph != null && GraphDataObject.class.isAssignableFrom(foreignGraph)) {
                    List<String> foreignKeys = mapping.getForeignKeys(propertyName);
                    Set<String> keys = MappingFactory.getInstance(foreignGraph).getKeyFields();
                    if (foreignKeys != null && keys != null && foreignKeys.size() == keys.size()) {
                        int i = 0;
                        for (String key : keys) {
                            String title = propertyName + '.' + key;
                            String foreignKey = foreignKeys.get(i++);
                            addPropertyMetaData(className, foreignKey, bean, buf, title, mapping.getDomainClass(), graphInstance, beanInstance, foreignKeyMappings);
                        }
                    }
                }
                // For new code, do not generate MetaData for the propertyName
                if (!legacy)
                    continue;
            }
            addPropertyMetaData(className, propertyName, bean, buf, propertyName, null, graphInstance, beanInstance, foreignKeyMappings);
        }
        if (buf.length() > 0)
            buf.append('\n');
        out.write(buf.toString());
    }
}
Also used : GraphDataObject(org.jaffa.soa.graph.GraphDataObject) MappingFilter(org.jaffa.soa.dataaccess.MappingFilter) FlexClass(org.jaffa.flexfields.FlexClass) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) JSONObject(net.sf.json.JSONObject) FlexClass(org.jaffa.flexfields.FlexClass) GraphMapping(org.jaffa.soa.dataaccess.GraphMapping)

Aggregations

IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 JSONObject (net.sf.json.JSONObject)2 FlexClass (org.jaffa.flexfields.FlexClass)2 GraphMapping (org.jaffa.soa.dataaccess.GraphMapping)2 MappingFilter (org.jaffa.soa.dataaccess.MappingFilter)2 GraphDataObject (org.jaffa.soa.graph.GraphDataObject)2