1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.melati.admin;
45
46 import java.util.Enumeration;
47
48 import org.melati.LogicalDatabase;
49 import org.melati.Melati;
50 import org.melati.poem.Database;
51 import org.melati.poem.Field;
52 import org.melati.poem.Persistent;
53 import org.melati.poem.PoemTask;
54 import org.melati.poem.PoemThread;
55 import org.melati.poem.Table;
56 import org.melati.poem.util.MappedEnumeration;
57 import org.melati.servlet.PathInfoException;
58 import org.melati.servlet.TemplateServlet;
59 import org.melati.template.ServletTemplateContext;
60
61
62
63
64
65
66
67
68
69 public class Copy extends TemplateServlet {
70
71
72
73
74 private static final long serialVersionUID = 1L;
75 static Database fromDb = null;
76 static Database toDb = null;
77
78
79
80
81
82
83 protected void prePoemSession(Melati melati) throws Exception {
84 super.prePoemSession(melati);
85 String[] parts = melati.getPathInfoParts();
86 if (parts.length != 2)
87 throw new PathInfoException("Two database names expecetd");
88 String fromDbName = parts[0];
89 String toDbName = parts[1];
90
91 copy(fromDbName, toDbName);
92
93 }
94
95
96
97
98
99 protected String doTemplateRequest(Melati melati,
100 ServletTemplateContext templateContext) throws Exception {
101 melati.setPassbackExceptionHandling();
102 melati.setResponseContentType("text/html");
103 templateContext.put("admin", new AdminUtils(melati));
104
105 String[] parts = melati.getPathInfoParts();
106 String fromDbName = parts[0];
107 String toDbName = parts[1];
108
109 templateContext.put("fromDbName",fromDbName);
110 templateContext.put("toDbName",toDbName);
111 return "org/melati/admin/CopyDone";
112 }
113
114
115
116
117
118
119 public static Database copy(String from, String to) {
120 fromDb = LogicalDatabase.getDatabase(from);
121 toDb = LogicalDatabase.getDatabase(to);
122 return copy();
123 }
124
125
126
127
128
129 public static Database copy(Database fromDbIn, final Database toDbIn) {
130 fromDb = fromDbIn;
131 toDb = toDbIn;
132 return copy();
133 }
134
135
136
137 public static Database copy() {
138 if (fromDb.getClass() != toDb.getClass())
139 throw new AnticipatedException("Both from(" + fromDb.getClass() + ") and " +
140 "to(" + toDb.getClass() + ") databases " +
141 "must be of the same class");
142 toDb.inSessionAsRoot(
143 new PoemTask() {
144 public void run() {
145 System.err.println("PoemThread " + PoemThread.database().getDisplayName());
146 try {
147 Enumeration<Table> en = fromDb.displayTables(null);
148 while(en.hasMoreElements()) {
149 Table fromTable = (Table)en.nextElement();
150 String fromTableName = fromTable.getName();
151 System.err.println("From " + fromDb + " table " + fromTableName);
152 Table toTable = toDb.getTable(fromTableName);
153 int count = toTable.count();
154 if (count != 0 ) {
155 System.err.println("Skipping " + toTable.getName() + " as it contains " + count + " records." );
156 } else {
157 System.err.println(toTable.getName() + " in both and empty in destination.");
158 Enumeration<Persistent> recs = objectsFromTroids(
159 fromTable.troidSelection((String)null,
160 (String)null, false, null),
161 fromTable);
162 while (recs.hasMoreElements()) {
163 Persistent p = (Persistent)recs.nextElement();
164 Persistent p2 = toTable.newPersistent();
165 Enumeration<Field> fields = p.getFields();
166 while (fields.hasMoreElements()) {
167 Field f = fields.nextElement();
168 p2.setRaw(f.getName(), f.getRaw());
169 }
170 p2.makePersistent();
171 System.err.println("Created:" + p2.displayString());
172 }
173 }
174 }
175 } catch (Throwable e) {
176 e.printStackTrace();
177 e.fillInStackTrace();
178 throw new RuntimeException(e);
179 }
180 }
181 public String toString() {
182 return "Copying";
183 }
184 });
185 return toDb;
186
187 }
188 static Enumeration<Persistent> objectsFromTroids(Enumeration<Integer> troids, final Table t) {
189 return new MappedEnumeration<Persistent, Integer>(troids) {
190 public Persistent mapped(Integer troid) {
191 return t.getObject(troid);
192 }
193 };
194 }
195
196 }