Search in sources :

Example 1 with StringBasedPropertyDefinition

use of org.structr.schema.parser.StringBasedPropertyDefinition in project structr by structr.

the class SchemaHelper method extractProperties.

public static String extractProperties(final Schema entity, final Set<String> propertyNames, final Set<Validator> validators, final Set<String> compoundIndexKeys, final Set<String> enums, final Map<String, Set<String>> views, final List<String> propertyValidators, final ErrorBuffer errorBuffer) throws FrameworkException {
    final PropertyContainer propertyContainer = entity.getPropertyContainer();
    final StringBuilder src = new StringBuilder();
    // output property source code and collect views
    for (String propertyName : SchemaHelper.getProperties(propertyContainer)) {
        if (!propertyName.startsWith("__") && propertyContainer.hasProperty(propertyName)) {
            String rawType = propertyContainer.getProperty(propertyName).toString();
            PropertySourceGenerator parser = SchemaHelper.getSourceGenerator(errorBuffer, entity.getClassName(), new StringBasedPropertyDefinition(propertyName, rawType));
            if (parser != null) {
                // migrate properties
                if (entity instanceof AbstractSchemaNode) {
                    parser.createSchemaPropertyNode((AbstractSchemaNode) entity, propertyName);
                }
            }
        }
    }
    final List<SchemaProperty> schemaProperties = entity.getSchemaProperties();
    if (schemaProperties != null) {
        for (final SchemaProperty schemaProperty : schemaProperties) {
            String propertyName = schemaProperty.getPropertyName();
            String oldName = propertyName;
            int count = 1;
            if (propertyNames.contains(propertyName)) {
                while (propertyNames.contains(propertyName)) {
                    propertyName = propertyName + count++;
                }
                logger.warn("Property name {} already present in type {}, renaming to {}", oldName, entity.getClassName(), propertyName);
                schemaProperty.setProperty(SchemaProperty.name, propertyName);
            }
            propertyNames.add(propertyName);
            if (!schemaProperty.getProperty(SchemaProperty.isBuiltinProperty)) {
                // migrate property source
                if (Type.Function.equals(schemaProperty.getPropertyType())) {
                    // Note: This is a temporary migration from the old format to the new readFunction property
                    final String format = schemaProperty.getFormat();
                    if (format != null) {
                        try {
                            schemaProperty.setProperty(SchemaProperty.readFunction, format);
                            schemaProperty.setProperty(SchemaProperty.format, null);
                        } catch (FrameworkException ex) {
                            logger.warn("", ex);
                        }
                    }
                }
                final PropertySourceGenerator parser = SchemaHelper.getSourceGenerator(errorBuffer, entity.getClassName(), schemaProperty);
                if (parser != null) {
                    // add property name to set for later use
                    propertyNames.add(schemaProperty.getPropertyName());
                    // append created source from parser
                    parser.getPropertySource(src, entity);
                    // register global elements created by parser
                    validators.addAll(parser.getGlobalValidators());
                    compoundIndexKeys.addAll(parser.getCompoundIndexKeys());
                    enums.addAll(parser.getEnumDefinitions());
                    // built-in schema properties are configured manually
                    if (!schemaProperty.isPartOfBuiltInSchema()) {
                        // register property in default view
                        addPropertyToView(PropertyView.Ui, propertyName, views);
                    }
                }
            }
            final String[] propertyValidatorsArray = schemaProperty.getProperty(SchemaProperty.validators);
            if (propertyValidatorsArray != null) {
                propertyValidators.addAll(Arrays.asList(propertyValidatorsArray));
            }
        }
    }
    return src.toString();
}
Also used : PropertyContainer(org.structr.api.graph.PropertyContainer) SchemaProperty(org.structr.core.entity.SchemaProperty) FrameworkException(org.structr.common.error.FrameworkException) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) PropertySourceGenerator(org.structr.schema.parser.PropertySourceGenerator) PasswordPropertySourceGenerator(org.structr.schema.parser.PasswordPropertySourceGenerator) StringPropertySourceGenerator(org.structr.schema.parser.StringPropertySourceGenerator) StringBasedPropertyDefinition(org.structr.schema.parser.StringBasedPropertyDefinition)

Aggregations

PropertyContainer (org.structr.api.graph.PropertyContainer)1 FrameworkException (org.structr.common.error.FrameworkException)1 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)1 SchemaProperty (org.structr.core.entity.SchemaProperty)1 PasswordPropertySourceGenerator (org.structr.schema.parser.PasswordPropertySourceGenerator)1 PropertySourceGenerator (org.structr.schema.parser.PropertySourceGenerator)1 StringBasedPropertyDefinition (org.structr.schema.parser.StringBasedPropertyDefinition)1 StringPropertySourceGenerator (org.structr.schema.parser.StringPropertySourceGenerator)1