| 1 | |
package liquibase.serializer.core.string; |
| 2 | |
|
| 3 | |
import liquibase.change.Change; |
| 4 | |
import liquibase.change.ChangeProperty; |
| 5 | |
import liquibase.change.ColumnConfig; |
| 6 | |
import liquibase.change.ConstraintsConfig; |
| 7 | |
import liquibase.change.custom.CustomChange; |
| 8 | |
import liquibase.changelog.ChangeSet; |
| 9 | |
import liquibase.changelog.DatabaseChangeLog; |
| 10 | |
import liquibase.exception.UnexpectedLiquibaseException; |
| 11 | |
import liquibase.serializer.ChangeLogSerializer; |
| 12 | |
import liquibase.sql.visitor.SqlVisitor; |
| 13 | |
import liquibase.util.StringUtils; |
| 14 | |
|
| 15 | |
import java.lang.reflect.Field; |
| 16 | |
import java.util.Collection; |
| 17 | |
import java.util.List; |
| 18 | |
import java.util.Map; |
| 19 | |
import java.util.SortedSet; |
| 20 | |
import java.util.TreeSet; |
| 21 | |
import java.io.OutputStream; |
| 22 | |
import java.io.IOException; |
| 23 | |
|
| 24 | 625 | public class StringChangeLogSerializer implements ChangeLogSerializer { |
| 25 | |
|
| 26 | |
private static final int INDENT_LENGTH = 4; |
| 27 | |
|
| 28 | |
public String[] getValidFileExtensions() { |
| 29 | 8 | return new String[] { "txt" }; |
| 30 | |
} |
| 31 | |
|
| 32 | |
public String serialize(DatabaseChangeLog databaseChangeLog) { |
| 33 | 0 | return null; |
| 34 | |
} |
| 35 | |
|
| 36 | |
public String serialize(Change change) { |
| 37 | 617 | return change.getChangeMetaData().getName() + ":" + serializeObject(change, 1); |
| 38 | |
} |
| 39 | |
|
| 40 | |
public String serialize(SqlVisitor visitor) { |
| 41 | 0 | return visitor.getName() + ":" + serializeObject(visitor, 1); |
| 42 | |
} |
| 43 | |
|
| 44 | |
private String serializeObject(Object objectToSerialize, int indent) { |
| 45 | |
try { |
| 46 | 706 | StringBuffer buffer = new StringBuffer(); |
| 47 | 706 | buffer.append("["); |
| 48 | |
|
| 49 | 706 | SortedSet<String> values = new TreeSet<String>(); |
| 50 | 706 | Class<? extends Object> classToCheck = objectToSerialize.getClass(); |
| 51 | 2040 | while (!classToCheck.equals(Object.class)) { |
| 52 | 8073 | for (Field field : classToCheck.getDeclaredFields()) { |
| 53 | 6739 | field.setAccessible(true); |
| 54 | 6739 | ChangeProperty changePropertyAnnotation = field.getAnnotation(ChangeProperty.class); |
| 55 | 6739 | if (changePropertyAnnotation != null && !changePropertyAnnotation.includeInSerialization()) { |
| 56 | 1901 | continue; |
| 57 | |
} |
| 58 | 4838 | if (field.getName().equals("serialVersionUID")) { |
| 59 | 0 | continue; |
| 60 | |
} |
| 61 | 4838 | if (field.getName().equals("$VRc")) { |
| 62 | 0 | continue; |
| 63 | |
} |
| 64 | 4838 | if (field.getName().equals("serialVersionUID")) { |
| 65 | 0 | continue; |
| 66 | |
} |
| 67 | |
|
| 68 | 4838 | String propertyName = field.getName(); |
| 69 | |
|
| 70 | 4838 | Object value = field.get(objectToSerialize); |
| 71 | 4838 | if (value instanceof ColumnConfig) { |
| 72 | 0 | values.add(indent(indent) |
| 73 | |
+ serializeColumnConfig((ColumnConfig) field.get(objectToSerialize), indent + 1)); |
| 74 | 4838 | } else if (value instanceof ConstraintsConfig) { |
| 75 | 10 | values.add(indent(indent) |
| 76 | |
+ serializeConstraintsConfig((ConstraintsConfig) field.get(objectToSerialize), |
| 77 | |
indent + 1)); |
| 78 | 4828 | } else if (value instanceof CustomChange) { |
| 79 | 0 | values.add(indent(indent) |
| 80 | |
+ serializeCustomChange((CustomChange) field.get(objectToSerialize), indent + 1)); |
| 81 | |
} else { |
| 82 | 4828 | if (value != null) { |
| 83 | 2468 | if (value instanceof Map) { |
| 84 | 2 | values.add(indent(indent) + propertyName + "=" |
| 85 | |
+ serializeObject((Map) value, indent + 1)); |
| 86 | 2466 | } else if (value instanceof Collection) { |
| 87 | 96 | values.add(indent(indent) + propertyName + "=" |
| 88 | |
+ serializeObject((Collection) value, indent + 1)); |
| 89 | |
} else { |
| 90 | 2370 | values.add(indent(indent) + propertyName + "=\"" + value.toString() + "\""); |
| 91 | |
} |
| 92 | |
} |
| 93 | |
} |
| 94 | |
} |
| 95 | 1334 | classToCheck = classToCheck.getSuperclass(); |
| 96 | |
} |
| 97 | |
|
| 98 | 706 | if (values.size() > 0) { |
| 99 | 678 | buffer.append("\n"); |
| 100 | 678 | buffer.append(StringUtils.join(values, "\n")); |
| 101 | 678 | buffer.append("\n"); |
| 102 | |
} |
| 103 | 706 | buffer.append(indent(indent - 1)).append("]"); |
| 104 | 706 | return buffer.toString().replace("\r\n", "\n").replace("\r", "\n"); |
| 105 | |
|
| 106 | 0 | } catch (Exception e) { |
| 107 | 0 | throw new UnexpectedLiquibaseException(e); |
| 108 | |
} |
| 109 | |
|
| 110 | |
} |
| 111 | |
|
| 112 | |
private String indent(int indent) { |
| 113 | 3325 | return StringUtils.repeat(" ", INDENT_LENGTH * indent); |
| 114 | |
} |
| 115 | |
|
| 116 | |
private String serializeObject(Collection collection, int indent) { |
| 117 | 96 | if (collection.size() == 0) { |
| 118 | 46 | return "[]"; |
| 119 | |
} |
| 120 | |
|
| 121 | 50 | String returnString = "[\n"; |
| 122 | 50 | for (Object object : collection) { |
| 123 | 83 | if (object instanceof ColumnConfig) { |
| 124 | 79 | returnString += indent(indent) + serializeColumnConfig((ColumnConfig) object, indent + 1) + ",\n"; |
| 125 | |
} else { |
| 126 | 4 | returnString += indent(indent) + object.toString() + ",\n"; |
| 127 | |
} |
| 128 | |
} |
| 129 | 50 | returnString = returnString.replaceFirst(",$", ""); |
| 130 | 50 | returnString += indent(indent - 1) + "]"; |
| 131 | |
|
| 132 | 50 | return returnString; |
| 133 | |
|
| 134 | |
} |
| 135 | |
|
| 136 | |
private String serializeObject(Map collection, int indent) { |
| 137 | 2 | if (collection.size() == 0) { |
| 138 | 0 | return "[]"; |
| 139 | |
} |
| 140 | |
|
| 141 | 2 | String returnString = "{\n"; |
| 142 | 2 | for (Object key : new TreeSet(collection.keySet())) { |
| 143 | 6 | returnString += indent(indent) + key.toString() + "=\"" + collection.get(key) + "\",\n"; |
| 144 | |
} |
| 145 | 2 | returnString = returnString.replaceFirst(",$", ""); |
| 146 | 2 | returnString += indent(indent - 1) + "}"; |
| 147 | |
|
| 148 | 2 | return returnString; |
| 149 | |
|
| 150 | |
} |
| 151 | |
|
| 152 | |
public String serialize(ColumnConfig columnConfig) { |
| 153 | 0 | return null; |
| 154 | |
} |
| 155 | |
|
| 156 | |
private String serializeColumnConfig(ColumnConfig columnConfig, int indent) { |
| 157 | 79 | return "column:" + serializeObject(columnConfig, indent); |
| 158 | |
} |
| 159 | |
|
| 160 | |
private String serializeConstraintsConfig(ConstraintsConfig constraintsConfig, int indent) { |
| 161 | 10 | return "constraints:" + serializeObject(constraintsConfig, indent); |
| 162 | |
} |
| 163 | |
|
| 164 | |
private String serializeCustomChange(CustomChange customChange, int indent) { |
| 165 | 0 | return "customChange:" + serializeObject(customChange, indent); |
| 166 | |
} |
| 167 | |
|
| 168 | |
public String serialize(ChangeSet changeSet) { |
| 169 | 0 | return null; |
| 170 | |
} |
| 171 | |
|
| 172 | |
public void write(List<ChangeSet> changeSets, OutputStream out) throws IOException { |
| 173 | |
|
| 174 | 0 | } |
| 175 | |
} |