Search in sources :

Example 1 with LiquibaseSerializable

use of liquibase.serializer.LiquibaseSerializable in project liquibase by liquibase.

the class StringSnapshotSerializer method serializeObject.

private String serializeObject(LiquibaseSerializable objectToSerialize, int indent) {
    try {
        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        SortedSet<String> values = new TreeSet<String>();
        for (String field : objectToSerialize.getSerializableFields()) {
            Object value = objectToSerialize.getSerializableFieldValue(field);
            if (value instanceof LiquibaseSerializable) {
                values.add(indent(indent) + serializeObject((LiquibaseSerializable) value, indent + 1));
            } else {
                if (value != null) {
                    if (value instanceof Map) {
                        values.add(indent(indent) + field + "=" + serializeObject((Map) value, indent + 1));
                    } else if (value instanceof Collection) {
                        values.add(indent(indent) + field + "=" + serializeObject((Collection) value, indent + 1));
                    } else if (value instanceof Object[]) {
                        values.add(indent(indent) + field + "=" + serializeObject((Object[]) value, indent + 1));
                    } else {
                        String valueString = value.toString();
                        if (value instanceof Double || value instanceof Float) {
                            //java 6 adds additional zeros to the end of doubles and floats
                            if (valueString.contains(".")) {
                                valueString = valueString.replaceFirst("0*$", "");
                            }
                        }
                        values.add(indent(indent) + field + "=\"" + valueString + "\"");
                    }
                }
            }
        }
        if (values.size() > 0) {
            buffer.append("\n");
            buffer.append(StringUtils.join(values, "\n"));
            buffer.append("\n");
        }
        buffer.append(indent(indent - 1)).append("]");
        //standardize all newline chars
        return buffer.toString().replace("\r\n", "\n").replace("\r", "\n");
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : LiquibaseSerializable(liquibase.serializer.LiquibaseSerializable) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 2 with LiquibaseSerializable

use of liquibase.serializer.LiquibaseSerializable in project liquibase by liquibase.

the class VerifyChangeClassesTest method formatParameter.

private String formatParameter(Object paramValue) {
    String serializedValue;
    if (paramValue instanceof Collection) {
        serializedValue = "[";
        for (Object obj : (Collection) paramValue) {
            serializedValue += formatParameter(obj) + ", ";
        }
        serializedValue += "]";
    } else if (paramValue instanceof LiquibaseSerializable) {
        serializedValue = new StringChangeLogSerializer().serialize(((LiquibaseSerializable) paramValue), true);
    } else {
        serializedValue = paramValue.toString();
    }
    return serializedValue;
}
Also used : StringChangeLogSerializer(liquibase.serializer.core.string.StringChangeLogSerializer) LiquibaseSerializable(liquibase.serializer.LiquibaseSerializable)

Example 3 with LiquibaseSerializable

use of liquibase.serializer.LiquibaseSerializable in project liquibase by liquibase.

the class AbstractChange method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    ChangeMetaData metaData = ChangeFactory.getInstance().getChangeMetaData(this);
    this.setResourceAccessor(resourceAccessor);
    try {
        Collection<ChangeParameterMetaData> changeParameters = metaData.getParameters().values();
        for (ChangeParameterMetaData param : changeParameters) {
            if (Collection.class.isAssignableFrom(param.getDataTypeClass())) {
                if (param.getDataTypeClassParameters().length == 1) {
                    Class collectionType = (Class) param.getDataTypeClassParameters()[0];
                    if (ColumnConfig.class.isAssignableFrom(collectionType)) {
                        List<ParsedNode> columnNodes = new ArrayList<ParsedNode>(parsedNode.getChildren(null, param.getParameterName()));
                        columnNodes.addAll(parsedNode.getChildren(null, "column"));
                        Object nodeValue = parsedNode.getValue();
                        if (nodeValue instanceof ParsedNode) {
                            columnNodes.add((ParsedNode) nodeValue);
                        } else if (nodeValue instanceof Collection) {
                            for (Object nodeValueChild : ((Collection) nodeValue)) {
                                if (nodeValueChild instanceof ParsedNode) {
                                    columnNodes.add((ParsedNode) nodeValueChild);
                                }
                            }
                        }
                        for (ParsedNode child : columnNodes) {
                            if (child.getName().equals("column") || child.getName().equals("columns")) {
                                List<ParsedNode> columnChildren = child.getChildren(null, "column");
                                if (columnChildren != null && columnChildren.size() > 0) {
                                    for (ParsedNode columnChild : columnChildren) {
                                        ColumnConfig columnConfig = createEmptyColumnConfig(collectionType);
                                        columnConfig.load(columnChild, resourceAccessor);
                                        ((ChangeWithColumns) this).addColumn(columnConfig);
                                    }
                                } else {
                                    ColumnConfig columnConfig = createEmptyColumnConfig(collectionType);
                                    columnConfig.load(child, resourceAccessor);
                                    ((ChangeWithColumns) this).addColumn(columnConfig);
                                }
                            }
                        }
                    } else if (LiquibaseSerializable.class.isAssignableFrom(collectionType)) {
                        if (!collectionType.isInterface() && !Modifier.isAbstract(collectionType.getModifiers())) {
                            String elementName = ((LiquibaseSerializable) collectionType.newInstance()).getSerializedObjectName();
                            List<ParsedNode> nodes = new ArrayList<ParsedNode>(parsedNode.getChildren(null, param.getParameterName()));
                            if (!elementName.equals(param.getParameterName())) {
                                nodes.addAll(parsedNode.getChildren(null, elementName));
                            }
                            Object nodeValue = parsedNode.getValue();
                            if (nodeValue instanceof ParsedNode) {
                                nodes.add((ParsedNode) nodeValue);
                            } else if (nodeValue instanceof Collection) {
                                for (Object nodeValueChild : ((Collection) nodeValue)) {
                                    if (nodeValueChild instanceof ParsedNode) {
                                        nodes.add((ParsedNode) nodeValueChild);
                                    }
                                }
                            }
                            for (ParsedNode node : nodes) {
                                if (node.getName().equals(elementName) || node.getName().equals(param.getParameterName())) {
                                    List<ParsedNode> childNodes = node.getChildren(null, elementName);
                                    if (childNodes != null && childNodes.size() > 0) {
                                        for (ParsedNode childNode : childNodes) {
                                            LiquibaseSerializable childObject = (LiquibaseSerializable) collectionType.newInstance();
                                            childObject.load(childNode, resourceAccessor);
                                            ((Collection) param.getCurrentValue(this)).add(childObject);
                                        }
                                    } else {
                                        LiquibaseSerializable childObject = (LiquibaseSerializable) collectionType.newInstance();
                                        childObject.load(node, resourceAccessor);
                                        ((Collection) param.getCurrentValue(this)).add(childObject);
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (LiquibaseSerializable.class.isAssignableFrom(param.getDataTypeClass())) {
                if (!param.getDataTypeClass().isInterface() && !Modifier.isAbstract(param.getDataTypeClass().getModifiers())) {
                    try {
                        ParsedNode child = parsedNode.getChild(null, param.getParameterName());
                        if (child != null) {
                            LiquibaseSerializable serializableChild = (LiquibaseSerializable) param.getDataTypeClass().newInstance();
                            serializableChild.load(child, resourceAccessor);
                            param.setValue(this, serializableChild);
                        }
                    } catch (InstantiationException e) {
                        throw new UnexpectedLiquibaseException(e);
                    } catch (IllegalAccessException e) {
                        throw new UnexpectedLiquibaseException(e);
                    }
                }
            } else {
                Object childValue = parsedNode.getChildValue(null, param.getParameterName(), param.getDataTypeClass());
                if (childValue == null && param.getSerializationType() == SerializationType.DIRECT_VALUE) {
                    childValue = parsedNode.getValue();
                }
                param.setValue(this, childValue);
            }
        }
    } catch (InstantiationException e) {
        throw new UnexpectedLiquibaseException(e);
    } catch (IllegalAccessException e) {
        throw new UnexpectedLiquibaseException(e);
    }
    customLoadLogic(parsedNode, resourceAccessor);
    try {
        this.finishInitialization();
    } catch (SetupException e) {
        throw new ParsedNodeException(e);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) ParsedNodeException(liquibase.parser.core.ParsedNodeException) DatabaseObject(liquibase.structure.DatabaseObject) LiquibaseSerializable(liquibase.serializer.LiquibaseSerializable)

Example 4 with LiquibaseSerializable

use of liquibase.serializer.LiquibaseSerializable in project liquibase by liquibase.

the class YamlSerializer method toMap.

protected Object toMap(LiquibaseSerializable object) {
    Comparator<String> comparator;
    comparator = getComparator(object);
    Map<String, Object> objectMap = new TreeMap<String, Object>(comparator);
    for (String field : object.getSerializableFields()) {
        Object value = object.getSerializableFieldValue(field);
        if (value != null) {
            if (value instanceof DataType) {
                value = ((Map) toMap((DataType) value)).values().iterator().next();
            }
            if (value instanceof Column.AutoIncrementInformation) {
                value = ((Map) toMap((Column.AutoIncrementInformation) value)).values().iterator().next();
            }
            if (value instanceof ConstraintsConfig) {
                value = ((Map) toMap((ConstraintsConfig) value)).values().iterator().next();
            }
            if (value instanceof LiquibaseSerializable) {
                value = toMap((LiquibaseSerializable) value);
            }
            if (value instanceof Collection) {
                List valueAsList = new ArrayList((Collection) value);
                if (valueAsList.size() == 0) {
                    continue;
                }
                for (int i = 0; i < valueAsList.size(); i++) {
                    if (valueAsList.get(i) instanceof LiquibaseSerializable) {
                        valueAsList.set(i, toMap((LiquibaseSerializable) valueAsList.get(i)));
                    }
                }
                value = valueAsList;
            }
            if (value instanceof Map) {
                if (((Map) value).size() == 0) {
                    continue;
                }
                for (Object key : ((Map) value).keySet()) {
                    Object mapValue = ((Map) value).get(key);
                    if (mapValue instanceof LiquibaseSerializable) {
                        ((Map) value).put(key, toMap((LiquibaseSerializable) mapValue));
                    } else if (mapValue instanceof Collection) {
                        List valueAsList = new ArrayList((Collection) mapValue);
                        if (valueAsList.size() == 0) {
                            continue;
                        }
                        for (int i = 0; i < valueAsList.size(); i++) {
                            if (valueAsList.get(i) instanceof LiquibaseSerializable) {
                                valueAsList.set(i, toMap((LiquibaseSerializable) valueAsList.get(i)));
                            }
                        }
                        ((Map) value).put(key, valueAsList);
                    }
                }
            }
            objectMap.put(field, value);
        }
    }
    Map<String, Object> containerMap = new HashMap<String, Object>();
    containerMap.put(object.getSerializedObjectName(), objectMap);
    return containerMap;
}
Also used : ColumnConstraint(liquibase.statement.ColumnConstraint) Column(liquibase.structure.core.Column) ConstraintsConfig(liquibase.change.ConstraintsConfig) DataType(liquibase.structure.core.DataType) LiquibaseSerializable(liquibase.serializer.LiquibaseSerializable)

Example 5 with LiquibaseSerializable

use of liquibase.serializer.LiquibaseSerializable in project liquibase by liquibase.

the class StringChangeLogSerializer method serializeObject.

private String serializeObject(LiquibaseSerializable objectToSerialize, int indent) {
    try {
        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        SortedSet<String> values = new TreeSet<String>();
        for (String field : objectToSerialize.getSerializableFields()) {
            Object value = objectToSerialize.getSerializableFieldValue(field);
            if (value == null) {
                continue;
            }
            if (!fieldFilter.include(objectToSerialize, field, value)) {
                continue;
            }
            if (value instanceof LiquibaseSerializable) {
                values.add(indent(indent) + serializeObject((LiquibaseSerializable) value, indent + 1));
            } else {
                if (value != null) {
                    if (value instanceof Map) {
                        values.add(indent(indent) + field + "=" + serializeObject((Map) value, indent + 1));
                    } else if (value instanceof Collection) {
                        values.add(indent(indent) + field + "=" + serializeObject((Collection) value, indent + 1));
                    } else if (value instanceof Object[]) {
                        values.add(indent(indent) + field + "=" + serializeObject((Object[]) value, indent + 1));
                    } else {
                        String valueString = value.toString();
                        if (value instanceof Double || value instanceof Float) {
                            //java 6 adds additional zeros to the end of doubles and floats
                            if (valueString.contains(".")) {
                                valueString = valueString.replaceFirst("(\\.[0-9]+)0+$", "$1");
                                valueString = valueString.replaceFirst("\\.0+$", "");
                            }
                        }
                        values.add(indent(indent) + field + "=\"" + valueString + "\"");
                    }
                }
            }
        }
        if (values.size() > 0) {
            buffer.append("\n");
            buffer.append(StringUtils.join(values, "\n"));
            buffer.append("\n");
        }
        buffer.append(indent(indent - 1)).append("]");
        //standardize all newline chars
        return buffer.toString().replace("\r\n", "\n").replace("\r", "\n");
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : LiquibaseSerializable(liquibase.serializer.LiquibaseSerializable) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

LiquibaseSerializable (liquibase.serializer.LiquibaseSerializable)5 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 ConstraintsConfig (liquibase.change.ConstraintsConfig)1 ParsedNode (liquibase.parser.core.ParsedNode)1 ParsedNodeException (liquibase.parser.core.ParsedNodeException)1 StringChangeLogSerializer (liquibase.serializer.core.string.StringChangeLogSerializer)1 ColumnConstraint (liquibase.statement.ColumnConstraint)1 DatabaseObject (liquibase.structure.DatabaseObject)1 Column (liquibase.structure.core.Column)1 DataType (liquibase.structure.core.DataType)1