Mercurial > hg > openmind
comparison src/main/java/org/mpi/openmind/scripts/MigratePrimeAliases.java @ 112:933d17f95016
new script MigratePrimeAliases to migrate is_prime_alias_X_of.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 14 Aug 2019 20:48:02 +0200 |
parents | |
children | 8acfd380fffe |
comparison
equal
deleted
inserted
replaced
111:71465cead59c | 112:933d17f95016 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package org.mpi.openmind.scripts; | |
5 | |
6 import java.util.ArrayList; | |
7 import java.util.List; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 import org.mpi.openmind.cache.WrapperService; | |
11 import org.mpi.openmind.repository.bo.Entity; | |
12 import org.mpi.openmind.repository.bo.Relation; | |
13 import org.mpi.openmind.repository.services.ServiceRegistry; | |
14 | |
15 /** | |
16 * @author casties | |
17 * | |
18 */ | |
19 public class MigratePrimeAliases { | |
20 private static final String migrateUser = "migrate-alias user"; | |
21 | |
22 private static Logger logger = Logger.getLogger(MigratePrimeAliases.class); | |
23 | |
24 protected static void migrate(String oldRelation, String newRelation, WrapperService wrapper) { | |
25 | |
26 List<Entity> deleteList = new ArrayList<Entity>(); | |
27 List<Entity> saveList = new ArrayList<Entity>(); | |
28 | |
29 int cnt = 0; | |
30 List<Entity> aliasList = wrapper.getEntitiesByDef("ALIAS"); | |
31 for (Entity alias : aliasList) { | |
32 if (alias.isLightweight()) { | |
33 alias = wrapper.getEntityContent(alias); | |
34 } | |
35 // get relation | |
36 List<Relation> rels = alias.getSourceRelations(oldRelation, "*"); | |
37 if (rels.isEmpty()) continue; | |
38 if (rels.size() > 1) { | |
39 logger.error("ALIAS has more than one relation!"); | |
40 System.exit(1); | |
41 } | |
42 Relation rel = rels.get(0); | |
43 Entity tar = wrapper.getEntityById(rel.getTargetId()); | |
44 if (tar.isLightweight()) { | |
45 tar = wrapper.getEntityContent(tar); | |
46 } | |
47 boolean aliasExists = false; | |
48 // get other aliases of target | |
49 List<Relation> otherrels = tar.getTargetRelations(newRelation, "ALIAS"); | |
50 for (Relation otherrel : otherrels) { | |
51 Entity otheralias = wrapper.getEntityById(otherrel.getSourceId()); | |
52 // compare ownvalue | |
53 if (alias.getOwnValue().equals(otheralias.getOwnValue())) { | |
54 aliasExists = true; | |
55 break; | |
56 } | |
57 } | |
58 if (aliasExists) { | |
59 deleteList.add(alias); | |
60 } else { | |
61 // change relation | |
62 rel.setOwnValue(newRelation); | |
63 rel.setObjectClass(newRelation); | |
64 saveList.add(alias); | |
65 } | |
66 if (++cnt % 100 == 0) { | |
67 logger.debug(cnt+" aliases..."); | |
68 } | |
69 } | |
70 // delete obsolete aliases | |
71 for (Entity alias : deleteList) { | |
72 try { | |
73 logger.debug("Deleting obsolete alias: "+alias); | |
74 wrapper.removeCurrentVersionEntity(alias, migrateUser); | |
75 } catch (Exception e) { | |
76 logger.error("Error deleting alias!", e); | |
77 } | |
78 } | |
79 // save changed aliases | |
80 for (Entity alias: saveList) { | |
81 try { | |
82 logger.debug("Saving changed alias: "+alias); | |
83 wrapper.saveEntity(alias, migrateUser, null); | |
84 } catch (Exception e) { | |
85 logger.error("Error saving changed aliases!", e); | |
86 } | |
87 } | |
88 logger.info("Deleted "+deleteList.size()+" aliases."); | |
89 logger.info("Changed "+saveList.size()+" aliases."); | |
90 } | |
91 | |
92 /** | |
93 * @param args | |
94 */ | |
95 public static void main(String[] args) { | |
96 ServiceRegistry services = new ServiceRegistry(); | |
97 services.getWrapper(); | |
98 | |
99 logger.info("Migrating ALIAS from is_prime_alias_name_of to is_alias_name_of"); | |
100 migrate("is_prime_alias_name_of", "is_alias_name_of", services.getWrapper()); | |
101 | |
102 logger.info("Migrating ALIAS from is_prime_alias_title_of to is_alias_title_of"); | |
103 migrate("is_prime_alias_title_of", "is_alias_title_of", services.getWrapper()); | |
104 | |
105 System.exit(0); | |
106 } | |
107 | |
108 } |