Mercurial > hg > openmind
changeset 116:8acfd380fffe
new MigrateAhlwardtNo script.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 27 Aug 2019 12:37:36 +0200 |
parents | 42d5588d11b6 |
children | 2db6271a0663 |
files | src/main/java/org/mpi/openmind/scripts/MigrateAhlwardtNo.java src/main/java/org/mpi/openmind/scripts/MigratePrimeAliases.java |
diffstat | 2 files changed, 104 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/MigrateAhlwardtNo.java Tue Aug 27 12:37:36 2019 +0200 @@ -0,0 +1,104 @@ +package org.mpi.openmind.scripts; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.repository.bo.Attribute; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Node; +import org.mpi.openmind.repository.bo.Relation; +import org.mpi.openmind.repository.services.ServiceRegistry; + +/** + * Migrate WITNESS.ahlwardt_no to is_alias_identifier_of. + * + * @author casties + * + */ +public class MigrateAhlwardtNo { + public static final String WITNESS = "WITNESS"; + public static final String ALIAS = "ALIAS"; + public static final String AHLWARDT_NO = "ahlwardt_no"; + public static final String IS_ALIAS_IDENTIFIER_OF = "is_alias_identifier_of"; + + private static final String migrateUser = "migrate-ahlwardt user"; + + private static Logger logger = Logger.getLogger(MigrateAhlwardtNo.class); + + protected static void migrate(WrapperService wrapper) throws Exception { + + List<Entity> saveList = new ArrayList<Entity>(); + + int cnt = 0; + List<Entity> entityList = wrapper.getEntitiesByDef(WITNESS); + for (Entity entity : entityList) { + if (entity.isLightweight()) { + entity = wrapper.getEntityContent(entity); + } + // get attribute + Attribute att = entity.getAttributeByName(AHLWARDT_NO); + if (att == null || StringUtils.isBlank(att.getValue())) continue; + String ahlwardtNo = att.getValue(); + String ahlwardtVal = "Ahlwardt: " + ahlwardtNo; + boolean aliasExists = false; + // get other aliases of target + List<Relation> otherrels = entity.getTargetRelations(IS_ALIAS_IDENTIFIER_OF, ALIAS); + for (Relation otherrel : otherrels) { + Entity otheralias = wrapper.getEntityById(otherrel.getSourceId()); + // compare ownvalue + if (ahlwardtVal.equals(otheralias.getOwnValue())) { + aliasExists = true; + break; + } + } + if (aliasExists) { + entity.removeAttributeByName(AHLWARDT_NO); + saveList.add(entity); + } else { + // new ALIAS + Entity alias = new Entity(Node.TYPE_ABOX, ALIAS, false); + alias.addAttribute(new Attribute("alias", "text", ahlwardtVal)); + // save ALIAS to make sure we can add a Relation + alias = wrapper.saveEntity(alias, migrateUser, null); + new Relation(alias, entity, IS_ALIAS_IDENTIFIER_OF); + saveList.add(alias); + entity.removeAttributeByName(AHLWARDT_NO); + saveList.add(entity); + } + if (++cnt % 100 == 0) { + logger.debug(cnt+" witnesses..."); + } + } + // save changed entities + for (Entity alias: saveList) { + try { + logger.debug("Saving changed entity: "+alias); + wrapper.saveEntity(alias, migrateUser, null); + } catch (Exception e) { + logger.error("Error saving changed en entity!", e); + } + } + logger.info("Changed "+saveList.size()+" entities."); + } + + /** + * @param args + */ + public static void main(String[] args) { + ServiceRegistry services = new ServiceRegistry(); + services.getWrapper(); + + logger.info("Migrating WITNESS.ahlwardt_no to is_alias_identifier_of"); + try { + migrate(services.getWrapper()); + } catch (Exception e) { + logger.error(e); + } + + System.exit(0); + } + +}