1
|
1 package org.mpi.openmind.scripts;
|
|
2
|
|
3 import java.sql.Connection;
|
|
4 import java.sql.DriverManager;
|
|
5 import java.sql.ResultSet;
|
|
6 import java.sql.SQLException;
|
|
7 import java.sql.Statement;
|
|
8 import java.util.ArrayList;
|
|
9 import java.util.List;
|
|
10
|
|
11 import org.apache.commons.lang.StringUtils;
|
|
12
|
|
13 /**
|
|
14 * Some relation, which are marked as CURRENT_VERSION,
|
|
15 * has not the correct srcTimeModif,
|
|
16 * which is necessary to be found from the corresponding entity associated.
|
|
17 * This script does the following:
|
|
18 * -Find the relation with incorrect srcModifTime
|
|
19 * -
|
|
20 * @author jurzua
|
|
21 *
|
|
22 */
|
|
23 public class CurrentVersionSrcRelation {
|
|
24
|
|
25 public static String WITNESS = "WITNESS";
|
|
26 public static String TEXT = "TEXT";
|
|
27 public static String CODEX = "CODEX";
|
|
28 public static String PLACE = "PLACE";
|
|
29 public static String PERSON = "PERSON";
|
|
30 public static String REPOSITORY = "REPOSITORY";
|
|
31 public static String COPY_EVENT = "COPY_EVENT";
|
|
32 public static String COLLECTION = "COLLECTION";
|
|
33 public static String REFERENCE = "REFERENCE";
|
|
34 public static String ROLE = "ROLE";
|
|
35 public static String STUDY_EVENT = "STUDY_EVENT";
|
|
36 public static String SUBJECT = "SUBJECT";
|
|
37 public static String TRANSFER_EVENT = "TRANSFER_EVENT";
|
|
38
|
|
39 private static String query;
|
|
40 private static List<Relation> uniqueRelations;
|
|
41
|
|
42 static{
|
|
43 query =
|
|
44 "select " +
|
|
45 "entity.id as entId, entity.own_value as entOW, entity.modification_time as entModif, entity.version as entVersion, " +
|
|
46 "relation.id as relId, relation.source_modif as relSrcModif, relation.version as relVersion, relation.own_value as relOW, " +
|
|
47 "relation.source_obj_class as relSrcOC, relation.target_obj_class as relTarOC, " +
|
|
48 "relation.source_id as relSrcId, " +
|
|
49 "relation.target_id as relTarId, " +
|
|
50 "relation.row_id as relRowId " +
|
|
51 "from node as entity, node as relation " +
|
|
52 "where " +
|
|
53 "relation.node_type = 'RELATION' AND " +
|
|
54 "entity.node_type = 'ENTITY' AND " +
|
|
55 "relation.system_status = 'CURRENT_VERSION' AND " +
|
|
56 "entity.system_status = 'CURRENT_VERSION' AND " +
|
|
57 "relation.source_id = entity.id AND " +
|
|
58 "relation.source_modif != entity.modification_time ";// +
|
|
59 //"group by entity.id ";
|
|
60
|
|
61 CurrentVersionSrcRelation tthis = new CurrentVersionSrcRelation();
|
|
62 uniqueRelations = new ArrayList<CurrentVersionSrcRelation.Relation>();
|
|
63 uniqueRelations.add(tthis.new Relation(WITNESS, "is_exemplar_of", TEXT));
|
|
64 uniqueRelations.add(tthis.new Relation(WITNESS, "is_part_of", CODEX));
|
|
65 uniqueRelations.add(tthis.new Relation(WITNESS, "was_copied_by", PERSON));
|
|
66 uniqueRelations.add(tthis.new Relation(WITNESS, "was_copied_in", PLACE));
|
|
67 uniqueRelations.add(tthis.new Relation(WITNESS, "was_created_by", PERSON));
|
|
68 uniqueRelations.add(tthis.new Relation(WITNESS, "was_created_in", PLACE));
|
|
69
|
|
70 uniqueRelations.add(tthis.new Relation(PLACE, "is_part_of", PLACE));
|
|
71
|
|
72 uniqueRelations.add(tthis.new Relation(REPOSITORY, "is_in", PLACE));
|
|
73
|
|
74 uniqueRelations.add(tthis.new Relation(COLLECTION, "is_part_of", REPOSITORY));
|
|
75
|
|
76 uniqueRelations.add(tthis.new Relation(CODEX, "is_part_of", COLLECTION));
|
|
77 uniqueRelations.add(tthis.new Relation(CODEX, "is_part_of", REPOSITORY));
|
|
78
|
|
79 uniqueRelations.add(tthis.new Relation(PERSON, "was_born_in", PLACE));
|
|
80 uniqueRelations.add(tthis.new Relation(PERSON, "died_in", PLACE));
|
|
81
|
|
82 uniqueRelations.add(tthis.new Relation(COPY_EVENT, "is_a_copy_of", WITNESS));
|
|
83 uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_in", PLACE));
|
|
84 uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_in", REPOSITORY));
|
|
85 uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_for", PERSON));
|
|
86
|
|
87 uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "is_a_study_of", WITNESS));
|
|
88 uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_advised_by", PERSON));
|
|
89 uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_by", PERSON));
|
|
90 uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_in", REPOSITORY));
|
|
91 uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_in", PLACE));
|
|
92
|
|
93 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_new_location", PLACE));
|
|
94 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_new_location", REPOSITORY));
|
|
95 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_original_location", PLACE));
|
|
96 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_original_location", REPOSITORY));
|
|
97 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_from", PLACE));
|
|
98 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_from", REPOSITORY));
|
|
99 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_in", PLACE));
|
|
100 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_in", REPOSITORY));
|
|
101 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_to", PLACE));
|
|
102 uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_to", REPOSITORY));
|
|
103
|
|
104 uniqueRelations.add(tthis.new Relation(TEXT, "is_version_of", TEXT));
|
|
105 uniqueRelations.add(tthis.new Relation(TEXT, "was_created_by", PERSON));
|
|
106 uniqueRelations.add(tthis.new Relation(TEXT, "was_created_in", PLACE));
|
|
107 uniqueRelations.add(tthis.new Relation(TEXT, "is_translation_of", TEXT));
|
|
108 uniqueRelations.add(tthis.new Relation(TEXT, "was_dedicated_to", TEXT));
|
|
109 uniqueRelations.add(tthis.new Relation(TEXT, "is_commentary_on", TEXT));
|
|
110 }
|
|
111
|
|
112 public static ResultSet select(Connection conn, String arg){
|
|
113 CurrentVersionSrcRelation tthis = new CurrentVersionSrcRelation();
|
|
114 ResultSet rs = null;
|
|
115 try{
|
|
116 Statement st = conn.createStatement();
|
|
117 rs = st.executeQuery(query);
|
|
118 int count = 0;
|
|
119 int case1 =0;
|
|
120 int case2 =0;
|
|
121 int case3 =0;
|
|
122 while (rs.next()){
|
|
123 Entity ent = tthis.new Entity(rs);
|
|
124 Relation rel = tthis.new Relation(rs);
|
|
125
|
|
126 System.out.println(rel.toString());
|
|
127
|
|
128 if(rel.srcId.equals(new Long(51270))){
|
|
129 System.out.println("*****************");
|
|
130 }
|
|
131
|
|
132 Relation otherRel = getCurrentRelation(ent, rel, conn);
|
|
133 if(otherRel == null){
|
|
134 System.out.println("\tCASE1");
|
|
135 case1++;
|
|
136 makeRelationCurrentVersion(conn, rel, ent);
|
|
137 }else if(shouldBeRelationUniqueForMetadaten(rel)){
|
|
138 System.out.println("\tCASE2");
|
|
139 case2++;
|
|
140 makeRelationPreviuosVersion(conn, rel);
|
|
141 }else{
|
|
142 System.out.println("\tCASE3");
|
|
143 case3++;
|
|
144 makeRelationCurrentVersion(conn, rel, ent);
|
|
145 }
|
|
146 count++;
|
|
147 }
|
|
148 System.out.println( "*****************");
|
|
149 System.out.println("CASE1=" + case1 + " no exist other similar relation, the old should be refreshed");
|
|
150 System.out.println("CASE2=" + case2 + " this relation should be unique, but there is more than one (DELETING).");
|
|
151 System.out.println("CASE3=" + case3 + " this relation can exist more tha one instance");
|
|
152 System.out.println("TOTAL=" + count);
|
|
153 System.out.println("*****************");
|
|
154 }catch (SQLException ex){
|
|
155 ex.printStackTrace();
|
|
156 }
|
|
157 return rs;
|
|
158 }
|
|
159
|
|
160
|
|
161 public static void makeRelationPreviuosVersion(Connection conn, Relation rel) throws SQLException{
|
|
162 System.out.println("Making PREVIOUS...");
|
|
163 Statement st = conn.createStatement();
|
|
164 //st.executeUpdate("DELETE FROM node WHERE row_id = '"+ rel.rowId + "'");
|
|
165 st.executeUpdate("UPDATE node SET system_status='PREVIOUS_VERSION' WHERE row_id='"+ rel.rowId +"'");
|
|
166 }
|
|
167
|
|
168 public static void makeRelationCurrentVersion(Connection conn, Relation rel, Entity ent) throws SQLException{
|
|
169 System.out.println("Making Current Version...");
|
|
170 Statement st = conn.createStatement();
|
|
171 st.executeUpdate("UPDATE node SET source_modif='" + ent.modif + "' WHERE row_id='"+ rel.rowId +"'");
|
|
172 }
|
|
173
|
|
174 public static boolean shouldBeRelationUniqueForMetadaten(Relation rel){
|
|
175 if(uniqueRelations.contains(rel)){
|
|
176 return true;
|
|
177 }
|
|
178 return false;
|
|
179 }
|
|
180
|
|
181 public static Relation getCurrentRelation(Entity ent, Relation rel, Connection conn){
|
|
182 ResultSet rs = null;
|
|
183 CurrentVersionSrcRelation tthis = new CurrentVersionSrcRelation();
|
|
184
|
|
185 String relQuery =
|
|
186 "select " +
|
|
187 "relation.id as relId, " +
|
|
188 "relation.source_modif as relSrcModif, " +
|
|
189 "relation.version as relVersion, " +
|
|
190 "relation.own_value as relOW, " +
|
|
191 "relation.source_id as relSrcId, " +
|
|
192 "relation.target_id as relTarId, " +
|
|
193 "relation.source_obj_class as relSrcOC, " +
|
|
194 "relation.target_obj_class as relTarOC, " +
|
|
195 "relation.row_id as relRowId " +
|
|
196 "from node as relation " +
|
|
197 "where " +
|
|
198 "relation.node_type = 'RELATION' AND " +
|
|
199 "relation.system_status = 'CURRENT_VERSION' AND " +
|
|
200 "relation.source_id = '" + rel.srcId + "' AND " +
|
|
201 //"relation.target_id = '" + rel.tarId + "' AND " +
|
|
202 "relation.source_modif = '" + ent.modif + "' ";
|
|
203
|
|
204 try{
|
|
205 Statement st = conn.createStatement();
|
|
206 rs = st.executeQuery(relQuery);
|
|
207 Relation otherRel = null;
|
|
208 while(rs.next()){
|
|
209 otherRel = tthis.new Relation(rs);
|
|
210 break;
|
|
211 }
|
|
212 return otherRel;
|
|
213 }catch (SQLException ex){
|
|
214 ex.printStackTrace();
|
|
215 }
|
|
216
|
|
217 return null;
|
|
218 }
|
|
219
|
|
220
|
|
221
|
|
222 public static void execute() {
|
|
223 try {
|
|
224 Connection conn;
|
|
225
|
|
226 Class.forName("com.mysql.jdbc.Driver").newInstance();
|
|
227 String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8";
|
|
228 conn = DriverManager.getConnection(url, "ismi", "ismipw");
|
|
229
|
|
230 ResultSet rs = select(conn, "");
|
|
231
|
|
232 conn.close();
|
|
233 } catch (ClassNotFoundException ex) {
|
|
234 System.err.println(ex.getMessage());
|
|
235 } catch (IllegalAccessException ex) {
|
|
236 System.err.println(ex.getMessage());
|
|
237 } catch (InstantiationException ex) {
|
|
238 System.err.println(ex.getMessage());
|
|
239 } catch (SQLException ex) {
|
|
240 System.err.println(ex.getMessage());
|
|
241 }
|
|
242 }
|
|
243
|
|
244 public static void main(String[] args) {
|
|
245
|
|
246 CurrentVersionSrcRelation.execute();
|
|
247 System.exit(0);
|
|
248 }
|
|
249
|
|
250 private class Entity{
|
|
251 public Long id;
|
|
252 public Long modif;
|
|
253 public String ow;
|
|
254 public Entity(ResultSet rs){
|
|
255 try {
|
|
256 //"entity.id as entId, entity.own_value as entOW, entity.modification_time as entModif, entity.version as entVersion, " +
|
|
257 this.modif = rs.getLong("entModif");
|
|
258 this.id = rs.getLong("entId");
|
|
259 this.ow = rs.getString("entOW");
|
|
260 } catch (SQLException e) {
|
|
261 System.out.println();
|
|
262 e.printStackTrace();
|
|
263 System.out.println();
|
|
264 }
|
|
265 }
|
|
266 }
|
|
267
|
|
268 private class Relation{
|
|
269 public Long id;
|
|
270 public Long srcModif;
|
|
271 public Long srcId;
|
|
272 public Long tarId;
|
|
273 public String ow;
|
|
274 public String srcOC;
|
|
275 public String tarOC;
|
|
276 public String rowId;
|
|
277
|
|
278 public Relation(String srcOC, String ow, String tarOC){
|
|
279 this.ow = ow;
|
|
280 this.srcOC = srcOC;
|
|
281 this.tarOC = tarOC;
|
|
282 }
|
|
283
|
|
284 public Relation(ResultSet rs){
|
|
285 //s relId, relation.source_modif as relSrcModif, relation.version as relVersion, relation.own_value as relOW
|
|
286 try {
|
|
287 this.srcModif = rs.getLong("relSrcModif");
|
|
288 this.id = rs.getLong("relId");
|
|
289 this.ow = rs.getString("relOW");
|
|
290 this.srcOC = rs.getString("relSrcOC");
|
|
291 this.tarOC = rs.getString("relTarOC");
|
|
292 this.rowId = rs.getString("relRowId");
|
|
293 this.srcId = rs.getLong("relSrcId");
|
|
294 this.tarId = rs.getLong("relTarId");
|
|
295 } catch (SQLException e) {
|
|
296 System.out.println();
|
|
297 e.printStackTrace();
|
|
298 System.out.println();
|
|
299 }
|
|
300 }
|
|
301 @Override
|
|
302 public boolean equals(Object obj){
|
|
303 if(obj != null){
|
|
304 if(obj instanceof Relation){
|
|
305 Relation other = (Relation)obj;
|
|
306 if(StringUtils.equals(this.ow, other.ow) &&
|
|
307 StringUtils.equals(this.srcOC, other.srcOC) &&
|
|
308 StringUtils.equals(this.tarOC, other.tarOC)){
|
|
309 return true;
|
|
310 }
|
|
311 }
|
|
312 }
|
|
313 return false;
|
|
314 }
|
|
315
|
|
316 @Override
|
|
317 public String toString(){
|
|
318 StringBuilder sb = new StringBuilder();
|
|
319 sb.append("ROW_ID=" + this.rowId + "\t");
|
|
320 sb.append(this.srcOC + " [" + this.ow + "] " + this.tarOC + "\t SrcID=" + this.srcId + "\t TarID=" + this.tarId);
|
|
321 return sb.toString();
|
|
322 }
|
|
323 }
|
|
324 }
|