changeset 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 71465cead59c
children 8dc4ac635d11
files src/main/java/org/mpi/openmind/repository/bo/utils/EntitySortByNormalizedOwnValue.java src/main/java/org/mpi/openmind/scripts/MigratePrimeAliases.java
diffstat 2 files changed, 111 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/repository/bo/utils/EntitySortByNormalizedOwnValue.java	Mon Aug 12 18:05:16 2019 +0200
+++ b/src/main/java/org/mpi/openmind/repository/bo/utils/EntitySortByNormalizedOwnValue.java	Wed Aug 14 20:48:02 2019 +0200
@@ -53,9 +53,9 @@
             	s1 = parensPattern.matcher(s1).replaceAll("");
             	s2 = parensPattern.matcher(s2).replaceAll("");
                 
-                // TODO: does this make sense?
-                s1 = TransliterationUtil.getTransliteration(s1);
-                s2 = TransliterationUtil.getTransliteration(s2);
+                // TODO: does this make sense? I don't trust it
+                //s1 = TransliterationUtil.getTransliteration(s1);
+                //s2 = TransliterationUtil.getTransliteration(s2);
                 
                 // compare
                 int value = s1.compareTo(s2);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/mpi/openmind/scripts/MigratePrimeAliases.java	Wed Aug 14 20:48:02 2019 +0200
@@ -0,0 +1,108 @@
+/**
+ * 
+ */
+package org.mpi.openmind.scripts;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.mpi.openmind.cache.WrapperService;
+import org.mpi.openmind.repository.bo.Entity;
+import org.mpi.openmind.repository.bo.Relation;
+import org.mpi.openmind.repository.services.ServiceRegistry;
+
+/**
+ * @author casties
+ *
+ */
+public class MigratePrimeAliases {
+	private static final String migrateUser = "migrate-alias user";
+	
+	private static Logger logger = Logger.getLogger(MigratePrimeAliases.class);
+	
+	protected static void migrate(String oldRelation, String newRelation, WrapperService wrapper) {
+
+		List<Entity> deleteList = new ArrayList<Entity>();
+		List<Entity> saveList = new ArrayList<Entity>();
+		
+		int cnt = 0;
+		List<Entity> aliasList = wrapper.getEntitiesByDef("ALIAS");
+        for (Entity alias : aliasList) {
+        	if (alias.isLightweight()) {
+        		alias = wrapper.getEntityContent(alias);
+        	}
+        	// get relation
+        	List<Relation> rels = alias.getSourceRelations(oldRelation, "*");
+        	if (rels.isEmpty()) continue;
+        	if (rels.size() > 1) {
+        		logger.error("ALIAS has more than one relation!");
+        		System.exit(1);
+        	}
+        	Relation rel = rels.get(0);
+    		Entity tar = wrapper.getEntityById(rel.getTargetId());
+        	if (tar.isLightweight()) {
+        		tar = wrapper.getEntityContent(tar);
+        	}
+        	boolean aliasExists = false;
+        	// get other aliases of target
+        	List<Relation> otherrels = tar.getTargetRelations(newRelation, "ALIAS");
+        	for (Relation otherrel : otherrels) {
+        		Entity otheralias = wrapper.getEntityById(otherrel.getSourceId());
+        		// compare ownvalue
+        		if (alias.getOwnValue().equals(otheralias.getOwnValue())) {
+        			aliasExists = true;
+        			break;
+        		}
+        	}
+        	if (aliasExists) {
+        		deleteList.add(alias);
+        	} else {
+        		// change relation
+        		rel.setOwnValue(newRelation);
+        		rel.setObjectClass(newRelation);
+        		saveList.add(alias);
+        	}
+        	if (++cnt % 100 == 0) {
+        		logger.debug(cnt+" aliases...");
+        	}
+        }
+        // delete obsolete aliases
+        for (Entity alias : deleteList) {
+        	try {
+        		logger.debug("Deleting obsolete alias: "+alias);
+				wrapper.removeCurrentVersionEntity(alias, migrateUser);
+			} catch (Exception e) {
+				logger.error("Error deleting alias!", e);
+			}
+        }
+        // save changed aliases
+        for (Entity alias: saveList) {
+	        try {
+        		logger.debug("Saving changed alias: "+alias);
+				wrapper.saveEntity(alias, migrateUser, null);
+			} catch (Exception e) {
+				logger.error("Error saving changed aliases!", e);
+			}
+        }
+        logger.info("Deleted "+deleteList.size()+" aliases.");
+        logger.info("Changed "+saveList.size()+" aliases.");
+	}
+
+	/**
+	 * @param args
+	 */
+	public static void main(String[] args) {
+		ServiceRegistry services = new ServiceRegistry();
+		services.getWrapper();
+
+		logger.info("Migrating ALIAS from is_prime_alias_name_of to is_alias_name_of");
+		migrate("is_prime_alias_name_of", "is_alias_name_of", services.getWrapper());
+
+		logger.info("Migrating ALIAS from is_prime_alias_title_of to is_alias_title_of");
+		migrate("is_prime_alias_title_of", "is_alias_title_of", services.getWrapper());
+
+		System.exit(0);
+	}
+
+}