|
1 | | -package ru.fusionsoft.dbgit.meta; |
2 | | - |
3 | | -import java.io.IOException; |
4 | | -import java.io.InputStream; |
5 | | -import java.io.OutputStream; |
6 | | -import java.nio.charset.Charset; |
7 | | - |
8 | | -import org.yaml.snakeyaml.DumperOptions; |
9 | | -import org.yaml.snakeyaml.Yaml; |
10 | | -import org.yaml.snakeyaml.DumperOptions.ScalarStyle; |
11 | | -import org.yaml.snakeyaml.nodes.Tag; |
12 | | - |
13 | | -import ru.fusionsoft.dbgit.adapters.AdapterFactory; |
14 | | -import ru.fusionsoft.dbgit.core.DBGit; |
15 | | -import ru.fusionsoft.dbgit.core.DBGitPath; |
16 | | -import ru.fusionsoft.dbgit.core.ExceptionDBGit; |
17 | | -import ru.fusionsoft.dbgit.core.ExceptionDBGitRunTime; |
18 | | -import ru.fusionsoft.dbgit.core.db.DbType; |
19 | | -import ru.fusionsoft.dbgit.utils.ConsoleWriter; |
20 | | -import ru.fusionsoft.dbgit.yaml.DBGitYamlConstructor; |
21 | | -import ru.fusionsoft.dbgit.yaml.DBGitYamlRepresenter; |
22 | | -import ru.fusionsoft.dbgit.yaml.YamlOrder; |
23 | | - |
24 | | - |
25 | | -/** |
26 | | - * Base class for all meta objects |
27 | | - * @author mikle |
28 | | - * |
29 | | - */ |
30 | | -public abstract class MetaBase implements IMetaObject { |
31 | | - @YamlOrder(0) |
32 | | - protected String name; |
33 | | - |
34 | | - @YamlOrder(1) |
35 | | - protected DbType dbType; |
36 | | - |
37 | | - @YamlOrder(1) |
38 | | - protected String dbVersion; |
39 | | - |
40 | | - @Override |
41 | | - public String getName() { |
42 | | - return name; |
43 | | - } |
44 | | - |
45 | | - @Override |
46 | | - public void setDbType(DbType dbType) { |
47 | | - this.dbType = dbType; |
48 | | - } |
49 | | - |
50 | | - @Override |
51 | | - public DbType getDbType() { |
52 | | - return dbType; |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public void setDbVersion(String dbVersion) { |
57 | | - this.dbVersion = dbVersion; |
58 | | - } |
59 | | - |
60 | | - @Override |
61 | | - public String getDbVersion() { |
62 | | - return dbVersion; |
63 | | - } |
64 | | - |
65 | | - @Override |
66 | | - public void setName(String name) throws ExceptionDBGit { |
67 | | - this.name = name; |
68 | | - } |
69 | | - |
70 | | - @Override |
71 | | - public String getFileName() { |
72 | | - return getName(); |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * <div class="en">When you save the yaml object, the library ignores properties for which there is no getter and setter</div> |
77 | | - * <div class="ru">При сохранении объекта yaml библиотека игнорирует свойства для которых нет геттера и сеттера</div> |
78 | | - * @param stream |
79 | | - * @throws IOException |
80 | | - */ |
81 | | - public boolean yamlSerialize(OutputStream stream) throws IOException { |
82 | | - Yaml yaml = createYaml(); |
83 | | - String output = yaml.dumpAs(this, Tag.MAP, DumperOptions.FlowStyle.BLOCK); |
84 | | - |
85 | | - stream.write(output.getBytes(Charset.forName("UTF-8"))); |
86 | | - return true; |
87 | | - } |
88 | | - |
89 | | - public IMetaObject yamlDeSerialize(InputStream stream) { |
90 | | - Yaml yaml = createYaml(); |
91 | | - |
92 | | - IMetaObject meta = yaml.loadAs(stream, this.getClass()); |
93 | | - return meta; |
94 | | - } |
95 | | - |
96 | | - public Yaml createYaml() { |
97 | | - DumperOptions options = new DumperOptions(); |
98 | | - options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
99 | | - options.setPrettyFlow(true); |
100 | | - Yaml yaml = new Yaml(new DBGitYamlConstructor(), new DBGitYamlRepresenter(), options); |
101 | | - return yaml; |
102 | | - } |
103 | | - |
104 | | - @Override |
105 | | - public int addToGit() throws ExceptionDBGit { |
106 | | - DBGit dbGit = DBGit.getInstance(); |
107 | | - dbGit.addFileToIndexGit(DBGitPath.DB_GIT_PATH+"/"+getFileName()); |
108 | | - return 1; |
109 | | - } |
110 | | - |
111 | | - @Override |
112 | | - public int removeFromGit() throws ExceptionDBGit { |
113 | | - DBGit dbGit = DBGit.getInstance(); |
114 | | - dbGit.removeFileFromIndexGit(DBGitPath.DB_GIT_PATH+"/"+getFileName()); |
115 | | - return 1; |
116 | | - } |
117 | | - |
118 | | - public void setDbType() { |
119 | | - try { |
120 | | - setDbType(AdapterFactory.createAdapter().getDbType()); |
121 | | - } catch (ExceptionDBGit e) { |
122 | | - throw new ExceptionDBGitRunTime(e.getLocalizedMessage()); |
123 | | - } |
124 | | - |
125 | | - } |
126 | | - |
127 | | - public void setDbVersion() { |
128 | | - try { |
129 | | - setDbVersion(AdapterFactory.createAdapter().getDbVersion()); |
130 | | - } catch (ExceptionDBGit e) { |
131 | | - throw new ExceptionDBGitRunTime(e.getLocalizedMessage()); |
132 | | - } |
133 | | - |
134 | | - } |
135 | | - |
136 | | - |
137 | | -} |
| 1 | +package ru.fusionsoft.dbgit.meta; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.nio.charset.Charset; |
| 7 | +import java.util.regex.Matcher; |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +import org.yaml.snakeyaml.DumperOptions; |
| 11 | +import org.yaml.snakeyaml.Yaml; |
| 12 | +import org.yaml.snakeyaml.DumperOptions.ScalarStyle; |
| 13 | +import org.yaml.snakeyaml.nodes.Tag; |
| 14 | + |
| 15 | +import ru.fusionsoft.dbgit.adapters.AdapterFactory; |
| 16 | +import ru.fusionsoft.dbgit.core.DBGit; |
| 17 | +import ru.fusionsoft.dbgit.core.DBGitPath; |
| 18 | +import ru.fusionsoft.dbgit.core.ExceptionDBGit; |
| 19 | +import ru.fusionsoft.dbgit.core.ExceptionDBGitRunTime; |
| 20 | +import ru.fusionsoft.dbgit.core.db.DbType; |
| 21 | +import ru.fusionsoft.dbgit.utils.ConsoleWriter; |
| 22 | +import ru.fusionsoft.dbgit.yaml.DBGitYamlConstructor; |
| 23 | +import ru.fusionsoft.dbgit.yaml.DBGitYamlRepresenter; |
| 24 | +import ru.fusionsoft.dbgit.yaml.YamlOrder; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Base class for all meta objects |
| 29 | + * @author mikle |
| 30 | + * |
| 31 | + */ |
| 32 | +public abstract class MetaBase implements IMetaObject { |
| 33 | + @YamlOrder(0) |
| 34 | + protected String name; |
| 35 | + |
| 36 | + @YamlOrder(1) |
| 37 | + protected DbType dbType; |
| 38 | + |
| 39 | + @YamlOrder(1) |
| 40 | + protected String dbVersion; |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getName() { |
| 44 | + return name; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void setDbType(DbType dbType) { |
| 49 | + this.dbType = dbType; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public DbType getDbType() { |
| 54 | + return dbType; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void setDbVersion(String dbVersion) { |
| 59 | + this.dbVersion = dbVersion; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String getDbVersion() { |
| 64 | + return dbVersion; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Double getDbVersionNumber() { |
| 69 | + Matcher matcher = Pattern.compile("\\D*(\\d+)\\.(\\d+)").matcher(getDbVersion()); |
| 70 | + matcher.find(); |
| 71 | + Double result = Double.valueOf(matcher.group(0)+matcher.group(1)); |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void setName(String name) throws ExceptionDBGit { |
| 77 | + this.name = name; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String getFileName() { |
| 82 | + return getName(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * <div class="en">When you save the yaml object, the library ignores properties for which there is no getter and setter</div> |
| 87 | + * <div class="ru">При сохранении объекта yaml библиотека игнорирует свойства для которых нет геттера и сеттера</div> |
| 88 | + * @param stream |
| 89 | + * @throws IOException |
| 90 | + */ |
| 91 | + public boolean yamlSerialize(OutputStream stream) throws IOException { |
| 92 | + Yaml yaml = createYaml(); |
| 93 | + String output = yaml.dumpAs(this, Tag.MAP, DumperOptions.FlowStyle.BLOCK); |
| 94 | + |
| 95 | + stream.write(output.getBytes(Charset.forName("UTF-8"))); |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + public IMetaObject yamlDeSerialize(InputStream stream) { |
| 100 | + Yaml yaml = createYaml(); |
| 101 | + |
| 102 | + IMetaObject meta = yaml.loadAs(stream, this.getClass()); |
| 103 | + return meta; |
| 104 | + } |
| 105 | + |
| 106 | + public Yaml createYaml() { |
| 107 | + DumperOptions options = new DumperOptions(); |
| 108 | + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
| 109 | + options.setPrettyFlow(true); |
| 110 | + Yaml yaml = new Yaml(new DBGitYamlConstructor(), new DBGitYamlRepresenter(), options); |
| 111 | + return yaml; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public int addToGit() throws ExceptionDBGit { |
| 116 | + DBGit dbGit = DBGit.getInstance(); |
| 117 | + dbGit.addFileToIndexGit(DBGitPath.DB_GIT_PATH+"/"+getFileName()); |
| 118 | + return 1; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int removeFromGit() throws ExceptionDBGit { |
| 123 | + DBGit dbGit = DBGit.getInstance(); |
| 124 | + dbGit.removeFileFromIndexGit(DBGitPath.DB_GIT_PATH+"/"+getFileName()); |
| 125 | + return 1; |
| 126 | + } |
| 127 | + |
| 128 | + public void setDbType() { |
| 129 | + try { |
| 130 | + setDbType(AdapterFactory.createAdapter().getDbType()); |
| 131 | + } catch (ExceptionDBGit e) { |
| 132 | + throw new ExceptionDBGitRunTime(e.getLocalizedMessage()); |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + public void setDbVersion() { |
| 138 | + try { |
| 139 | + setDbVersion(AdapterFactory.createAdapter().getDbVersion()); |
| 140 | + } catch (ExceptionDBGit e) { |
| 141 | + throw new ExceptionDBGitRunTime(e.getLocalizedMessage()); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | +} |
0 commit comments