changeset 113:8dc4ac635d11

new script MigrateWitnessNotes.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Wed, 21 Aug 2019 14:25:42 +0200
parents 933d17f95016
children 70a02006675c
files src/main/java/org/mpi/openmind/repository/bo/Entity.java src/main/java/org/mpi/openmind/scripts/MigrateWitnessNotes.java
diffstat 2 files changed, 118 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/repository/bo/Entity.java	Wed Aug 14 20:48:02 2019 +0200
+++ b/src/main/java/org/mpi/openmind/repository/bo/Entity.java	Wed Aug 21 14:25:42 2019 +0200
@@ -773,6 +773,27 @@
         return null;
     }
     
+    /**
+     * Removes the named attribute.
+     * 
+     * @param name
+     * @return
+     */
+    public void removeAttributeByName(String name) {
+        try {
+            if (this.lightweight) {
+                throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB.");
+            }
+            for (int i = 0; i < this.attributes.size(); ++i) {
+            	if (this.attributes.get(i).getName().equals(name)) {
+            		this.attributes.remove(i);
+            	}
+            }
+        } catch (Exception e) {
+        	logger.error(e.getMessage(), e);
+        }
+    }
+    
     public Attribute getAttributeByOwnValue(String ow) {
         try {
             if (this.lightweight) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/mpi/openmind/scripts/MigrateWitnessNotes.java	Wed Aug 21 14:25:42 2019 +0200
@@ -0,0 +1,97 @@
+/**
+ * 
+ */
+package org.mpi.openmind.scripts;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+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.services.ServiceRegistry;
+
+/**
+ * @author casties
+ *
+ */
+public class MigrateWitnessNotes {
+	private static final String migrateUser = "migrate-notes user";
+	protected static final Set<String> attsToMigrate = new HashSet<>(Arrays.asList(
+			"notes_on_collation_and_corrections", "notes_on_ownership", "notes_on_title_author", 
+			"other_features", "page_layout"));
+	
+	private static Logger logger = Logger.getLogger(MigrateWitnessNotes.class);
+	
+	protected static void migrate(WrapperService wrapper) {
+		List<Entity> saveList = new ArrayList<Entity>();
+		
+		int cnt = 0;
+		List<Entity> witnessList = wrapper.getEntitiesByDef("WITNESS");
+        for (Entity witness : witnessList) {
+        	if (witness.isLightweight()) {
+        		witness = wrapper.getEntityContent(witness);
+        	}
+        	
+        	boolean modified = false;
+        	for (String attName : attsToMigrate) {
+        		if (witness.containsAttribute(attName)) {
+        			Attribute att = witness.getAttributeByName(attName);
+        			String attVal = att.getValue();
+        			if (StringUtils.isNotBlank(attVal)) {
+        				// check destination attribute
+        				Attribute notesAtt = witness.getAttributeByName("notes_old");
+        				if (notesAtt == null) {
+        					notesAtt = new Attribute();
+        					notesAtt.setName("notes_old");
+        					witness.addAttribute(notesAtt);        					
+        				}
+        				String notesVal = notesAtt.getValue();
+        				if (notesVal == null) notesVal = "";
+        				// add value to the end
+        				notesVal += "\n" + attName + ": " + attVal;
+        				notesAtt.setValue(notesVal);
+        			}
+        			// remove attribute
+        			witness.removeAttributeByName(attName);
+        			modified = true;
+        		}
+        	}
+        	if (modified) {
+        		saveList.add(witness);
+    		}
+        	if (++cnt % 100 == 0) {
+        		logger.debug(cnt+" witnesses");
+        	}
+        }
+        // save changed entities
+        for (Entity entity: saveList) {
+	        try {
+        		logger.debug("Saving changed witness: "+entity);
+				wrapper.saveEntity(entity, migrateUser, null);
+			} catch (Exception e) {
+				logger.error("Error saving changed witnesses!", e);
+			}
+        }
+        logger.info("Changed "+saveList.size()+" witnesses.");
+	}
+
+	/**
+	 * @param args
+	 */
+	public static void main(String[] args) {
+		ServiceRegistry services = new ServiceRegistry();
+		services.getWrapper();
+
+		logger.info("Migrating WITNESS attributes");
+		migrate(services.getWrapper());
+		
+		System.exit(0);
+	}
+
+}