1 | |
package org.jtheque.core.managers.schema; |
2 | |
|
3 | |
import org.jtheque.core.managers.AbstractActivableManager; |
4 | |
import org.jtheque.core.managers.ManagerException; |
5 | |
import org.jtheque.core.managers.Managers; |
6 | |
import org.jtheque.core.managers.error.IErrorManager; |
7 | |
import org.jtheque.core.managers.error.InternationalizedError; |
8 | |
import org.jtheque.core.managers.log.ILoggingManager; |
9 | |
import org.jtheque.core.managers.state.IStateManager; |
10 | |
import org.jtheque.core.managers.state.StateException; |
11 | |
import org.jtheque.utils.bean.Version; |
12 | |
|
13 | |
import java.io.File; |
14 | |
import java.util.ArrayList; |
15 | |
import java.util.Collection; |
16 | |
import java.util.Collections; |
17 | |
import java.util.List; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public final class SchemaManager extends AbstractActivableManager implements ISchemaManager { |
42 | 0 | private final List<Schema> schemas = new ArrayList<Schema>(10); |
43 | |
|
44 | |
private SchemaConfiguration configuration; |
45 | |
|
46 | |
@Override |
47 | |
public void init() throws ManagerException { |
48 | 0 | loadConfiguration(); |
49 | |
|
50 | 0 | Collections.sort(schemas); |
51 | |
|
52 | 0 | boolean recoveryNecessary = checkForRecovery(); |
53 | |
|
54 | 0 | if (recoveryNecessary) { |
55 | 0 | recoverData(); |
56 | |
} |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private void recoverData() { |
63 | 0 | File logFile = new File(Managers.getCore().getFolders().getApplicationFolder(), "localhost.log"); |
64 | 0 | File scriptFile = new File(Managers.getCore().getFolders().getApplicationFolder(), "localhost.script"); |
65 | |
|
66 | 0 | Collection<Insert> inserts = new ArrayList<Insert>(50); |
67 | |
|
68 | 0 | read(logFile, inserts); |
69 | 0 | read(scriptFile, inserts); |
70 | |
|
71 | 0 | for (Schema schema : schemas) { |
72 | 0 | recoverIfNecessary(inserts, schema); |
73 | |
} |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private void recoverIfNecessary(Iterable<Insert> inserts, Schema schema) { |
83 | 0 | if (configuration.isNotRecovered(schema.getId())) { |
84 | 0 | schema.importDataFromHSQL(inserts); |
85 | |
|
86 | 0 | configuration.setRecovered(schema.getId()); |
87 | |
} |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
private static void read(File file, Collection<Insert> inserts) { |
97 | 0 | if (file.exists()) { |
98 | 0 | inserts.addAll(HSQLFileReader.readFile(file)); |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
private boolean checkForRecovery() { |
108 | 0 | boolean dataToRecover = false; |
109 | |
|
110 | 0 | for (Schema schema : schemas) { |
111 | 0 | Version installedVersion = configuration.getVersion(schema.getId()); |
112 | |
|
113 | 0 | if (installedVersion == null) { |
114 | 0 | schema.install(); |
115 | |
|
116 | 0 | configuration.setVersion(schema.getId(), schema.getVersion()); |
117 | 0 | } else if (schema.getVersion().isGreaterThan(installedVersion)) { |
118 | 0 | schema.update(installedVersion); |
119 | |
} |
120 | |
|
121 | 0 | if (configuration.isNotRecovered(schema.getId())) { |
122 | 0 | dataToRecover = true; |
123 | |
} |
124 | 0 | } |
125 | |
|
126 | 0 | return dataToRecover; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
private void loadConfiguration() { |
133 | |
try { |
134 | 0 | configuration = Managers.getManager(IStateManager.class).getOrCreateState(SchemaConfiguration.class); |
135 | 0 | } catch (StateException e) { |
136 | 0 | configuration = new SchemaConfiguration(); |
137 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
138 | 0 | Managers.getManager(IErrorManager.class).addStartupError(new InternationalizedError("error.loading.configuration")); |
139 | 0 | } |
140 | 0 | } |
141 | |
|
142 | |
@Override |
143 | |
public void registerSchema(Schema schema) { |
144 | 0 | schemas.add(schema); |
145 | 0 | } |
146 | |
|
147 | |
@Override |
148 | |
public void unregisterSchema(Schema schema) { |
149 | 0 | schemas.remove(schema); |
150 | 0 | } |
151 | |
} |