view src/main/java/org/mpi/openmind/scripts/RepositoryName.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 615d27dce9b3
children
line wrap: on
line source

package org.mpi.openmind.scripts;

import java.util.List;

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;

public class RepositoryName {

	public static String USER = "script-repo-name";
	
	private WrapperService ontology;
	
	public RepositoryName(WrapperService ot){
		this.ontology = ot;
	}
	
	int countCorrect =0;
	int countError =0;
	public void execute() throws Exception{
		List<Entity> repos = this.ontology.getLightweightAssertions("REPOSITORY", null,-1);
		for(Entity repo : repos){
			Attribute att = this.ontology.getPS().getAttributeByName(repo, "name");
			if(att == null){
				countError++;
				
				repo = this.ontology.getEntityContent(repo);
				System.out.println("(" +countError +")" + repo);
				repo.addAttribute(new Attribute("name", "text", repo.getOwnValue()));
				
				try {
					this.ontology.saveAssertion(repo, USER);	
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}else{
				countCorrect++;
			}
		}
		System.out.println("For " + countError + " Repositories was created an attribute called name, which contain its ownValue.");
	}
	
	public static void main(String[] args) {
		try {
			ServiceRegistry services = new ServiceRegistry();
			RepositoryName script = new RepositoryName(services.getWrapper());
			script.execute();	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.exit(0);
	}
}