Mercurial > hg > openmind
changeset 1:615d27dce9b3
(none)
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/cl/maps/duplex/DuplexMap.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,169 @@ +package cl.maps.duplex; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.mpi.openmind.repository.bo.Entity; + +import cl.maps.utils.DuplexMapKeepSortByNormalizedOV; +import cl.maps.utils.DuplexMapSortByNormalizedOV; + +public class DuplexMap<V, A, B> implements IDuplexMap<V, A, B>{ + + private Map<DuplexKey<A, B>, V> map; + private Map<A, List<DuplexKey<A, B>>> mapAKey; + private Map<B, DuplexKey<A, B>> mapOwnKey; + + public DuplexMap(){ + this.map = new HashMap<DuplexKey<A, B>, V>(); + this.mapAKey = new HashMap<A, List<DuplexKey<A, B>>>(); + this.mapOwnKey = new HashMap<B, DuplexKey<A, B>>(); + } + + public DuplexMap(DuplexMap<? extends V, A, B> m) { + this.map = new HashMap<DuplexKey<A, B>, V>(); + this.mapAKey = new HashMap<A, List<DuplexKey<A, B>>>(); + this.mapOwnKey = new HashMap<B, DuplexKey<A, B>>(); + this.putAllForCreate(m); + } + + private void putAllForCreate(DuplexMap<? extends V, A, B> m) { + for(Map.Entry<DuplexKey<A, B>, ? extends V> e : m.entrySet()){ + DuplexKey<A, B> tKey = e.getKey(); + this.map.put(tKey, e.getValue()); + this.mapOwnKey.put(tKey.getOwnKey(), tKey); + + if(!mapAKey.containsKey(tKey.getAKey())){ + mapAKey.put(tKey.getAKey(), new LinkedList<DuplexKey<A, B>>()); + } + if(!mapAKey.get(tKey.getAKey()).contains(tKey)){ + mapAKey.get(tKey.getAKey()).add(tKey); + } + } + } + + public List<V>getValuesByAKey(A srcKey){ + List<V> list = new ArrayList<V>(); + if(mapAKey.containsKey(srcKey)){ + for(DuplexKey<A, B> tKey : mapAKey.get(srcKey)){ + list.add(map.get(tKey)); + } + } + return list; + } + + public V getValuesByOwnKey(B ownKey){ + DuplexKey<A, B> tKey = mapOwnKey.get(ownKey); + if(tKey != null){ + return this.map.get(tKey); + } + return null; + } + + public Set<DuplexKey<A, B>> keySet(){ + return this.map.keySet(); + } + + public Set<Map.Entry<DuplexKey<A, B>, V>> entrySet() { + return this.map.entrySet(); + } + + @Override + public int size() { + return this.map.size(); + } + + @Override + public boolean isEmpty() { + return this.map.isEmpty(); + } + + @Override + public boolean containsKey(DuplexKey<A, B> key) { + return this.map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return this.map.containsValue(value); + } + + @Override + public V get(DuplexKey<A, B> key) { + return map.get(key); + } + + @Override + public V put(DuplexKey<A, B> tKey, V value) { + + if(!mapAKey.containsKey(tKey.getAKey())){ + List<DuplexKey<A, B>> list = new LinkedList<DuplexKey<A, B>>(); + Collections.sort(list, new DuplexMapSortByNormalizedOV<V, A, B>(this)); + mapAKey.put(tKey.getAKey(), list); + } + + List<DuplexKey<A, B>> list = mapAKey.get(tKey.getAKey()); + if(value instanceof Entity){ + //sorted insertion + if(!list.contains(tKey)){ + int index = Collections.binarySearch(list, tKey, new DuplexMapKeepSortByNormalizedOV<V, A, B>(this, value)); + if (index < 0) index = ~index; + list.add(index, tKey); + } + }else{ + //unsorted insertion + if(!list.contains(tKey)){ + list.add(tKey); + } + } + + this.mapOwnKey.put(tKey.getOwnKey(), tKey); + return this.map.put(tKey, value); + + //**** + /* + + + + if(!mapAKey.containsKey(tKey.getAKey())){ + List<DuplexKey<A, B>> list = new ArrayList<DuplexKey<A, B>>(); + mapAKey.put(tKey.getAKey(), list); + } + if(!mapAKey.get(tKey.getAKey()).contains(tKey)){ + mapAKey.get(tKey.getAKey()).add(tKey); + } + + this.mapOwnKey.put(tKey.getOwnKey(), tKey); + return this.map.put(tKey, value); + */ + } + + @Override + public V remove(DuplexKey<A, B> key) { + if(mapAKey.containsKey(key.getAKey())){ + mapAKey.get(key.getAKey()).remove(key); + } + this.mapOwnKey.remove(key.getOwnKey()); + return this.map.remove(key); + } + + @Override + public void clear() { + this.map.clear(); + this.mapAKey.clear(); + this.mapOwnKey.clear(); + } + + @Override + public Collection<V> values() { + return this.map.values(); + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/cl/maps/utils/AttKey.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,53 @@ +package cl.maps.utils; + +import org.apache.commons.lang.StringUtils; + +public class AttKey{ + private String oc; + private String attName; + + public AttKey(String oc, String attName){ + this.oc = oc; + this.attName = attName; + } + + public String getOc() { + return oc; + } + + public void setOc(String oc) { + this.oc = oc; + } + + public String getAttName() { + return attName; + } + + public void setAttName(String attName) { + this.attName = attName; + } + + @Override + public int hashCode() { + String s = this.oc + this.attName; + return s.hashCode(); + } + + @Override + public boolean equals(Object o){ + if(o instanceof AttKey){ + AttKey other = (AttKey)o; + if(StringUtils.equals(oc, other.oc) && + StringUtils.equals(attName, other.attName)){ + return true; + } + } + return false; + } + + @Override + public String toString(){ + return "AttKey [" + oc + ", " + attName + "]"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/cl/maps/utils/DuplexMapKeepSortByNormalizedOV.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,34 @@ +package cl.maps.utils; + +import java.util.Comparator; + +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.utils.EntitySortByNormalizedOwnValue; + +import cl.maps.duplex.DuplexKey; +import cl.maps.duplex.DuplexMap; + +public class DuplexMapKeepSortByNormalizedOV<V, A, B> implements Comparator<DuplexKey<A, B>>{ + + private DuplexMap<V, A, B> dupleMap; + private Entity entB; + + public DuplexMapKeepSortByNormalizedOV(DuplexMap<V, A, B> map, + Object ent) { + this.dupleMap = map; + this.entB = (Entity) ent; + } + + public DuplexMapKeepSortByNormalizedOV(DuplexMap<V, A, B> map) { + this.dupleMap = map; + } + + public void setEntB(Object ent) { + this.entB = (Entity) ent; + } + + public int compare(DuplexKey<A, B> a, DuplexKey<A, B> b) { + Entity entA = (Entity) dupleMap.get(a); + return EntitySortByNormalizedOwnValue.compare0(entA, entB); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/cl/maps/utils/DuplexMapSortByNormalizedOV.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,32 @@ +package cl.maps.utils; + +import java.util.Comparator; + +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.utils.EntitySortByNormalizedOwnValue; + +import cl.maps.duplex.DuplexKey; +import cl.maps.duplex.DuplexMap; + +public class DuplexMapSortByNormalizedOV<V, A, B> implements Comparator<DuplexKey<A, B>>{ + + private DuplexMap<V, A, B> dupleMap; + + + public DuplexMapSortByNormalizedOV(DuplexMap<V, A, B> map){ + this.dupleMap = map; + } + + public int compare(DuplexKey<A, B> a, DuplexKey<A, B> b){ + Entity entA = (Entity)dupleMap.get(a); + Entity entB = (Entity)dupleMap.get(b); + if(entA == null || entB == null){ + try { + throw new Exception("Sorting: in comparison a entity was null"); + } catch (Exception e) { + e.printStackTrace(); + } + } + return EntitySortByNormalizedOwnValue.compare0(entA, entB); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/cl/maps/utils/RelKey.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,55 @@ +package cl.maps.utils; + +import org.apache.commons.lang.StringUtils; + +public class RelKey { + + private Long srcId; + private Long tarId; + private String relName; + + public RelKey(Long srcId, Long tarId, String relName){ + this.srcId = srcId; + this.tarId = tarId; + this.relName = relName; + } + public Long getSrcId() { + return srcId; + } + public void setSrcId(Long srcId) { + this.srcId = srcId; + } + public Long getTarId() { + return tarId; + } + public void setTarId(Long tarId) { + this.tarId = tarId; + } + public String getRelName() { + return relName; + } + public void setRelName(String relName) { + this.relName = relName; + } + + @Override + public int hashCode() { + String s = this.srcId + "_" + this.tarId + "_" + relName; + return s.hashCode(); + } + + @Override + public boolean equals(Object o){ + if(o instanceof RelKey){ + RelKey other = (RelKey)o; + if(srcId.equals(other.srcId) && + tarId.equals(other.tarId) && + StringUtils.equals(relName, other.relName)){ + return true; + } + } + return false; + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/batch/ImportOM3Batch.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,15 @@ +package org.mpi.openmind.batch; + +import org.mpi.openmind.repository.services.ServiceRegistry; +import org.mpi.openmind.security.bo.User; + +public class ImportOM3Batch { + + public static void importAll(ServiceRegistry serviceRegistry){ + + serviceRegistry.getWrapper().importOM3Concepts("/Users/jurzua/Projects/max-planck/openmind4/trunk/data/ismi-data-100414/definitions-om3.xml",true); + //serviceRegistry.getOntologyService().importOM3Assertions("/Users/jurzua/Projects/max-planck/openmind4/trunk/data/ismi-data-100414/data-om3.xml",true); + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/batch/MainBatch.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,53 @@ +package org.mpi.openmind.batch; + +import org.hibernate.Session; +import org.hibernate.cfg.AnnotationConfiguration; +import org.mpi.openmind.repository.services.ServiceRegistry; +import org.mpi.openmind.repository.utils.HibernateUtil; + +public class MainBatch { + + /** + * @param args + */ + public static void main(String[] args) { + + if(args.length > 0){ + ServiceRegistry serviceRegistry = new ServiceRegistry(); + if(args[0].equals("initialize-system")){ + System.out.println("*** Executing 'initialize-system' ***"); + //System.setProperty("hibernate.hbm2ddl.auto", "create"); + SecurityInitializationBatch.initialize(serviceRegistry); + ImportOM3Batch.importAll(serviceRegistry); + + }else if(args[0].equals("own-value-generate")){ + System.out.println("*** Executing 'own-value-generate' ***"); + OwnValueGeneratorBatch.generateOwnValues(); + + }else if(args[0].equals("import-om3")){ + //System.setProperty("hibernate.hbm2ddl.auto", "create"); + System.out.println("*** Executing 'own-value-generate' ***"); + ImportOM3Batch.importAll(serviceRegistry); + + }else if(args[0].equals("initialize-security")){ + SecurityInitializationBatch.initialize(serviceRegistry); + + }else if(args[0].equals("drop-entities")){ + System.out.println("*** Executing 'drop-entities' ***"); + ServiceRegistry service = new ServiceRegistry(); + int assertions = service.getPS().dropAssertions(); + int defs = service.getPS().dropDefinitions(); + System.out.println("\t### Summary ###"); + System.out.println("\tAssertions deleted= " + assertions); + System.out.println("\tDefinitions deleted= " + defs); + } + + + +// Session session = HibernateUtil.getSessionFactory().getCurrentSession(); +// new AnnotationConfiguration().configure("hibernate.cfg.xml").setProperty("hbm2ddl.auto", "create"); + } + System.exit(0); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/batch/OwnValueGeneratorBatch.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,43 @@ +package org.mpi.openmind.batch; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Node; +import org.mpi.openmind.repository.services.ServiceRegistry; + +public class OwnValueGeneratorBatch { + + + public static void generateOwnValues(){ + try { + ServiceRegistry services = new ServiceRegistry(); + List<Entity> entities = services.getWrapper().getLightweightAssertions(null, null, -1); + + List<Node> dirtyEntities = new ArrayList<Node>(); + int count = 0; + for(Entity entity : entities){ + if(services.getConfigurationService().getPrintRules(entity.getObjectClass()).size() > 0 && !entity.getObjectClass().equals("WITNESS")){ + System.out.println(entity); + String ownValue = services.getPS().generateOwnValue(entity); + if(StringUtils.isNotEmpty(ownValue)){ + entity.setOwnValue(ownValue); + dirtyEntities.add((Node)entity); + } + } + count++; + if((count % 50) == 0){ + System.out.println("*"); + } + if((count % 200) == 0){ + System.out.println("\n["+ count + "/" + entities.size() + "]" + (double)(count/entities.size()) * 100 + "%"); + } + } + services.getWrapper().saveNodeListOnlyForScripts(dirtyEntities); + } catch (Exception e) { + e.printStackTrace(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/batch/SecurityInitializationBatch.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,66 @@ +package org.mpi.openmind.batch; + +import org.mpi.openmind.repository.services.ServiceRegistry; +import org.mpi.openmind.security.bo.User; + +public class SecurityInitializationBatch { + public static void initialize(ServiceRegistry serviceRegistry){ + System.out.println("*** Executing 'initialize-security' ***"); + + User admin = new User(); + admin.setEmail("admin"); + admin.setPassword("admin"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test1"); + admin.setPassword("test1"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test2"); + admin.setPassword("test2"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test3"); + admin.setPassword("test3"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test4"); + admin.setPassword("test4"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test5"); + admin.setPassword("test5"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test6"); + admin.setPassword("test6"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test7"); + admin.setPassword("test7"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test8"); + admin.setPassword("test8"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test9"); + admin.setPassword("test9"); + serviceRegistry.getSecurityService().saveUser(admin); + + admin = new User(); + admin.setEmail("test10"); + admin.setPassword("test10"); + serviceRegistry.getSecurityService().saveUser(admin); + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/batch/Test.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,17 @@ +package org.mpi.openmind.batch; + +import org.mpi.openmind.repository.services.ServiceRegistry; + +public class Test { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + ServiceRegistry services = new ServiceRegistry(); + services.getPS().dropAssertions(); + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/cache/AbstractCacheService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,416 @@ +package org.mpi.openmind.cache; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; +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.bo.ViewerAttribute; +import org.mpi.openmind.repository.bo.ViewerPage; +import org.mpi.openmind.repository.services.PersistenceService; + +import cl.maps.duplex.DuplexKey; +import cl.maps.duplex.DuplexMap; +import cl.maps.penta.PentaMap; +import cl.maps.triple.TripleMap; +import cl.maps.utils.AttKey; +import cl.maps.utils.RelKey; + +public abstract class AbstractCacheService{ + + private static Logger logger = Logger.getLogger(AbstractCacheService.class); + + private PersistenceService persistenceService; + + private DuplexMap<Entity, String, Long> entMap; + private Map<String, Boolean> entLoadedByOC; + + public AbstractCacheService(){ + logger.info( + "[" + Thread.currentThread().getId() + + "]\tInitializing AbstractCacheService HashCode=" + this.hashCode()); + + //Initializing entities structures + this.entMap = new DuplexMap<Entity, String, Long>(); + this.entLoadedByOC = new HashMap<String, Boolean>(); + + //Initializing attributes structures + this.attMap = new TripleMap<Attribute, AttKey, Long, Long>(); + this.attLoadedByOCAndName = new HashMap<AttKey, Boolean>(); + this.attLoadedBySrcId = new HashMap<Long, Boolean>(); + + //Initializing relations structures + this.relMap = new PentaMap<Relation, RelKey, Long, Long, String, Long>(); + this.relLoadedBySrcId = new HashMap<Long, Boolean>(); + this.relLoadedByTarId = new HashMap<Long, Boolean>(); + this.relLoadedByName = new HashMap<String, Boolean>(); + } + + protected Entity removeEntity(Long entId, String oc){ + return this.entMap.remove(new DuplexKey<String, Long>(oc, entId)); + } + + protected void updateLWEntity(Entity ent){ + this.entMap.put(ent.getKey(), ent); + } + + public Entity getEntityById(Long id){ + Entity ent = getEntityByIdReadOnly(id); + if(ent != null){ + return (Entity)ent.clone(); + } + return null; + } + + public Entity getEntityByIdReadOnly(Long id){ + if(id == null){ + try { + throw new Exception("Id of entity can not be null."); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + }else{ + Entity ent = this.entMap.getValuesByOwnKey(id); + if(ent == null){ + logger.debug("Entity no found in cache ID=" + id); + List<Entity> list = + getPs().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, id, Node.TYPE_ABOX, null, null, true, 1); + if (list.size() > 0) { + ent = list.get(0); + getEntitiesByDef(ent.getObjectClass()); + } + } + return ent; + } + return null; + } + + public List<Entity> getEntitiesByDef(String oc){ + logger.debug("[" + Thread.currentThread().getId() + "]\tgetEntitiesByDef=" + oc + "\nHashCode=" + this.hashCode()); + if(entLoadedByOC.get(oc) == null || entLoadedByOC.get(oc) == false){ + + synchronized (entMap) { + if(entLoadedByOC.get(oc) == null){ + long start = System.currentTimeMillis(); + + logger.debug("[" + Thread.currentThread().getId() + + "]\t Starting loading Entities from DB for OC=" + oc + "\nHashCode=" + this.hashCode()); + //################### + entLoadedByOC.put(oc, false); + List<Entity> list = + getPs().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, null, Node.TYPE_ABOX, oc, null, true, -1); + for(Entity ent : list){ + entMap.put(ent.getKey(), ent); + } + entLoadedByOC.put(oc, true); + //################### + long end = System.currentTimeMillis(); + logger.debug("[" + Thread.currentThread().getId() + + "]\tFinished loading Entities from DB for OC=" + oc + " - time= " + (end-start) + "\nHashCode=" + this.hashCode()); + } + } + + } + List<Entity> list = entMap.getValuesByAKey(oc); + return list; + } + + public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList) throws Exception{ + this.getPs().saveEntityListAsNodeWithoutContent(nodeList); + for(Entity ent : nodeList){ + entMap.put(ent.getKey(), ent); + } + } + + //****************************************** + //************Attributes******************** + //****************************************** + + //AttKey(OC, attName), SrcId + private TripleMap<Attribute, AttKey, Long, Long> attMap; + private Map<Long, Boolean> attLoadedBySrcId; + private Map<AttKey, Boolean> attLoadedByOCAndName; + + protected boolean removeAtt(Long attId){ + Attribute att = this.attMap.getValuesByOwnKey(attId); + if(att != null){ + this.attMap.remove(att.getKey()); + return true; + } + return false; + } + + protected boolean removeAtt(Attribute att){ + if(att != null){ + this.attMap.remove(att.getKey()); + return true; + } + return false; + } + + protected void updateAtt(Attribute att){ + this.attMap.put(att.getKey(), att); + } + + protected void updateAttList(List<Attribute> list){ + for(Attribute att : list){ + this.attMap.put(att.getKey(), att); + } + } + + public List<Attribute> getAttsBySrcId(Long entId){ + List<Attribute> list = new ArrayList<Attribute>(); + for(Attribute att : getAttsBySrcIdReadOnly(entId)){ + list.add((Attribute)att.clone()); + } + return list; + } + + public List<Attribute> getAttsBySrcIdReadOnly(Long srcId){ + //logger.info("getAttsBySrcIdReadOnly srcId=" + srcId); + if(attLoadedBySrcId.get(srcId) == null || attLoadedBySrcId.get(srcId) == false){ + synchronized (attMap) { + if(attLoadedBySrcId.get(srcId) == null){ + //logger.info("getAttsBySrcIdReadOnly fromDB entId=" + srcId); + attLoadedBySrcId.put(srcId, false); + List<Attribute> list = + getPs().getAllAttributes(srcId, -1); + for(Attribute att : list){ + this.attMap.put(att.getKey(), att); + } + attLoadedBySrcId.put(srcId, true); + //logger.info("STARTING getAttsBySrcIdReadOnly srcId=" + srcId + " - size=" + list.size()); + } + } + } + return this.attMap.getValuesByBKey(srcId); + } + + public List<Attribute> getAttsByOCAndName(String oc, String attName){ + AttKey ocNameKey = new AttKey(oc, attName); + //logger.info("[" + Thread.currentThread().getId() + + // "]\tgetAttsByOCAndNames oc=" + oc + " - attName=" + attName + "\nHashCode=" + this.hashCode()); + if(attLoadedByOCAndName.get(ocNameKey) == null || attLoadedByOCAndName.get(ocNameKey) == false){ + synchronized (attMap) { + if(attLoadedByOCAndName.get(ocNameKey) == null){ + logger.info("STARTING getAttsByOCAndNames oc=" + oc + " - attName=" + attName); + attLoadedByOCAndName.put(ocNameKey, false); + List<Attribute> list = + getPs().getAttributeByDefByName(oc, attName, -1); + for(Attribute att : list){ + this.attMap.put(att.getKey(), att); + } + attLoadedByOCAndName.put(ocNameKey, true); + logger.info("FINISHED getAttsByOCAndNames oc=" + oc + " - attName=" + attName + " - size=" + list.size()); + } + } + } + return this.attMap.getValuesByAKey(ocNameKey); + } + + //****************************************** + //************Relations********************* + //****************************************** + //RelKey<>, src, tar, relId + private PentaMap<Relation, RelKey, Long, Long, String, Long> relMap; + private Map<Long, Boolean> relLoadedBySrcId; + private Map<Long, Boolean> relLoadedByTarId; + private Map<String, Boolean> relLoadedByName; + + protected boolean removeRel(Long relId){ + Relation rel = this.relMap.getValuesByOwnKey(relId); + + if(rel != null){ + this.relMap.remove(rel.getKey()); + return true; + } + return false; + } + + protected boolean removeRel(Relation rel){ + if(rel != null){ + this.relMap.remove(rel.getKey()); + return true; + } + return false; + } + + protected void updateRel(Relation rel){ + this.relMap.put(rel.getKey(), rel); + } + + protected void updateRelList(List<Relation> list){ + for(Relation rel : list){ + this.relMap.put(rel.getKey(), rel); + } + } + + public List<Relation> getRelsBySrcId(long srcId) throws Exception{ + if(relLoadedBySrcId.get(srcId) == null || relLoadedBySrcId.get(srcId) == false){ + synchronized (relMap) { + if(relLoadedBySrcId.get(srcId) == null){ + //logger.info("getRelsBySrcId fromDB srcId=" + srcId); + relLoadedBySrcId.put(srcId, false); + List<Relation> list = + getPs().getSourceRelationsFromDB(this.getEntityByIdReadOnly(srcId), null, null, -1, false); + for(Relation rel : list){ + this.relMap.put(rel.getKey(), rel); + } + relLoadedBySrcId.put(srcId, true); + } + } + } + return relMap.getValuesByBKey(srcId); + } + + public List<Relation> getRelsByTarId(Long tarId){ + if(relLoadedByTarId.get(tarId) == null || relLoadedByTarId.get(tarId) == false){ + synchronized (relMap) { + if(relLoadedByTarId.get(tarId) == null){ + //logger.info("getRelsByTarId fromDB tarId=" + tarId); + relLoadedByTarId.put(tarId, false); + List<Relation> list = + getPs().getTargetRelationsFromDB(this.getEntityByIdReadOnly(tarId), null, null, -1, false); + for(Relation rel : list){ + this.relMap.put(rel.getKey(), rel); + } + relLoadedByTarId.put(tarId, true); + } + } + } + return relMap.getValuesByCKey(tarId); + } + + /** + * TODO the relations returned by this method have no attributes. + * 'cause the performance. + * There are to ways to correct this issue: + * 1) load the attributes directly from the DB (slow solution) + * 2) use LW state for the relation, like the entities. + * + * It should not mean a problem, because the other methods to get relations load the relation with attributes. + * Only getting relation one by one (using ID) would be a problem, but this method does not exist. + * + * @param name + * @return + */ + public List<Relation> getRelsByName(String name) throws Exception{ + + if(relLoadedByName.get(name) == null || relLoadedByName.get(name) == false){ + synchronized (relMap) { + if(relLoadedByName.get(name) == null){ + logger.info("getRelsByName fromDB name=" + name); + relLoadedByName.put(name, false); + + List<Relation> list = + getPs().getRelationByNameDB(name, -1); + for(Relation rel : list){ + this.relMap.put(rel.getKey(), rel); + } + + relLoadedByName.put(name, true); + } + } + } + + return relMap.getValuesByDKey(name); + } + + public PersistenceService getPs() { + return persistenceService; + } + + public void setPs(PersistenceService ps){ + this.persistenceService = ps; + } + + + //*********************************** + //*********************************** + //*********************************** + //*********************************** + + private Map<Long, ViewerPage> viewerPageMap; + private DuplexMap<ViewerAttribute, Long, Long> viewerAttMap; + + protected Map<Long, ViewerPage> getViewerPageMap(){ + if(viewerPageMap == null){ + this.loadViewerPageMap(); + } + return viewerPageMap; + } + + protected DuplexMap<ViewerAttribute, Long, Long> getViewerAttMap(){ + if(viewerAttMap == null){ + this.loadViewerAttMap(); + } + return viewerAttMap; + } + + public int removeViewerPage(Long id){ + int rows = 0; + if(viewerPageMap == null){ + this.loadViewerPageMap(); + } + synchronized (viewerPageMap) { + viewerPageMap.remove(id); + rows += getPs().removeViewerPage(id); + + List<ViewerAttribute> attrs = getViewerAttMap().getValuesByAKey(id); + for(ViewerAttribute attr : attrs){ + rows += removeViewerAttribute(attr.getId()); + } + } + + return rows; + } + + public int removeViewerAttribute(Long id){ + int rows = 0; + if(viewerAttMap == null){ + this.loadViewerAttMap(); + } + synchronized (viewerAttMap) { + ViewerAttribute att = viewerAttMap.getValuesByOwnKey(id); + if(att != null){ + viewerAttMap.remove(att.getKey()); + rows = getPs().removeViewerAttribute(att.getId()); + } + } + return rows; + } + + + + private void loadViewerAttMap(){ + if(viewerAttMap == null){ + viewerAttMap = new DuplexMap<ViewerAttribute, Long, Long>(); + synchronized (viewerAttMap) { + List<ViewerAttribute> list = getPs().getViewerAttributes(); + for(ViewerAttribute att : list){ + viewerAttMap.put(att.getKey(), att); + } + } + } + + } + + private void loadViewerPageMap(){ + if(viewerPageMap == null){ + viewerPageMap = new HashMap<Long, ViewerPage>(); + synchronized (viewerPageMap) { + viewerPageMap = new HashMap<Long, ViewerPage>(); + List<ViewerPage> list = getPs().getViewerPages(); + for(ViewerPage page : list){ + viewerPageMap.put(page.getId(), page); + } + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/cache/CacheService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,360 @@ +package org.mpi.openmind.cache; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.mpi.openmind.repository.bo.Attribute; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Relation; +import org.mpi.openmind.repository.bo.ViewerAttribute; +import org.mpi.openmind.repository.bo.ViewerPage; + +public class CacheService extends AbstractCacheService{ + + + private static Logger logger = Logger.getLogger(CacheService.class); + + private Map<String, Entity> lwDefs = new HashMap<String, Entity>(); + private Map<Long, Map<Long, Attribute>> defAttrs = new HashMap<Long, Map<Long, Attribute>>(); + private Map<Long, Relation> defRels = new HashMap<Long, Relation>(); + + + public CacheService(){ + super(); + logger.info( + "[" + Thread.currentThread().getId() + + "]\tInitializing CacheService HashCode=" + + this.hashCode()); + } + + public void initDefinitions(List<Entity> defs, List<Attribute> atts, List<Relation> rels){ + for(Entity def : defs){ + this.lwDefs.put(def.getOwnValue(), def); + } + for(Attribute att : atts){ + if(!this.defAttrs.containsKey(att.getSourceId())){ + this.defAttrs.put(att.getSourceId(), new HashMap<Long, Attribute>()); + } + this.defAttrs.get(att.getSourceId()).put(att.getId(), att); + } + + for(Relation rel : rels){ + this.defRels.put(rel.getId(), rel); + } + + this.defsLoaded = true; + } + + private boolean defsLoaded = false; + + public void saveLWDefinition(Entity definition){ + synchronized (lwDefs) { + this.lwDefs.put(definition.getOwnValue(), definition); + } + } + + public void createLWDefinition(Entity definition){ + synchronized (lwDefs) { + this.lwDefs.put(definition.getOwnValue(), definition); + this.defAttrs.put(definition.getId(), new HashMap<Long, Attribute>()); + } + } + + public void saveDefAttribute(Attribute att){ + if(!this.defsLoaded){ + this.throwDefLoadingException(); + return; + } + synchronized (defAttrs) { + if(!this.defAttrs.containsKey(att.getSourceId())){ + this.defAttrs.put(att.getSourceId(), new HashMap<Long, Attribute>()); + } + this.defAttrs.get(att.getSourceId()).put(att.getId(), att); + } + } + + public void saveDefRelation(Relation rel){ + if(!this.defsLoaded){ + this.throwDefLoadingException(); + return; + } + synchronized (defRels) { + this.defRels.put(rel.getId(), rel); + } + } + + public void deleteDefRelation(Relation rel){ + if(!this.defsLoaded){ + this.throwDefLoadingException(); + return; + } + synchronized (defRels) { + this.defRels.remove(rel.getId()); + } + } + + public void deleteDefAttribute(Attribute att){ + if(!this.defsLoaded){ + this.throwDefLoadingException(); + return; + } + synchronized (defAttrs) { + if(this.defAttrs.containsKey(att.getSourceId())){ + this.defAttrs.get(att.getSourceId()).remove(att.getId()); + + } + } + } + + public void deleteDefinition(Long defId){ + if(!this.defsLoaded){ + this.throwDefLoadingException(); + return; + } + Entity lwDef = getLWDefinitionById(defId); + if(lwDef != null){ + + synchronized (lwDefs) { + this.lwDefs.remove(lwDef.getOwnValue()); + } + + synchronized (defAttrs) { + this.defAttrs.remove(defId); + } + + List<Relation> toRemove = new ArrayList<Relation>(); + for(Relation rel : this.defRels.values()){ + if(rel.getSourceId().equals(defId) || rel.getTargetId().equals(defId)){ + toRemove.add(rel); + } + } + synchronized (defRels) { + for(Relation rel : toRemove){ + this.defRels.remove(rel.getId()); + } + } + } + } + + + + + public boolean areDefsLoaded(){ + return defsLoaded; + } + + public int getEntitiesCount(String def){ + return this.getEntitiesByDef(def).size(); + + } + + + public void updateRelationAsNode(Relation rel){ + this.updateRel(rel); + } + + public Entity getEntityContent(Entity ent) throws Exception{ + if(ent.isLightweight()){ + boolean valid = false; + List<Attribute> atts = getAttsBySrcId(ent.getId()); + if(atts != null){ + ent.setAttributes(atts); + Collection<Relation> srcRels = getRelsBySrcId(ent.getId()); + if(srcRels != null){ + ent.setSourceRelations(new ArrayList<Relation>(srcRels)); + Collection<Relation> tarRels = getRelsByTarId(ent.getId()); + if(tarRels != null){ + ent.setTargetRelations(new ArrayList<Relation>(tarRels)); + ent.setLightweight(false); + valid = true; + } + } + } + if(valid){ + return ent; + } + } + return null; + } + + public List<Entity> getLWDefinitions(){ + List<Entity> list = new ArrayList<Entity>(this.lwDefs.values()); + Collections.sort(list); + return list; + } + + public Entity getLWDefinition(String ownValue){ + Entity def = this.lwDefs.get(ownValue); + return def; + } + + private void throwDefLoadingException(){ + try { + throw new Exception("the definitions were loaded"); + } catch (Exception e) { + e.printStackTrace(); + logger.error(e.getMessage(), e); + } + } + + public List<Attribute> getDefAttributes(String ow){ + if(!this.defsLoaded){ + this.throwDefLoadingException(); + return null; + } + Entity lwDef = getLWDefinition(ow); + if(lwDef != null){ + List<Attribute> list = null; + if(this.defAttrs.containsKey(lwDef.getId())){ + list = new ArrayList<Attribute>(this.defAttrs.get(lwDef.getId()).values()); + Collections.sort(list); + }else{ + list = new ArrayList<Attribute>(); + } + return list; + } + return new ArrayList<Attribute>(); + } + + public List<Attribute> getDefAttributesById(Long id){ + if(!this.defsLoaded){ + this.throwDefLoadingException(); + return null; + } + + if(this.defAttrs.containsKey(id)){ + List<Attribute> list = new ArrayList<Attribute>(this.defAttrs.get(id).values()); + Collections.sort(list); + return list; + } + + return new ArrayList<Attribute>(); + } + + public List<Relation> getDefSourceRelations(String ow){ + if(!this.defsLoaded){ + this.throwDefLoadingException(); + return null; + } + Entity lwDef = getLWDefinition(ow); + List<Relation> list = new ArrayList<Relation>(); + if(lwDef != null){ + for(Relation rel : this.defRels.values()){ + if(lwDef.getId().equals(rel.getSourceId())){ + list.add(rel); + } + } + } + Collections.sort(list); + return list; + } + + public List<Relation> getDefTargetRelations(String ow){ + if(!this.defsLoaded){ + try { + throw new Exception("the definitions were loaded when getDefTargetRelations"); + } catch (Exception e) { + e.printStackTrace(); + } + } + Entity lwDef = getLWDefinition(ow); + List<Relation> list = new ArrayList<Relation>(); + if(lwDef != null){ + for(Relation rel : this.defRels.values()){ + if(lwDef.getId().equals(rel.getTargetId())){ + list.add(rel); + } + } + } + Collections.sort(list); + return list; + } + + public Entity getLWDefinitionById(Long id){ + for(Entity def : lwDefs.values()){ + if(def.getId().equals(id)){ + return def; + } + } + return null; + } + + /** + * This method should be called when a entity has been changed. + * It means that every trail of the old entity will be removed from the cache. + * @param ent + */ + public void saveEntity(Entity ent){ + try { + logger.debug("Saving in cache " + ent.getObjectClass() + " - " + ent.getId()); + this.deleteEntity(ent.getId(), ent.getObjectClass()); + + this.updateLWEntity(ent); + + if(!ent.isLightweight()){ + this.updateRelList(ent.getSourceRelations()); + this.updateRelList(ent.getTargetRelations()); + this.updateAttList(ent.getAttributes()); + } + } catch (Exception e) { + e.printStackTrace(); + logger.error(e.getMessage(), e); + } + } + + public void deleteEntity(Long entId, String oc){ + try { + this.removeEntity(entId, oc); + List<Attribute> attList = getAttsBySrcId(entId); + for(Attribute att : attList){ + removeAtt(att); + } + List<Relation> srcRelList = getRelsBySrcId(entId); + for(Relation rel : srcRelList){ + this.removeRel(rel); + } + + List<Relation> tarRelList = getRelsByTarId(entId); + for(Relation rel : tarRelList){ + this.removeRel(rel); + } + + } catch (Exception e) { + e.printStackTrace(); + logger.error(e.getMessage(), e); + } + } + + + public List<ViewerAttribute> getViewerAttributes(Long page){ + return getViewerAttMap().getValuesByAKey(page); + } + + public Collection<ViewerPage> getViewerPages(){ + return getViewerPageMap().values(); + } + + public ViewerPage saveViewerPage(ViewerPage page){ + this.getPs().saveViewerPage(page); + this.getViewerPageMap().put(page.getId(), page); + return page; + } + + public ViewerAttribute saveViewerAttribute(ViewerPage page, ViewerAttribute att) throws Exception{ + logger.info("saveViewerAttribute Page[id="+ page.getId() +", label=" + page.getLabel() + "] Att[id="+ att.getId() +", label="+ att.getLabel() +"]"); + if(!page.isPersistent()){ + throw new Exception("The ViewerPage associated the the current attribute is not persistent."); + } + att.setPage(page.getId()); + this.getPs().saveViewerAttribute(att); + this.getViewerAttMap().put(att.getKey(), att); + return att; + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/cache/WrapperService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,1291 @@ +package org.mpi.openmind.cache; + +import java.io.Serializable; +import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +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.bo.ViewerAttribute; +import org.mpi.openmind.repository.bo.ViewerPage; +import org.mpi.openmind.repository.bo.utils.EntitySortByNormalizedOwnValue; +import org.mpi.openmind.repository.bo.utils.EntitySortByVersion; +import org.mpi.openmind.repository.bo.utils.RelationSortBySourceOW; +import org.mpi.openmind.repository.bo.utils.RelationSortByTargetOW; +import org.mpi.openmind.repository.services.PersistenceService; +import org.mpi.openmind.repository.services.utils.AttributeFilter; +import org.mpi.openmind.repository.utils.ImportOM3Util; +import org.mpi.openmind.repository.utils.NormalizerUtils; + +public class WrapperService implements Serializable{ + + private static final long serialVersionUID = 2578074607841626396L; + + public static final String IS_TYPE_OF = "is_type_of"; + + private static Logger logger = Logger.getLogger(WrapperService.class); + + private transient CacheService cache; + + public long getSourceRelationsCount(Entity entity, String relationName, + String tarObjClass) { + return this.getPS().getSourceRelationsCount(entity, relationName, + tarObjClass); + } + + public Long getTargetRelationsCount(Entity entity, String relationName, + String srcObjClass) { + return this.getPS().getTargetRelationsCount(entity, relationName, + srcObjClass); + } + + public Map<Entity, Attribute> searchEntityByAttributeFilter( + List<AttributeFilter> filters, int maxResult) { + return this.getPS().searchEntityByAttributeFilter(filters, maxResult); + } + + public Map<Attribute, Entity> searchAttEntityByAttributeFilter( + List<AttributeFilter> filters, int maxResult) { + return this.getPS() + .searchAttEntityByAttributeFilter(filters, maxResult); + } + + public List<Entity> searchEntityByOwnValue(String oc, String term){ + List<Entity> rs = new ArrayList<Entity>(); + if(StringUtils.isEmpty(term)) + return rs; + + List<Entity> entList = this.cache.getEntitiesByDef(oc); + term = NormalizerUtils.normalize(term); + + for(Entity ent : entList){ + if(StringUtils.isNotEmpty(ent.getNormalizedOwnValue()) && + StringUtils.contains(ent.getNormalizedOwnValue(), term)){ + rs.add(ent); + } + } + return rs; + } + + // ************************************************************ + // ************************************************************ + // ************************************************************ + // ************************************************************ + + public List<Entity> getPreviousEntitiesById(Long id) { + List<Entity> list = this.getPS().getEntities(id, + Node.SYS_STATUS_PREVIOUS_VERSION, null, null); + Collections.sort(list, new EntitySortByVersion()); + return list; + } + + public void initCache() { + logger.info("##### Initializing Cache #####"); + logger.info(ManagementFactory.getRuntimeMXBean().getName()); + + try { + int mb = 1024 * 1024; + + // Getting the runtime reference from system + Runtime runtime = Runtime.getRuntime(); + + logger.info("##### Heap utilization statistics [MB] #####"); + + // Print used memory + logger.info("Used Memory:" + + (runtime.totalMemory() - runtime.freeMemory()) / mb); + + // Print free memory + logger.info("Free Memory:" + runtime.freeMemory() / mb); + + // Print total available memory + logger.info("Total Memory:" + runtime.totalMemory() / mb); + + // Print Maximum available memory + logger.info("Max Memory:" + runtime.maxMemory() / mb + "\n"); + } catch (Exception e) { + e.printStackTrace(); + } + + if (!this.cache.areDefsLoaded()) { + // List<Entity> list = this.searchServ.getEntities(null, + // Node.SYS_STATUS_CURRENT_VERSION, Node.TYPE_TBOX, null); + // this.cache.initDefs(list); + try { + this.cache.initDefinitions(getPS().getLWDefinitions(), getPS() + .getDefAttributes(), getPS().getDefRelations()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public void removeCurrentVersionEntity(Entity entity) throws Exception { + logger.info("removeCurrentVersionEntity " + entity); + this.cache.deleteEntity(entity.getId(), entity.getObjectClass()); + this.getPS().removeEntCurrentVersion(entity.getId(), entity.getType()); + System.gc(); + } + + public Map<Entity, Attribute> searchEntityByAttributeFilter0(String term, + List<AttributeFilter> filters, int maxResults) { + Map<Entity, Attribute> map = new HashMap<Entity, Attribute>(); + List<Long> usedIds = new ArrayList<Long>(); + + boolean mustBreak = false; + int count = 0; + if (StringUtils.isNotEmpty(term)) { + String normalizedTerm = NormalizerUtils.normalize(term); + for (AttributeFilter filter : filters) { + if (mustBreak) { + break; + } + for (Attribute att : getAttributesByDefByAttName( + filter.getEntObjectClass(), filter.getName(), -1)) { + if (!usedIds.contains(att.getSourceId()) + && StringUtils.isNotEmpty(att + .getNormalizedOwnValue()) + && att.getNormalizedOwnValue().contains( + normalizedTerm)) { + map.put(getEntityById(att.getSourceId()), att); + usedIds.add(att.getSourceId()); + count++; + if (maxResults > 0 && count == maxResults) { + mustBreak = true; + break; + } + } + } + } + } + return map; + } + + public int getEntitiesCount(String def) { + int count = this.cache.getEntitiesCount(def); + return count; + } + + public List<Entity> getEntityByDefSubList(String def, int fromIndex, + int toIndex) { + List<Entity> list = new ArrayList<Entity>(); + List<Entity> all = getEntitiesByDef(def); + if (all != null && all.size() >= toIndex) { + + long start = System.currentTimeMillis(); + Collections.sort(list, new EntitySortByNormalizedOwnValue()); + long diff = System.currentTimeMillis() - start; + logger.info("Sorting entities time[ms] " + diff); + + for (Entity ent : all.subList(fromIndex, toIndex)) { + list.add(ent); + } + } + return list; + } + + public List<Entity> getEntitiesByDef(String def) { + List<Entity> list = this.cache.getEntitiesByDef(def); + return list; + } + + public List<Entity> getEntityByDefAndOW(String def, String ow, + int maxResults) { + List<Entity> result = new ArrayList<Entity>(); + if (StringUtils.isNotEmpty(ow)) { + List<Entity> list = this.cache.getEntitiesByDef(def); + + int count = 0; + ow = NormalizerUtils.normalize(ow); + for (Entity e : list) { + // String eow = (StringUtils.isNotEmpty(e.getOwnValue())) ? + // e.getOwnValue().toLowerCase() : ""; + if (StringUtils.isNotBlank(e.getNormalizedOwnValue()) + && e.getNormalizedOwnValue().contains(ow)) { + result.add((Entity) e.clone()); + count++; + } + + if (maxResults > 0 && maxResults == count) { + break; + } + } + } + return result; + } + + /** + * this method does not execute: - ownValue generation - new version + * generation This method only save the entity in the current state. + * + * @param nodeList + * @param user + */ + public void saveEntityListAsNode(List<Entity> entList, String user) { + for (Entity ent : entList) { + ent.setUser(user); + } + getPS().saveEntityListAsNode(entList); + for (Entity ent : entList) { + cache.saveEntity(ent); + } + } + + public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList, + String user) throws Exception { + this.cache.saveEntityListAsNodeWithoutContent(nodeList); + } + + public boolean existEntity(Long entId) { + return getCache().getEntityByIdReadOnly(entId) != null; + } + + public boolean existRelation(Long srcId, Long tarId, String relName) { + + try { + if (srcId == null || tarId == null) { + throw new Exception("srcId and tarId can not be null."); + } + + List<Relation> relList = this.cache.getRelsBySrcId(srcId); + + for (Relation rel : relList) { + if (rel.getTargetId().equals(tarId) + && rel.getOwnValue().equals(relName)) + return true; + } + } catch (Exception e) { + e.printStackTrace(); + } + + return false; + } + + public void saveRelationAsNode(Relation rel, String user) throws Exception { + + if (rel.getAttributes() != null && rel.getAttributes().size() > 0) { + throw new Exception( + "This method can be only used if the relation does not contains attributes, this is not the case."); + } + + rel.setUser(user); + this.getPS().saveNode(rel); + this.cache.updateRelationAsNode(rel); + } + + public List<Entity> searchEntityByAttributeOfTarRelation(String objClass, + String relName, String objClassSrc, String attName, + String attValue, int maxResults) throws Exception { + List<Entity> list = new ArrayList<Entity>(); + + List<Attribute> attList = getAttributesByDefByAttName(objClassSrc, + attName, attValue, -1); + int count = 0; + for (Attribute att : attList) { + Relation rel = getFirstRelationByTargetOCByName( + cache.getRelsBySrcId(att.getSourceId()), objClass, relName); + if (rel != null) { + list.add(getEntityById(rel.getTargetId())); + count++; + if (maxResults > 0 && maxResults == count) { + break; + } + } + } + + return list; + } + + private static Relation getFirstRelationByTargetOCByName( + List<Relation> list, String tarOC, String name) { + if (StringUtils.isNotEmpty(name)) { + name = name.toLowerCase(); + for (Relation rel : list) { + if (rel.getTargetObjectClass().equals(tarOC)) { + if (StringUtils.isNotEmpty(rel.getOwnValue()) + && name.equals(rel.getOwnValue().toLowerCase())) { + return rel; + } + } + } + } + return null; + } + + public List<Attribute> getAttributeByEntId(Long entId) { + List<Attribute> list = cache.getAttsBySrcId(entId); + return list; + } + + public Attribute getAttributeByName(Long entId, String name) { + List<Attribute> list = cache.getAttsBySrcId(entId); + + for (Attribute att : list) { + if (att.getName().equals(name)) { + return (Attribute) att.clone(); + } + } + return null; + } + + public List<Attribute> searchAttribute(String firstName, String firstValue, + String secondName, String secondValue, String def, int maxResults) { + List<Attribute> list = new ArrayList<Attribute>(); + + List<Attribute> firstAttList = cache.getAttsByOCAndName(def, firstName); + + firstValue = StringUtils.isNotEmpty(firstValue) ? NormalizerUtils + .normalize(firstValue) : null; + secondValue = StringUtils.isNotEmpty(secondValue) ? NormalizerUtils + .normalize(secondValue) : null; + + if (StringUtils.isNotEmpty(firstValue)) { + for (Attribute firstAtt : firstAttList) { + String attValue1 = (StringUtils.isNotEmpty(firstAtt + .getNormalizedOwnValue())) ? firstAtt + .getNormalizedOwnValue() : ""; + // (StringUtils.isNotEmpty(firstAtt.getValue())) ? + // firstAtt.getValue().toLowerCase() : ""; + if (StringUtils.isNotEmpty(firstValue) + && attValue1.contains(firstValue)) { + Attribute secondAtt = getAttributeByName( + firstAtt.getSourceId(), secondName); + if (secondAtt != null) { + String attValue2 = (StringUtils.isNotEmpty(secondAtt + .getNormalizedOwnValue())) ? secondAtt + .getNormalizedOwnValue() : ""; + // (StringUtils.isNotEmpty(secondAtt.getValue())) ? + // secondAtt.getValue().toLowerCase() : ""; + if (StringUtils.isNotEmpty(secondValue) + && attValue2.contains(attValue2)) { + list.add((Attribute) firstAtt.clone()); + } + } + } + } + } + + return list; + } + + private List<Attribute> getAttributesByDefByAttName(String def, + String attName, int maxResults) { + List<Attribute> list = cache.getAttsByOCAndName(def, attName); + return list; + } + + /** + * Returns a list of attributes by objectClass of the entity, the att Name + * and its value. + * + * @param def + * @param attName + * @param attValue + * sub string and ignores case sensitive. + * @param maxResults + * @return + */ + public List<Attribute> getAttributesByDefByAttName(String def, + String attName, String attValue, int maxResults) { + + List<Attribute> list = getAttributesByDefByAttName0(def, attName, attValue, maxResults); + Collections.sort(list); + return list; + } + + public List<Attribute> getAttributes(String oc, String attName){ + return cache.getAttsByOCAndName(oc, attName); + } + + /** + * + * @param attName + * @param attValue + * @return + */ + public List<Attribute> getAttributesByExactValue(String attName, String attValue){ + List<Attribute> list = new ArrayList<Attribute>(); + + if(StringUtils.isNotEmpty(attValue)){ + for(Entity def : cache.getLWDefinitions()){ + + List<Attribute> attList = cache.getAttsByOCAndName(def.getOwnValue(), attName); + attValue = NormalizerUtils.normalize(attValue); + + for(Attribute att : attList){ + if(StringUtils.equals(attValue, att.getNormalizedOwnValue())){ + list.add(att); + } + } + + } + Collections.sort(list); + } + return list; + } + + private List<Attribute> getAttributesByDefByAttName0(String def, + String attName, String attValue, int maxResults) { + List<Attribute> list = new ArrayList<Attribute>(); + List<Attribute> attList = cache.getAttsByOCAndName(def, attName); + attValue = NormalizerUtils.normalize(attValue); + int count = 0; + if (StringUtils.isEmpty(attValue)) { + for (Attribute att : attList) { + list.add((Attribute) att.clone()); + count++; + if (maxResults > 0 && maxResults == count) { + break; + } + } + } else { + for (Attribute att : attList) { + String attValue0 = StringUtils.isNotEmpty(att + .getNormalizedOwnValue()) ? att.getNormalizedOwnValue() + : ""; + if (StringUtils.isNotEmpty(attValue) + && attValue0.contains(attValue)) { + list.add((Attribute) att.clone()); + count++; + if (maxResults > 0 && maxResults == count) { + break; + } + } + } + } + return list; + } + + /** + * Returns a list of entities. + * + * @param def + * @param attName + * @param attValue + * ignores case sensitive + * @param maxResults + * @param subString + * if true the attValue is searching as substring + * @return + */ + public List<Entity> getEntitiesByAtt(String def, String attName, + String attValue, int maxResults, boolean subString) { + List<Entity> list = new ArrayList<Entity>(); + + List<Attribute> attList = cache.getAttsByOCAndName(def, attName); + + int count = 0; + if (StringUtils.isEmpty(attValue)) { + for (Attribute att : attList) { + list.add(getEntityById(att.getSourceId())); + count++; + if (maxResults > 0 && maxResults == count) { + break; + } + } + } else { + attValue = (StringUtils.isNotEmpty(attValue)) ? NormalizerUtils + .normalize(attValue) : ""; + for (Attribute att : attList) { + String attValue0 = (StringUtils.isNotEmpty(att + .getNormalizedOwnValue())) ? att + .getNormalizedOwnValue() : ""; + if ((subString && attValue0.contains(attValue)) + || (!subString && attValue.equals(attValue0))) { + list.add(getEntityById(att.getSourceId())); + count++; + if (maxResults > 0 && maxResults == count) { + break; + } + } + } + } + return list; + } + + public void removeDefAttribute(Attribute att) throws Exception { + this.cache.deleteDefAttribute(att); + att.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); + this.getPS().saveNode(att); + } + + public void removeDefRelation(Relation rel) throws Exception { + this.cache.deleteDefRelation(rel); + rel.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); + this.getPS().saveNode(rel); + } + + public Entity saveLWDefinition(Entity def, String user) throws Exception { + boolean isNew = !def.isPersistent(); + def.setUser(user); + def.increaseVersion(); + def.setModificationTime(System.currentTimeMillis()); + def.setType(Node.TYPE_TBOX); + def.setObjectClass(Node.TYPE_TBOX); + def.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + this.getPS().saveNode(def); + + if (isNew) { + this.cache.createLWDefinition(def); + } else { + this.cache.saveLWDefinition(def); + } + + return def; + } + + public Attribute saveDefAttribute(Attribute att, String user) + throws Exception { + att.setUser(user); + att.increaseVersion(); + att.setModificationTime(System.currentTimeMillis()); + att.setObjectClass(Node.TYPE_TBOX); + att.setType(Node.TYPE_TBOX); + att.getPossibleValues(); + this.getPS().saveNode(att); + this.cache.saveDefAttribute(att); + return att; + } + + public Relation saveDefRelation(Relation rel, String user) throws Exception { + rel.setUser(user); + rel.increaseVersion(); + rel.setModificationTime(System.currentTimeMillis()); + rel.setObjectClass(Node.TYPE_TBOX); + rel.setType(Node.TYPE_TBOX); + this.getPS().saveNode(rel); + this.cache.saveDefRelation(rel); + return rel; + } + + /* + * public Entity saveDefinition(Entity def, String user){ long start = + * System.currentTimeMillis(); def.setObjectClass(Node.TYPE_TBOX); + * def.setUser(user); + * + * boolean b = this.searchServ.saveEntity(def); + * this.cache.saveDefinition(def); + * logger.info("saveEntityAsNew - execution time[ms]: " + + * (System.currentTimeMillis() - start)); return (Entity)def.clone(); } + */ + public Entity saveEntityAsNew(Entity entity, String user) throws Exception { + long start = System.currentTimeMillis(); + entity.resetRowId(); + entity.resetId(); + entity.setType(Node.TYPE_ABOX); + entity.setUser(user); + + this.getPS().saveEntity(entity); + cache.saveEntity(entity); + logger.info("[U=" + user + "] SaveEntityAsNew - execution time[ms]: " + (System.currentTimeMillis() - start)); + return (Entity) entity.clone(); + } + + public Entity saveEntity(Entity entity, String user) throws Exception { + long start = System.currentTimeMillis(); + if (StringUtils.isEmpty(entity.getType())) { + entity.setType(Node.TYPE_ABOX); + } + entity.setUser(user); + + entity = removeWrongRelations(entity); + entity = removeEmptyAttributes(entity); + + this.getPS().saveEntity(entity); + cache.saveEntity(entity); + System.gc(); + logger.info("[U=" + user + "] SaveEntity - execution time[ms]: " + (System.currentTimeMillis() - start)); + Entity clone = (Entity) entity.clone(); + // logger.info(clone.toString()); + return clone; + } + + public void saveEntityList(List<Entity> list, String user) throws Exception{ + long start = System.currentTimeMillis(); + for(Entity entity : list){ + if (StringUtils.isEmpty(entity.getType())) { + entity.setType(Node.TYPE_ABOX); + } + entity.setUser(user); + + entity = removeWrongRelations(entity); + entity = removeEmptyAttributes(entity); + } + this.getPS().saveEntityList(list); + for(Entity entity : list){ + cache.saveEntity(entity); + } + logger.info("[U=" + user + "] SaveEntityList - execution time[ms]: " + (System.currentTimeMillis() - start)); + } + + private Entity removeEmptyAttributes(Entity ent){ + + for(Attribute att : new ArrayList<Attribute>(ent.getAttributes())){ + if(StringUtils.isEmpty(att.getValue())){ + ent.getAttributes().remove(att); + } + } + return ent; + + } + + private boolean hasWrongRelations(Entity ent) { + for (Relation srcRel : ent.getSourceRelations()) { + if (getEntityById(srcRel.getTargetId()) == null) { + return true; + } + } + + for (Relation tarRel : ent.getTargetRelations()) { + if (getEntityById(tarRel.getSourceId()) == null) { + return true; + } + } + return false; + } + + private Entity removeWrongRelations(Entity ent) { + + for (Relation srcRel : new ArrayList<Relation>(ent.getSourceRelations())) { + if (srcRel.getTargetId() == null || getEntityByIdReadOnly(srcRel.getTargetId()) == null) { + ent.getSourceRelations().remove(srcRel); + logger.error( + "Inconsistency detected saving entity [" + + ent.getId() + "] " + srcRel.toString()); + } + } + + for (Relation tarRel : new ArrayList<Relation>(ent.getTargetRelations())) { + if (tarRel.getSourceId() == null || getEntityByIdReadOnly(tarRel.getSourceId()) == null) { + ent.getTargetRelations().remove(tarRel); + logger.error( + "Inconsistency detected saving entity [" + + ent.getId() + "] " + tarRel.toString()); + } + } + + return ent; + } + + public Attribute getDefAttributeByOwnValue(String defOC, String attOW) { + for (Attribute att : this.cache.getDefAttributes(defOC)) { + if (att.getOwnValue().equals(attOW)) { + return att; + } + } + return null; + } + + public List<Attribute> getDefRelationAttributes(Long id) { + return this.cache.getDefAttributesById(id); + } + + public List<Attribute> getDefAttributes(String ow) { + return this.cache.getDefAttributes(ow); + } + + public List<Relation> getDefSourceRelations(String ow) { + return this.cache.getDefSourceRelations(ow); + } + + public List<Relation> getDefTargetRelations(String ow) { + return this.cache.getDefTargetRelations(ow); + } + + public Entity getDefinitionById(Long id) { + return this.cache.getLWDefinitionById(id); + /* + * Entity def = cache.getDefinitionById(id); if(def == null){ + * List<Entity> list = this.searchServ.getEntities(id, + * Node.SYS_STATUS_CURRENT_VERSION, Node.TYPE_TBOX, null); if + * (list.size() > 0) { def = list.get(0); cache.saveDefinition(def); } } + * return def; + */ + } + + public List<Entity> getLWDefinitions() { + return this.cache.getLWDefinitions(); + /* + * List<Entity> defs = this.cache.getDefinitions(); + * Collections.sort(defs, new EntitySortByNormalizedOwnValue()); return + * defs; + */ + } + + public Entity getDefinition(String ownValue) { + return this.cache.getLWDefinition(ownValue); + } + + public List<Relation> getRelation(String name, String srcOC, String tarOC) + throws Exception { + // TODO read only??? + List<Relation> list = new ArrayList<Relation>(); + if (StringUtils.isNotEmpty(name)) { + List<Relation> list0 = cache.getRelsByName(name); + if (StringUtils.isEmpty(srcOC) && StringUtils.isEmpty(tarOC)) { + list = list0; + } else { + for (Relation rel : list0) { + if ((StringUtils.isEmpty(srcOC) || srcOC.equals(rel + .getSourceObjectClass())) + && (StringUtils.isEmpty(tarOC) || tarOC.equals(rel + .getTargetObjectClass()))) { + list.add(rel); + } + } + } + } + + return list; + } + + public List<Entity> getSourcesForTargetRelation(Entity tar, + String relationName, String srcObjClass, int maxResult) { + return getSourcesForTargetRelation(tar.getId(), relationName, + srcObjClass, maxResult); + } + + public List<Entity> getSourcesForTargetRelation(Long tarId, + String relationName, String srcObjClass, int maxResult) { + List<Entity> rs = new ArrayList<Entity>(); + + List<Relation> tarRelList = this.cache.getRelsByTarId(tarId); + + long start = System.currentTimeMillis(); + int count = 0; + for (Relation rel : tarRelList) { + if (stringEquals(relationName, rel.getOwnValue()) + && stringEquals(srcObjClass, rel.getSourceObjectClass())) { + Entity ent = getEntityByIdReadOnly(rel.getSourceId()); + if (ent != null) { + rs.add(ent); + count++; + if (maxResult > 0 && count == maxResult) { + break; + } + } + } + } + logger.debug("getSourcesForTargetRelation (loading sources) - execution time[ms]: " + + (System.currentTimeMillis() - start)); + Collections.sort(rs, new EntitySortByNormalizedOwnValue()); + return rs; + } + + public List<Entity> getTargetsForSourceRelation(Entity src, + String relationName, String tarObjClass, int maxResult) { + return getTargetsForSourceRelation(src.getId(), relationName, + tarObjClass, maxResult); + } + + public List<Entity> getTargetsForSourceRelation(Long srcId, + String relationName, String tarObjClass, int maxResult) { + List<Entity> rs = new ArrayList<Entity>(); + + try { + Collection<Relation> srcRelList = this.cache.getRelsBySrcId(srcId); + + int count = 0; + for (Relation rel : srcRelList) { + if (stringEquals(relationName, rel.getOwnValue()) + && stringEquals(tarObjClass, rel.getTargetObjectClass())) { + rs.add(getEntityByIdReadOnly(rel.getTargetId())); + count++; + if (maxResult > 0 && count == maxResult) { + break; + } + } + } + Collections.sort(rs, new EntitySortByNormalizedOwnValue()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + + return rs; + } + + public List<Relation> getTargetRelations(Entity target, + String relationName, String srcObjClass, int maxResult) { + long start = System.currentTimeMillis(); + List<Relation> list = new ArrayList<Relation>(); + + List<Relation> tarRelList = this.cache.getRelsByTarId(target.getId()); + /* + * if(coll == null){ coll = this.getPS().getTargetRelations(target, + * null, null, -1, false); cache.setRelsForTar(target.getId(), coll); } + */ + + int count = 0; + for (Relation rel : tarRelList) { + if (stringEquals(relationName, rel.getOwnValue()) + && stringEquals(srcObjClass, rel.getSourceObjectClass())) { + Entity source = getEntityById(rel.getSourceId()); + if (source != null) { + rel.setTarget(target); + rel.setSource(source); + list.add(rel); + count++; + if (maxResult > 0 && count == maxResult) { + break; + } + } else { + logger.error("#########ERROR: Relation without source " + + rel.toString()); + } + + } + } + Collections.sort(list, new RelationSortBySourceOW()); + logger.debug("getTargetRelations - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return list; + } + + /** + * Returns a list of relations found by entity source. + * + * @param source + * @param relationName + * @param tarObjClass + * @param maxResult + * @return + */ + public List<Relation> getSourceRelations(Entity source, + String relationName, String tarObjClass, int maxResult) + throws Exception { + + long start = System.currentTimeMillis(); + List<Relation> list = new ArrayList<Relation>(); + + // the collection coll should not be modified + // otherwise java.util.ConcurrentModificationException will be thrown. + // be carefully using getEntityContent, cause it changes the cache + + List<Relation> srcRelList = this.cache.getRelsBySrcId(source.getId()); + + int count = 0; + for (Relation rel : srcRelList) { + if (stringEquals(relationName, rel.getOwnValue()) + && stringEquals(tarObjClass, rel.getTargetObjectClass())) { + rel.setSource(source); + rel.setTarget(getEntityById(rel.getTargetId())); + list.add(rel); + count++; + if (maxResult > 0 && count == maxResult) { + break; + } + } + } + Collections.sort(list, new RelationSortByTargetOW()); + logger.debug("getSourceRelations - execution time[ms]: " + + (System.currentTimeMillis() - start)); + + return list; + } + + /** + * This method should not be used inside this Wrapper class, because it + * could throws an java.util.ConcurrentModificationException. + * + * @param ent + * @return + */ + public Entity getEntityContent(Entity ent) { + try { + if (ent.isLightweight()) { + Entity e = this.cache.getEntityContent(ent); + if (e == null) { + e = this.getPS().getEntityContent(ent); + this.cache.saveEntity(e); + } else { + ent = e; + } + } + return (Entity) ent.clone(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return null; + } + + public Entity getEntityContentReadOnly(Entity ent) throws Exception { + if (ent.isLightweight()) { + Entity e = this.cache.getEntityContent(ent); + if (e == null) { + e = this.getPS().getEntityContent(ent); + this.cache.saveEntity(e); + } else { + ent = e; + } + } + return ent; + } + + /** + * The entity returned could be LW or not, it depends on the cache. + * additionally, it will be always a clone of the version in cache, + * therefore it method is thought for editing's issues. + * + * @param id + * @return + */ + public Entity getEntityById(Long id) { + Entity ent = this.cache.getEntityById(id); + return ent; + } + + public Entity getEntityByIdReadOnly(Long id) { + Entity ent = this.cache.getEntityByIdReadOnly(id); + return ent; + } + + public Entity getClonedEntityById(Long id) { + return (Entity) getEntityById(id).clone(); + } + + /** + * This method should not be used inside this Wrapper class, because it + * could throws an java.util.ConcurrentModificationException. + * + * @param id + * @return + */ + public Entity getEntityByIdWithContent(Long id) { + try { + Entity ent = getEntityById(id); + if (ent != null && ent.isLightweight()) { + ent = getEntityContent(ent); + } + return ent; + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return null; + } + + public Entity getEntityByIdWithContentReadOnly(Long id) throws Exception { + Entity ent = getEntityByIdReadOnly(id); + if (ent.isLightweight()) { + ent = getEntityContentReadOnly(ent); + } + return ent; + } + + public static boolean stringEquals(String term, String ow) { + if (StringUtils.isEmpty(term)) + return true; + return term.equals(ow); + } + + // ////**** + public Entity getLightweightEntityById(Long id) { + Entity entity = null; + if (id != null) { + List<Entity> list = this.getPS().getLightweightEntities( + Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null, + true, -1); + if (list.size() > 0) { + entity = list.get(0); + } + } + return entity; + } + + public List<Entity> getLightweightAssertions(String objectClass, + String ownValue, int maxResult) { + return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, + null, Node.TYPE_ABOX, objectClass, ownValue, true, maxResult); + } + + public void saveAssertion(Entity entity, String user) throws Exception { + this.saveEntity(entity, user); + } + + /** + * <p> + * This method saves a non-persistent entity. + * </p> + * <p> + * The reason of this is that nodes are stored only in the DB and not in the + * cache. + * </p> + * <p> + * Later, when the nodes are required, they will be loaded from the BD to + * the cache automatically. However there are some structures like + * attributes by entId that become inconsistent using this method. + * </p> + * + * @param node + * non-persistent node. + * @param user + * @throws Exception + */ + public void saveNodeOnlyForScripts(Node node, String user) throws Exception { + if (node.isPersistent()) { + throw new Exception( + "This method can save only non-persistent nodes. Because the nodes are stored only in the DB in not in the Cache."); + } + if (StringUtils.isEmpty(node.getType())) { + throw new Exception("The type of the node can not be empty"); + } + node.setUser(user); + this.getPS().saveNode(node); + + // if(StringUtils.isNotEmpty(node.getType())){ + // if(node.getType().equals(Node.TYPE_ABOX)){ + // + // }else if(node.getType().equals(Node.TYPE_TBOX)){ + // + // } + // } + } + + public void removeNode(Node node) { + this.getPS().removeNode(node); + } + + public Map<Long, Long> saveEntityListAsNew(List<Entity> entities, String user, + boolean testWrongRelations) throws Exception { + logger.info("\n ### Making persistent Entities size total " + + entities.size() + " ### \n"); + int sizePart = 1000; + List<Entity> tmpList = null; + + Map<Long, Long> idMap = new HashMap<Long, Long>(); + + for (int j = 0; j <= (entities.size() / sizePart); j++) { + tmpList = new ArrayList<Entity>(sizePart); + for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { + + if (i < entities.size()) { + if (StringUtils.isNotEmpty(user)) { + entities.get(i).setUser(user); + } + + if (testWrongRelations + && hasWrongRelations(entities.get(i))) { + throw new Exception("Wrong relations " + + entities.get(i)); + } + + tmpList.add(entities.get(i)); + } else { + break; + } + } + + logger.info("\nMaking persistent Entities part " + (j + 1) + " of " + + ((entities.size() / sizePart) + 1) + " size=" + + tmpList.size()); + idMap = this.getPS().saveEntityListAsNew(tmpList, idMap); + } + return idMap; + } + + /* + public void saveEntityList(List<Entity> entities, String user, + boolean testWrongRelations) throws Exception { + logger.info("\n ### Making persistent Entities size total " + + entities.size() + " ### \n"); + int sizePart = 1000; + List<Entity> tmpList = null; + + for (int j = 0; j <= (entities.size() / sizePart); j++) { + tmpList = new ArrayList<Entity>(sizePart); + for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { + + if (i < entities.size()) { + if (StringUtils.isNotEmpty(user)) { + entities.get(i).setUser(user); + } + + if (testWrongRelations + && hasWrongRelations(entities.get(i))) { + throw new Exception("Wrong relations " + + entities.get(i)); + } + + tmpList.add(entities.get(i)); + } else { + break; + } + } + + logger.info("\nMaking persistent Entities part " + (j + 1) + " of " + + ((entities.size() / sizePart) + 1) + " size=" + + tmpList.size()); + this.getPS().saveEntityList(tmpList); + + for(Entity ent : tmpList){ + logger.info("[U=" + user + "] SaveEntity " + ent.toSmallString()); + } + + } + }*/ + + public void saveConcept(Entity entity) throws Exception { + entity.setType(Node.TYPE_TBOX); + entity.setObjectClass(Node.TYPE_TBOX); + this.getPS().saveEntity(entity); + } + + public void saveNodeListOnlyForScripts(List<Node> nodeList, String user) + throws Exception { + logger.debug("### Making persistent Nodes size total " + + nodeList.size() + " ###"); + List<Node> list = null; + int sizePart = 1000; + for (int j = 0; j <= (nodeList.size() / sizePart); j++) { + list = new ArrayList<Node>(); + for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { + if (i < nodeList.size()) { + nodeList.get(i).setUser(user); + list.add(nodeList.get(i)); + } + } + logger.debug("Making persistent Nodes part " + (j + 1) + " of " + + ((nodeList.size() / sizePart) + 1) + " size=" + + list.size()); + this.getPS().saveNodeList(list); + } + } + + public void saveNodeListOnlyForScripts(List<Node> nodeList) + throws Exception { + logger.info("\n ### Making persistent Nodes size total " + + nodeList.size() + " ### \n"); + int sizePart = 1000; + List<Node> list = null; + for (int j = 0; j <= (nodeList.size() / sizePart); j++) { + list = new ArrayList<Node>(); + for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { + if (i < nodeList.size()) { + list.add(nodeList.get(i)); + } + } + logger.info("Making persistent Nodes part " + (j + 1) + " of " + + ((nodeList.size() / sizePart) + 1) + " size=" + + list.size()); + this.getPS().saveNodeList(list); + } + // this.persistenceService.saveNodeList(nodeList); + } + + public List<Entity> getConcepts() { + return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION, + Node.TYPE_TBOX, null); + } + + public List<Entity> getAssertion() { + return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION, + Node.TYPE_ABOX, null); + } + + public List<Entity> getLightweightAssertionsByExactOwnValue( + String objectClass, String ownValue, int maxResult) { + return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, + null, Node.TYPE_ABOX, objectClass, ownValue, false, maxResult); + } + + public void removeNodeList(List<Node> nodeList) { + System.out.println("\n ### Deleting Nodes size total " + + nodeList.size() + " ### \n"); + this.getPS().removeNodeList(nodeList); + } + + public void deleteAllConcepts() { + // persistenceService.deleteEntities(null, Node.TYPE_TBOX, true); + getPS().dropDefinitions(); + } + + public void importOM3Concepts(String fileName, Boolean dropConcepts) { + getPS().setImportModus(true); + ImportOM3Util.importConcepts(this, fileName, dropConcepts); + getPS().setImportModus(false); + } + + // /// + + public PersistenceService getPS() { + return this.cache.getPs(); + } + + public CacheService getCache() { + return cache; + } + + public void setCache(CacheService cache) { + this.cache = cache; + } + + public List<ViewerAttribute> getViewerAttributes(Long page){ + List<ViewerAttribute> list = cache.getViewerAttributes(page); + Collections.sort(list); + return list; + } + + public List<ViewerAttribute> getViewerAttributes4Edition(Long page) throws CloneNotSupportedException{ + List<ViewerAttribute> list = new ArrayList<ViewerAttribute>(); + for(ViewerAttribute att : cache.getViewerAttributes(page)){ + list.add((ViewerAttribute)att.clone()); + } + Collections.sort(list); + return list; + } + + public Collection<ViewerPage> getViewerPages(){ + return cache.getViewerPages(); + } + + public ViewerPage getViewerPage(Long id){ + return cache.getViewerPageMap().get(id); + } + + public ViewerPage getViewerPage(String definition){ + for(ViewerPage page : cache.getViewerPages()){ + if(page.getDefinition().equals(definition)){ + return page; + } + } + return null; + } + + public ViewerPage getViewerPage4Edition(Long id) throws CloneNotSupportedException{ + ViewerPage page = cache.getViewerPageMap().get(id); + return (page != null) ? (ViewerPage) page.clone() : null; + } + + public ViewerPage saveViewerPage(ViewerPage page, String user){ + page.setUser(user); + return cache.saveViewerPage(page); + + } + + public ViewerAttribute saveViewerAttribute(ViewerPage page, ViewerAttribute att, String user) throws Exception{ + att.setUser(user); + return cache.saveViewerAttribute(page, att); + } + + /** + * Removes a page and all attributes associated with it. + * @param page + * @return + */ + public int removeViewerPage(ViewerPage page){ + + return (page.getId() != null) ? cache.removeViewerPage(page.getId()) : 0; + } + + public boolean removeViewerAnttribute(ViewerAttribute att){ + return (att.getId() != null) ? cache.removeAtt(att.getId()) : false; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ConfigurationService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,148 @@ +package org.mpi.openmind.configuration; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.apache.log4j.Appender; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; +import org.mpi.openmind.configuration.ownvalue.OwnValueRule; +import org.mpi.openmind.configuration.ownvalue.PrintRule; +import org.mpi.openmind.configuration.ownvalue.loader.OwnValueRulesLoader; +import org.mpi.openmind.configuration.ownvalue.loader.PrintRulesLoader; + +public class ConfigurationService { + + private static Logger logger = Logger.getLogger(ConfigurationService.class); + + /* + static{ + logger.setLevel(Level.DEBUG); + PatternLayout layout = new PatternLayout("%d{ABSOLUTE} %5p %c{1}:%L - %m%n"); + Appender stdout = new ConsoleAppender(layout, "System.out"); + logger.addAppender(stdout); + } + */ + + //Property's names + public static String SCHEDULING_PATH = "scheduling-path"; + public static String SCHEDULING_ENABLE = "scheduling-enable"; + public static String DEBUG_MODUS = "debug-modus"; + + + + //File's names + private static String OWN_VALUE_CONF = "own-value.cfg.xml"; + private static String OM_PROPERTIES = "openmind.properties"; + //private static String OWN_VALUE_CONF = "conf/own-value.cfg.xml"; + + private Properties props = new Properties(); + + private Map<String, OwnValueRule> ownValueRules = new HashMap<String, OwnValueRule>(); + private List<PrintRule> printRules = new ArrayList<PrintRule>(); + private String schedulingPath; + private boolean schedulingEnable = false; + private boolean debugModus = false; + + public ConfigurationService(){ + logger.info("\n### Initialize Configuration Service ###\n"); + loadOwnValueRules(); + loadProperties(); + } + + private void loadProperties(){ + + try { + InputStream in = this.getClass().getClassLoader().getResourceAsStream(OM_PROPERTIES); + props.load(in); + in.close(); + String s = "Property's List"; + for(Object key : props.keySet()){ + s += "\n" + key + "=\t" + props.getProperty(key.toString()); + + if(SCHEDULING_ENABLE.equals(key.toString())){ + try{ + Boolean b = new Boolean(props.getProperty(key.toString())); + this.schedulingEnable = b; + }catch(Exception e){} + }else if(SCHEDULING_PATH.equals(key.toString())){ + this.schedulingPath = props.getProperty(key.toString()); + }else if(DEBUG_MODUS.equals(key.toString())){ + try{ + Boolean b = new Boolean(props.getProperty(key.toString())); + this.debugModus = b; + }catch(Exception e){} + } + } + s += "\n"; + logger.info(s); + } catch (IOException e) { + System.out.println("Properties file \'" + OM_PROPERTIES + " no found."); + e.printStackTrace(); + } + + + } + + private void loadOwnValueRules(){ + try{ + this.ownValueRules = OwnValueRulesLoader.load(OWN_VALUE_CONF); + this.printRules = PrintRulesLoader.load(OWN_VALUE_CONF); + String s = "\n### Print Rules ###\n"; + for(PrintRule printRule : this.printRules){ + s += printRule + "\n"; + } + s += "### Own Value Rules ###\n"; + for(OwnValueRule ownValueRule : this.ownValueRules.values()){ + s += ownValueRule + "\n"; + } + logger.info(s); + + }catch(Exception e){ + e.printStackTrace(); + } + } + + public List<PrintRule> getPrintRules(String objectClass){ + List<PrintRule> ruleList = new ArrayList<PrintRule>(); + for(PrintRule rule : this.printRules){ + if(rule.containObjectClass(objectClass)){ + ruleList.add(rule); + } + } + return ruleList; + } + + public OwnValueRule getOwnValueRule(String id){ + return this.ownValueRules.get(id); + } + + + public Map<String, OwnValueRule> getOwnValueRules() { + return ownValueRules; + } + + public void setPrintRules(List<PrintRule> printRules) { + this.printRules = printRules; + } + + public String getSchedulingPath() { + return schedulingPath; + } + + public boolean isSchedulingEnable() { + return schedulingEnable; + } + + public boolean isDebugModus() { + return debugModus; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/NodeOwnValueRule.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,33 @@ +package org.mpi.openmind.configuration.ownvalue; + +import java.util.Vector; + +public class NodeOwnValueRule extends OwnValueRule{ + private Vector<OwnValueNode> nodeList = new Vector<OwnValueNode>(); + + public Vector<OwnValueNode> getNodeList() { + return nodeList; + } + public void setNodeList(Vector<OwnValueNode> nodeList) { + this.nodeList = nodeList; + } + + @Override + public String toString(){ + String list = ""; + for(int i=0; i < nodeList.size(); i++){ + if( i > 0) + list += " -> "; + OwnValueNode node = nodeList.get(i); + if(node instanceof OwnValueEntity){ + list += ((OwnValueEntity)node).getType() + ":" + ((OwnValueEntity)node).getObjectClass(); + }else if(node instanceof OwnValueRelation){ + list += ((OwnValueRelation)node).getType() + ":" + ((OwnValueRelation)node).getName(); + }else if(node instanceof OwnValueAttribute){ + list += "Attribute:" + ((OwnValueAttribute)node).getName(); + } + } + + return "OwnValueRule [id= " + this.getId() + ", nodeList='" + list + "']"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/ORElement.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,44 @@ +package org.mpi.openmind.configuration.ownvalue; + +import org.apache.commons.lang.StringUtils; + +public class ORElement { + public static String CONSTANT = "constant"; + private String ref; + private String type; + private String value; + + public boolean isReady(){ + if(StringUtils.isNotEmpty(ref)) + return true; + if(StringUtils.isEmpty(ref) && StringUtils.isNotEmpty(type) && StringUtils.isNotEmpty(value)) + return true; + + return false; + } + + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + public String getRef() { + return ref; + } + public void setRef(String ref) { + this.ref = ref; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + + @Override + public String toString(){ + return "ORElement [ref= " + this.ref + ", type='" + type + ", value=" + value + " ]"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/OROwnValueRule.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,28 @@ +package org.mpi.openmind.configuration.ownvalue; + +import java.util.Vector; + +public class OROwnValueRule extends OwnValueRule{ + + private Vector<ORElement> orElementList = new Vector<ORElement>(); + + public Vector<ORElement> getOrElementList() { + return orElementList; + } + public void setOrElementList(Vector<ORElement> orElementList) { + this.orElementList = orElementList; + } + + @Override + public String toString(){ + + String orList = ""; + for(int i=0; i < orElementList.size(); i++){ + if( i > 0) + orList += " -> "; + orList += orElementList.get(i).toString(); + } + + return "OROwnValueRule [id= " + this.getId() + ", orList='" + orList + "']"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/OwnValueAttribute.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,18 @@ +package org.mpi.openmind.configuration.ownvalue; + +public class OwnValueAttribute extends OwnValueNode{ + private String name; + + @Override + public String toString(){ + return "OwnValueAttribute [name=" + name + "]"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/OwnValueEntity.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,42 @@ +package org.mpi.openmind.configuration.ownvalue; + +public class OwnValueEntity extends OwnValueNode{ + public String objectClass; + public String type; + public String ownValue; + + @Override + public String toString(){ + return "OwnValueEntity [objectClass=" + objectClass + ", type=" + type + ", ownValue=" + ownValue + "]"; + } + + public OwnValueEntity(String objectClass, String type){ + this.objectClass = objectClass; + this.type = type; + } + + public String getOwnValue() { + return ownValue; + } + + public void setOwnValue(String ownValue) { + this.ownValue = ownValue; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getObjectClass() { + return objectClass; + } + + public void setObjectClass(String objectClass) { + this.objectClass = objectClass; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/OwnValueNode.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,28 @@ +package org.mpi.openmind.configuration.ownvalue; + +public class OwnValueNode { + + public static String SOURCE_ENTITY = "source"; + public static String TARGET_ENTITY = "target"; + public static String TARGET_RELATION = "target-relation"; + public static String SOURCE_RELATION = "source-relation"; + + private boolean substring = false; + private boolean endNode = false; + + public boolean isEndNode() { + return endNode; + } + + public void setEndNode(boolean endNode) { + this.endNode = endNode; + } + + public boolean isSubstring() { + return substring; + } + + public void setSubstring(boolean substring) { + this.substring = substring; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/OwnValueRelation.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,29 @@ +package org.mpi.openmind.configuration.ownvalue; + +public class OwnValueRelation extends OwnValueNode{ + + private String type; + private String name; + + @Override + public String toString(){ + return "OwnValueRelation [name=" + name + ", type=" + type + "]"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/OwnValueRule.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,12 @@ +package org.mpi.openmind.configuration.ownvalue; + +public class OwnValueRule { + private String id; + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/PrintRule.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,69 @@ +package org.mpi.openmind.configuration.ownvalue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Vector; + +public class PrintRule { + + public static String MODUS_PRINT_ALWAYS = "print-always"; + public static String MODUS_PRINT_IF_NO_NULLS = "print-if-no-nulls"; + public static String MODUS_PRINT_IF_SOME_OW_NO_NULL = "print-if-some-ow-no-null"; + + //rule to determinate when the print should be used. + private String printModus = MODUS_PRINT_IF_SOME_OW_NO_NULL; + + //format string to print the own-value + private String format; + + //list of own-value-rules ids + private Vector<String> ownValueRuleList = new Vector<String>(); + + //list of object-class, for which this rule will be applied + private List<String> forList = new ArrayList<String>(); + + @Override + public String toString(){ + return "PrintRule [for=(" + this.forList + "), format='" + this.format + "', ownValueRules=("+ ownValueRuleList + ")]"; + } + + public boolean containObjectClass(String objectClass){ + for(String objClass : forList){ + if(objClass.equals(objectClass)) + return true; + } + return false; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public Vector<String> getOwnValueRuleList() { + return ownValueRuleList; + } + + public void setOwnValueRuleList(Vector<String> ownValueRuleList) { + this.ownValueRuleList = ownValueRuleList; + } + + public List<String> getForList() { + return forList; + } + + public void setForList(List<String> forList) { + this.forList = forList; + } + + public String getPrintModus() { + return printModus; + } + + public void setPrintModus(String printModus) { + this.printModus = printModus; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/loader/OwnValueRulesLoader.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,470 @@ +package org.mpi.openmind.configuration.ownvalue.loader; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.configuration.ownvalue.NodeOwnValueRule; +import org.mpi.openmind.configuration.ownvalue.ORElement; +import org.mpi.openmind.configuration.ownvalue.OROwnValueRule; +import org.mpi.openmind.configuration.ownvalue.OwnValueAttribute; +import org.mpi.openmind.configuration.ownvalue.OwnValueEntity; +import org.mpi.openmind.configuration.ownvalue.OwnValueRelation; +import org.mpi.openmind.configuration.ownvalue.OwnValueRule; +import org.mpi.openmind.repository.utils.XMLUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class OwnValueRulesLoader { + + public static void main(String[] args) { + try{ + + Map<String, OwnValueRule> map = OwnValueRulesLoader.load("conf/own-value.cfg.xml"); + for(OwnValueRule rule : map.values()){ + System.out.println(rule); + } + }catch(Exception e){ + e.printStackTrace(); + } + } + + + public static Map<String, OwnValueRule> load(String ownValueCfgFile) throws Exception { + Map<String, OwnValueRule> rulesMap = new HashMap<String, OwnValueRule>(); + + Document doc = XMLUtil.getDocumentFromPathContext(ownValueCfgFile); + NodeList list = doc.getElementsByTagName(XMLUtil.OWN_VALUE_CONFIG); + + if (list.getLength() > 0) { + Node rootElement = list.item(0); + rulesMap = getOwnValueRules(rootElement); + integrityTest(rulesMap); + } + return rulesMap; + } + + /** + * This method tests the integrity of the OwnValueRules. If there is some own-value-rule, + * which contains a ORList and one of whose element references a rule, which does not exist, it will be thrown a Exception. + * @param rulesMap + * @throws Exception + */ + private static void integrityTest(Map<String, OwnValueRule> rulesMap) throws Exception{ + List<String> errorList = new ArrayList<String>(); + for(OwnValueRule rule : rulesMap.values()){ + if(rule instanceof OROwnValueRule){ + for(ORElement orElement : ((OROwnValueRule)rule).getOrElementList()){ + if(StringUtils.isNotEmpty(orElement.getRef())){ + if(rulesMap.get(orElement.getRef()) == null){ + errorList.add("For the Rule=" + rule.getId() + " there is no ref=" + orElement.getRef()); + } + } + } + } + } + + if(errorList.size() > 0){ + throw new Exception("Loading own-value-cfg: the integrity test thrown the followings refence's errors: '" + errorList + "'"); + } + } + + private static Map<String, OwnValueRule> getOwnValueRules(Node rootElement) throws Exception{ + Map<String, OwnValueRule> rulesMap = new HashMap<String, OwnValueRule>(); + // importing entities + Node rulesNode = XMLUtil.getNodeByName(rootElement.getChildNodes(), + XMLUtil.OWN_VALUE_RULES); + if (rulesNode != null) { + for (int i = 0; i < rulesNode.getChildNodes().getLength(); i++) { + Node ruleNode = rulesNode.getChildNodes().item(i); + if (ruleNode.getNodeName().equals(XMLUtil.OWN_VALUE_RULE) && ruleNode instanceof Element) { + OwnValueRule rule = getRuleFromNode(ruleNode); + if(rule != null){ + rulesMap.put(rule.getId(), rule); + } + } + } + } + return rulesMap; + } + + /** + * @param node + * @return + * @throws Exception + */ + private static OwnValueRule getRuleFromNode(Node node) throws Exception{ + OwnValueRule rule = null; + if(node.getAttributes() != null){ + String id = node.getAttributes().getNamedItem("id").getNodeValue(); + id = id.replace(" ", ""); + int elementCount = 0; + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node subNode = node.getChildNodes().item(i); + if(subNode instanceof Element){ + if (subNode.getNodeName().equals(XMLUtil.TARGET_RELATION)) { + rule = new NodeOwnValueRule(); + rule.setId(id); + getTargetRelationFromNode(subNode, (NodeOwnValueRule)rule); + } else if (subNode.getNodeName().equals(XMLUtil.SOURCE_RELATION)) { + rule = new NodeOwnValueRule(); + rule.setId(id); + getSourceRelationFromNode(subNode, (NodeOwnValueRule)rule); + } else if (subNode.getNodeName().equals(XMLUtil.ATTRIBUTE)) { + rule = new NodeOwnValueRule(); + rule.setId(id); + getAttributeFromNode(subNode, (NodeOwnValueRule)rule); + }else if (subNode.getNodeName().equals(XMLUtil.XOR)) { + rule = new OROwnValueRule(); + rule.setId(id); + getORListFromNode(subNode, (OROwnValueRule)rule); + } else { + throw new Exception("Loading own-value-cfg: the element '" + subNode.getNodeName() + "' [id=" + rule.getId() + + "] + can not be use in this position"); + } + if(elementCount > 0){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + id + + "] + can have only one child"); + } + elementCount++; + } + } + } + return rule; + } + + private static void getAttributeFromNode(Node node, NodeOwnValueRule rule) throws Exception{ + if (node.getAttributes() != null) { + + if(node.getAttributes().getNamedItem(XMLUtil.ATTRIBUTE_NAME) == null){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + the attribute name was not found."); + } + + String name = node.getAttributes().getNamedItem(XMLUtil.ATTRIBUTE_NAME).getNodeValue(); + + String substring = "false"; + if(node.getAttributes().getNamedItem(XMLUtil.SUBSTRING) != null){ + substring = node.getAttributes().getNamedItem(XMLUtil.SUBSTRING).getNodeValue(); + } + + OwnValueAttribute att = new OwnValueAttribute(); + att.setName(name); + att.setSubstring(((new String("true")).equals(substring))? true : false); + rule.getNodeList().add(att); + } + } + + private static void getORListFromNode(Node node, OROwnValueRule rule) throws Exception{ + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node subNode = node.getChildNodes().item(i); + if(subNode instanceof Element){ + if (subNode.getNodeName().equals(XMLUtil.RULE)) { + ORElement orElement = new ORElement(); + if(subNode.getAttributes().getNamedItem(XMLUtil.REF) != null){ + orElement.setRef(subNode.getAttributes().getNamedItem(XMLUtil.REF).getNodeValue()); + } + if(subNode.getAttributes().getNamedItem(XMLUtil.TYPE) != null){ + orElement.setType(subNode.getAttributes().getNamedItem(XMLUtil.TYPE).getNodeValue()); + } + if(subNode.getAttributes().getNamedItem(XMLUtil.VALUE) != null){ + orElement.setValue(subNode.getAttributes().getNamedItem(XMLUtil.VALUE).getNodeValue()); + } + + if(!orElement.isReady()){ + throw new Exception("Loading own-value-cfg: the element '" + subNode.getNodeName() + "' [id=" + rule.getId() + + "] + the rule was not well set."); + } + rule.getOrElementList().add(orElement); + }else{ + throw new Exception("Loading own-value-cfg: the element '" + subNode.getNodeName() + "' [id=" + rule.getId() + + "] + can not be contained by a XOR element."); + } + } + } + } + + /** + * Element name= target-relation + * Attributes: + * -name (minOccurs="1", maxOccurs="1", type="xsd:string") + * -substring (minOccurs="0", maxOccurs="1", type="xsd:string") true/false default false. + * -end-node (minOccurs="0", maxOccurs="1", type="xsd:string") true/false default false. + * + * Children Elements: "source", "target". + * + * @param node + * @param OwnValueRule + * @return + * @throws Exception illegal schema of element. + */ + private static void getTargetRelationFromNode(Node node, NodeOwnValueRule rule) throws Exception{ + if (node.getAttributes() != null) { + + if(node.getAttributes().getNamedItem(XMLUtil.NAME) == null){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + the attribute name was not found."); + } + + String name = node.getAttributes().getNamedItem(XMLUtil.NAME).getNodeValue(); + + String substring = "false"; + if(node.getAttributes().getNamedItem(XMLUtil.SUBSTRING) != null){ + substring = node.getAttributes().getNamedItem(XMLUtil.SUBSTRING).getNodeValue(); + } + + String endNode = "false"; + if(node.getAttributes().getNamedItem(XMLUtil.END_NODE) != null){ + endNode = node.getAttributes().getNamedItem(XMLUtil.END_NODE).getNodeValue(); + } + + OwnValueRelation rel = new OwnValueRelation(); + rel.setName(name); + rel.setSubstring(((new String("true")).equals(substring))? true : false); + rel.setEndNode(((new String("true")).equals(endNode))? true : false); + rel.setType(XMLUtil.TARGET_RELATION); + + rule.getNodeList().add(rel); + + //if this node is signed as endNode, the algorithm end here + if(!rel.isEndNode()){ + int elementCount = 0; + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node subNode = node.getChildNodes().item(i); + if(subNode instanceof Element){ + if (subNode.getNodeName().equals(XMLUtil.SOURCE)) { + getSourceEntityFromNode(subNode, rule); + } else if (subNode.getNodeName().equals(XMLUtil.TARGET)) { + getTargetEntityFromNode(subNode, rule); + } else { + throw new Exception("Loading own-value-cfg: the element '" + subNode.getNodeName() + "' [id=" + rule.getId() + + "] + can not follow from a target-relation"); + } + if(elementCount > 0){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + can have only one child"); + } + elementCount++; + } + } + } + + } + } + + /** + * Element name= source-relation + * Attributes: + * -name (minOccurs="1", maxOccurs="1", type="xsd:string") + * -substring (minOccurs="0", maxOccurs="1", type="xsd:string") true/false default false. + * -own-value (minOccurs="0", maxOccurs="1", type="xsd:string") true/false default false. + * + * Children Elements: "source", "target". + * + * @param node + * @param OwnValueRule + * @return + * @throws Exception illegal schema of element. + */ + private static void getSourceRelationFromNode(Node node, NodeOwnValueRule rule) throws Exception{ + if (node.getAttributes() != null) { + if(node.getAttributes().getNamedItem(XMLUtil.NAME) == null){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + the attribute name was not found."); + } + + String name = node.getAttributes().getNamedItem(XMLUtil.NAME).getNodeValue(); + + String substring = "false"; + if(node.getAttributes().getNamedItem(XMLUtil.SUBSTRING) != null){ + substring = node.getAttributes().getNamedItem(XMLUtil.SUBSTRING).getNodeValue(); + } + + String endNode = "false"; + if(node.getAttributes().getNamedItem(XMLUtil.END_NODE) != null){ + endNode = node.getAttributes().getNamedItem(XMLUtil.END_NODE).getNodeValue(); + } + + OwnValueRelation rel = new OwnValueRelation(); + rel.setName(name); + rel.setSubstring(((new String("true")).equals(substring))? true : false); + rel.setEndNode(((new String("true")).equals(endNode))? true : false); + rel.setType(XMLUtil.SOURCE_RELATION); + + rule.getNodeList().add(rel); + + //if this node is signed as endNode, the algorithm end here + if(!rel.isEndNode()){ + int elementCount = 0; + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node subNode = node.getChildNodes().item(i); + if(subNode instanceof Element){ + if (subNode.getNodeName().equals(XMLUtil.SOURCE)) { + getSourceEntityFromNode(subNode, rule); + } else if (subNode.getNodeName().equals(XMLUtil.TARGET)) { + getTargetEntityFromNode(subNode, rule); + } else { + throw new Exception("Loading own-value-cfg: the element '" + subNode.getNodeName() + "' [id=" + rule.getId() + + "] + can not follow from a source-relation"); + } + if(elementCount > 0){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + can have only one child"); + } + elementCount++; + } + } + } + } + } + + /** + * Element name= "source" + * Attributes: + * -own-value + * -object-class + * -substring + * -end-node + * @param node + * @param rule + * @throws Exception + */ + private static void getSourceEntityFromNode(Node node, NodeOwnValueRule rule) throws Exception{ + if (node.getAttributes() != null) { + + if(node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS) == null){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + the attribute object-class was not found."); + } + + String objClass = node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS).getNodeValue(); + + String endNode = "false"; + if(node.getAttributes().getNamedItem(XMLUtil.END_NODE) != null){ + endNode = node.getAttributes().getNamedItem(XMLUtil.END_NODE).getNodeValue(); + } + + String substring = null; + if(node.getAttributes().getNamedItem(XMLUtil.SUBSTRING) != null){ + substring = node.getAttributes().getNamedItem(XMLUtil.SUBSTRING).getNodeValue(); + } + + String ownValue = null; + if(node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null){ + ownValue = node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE).getNodeValue(); + } + + OwnValueEntity source = new OwnValueEntity(objClass, XMLUtil.SOURCE); + source.setSubstring(((new String("true")).equals(substring))? true : false); + source.setEndNode(((new String("true")).equals(endNode))? true : false); + + if(StringUtils.isNotEmpty(ownValue)){ + source.setOwnValue(ownValue); + } + + rule.getNodeList().add(source); + + //if this node is signed as endNode, the algorithm end here + if(!source.isEndNode()){ + int elementCount = 0; + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node subNode = node.getChildNodes().item(i); + if(subNode instanceof Element){ + if (subNode.getNodeName().equals(XMLUtil.TARGET_RELATION)) { + getTargetRelationFromNode(subNode, rule); + } else if (subNode.getNodeName().equals(XMLUtil.SOURCE_RELATION)) { + getSourceRelationFromNode(subNode, rule); + } else if (subNode.getNodeName().equals(XMLUtil.ATTRIBUTE)) { + getAttributeFromNode(subNode, rule); + } else { + throw new Exception("Loading own-value-cfg: the element '" + subNode.getNodeName() + "' [id=" + rule.getId() + + "] + can not follow from a source's element"); + } + if(elementCount > 0){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + can have only one child"); + } + elementCount++; + } + } + } + } + } + + /** + * Element name= "target" + * Attributes: + * -own-value + * -object-class + * -substring + * -end-node + * @param node + * @param rule + * @throws Exception + */ + private static void getTargetEntityFromNode(Node node, NodeOwnValueRule rule) throws Exception{ + if (node.getAttributes() != null) { + if(node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS) == null){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + the attribute object-class was not found."); + } + + String objClass = node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS).getNodeValue(); + + String endNode = "false"; + if(node.getAttributes().getNamedItem(XMLUtil.END_NODE) != null){ + endNode = node.getAttributes().getNamedItem(XMLUtil.END_NODE).getNodeValue(); + } + + String substring = null; + if(node.getAttributes().getNamedItem(XMLUtil.SUBSTRING) != null){ + substring = node.getAttributes().getNamedItem(XMLUtil.SUBSTRING).getNodeValue(); + } + + String ownValue = null; + if(node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null){ + ownValue = node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE).getNodeValue(); + } + + OwnValueEntity target = new OwnValueEntity(objClass, XMLUtil.TARGET); + target.setSubstring(((new String("true")).equals(substring))? true : false); + target.setEndNode(((new String("true")).equals(endNode))? true : false); + + if(StringUtils.isNotEmpty(ownValue)){ + target.setOwnValue(ownValue); + } + + rule.getNodeList().add(target); + + //if this node is signed as endNode, the algorithm end here + if(!target.isEndNode()){ + int elementCount = 0; + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node subNode = node.getChildNodes().item(i); + if(subNode instanceof Element){ + if (subNode.getNodeName().equals(XMLUtil.TARGET_RELATION)) { + getTargetRelationFromNode(subNode, rule); + } else if (subNode.getNodeName().equals(XMLUtil.SOURCE_RELATION)) { + getSourceRelationFromNode(subNode, rule); + } else if (subNode.getNodeName().equals(XMLUtil.ATTRIBUTE)) { + getAttributeFromNode(subNode, rule); + } else { + throw new Exception("Loading own-value-cfg: the element '" + subNode.getNodeName() + "' [id=" + rule.getId() + + "] + can not follow from a source's element"); + } + if(elementCount > 0){ + throw new Exception("Loading own-value-cfg: the element '" + node.getNodeName() + "' [id=" + rule.getId() + + "] + can have only one child, but it has " + node.getChildNodes().getLength()); + } + elementCount++; + } + } + } + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/configuration/ownvalue/loader/PrintRulesLoader.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,117 @@ +package org.mpi.openmind.configuration.ownvalue.loader; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.configuration.ownvalue.PrintRule; +import org.mpi.openmind.repository.utils.XMLUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class PrintRulesLoader { + + + public static List<PrintRule> load(String ownValueCfgFile) throws Exception{ + Document doc = XMLUtil.getDocumentFromPathContext(ownValueCfgFile); + NodeList list = doc.getElementsByTagName(XMLUtil.OWN_VALUE_CONFIG); + if (list.getLength() > 0) { + Node rootElement = list.item(0); + List<PrintRule> ruleList = getPrintRules(rootElement); + return ruleList; + } + return null; + } + + private static List<PrintRule> getPrintRules(Node rootElement) throws Exception{ + List<PrintRule> ruleList = new ArrayList<PrintRule>(); + Node rulesNode = XMLUtil.getNodeByName(rootElement.getChildNodes(), + XMLUtil.PRINT_RULES); + + if (rulesNode != null) { + for (int i = 0; i < rulesNode.getChildNodes().getLength(); i++) { + Node ruleNode = rulesNode.getChildNodes().item(i); + if (ruleNode.getNodeName().equals(XMLUtil.PRINT_RULE) && ruleNode instanceof Element) { + PrintRule rule = getRuleFromNode(ruleNode); + if(rule != null){ + ruleList.add(rule); + } + } + } + } + return ruleList; + } + + /** + * Element name print-rule + * Attributes + * -for the name of some definition/concept. + * + * @param node + * @return + * @throws Exception + */ + private static PrintRule getRuleFromNode(Node node) throws Exception{ + PrintRule rule = null; + if(node.getAttributes() != null){ + if(node.getAttributes().getNamedItem("for") == null){ + throw new Exception("Loading own-value-cfg: print-rule must have an attribute 'for'"); + } + rule = new PrintRule(); + String forValue = node.getAttributes().getNamedItem("for").getNodeValue(); + forValue = forValue.replace(" ", ""); + String[] defArray = forValue.split(","); + for(int i=0; i < defArray.length; i++){ + rule.getForList().add(defArray[i]); + } + + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node subNode = node.getChildNodes().item(i); + if(subNode instanceof Element){ + if(subNode.getNodeName().equals("formatstr")){ + String formatValue = null; + if(subNode.getAttributes() != null){ + if(subNode.getAttributes().getNamedItem("value") != null){ + formatValue = subNode.getAttributes().getNamedItem("value").getNodeValue(); + } + } + if(StringUtils.isEmpty(formatValue)){ + throw new Exception("Loading own-value-cfg: every 'formatstr' element must contain an attribute 'value'"); + } + rule.setFormat(formatValue); + }else if(subNode.getNodeName().equals("entry")){ + String ownValueRuleId = null; + if(subNode.getAttributes() != null){ + if(subNode.getAttributes().getNamedItem(XMLUtil.OWN_VALUE_RULE) != null){ + ownValueRuleId = subNode.getAttributes().getNamedItem(XMLUtil.OWN_VALUE_RULE).getNodeValue(); + ownValueRuleId = ownValueRuleId.replace(" ", ""); + } + } + if(StringUtils.isEmpty(ownValueRuleId)){ + throw new Exception("Loading own-value-cfg: every 'entry' element must contain an attribute 'own-value-rule'"); + } + rule.getOwnValueRuleList().add(ownValueRuleId); + }else{ + throw new Exception("Loading own-value-cfg: illegal element for print-rule: '" + subNode.getNodeName() + "'"); + } + } + } + + } + return rule; + } + + public static void main(String[] args) { + try{ + List<PrintRule> ruleList = PrintRulesLoader.load("conf/own-value.cfg.xml"); + for(PrintRule rule : ruleList){ + System.out.println(rule); + } + + }catch(Exception e){ + e.printStackTrace(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/Attribute.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,213 @@ +package org.mpi.openmind.repository.bo; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.Transient; + +import org.apache.commons.lang.StringUtils; +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.json.JSONArray; + +import cl.maps.triple.TripleKey; +import cl.maps.utils.AttKey; + +/** + * ALTER TABLE `openmind`.`node` ADD COLUMN `possible_value` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci AFTER `user`; + * @author jurzua + */ +@Entity +@DiscriminatorValue("ATTRIBUTE") +@Cache(usage=CacheConcurrencyStrategy.READ_ONLY) +public class Attribute extends Node implements Serializable, Comparable<Attribute> { + + @Column(name="source_id") + private Long sourceId; + + @Column(name="source_modif") + private Long sourceModif; + + @Column(name="source_obj_class") + private String sourceObjectClass; + + /** + * Only definitions can have attributes + * which indicate possible values for the instances if the them. + */ + @Column(name="possible_value", columnDefinition="mediumtext") + private String possibleValues; + + + @Transient + private List<String> possibleValuesList = null; + + public List<String> getPossibleValuesList(){ + if(this.possibleValuesList == null){ + this.possibleValuesList = new ArrayList<String>(); + if(StringUtils.isNotBlank(this.possibleValues)){ + try{ + JSONArray array = new JSONArray(this.possibleValues); + for(int i=0; i < array.length(); i++){ + this.possibleValuesList.add(array.getString(i)); + } + }catch (Exception e) {e.printStackTrace();} + } + } + return this.possibleValuesList; + } + + public void setPossibleValuesList(List<String> possibleValuesList) { + this.possibleValuesList = possibleValuesList; + } + + public Attribute(){} + + public Attribute(Attribute defAtt){ + this.setObjectClass(defAtt.getOwnValue()); + this.setContentType(defAtt.getContentType()); + } + + + public String getName(){ + return getObjectClass(); + } + + public String getValue(){ + return getOwnValue(); + } + + public String getHtmlValue(){ + String value = getOwnValue(); + if(StringUtils.isNotEmpty(value)){ + StringBuilder sb = new StringBuilder(); + int lineLong = 80; + + + int count = 0; + while(!((count * lineLong) > value.length())){ + int beginIndex = count * lineLong; + int endIndex = beginIndex + ((value.length() >= (lineLong * (count+1))) ? lineLong : value.length() % lineLong); + sb.append(value.substring(beginIndex, endIndex) + "<br/>"); + count++; + } + return sb.toString(); + } + return null; + } + + public void setName(String name){ + setObjectClass(name); + } + + public void setValue(String value){ + setOwnValue(value); + } + + public Attribute(String label, String contentType, String value) { + this.setObjectClass(label); + this.setContentType(contentType); + this.setOwnValue(value); + } + + public Long getSourceId() { + return sourceId; + } + + + public void setSourceId(Long sourceId) { + this.sourceId = sourceId; + } + + + public Long getSourceModif() { + return sourceModif; + } + + + public void setSourceModif(Long sourceModif) { + this.sourceModif = sourceModif; + } + + + public String getSourceObjectClass() { + return sourceObjectClass; + } + + + public void setSourceObjectClass(String sourceObjectClass) { + this.sourceObjectClass = sourceObjectClass; + } + + public String getPossibleValues() { + if(this.possibleValuesList != null && this.possibleValuesList.size() > 0){ + JSONArray array = new JSONArray(); + for(String possibleValue : this.possibleValuesList){ + array.put(possibleValue); + } + possibleValues = array.toString(); + } + return possibleValues; + } + + public String getPossibleValuesShort(){ + String s = this.getPossibleValues(); + if(StringUtils.isNotBlank(s)){ + if(s.length() > 20){ + s = s.substring(0, 20); + s += "..."; + } + } + return s; + } + + public void setPossibleValues(String possibleValues) { + this.possibleValues = possibleValues; + } + + public boolean equalsContent(Attribute other){ + if(other == null) + return false; + + if(StringUtils.equals(getOwnValue(), other.getOwnValue()) && + StringUtils.equals(getObjectClass(), other.getObjectClass())){ + return true; + } + return false; + } + + public void loadEntValue(org.mpi.openmind.repository.bo.Entity ent){ + this.sourceId = ent.getId(); + this.sourceModif = ent.getModificationTime(); + this.sourceObjectClass = ent.getObjectClass(); + } + + public TripleKey<AttKey, Long, Long> getKey(){ + return new TripleKey<AttKey, Long, Long>(new AttKey(this.getSourceObjectClass(), this.getName()), this.getSourceId(), this.getId()); + } + + @Override + public int compareTo(Attribute e) { + if(e == null) + return 1; + if(StringUtils.isNotEmpty(this.getOwnValue())){ + if(StringUtils.isNotEmpty(e.getOwnValue())){ + return this.getOwnValue().compareTo(e.getOwnValue()); + }else{ + return 1; + } + } + return 0; + } + @Override + public String toString() { + String rowIdString = (this.getRowId() == null) ? "" : "rowId= " + this.getRowId() + ", "; + String idString = (this.getId() == null) ? "" : "id= " + getId() + ", "; + + return "Attribute[" + rowIdString + idString + "source=" + this.getSourceId() + ", name=" + this.getObjectClass() + ", ownValue=" + this.getOwnValue() + ", sysStatus=" + this.getSystemStatus() + "]"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/Entity.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,1100 @@ +package org.mpi.openmind.repository.bo; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Transient; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.repository.utils.AttributeMap; +import org.mpi.openmind.repository.utils.OMUtils; + +import cl.maps.duplex.DuplexKey; + +/** + * + * @author jurzua + */ +@javax.persistence.Entity +@DiscriminatorValue("ENTITY") +@Cache(usage=CacheConcurrencyStrategy.READ_ONLY) +public class Entity extends Node implements Serializable, Cloneable, Comparable<Entity> { + + private static final long serialVersionUID = 3498561370603862745L; + + private static Logger logger = Logger.getLogger(Entity.class); + + public Entity() { + } + + /** + * + * @param type could be either Node.TYPE_ABOX (for assertions) or Node.TYPE_TBOX (for definitions). + * @param lightweight + */ + public Entity(String type, Boolean lightweight) { + if(StringUtils.isEmpty(type) || (!Node.TYPE_ABOX.equals(type) && !Node.TYPE_TBOX.equals(type))){ + try { + throw new Exception("Creating a Entity. Type=" + type + + " is invalid. Type could be either ABOX (for assertions) or TBOX (for definitions). " + + "See org.mpi.openmind.repository.bo.Node"); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + this.setLightweight(lightweight); + this.setType(type); + + } + + /** + * + * @param type could be either Node.TYPE_ABOX (for assertions) or Node.TYPE_TBOX (for definitions). + * @param objectClass + * @param lightweight + */ + public Entity(String type, String objectClass, Boolean lightweight) { + if(StringUtils.isEmpty(type) || (!Node.TYPE_ABOX.equals(type) && !Node.TYPE_TBOX.equals(type))){ + try { + throw new Exception("Creating a Entity. Type=" + type + + " is invalid. Type could be either ABOX (for assertions) or TBOX (for definitions). " + + "See org.mpi.openmind.repository.bo.Node"); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + this.setLightweight(lightweight); + this.setObjectClass(objectClass); + this.setType(type); + } + + @Transient + private Boolean lightweight; + @Transient + private List<Attribute> attributes = new ArrayList<Attribute>(); + @Transient + private List<Relation> sourceRelations = new ArrayList<Relation>(); + @Transient + private List<Relation> targetRelations = new ArrayList<Relation>(); + + /** + * <p>This method returns the relations which were loaded from the DB. </p> + * <p>If the content of this entity was not loaded, it means is lightweight, the output will not be valid</p> + * @param ownValue + * @return + */ + public Relation getSourceRelationByOwnValue(String ownValue){ + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + for (Relation relation : this.getSourceRelations()) { + if (relation.getOwnValue().equals(ownValue)) { + return relation; + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return null; + } + + + public boolean containsSourceRelation(String relationName, Long targetId){ + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + for (Relation relation : this.getSourceRelations()) { + if (relation.getOwnValue().equals(relationName) && + OMUtils.equals(relation.getTargetId(), targetId)) { + return true; + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return false; + } + + public boolean containsTargetRelation(String relationName, Long sourceId){ + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + for (Relation relation : this.getTargetRelations()) { + if (relation.getOwnValue().equals(relationName) && + OMUtils.equals(relation.getSourceId(), sourceId)) { + return true; + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return false; + } + + public boolean equals(Long l1, Long l2){ + if(l1 != null && l2 != null){ + return l1.equals(l2); + } + return false; + } + + public List<Relation> getSourceRelations(String relationName, String tarObjClass){ + List<Relation> list = new ArrayList<Relation>(); + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + for (Relation relation : this.getSourceRelations()) { + if (relation.getOwnValue().equals(relationName) && relation.getTargetObjectClass().equals(tarObjClass)) { + list.add(relation); + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return list; + } + + public List<Relation> getTargetRelations(String relationName, String srcObjClass){ + List<Relation> list = new ArrayList<Relation>(); + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + for (Relation relation : this.getTargetRelations()) { + if (relation.getOwnValue().equals(relationName) && relation.getSourceObjectClass().equals(srcObjClass)) { + list.add(relation); + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return list; + } + + public Boolean isConcept() { + if (this.getType().equals(Node.TYPE_TBOX)) { + return true; + } + return false; + } + + public Boolean isAssertion() { + if (this.getType().equals(Node.TYPE_ABOX)) { + return true; + } + return false; + } + + public void setConcept(Entity concept) throws Exception { + if (this.getType().equals(Node.TYPE_TBOX)) { + throw new IllegalStateException("A concept can not belong to a concept"); + } + Relation toRemove = this.getSourceRelationByOwnValue(WrapperService.IS_TYPE_OF); + if (toRemove != null) { + this.getSourceRelations().remove(toRemove); + } + Relation isTypeOf = new Relation(this, concept); + isTypeOf.setOwnValue(WrapperService.IS_TYPE_OF); + isTypeOf.setType(this.getType()); + this.setObjectClass(concept.getOwnValue()); + } + + public void addAttribute(Attribute att) { + this.testLightweightState(); + if(!lightweight){ + if(att != null){ + att.setType(this.getType()); + att.setSourceId(this.getId()); + att.setSourceModif(this.getModificationTime()); + att.setSourceObjectClass(this.getObjectClass()); + if(Node.TYPE_TBOX.equals(this.getType())){ + att.setObjectClass(Node.TYPE_TBOX); + } + this.attributes.add(att); + } + } + } + + public void replaceTargetRelation(Entity newSrc, String srcObjClass, String relName){ + this.testLightweightState(); + if(!lightweight){ + if(newSrc == null || !newSrc.isValid()){ + this.removeAllTargetRelations(relName, srcObjClass); + }else{ + boolean found = false; + + for(Relation rel : new ArrayList<Relation>(getTargetRelations(relName, srcObjClass))){ + if(rel.getSourceId().equals(newSrc.getId())){ + found = true; + }else{ + this.targetRelations.remove(rel); + } + } + + if(!found){ + this.removeAllTargetRelations(relName, srcObjClass); + Relation rel = new Relation(newSrc, this); + rel.setOwnValue(relName); + } + } + } + } + + /** + * This methods should be used for relation one-to-many + * like witness->is_part_of->codex. + * + * @param newTar + * @param tarObjClass + * @param relName + */ + public void replaceSourceRelation(Entity newNonLwTar, String tarObjClass, String relName){ + testLightweightState(); + if(!lightweight){ + + if(newNonLwTar == null || !newNonLwTar.isValid()){ + this.removeAllSourceRelations(relName, tarObjClass); + }else{ + + boolean found = false; + for(Relation rel : new ArrayList<Relation>(getSourceRelations(relName, tarObjClass))){ + if(rel.getTargetId().equals(newNonLwTar.getId())){ + found = true; + }else{ + this.sourceRelations.remove(rel); + } + } + + if(!found){ + this.removeAllSourceRelations(relName, tarObjClass); + this.addSourceRelation(relName, newNonLwTar); + //Relation rel = new Relation(this, newTar); + //rel.setOwnValue(relName); + } + } + + } + } + + public void addSourceRelation(String relName, Entity tar){ + if(!containsSourceRelation(relName, tar.getId()) && + !tar.containsTargetRelation(relName, this.getId())){ + new Relation(this, tar, relName); + }else if(!containsSourceRelation(relName, tar.getId())){ + Relation rel = tar.getTargetRelation(relName, this.getId()); + if(rel != null){ + this.sourceRelations.add(rel); + } + }else if(!tar.containsTargetRelation(relName, this.getId())){ + Relation rel = this.getSourceRelation(relName, tar.getId()); + if(rel != null){ + tar.getTargetRelations().add(rel); + } + } + } + + public void addTargetRelation(String relName, Entity src){ + if(!containsTargetRelation(relName, src.getId()) && + !src.containsSourceRelation(relName, getId())){ + new Relation(src, this, relName); + }else if(!containsTargetRelation(relName, src.getId())){ + Relation rel = src.getSourceRelation(relName, getId()); + if(rel != null){ + this.targetRelations.add(rel); + } + }else if(!src.containsSourceRelation(relName, getId())){ + Relation rel = this.getTargetRelation(relName, src.getId()); + if(rel != null){ + src.getSourceRelations().add(rel); + } + } + } + + public Relation getTargetRelation(String relName, Long srcId){ + for(Relation rel : getTargetRelations()){ + if(rel.getOwnValue().equals(relName) && rel.getSourceId().equals(srcId)){ + return rel; + } + } + return null; + } + + public Relation getSourceRelation(String relName, Long tarId){ + for(Relation rel : getSourceRelations()){ + if(rel.getOwnValue().equals(relName) && rel.getTargetId().equals(tarId)){ + return rel; + } + } + return null; + } + + public void addSourceRelation(Relation srcRel) { + //@TODO + /* big problem definition: + * example TEXT is_version_of TEXT + * the relation is_version_of is saved twice in the DB + */ + Relation oldRel = this.containsSrcRel(srcRel); + if(oldRel != null){ + + Exception ex = new Exception( + "This entity has already this relation [\n" + + toString() + "\n" + + "NewRel=" + srcRel + "\n" + + "OldRel=" + oldRel + "]"); + logger.error(ex.getMessage(), ex); + //logger.error(ex.getMessage()); + this.removeSourceRelation(srcRel.getOwnValue(), srcRel.getTargetId()); + } + srcRel.setType(getType()); + this.sourceRelations.add(srcRel); + } + + public void addTargetRelation(Relation tarRel) { + Relation oldRel = this.containsTarRel(tarRel); + if(oldRel != null){ + Exception ex = new Exception( + "This entity has already this relation [\n" + + toString() + "\n" + + "NewRel=" + tarRel + "\n" + + "OldRel=" + oldRel + "]"); + logger.error(ex.getMessage(), ex); + //logger.error(ex.getMessage()); + this.removeTargetRelation(tarRel.getOwnValue(), tarRel.getSourceId()); + } + tarRel.setType(this.getType()); + this.targetRelations.add(tarRel); + + } + + private void testLightweightState(Entity ent){ + try { + if (ent.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB. " + ent); + } + }catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + private void testLightweightState(){ + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB. " + this.toString()); + } + }catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + public List<Relation> getSourceRelations() { + this.testLightweightState(); + return sourceRelations; + } + + public void setSourceRelations(List<Relation> sourceRelations) { + this.sourceRelations = sourceRelations; + } + + public List<Relation> getTargetRelations() { + testLightweightState(); + return targetRelations; + } + + public void setTargetRelations(List<Relation> targetRelations) { + this.targetRelations = targetRelations; + } + + public List<Attribute> getAttributes() { + testLightweightState(); + return attributes; + } + + public void setAttributes(List<Attribute> attributes) { + this.attributes = attributes; + } + + /** + * <p>This method sets the object class for the current entity and + * for the entityObjectClass of its attributes.</p> + * <p>The field entityObjectClass is added to the attributes + * to be used to performance some specific types of searchs.</p> + */ + @Override + public void setObjectClass(String objectClass) { + super.setObjectClass(objectClass); + for(Attribute att : this.attributes){ + att.setSourceObjectClass(objectClass); + } + for(Relation rel : this.sourceRelations){ + rel.setSourceObjectClass(objectClass); + } + for(Relation rel : this.targetRelations){ + rel.setTargetObjectClass(objectClass); + } + if(!Node.TYPE_TBOX.equals(objectClass)){ + //OJO + //this.setType(TYPE_ABOX); + } + } + + @Override + public void setUser(String user){ + super.setUser(user); + if(this.lightweight.equals(false)){ + for(Attribute att : this.getAttributes()){ + att.setUser(user); + } + for(Relation rel : this.getSourceRelations()){ + rel.setUser(user); + for(Attribute att : rel.getAttributes()){ + att.setUser(user); + } + } + for(Relation rel : this.getTargetRelations()){ + rel.setUser(user); + for(Attribute att : rel.getAttributes()){ + att.setUser(user); + } + } + } + } + + /* + * Methods needing to the integration + * + */ + public void removeAllSourceRelationsByName(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 (Relation rel : new ArrayList<Relation>(this.getSourceRelations())) { + if (rel.getOwnValue() != null && rel.getOwnValue().equals(name)) { + this.getSourceRelations().remove(rel); + } + } + + + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + /** + * <p>Removes transiently all relations from this entity, which conform the given input.</p> + * <p>It could be made persistent using ontology service.</p> + * @param nameRelation + * @param objClassSource + * @return + */ + public void removeAllTargetRelations(String nameRelation, String objClassSource){ + testLightweightState(); + if(!lightweight){ + for(Relation rel : new ArrayList<Relation>(getTargetRelations())){ + if(rel.getOwnValue().equals(nameRelation) && rel.getSourceObjectClass().equals(objClassSource)){ + this.targetRelations.remove(rel); + } + } + } + } + + /** + * <p>Removes transiently all relations from this entity, which conform the given input.</p> + * <p>It could be made persistent using ontology service.</p> + * @param nameRelation + * @param objClassSource + * @return + */ + public void removeAllSourceRelations(String nameRelation, String objClassTarget){ + testLightweightState(); + if(!lightweight){ + for(Relation rel : new ArrayList<Relation>(getSourceRelations())){ + if(rel.getOwnValue().equals(nameRelation) && rel.getTargetObjectClass().equals(objClassTarget)){ + this.sourceRelations.remove(rel); + } + } + } + } + + /** + * + * <p>The changes executed by this method are transiently</p> + * + * <p>Two source relations are not unique for the perspective of this entity, + * if they have the same 'relationName' and 'target object class' (one-to-many relations).</p> + * + * <p>This method create a source relation, if it does not exist, + * otherwise the first source relation will be updated with the new entity target.</p> + * + * <p>Attention: if already there is more than one relation with same 'relationName' and 'target object class', + * only the first one found will be updated with the new target entity.</p> + * + * @param relName + * @param source + */ + public void setAsUniqueSourceRelation(String relName, Entity target){ + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + + Relation srcRel = null; + for(Relation rel : getSourceRelations()){ + if(rel.getOwnValue().equals(relName) && rel.getSourceObjectClass().equals(target.getObjectClass())){ + srcRel = rel; + } + } + + if(srcRel == null){ + srcRel = new Relation(this, target, relName); + }else{ + srcRel.setTarget(this); + } + + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + /** + * <p>The changes executed by this method are transiently</p> + * + * <p>Two target relations are not unique for the perspective of this entity, + * if they have the same 'relationName' and 'source object class' (many-to-one relations).</p> + * + * <p>This method create a target relation, if it does not exist, + * otherwise the first target relation will be updated with the new entity source.</p> + * + * <p>Attention: if already there is more than one relation with same 'relationName' and 'source object class', + * only the first one found will be updated with the new source entity.</p> + * + * @param relName + * @param source + */ + public void setAsUniqueTargetRelation(String relName, Entity source){ + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + + Relation tarRel = null; + for(Relation rel : getTargetRelations()){ + if(rel.getOwnValue().equals(relName) && rel.getSourceObjectClass().equals(source.getObjectClass())){ + tarRel = rel; + } + } + + if(tarRel == null){ + tarRel = new Relation(source, this, relName); + }else{ + tarRel.setTarget(this); + } + + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + /** + * Removes transiently the relation from this entity. + * It could be made persistent using ontology service + * @param name + * @param target + * @return true if the relation was found and removed + */ + public boolean removeSourceRelation(String relName, Long tarId){ + for(Relation rel : new ArrayList<Relation>(getSourceRelations())){ + if(StringUtils.equals(rel.getOwnValue(), relName) && + rel.getTargetId().equals(tarId)){ + this.sourceRelations.remove(rel); + return true; + } + } + return false; + } + + /** + * + * @return true if this entity can be made persistent, otherwise false. + */ + public boolean isValid(){ + if(StringUtils.isNotEmpty(getType()) && (getType().equals(Node.TYPE_ABOX) || getType().equals(Node.TYPE_TBOX))){ + if(StringUtils.isNotEmpty(getObjectClass())){ + return true; + } + } + return false; + } + + /** + * Removes transiently the relation from this entity. + * It could be made persistent using ontology service + * @param name + * @param target + * @return true if the relation was found and removed + */ + public boolean removeTargetRelation(String relName, Long srcId){ + for(Relation rel : new ArrayList<Relation>(getTargetRelations())){ + if(StringUtils.equals(rel.getOwnValue(), relName) && + rel.getSourceId().equals(srcId)){ + this.targetRelations.remove(rel); + return true; + } + } + return false; + } + + /** + * Removes transiently the first target relation found, which match the given name. + * change: now delete all relation by name + * @param name ownValue of the relation. + */ + public void removeAllTargetRelationsByName(String name){ + + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + List<Relation> removeList = new ArrayList<Relation>(); + for (Relation rel : this.getTargetRelations()) { + if (rel.getOwnValue() != null && rel.getOwnValue().equals(name)) { + removeList.add(rel); + } + } + for(Relation r : removeList){ + this.getTargetRelations().remove(r); + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + public AttributeMap getAttributeMap(){ + return new AttributeMap(this); + } + + public Attribute getAttributeByName(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 (Attribute attribute : this.getAttributes()) { + if (attribute.getObjectClass().equals(name) /*|| attribute.getObjectClass().equals(name)*/) { + return attribute; + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return null; + } + + public Attribute getAttributeByOwnValue(String ow) { + try { + if (this.lightweight) { + throw new IllegalAccessException("This Entity is lightweight, so its relations and attributes were not loaded from the DB."); + } + for (Attribute attribute : this.getAttributes()) { + if (attribute.getOwnValue().equals(ow) /*|| attribute.getObjectClass().equals(name)*/) { + return attribute; + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return null; + } + + public boolean containsAttribute(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 (Attribute attribute : this.getAttributes()) { + if (attribute.getObjectClass().equals(name)) { + return true; + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + return false; + } + return false; + } + + @Override + public void increaseVersion() { + super.increaseVersion(); + for (Attribute att : this.attributes) { + att.increaseVersion(); + } + for (Relation rel : this.sourceRelations) { + rel.increaseVersion(); + for(Attribute att : rel.getAttributes()){ + att.increaseVersion(); + } + } + for (Relation rel : this.targetRelations) { + rel.increaseVersion(); + for(Attribute att : rel.getAttributes()){ + att.increaseVersion(); + } + } + for (View view : this.getViews()) { + view.increaseVersion(); + } + } + + /** + * deletes the value of rowId for the entity and all associated elements. + * As consequence of this method, when the entity is saved, it will be saved as a new row. + */ + public void resetRowId() { + this.setRowId(null); + for (Attribute att : this.attributes) { + att.setRowId(null); + } + for (Relation rel : this.sourceRelations) { + rel.setRowId(null); + for(Attribute att : rel.getAttributes()){ + att.setRowId(null); + } + } + for (Relation rel : this.targetRelations) { + rel.setRowId(null); + for(Attribute att : rel.getAttributes()){ + att.setRowId(null); + } + } + for (View view : this.getViews()) { + view.setRowId(null); + } + } + + public void resetId() { + this.setId(null); + for (Attribute att : this.attributes) { + att.setId(null); + att.setSourceId(null); + att.setSourceModif(null); + att.setSourceObjectClass(null); + } + for (Relation rel : this.sourceRelations) { + rel.setId(null); + for(Attribute att : rel.getAttributes()){ + att.setId(null); + } + } + for (Relation rel : this.targetRelations) { + rel.setId(null); + for(Attribute att : rel.getAttributes()){ + att.setId(null); + } + } + for (View view : this.getViews()) { + view.setId(null); + } + } + + @Override + public void setType(String type) { + super.setType(type); + for (Attribute attribute : this.attributes) { + attribute.setType(type); + } + for (Relation rel : this.sourceRelations) { + rel.setType(type); + for(Attribute att : rel.getAttributes()){ + att.setType(type); + } + } + for (Relation rel : this.targetRelations) { + rel.setType(type); + for(Attribute att : rel.getAttributes()){ + att.setType(type); + } + } + + } + + @Override + public void setModificationTime(Long time) { + super.setModificationTime(time); + for (Attribute attribute : this.getAttributes()) { + attribute.setModificationTime(time); + attribute.setSourceModif(time); + } + for (Relation rel : this.getSourceRelations()) { + //rel.setModificationTime(time); + rel.setSourceModif(time); + rel.setModificationTime(time); + for(Attribute att : rel.getAttributes()){ + att.setModificationTime(time); + } + if(this.getId() != null && this.getId().equals(rel.getTargetId())){ + rel.setTargetModif(time); + } + } + for (Relation rel : this.getTargetRelations()) { + //rel.setModificationTime(time); + rel.setTargetModif(time); + rel.setModificationTime(time); + for(Attribute att : rel.getAttributes()){ + att.setModificationTime(time); + } + if(this.getId() != null && this.getId().equals(rel.getSourceId())){ + rel.setSourceModif(time); + } + } + } + + @Override + public void setIsPublic(Boolean isPublic) { + super.setIsPublic(isPublic); + if(!this.isLightweight()){ + for (Attribute attribute : this.getAttributes()) { + attribute.setIsPublic(isPublic); + + } + for (Relation rel : this.getSourceRelations()) { + rel.setIsPublic(isPublic); + for(Attribute att : rel.getAttributes()){ + att.setIsPublic(isPublic); + } + + } + for (Relation rel : this.getTargetRelations()) { + rel.setIsPublic(isPublic); + for(Attribute att : rel.getAttributes()){ + att.setIsPublic(isPublic); + } + } + } + } + + @Override + public Object clone() { + try { + Entity clone = (Entity) super.clone(); + clone.setAttributes(new ArrayList<Attribute>()); + clone.setSourceRelations(new ArrayList<Relation>()); + clone.setTargetRelations(new ArrayList<Relation>()); + + if(!clone.isLightweight()){ + for (Attribute attribute : this.attributes) { + clone.addAttribute((Attribute) attribute.clone()); + } + for (Relation srcRelation : this.sourceRelations) { + Relation clonRelation = (Relation) srcRelation.clone(); + clonRelation.setAttributes(new ArrayList<Attribute>()); + for(Attribute att : srcRelation.getAttributes()){ + clonRelation.getAttributes().add((Attribute)att.clone()); + } + clone.addSourceRelation(clonRelation); + } + for (Relation targetRelation : this.targetRelations) { + Relation clonRelation = (Relation) targetRelation.clone(); + clonRelation.setAttributes(new ArrayList<Attribute>()); + for(Attribute att : targetRelation.getAttributes()){ + clonRelation.getAttributes().add((Attribute)att.clone()); + } + clone.addTargetRelation((Relation) targetRelation.clone()); + } + } + + return clone; + } catch (Exception e) { + logger.error(e.getMessage(), e); + return null; + } + } + + @Override + public void setSystemStatus(String status) { + super.setSystemStatus(status); + if(!lightweight){ + for (Attribute attribute : this.getAttributes()) { + attribute.setSystemStatus(status); + } + //really does matter if the system status is or no + //Current for a relation + for (Relation rel : this.getSourceRelations()) { + rel.setSystemStatus(status); + for(Attribute att : rel.getAttributes()){ + att.setSystemStatus(status); + } + } + for (Relation rel : this.getTargetRelations()) { + rel.setSystemStatus(status); + for(Attribute att : rel.getAttributes()){ + att.setSystemStatus(status); + } + } + } + } + + public DuplexKey<String, Long> getKey(){ + return new DuplexKey<String, Long>(this.getObjectClass(), this.getId()); + } + + public Boolean isLightweight() { + if(lightweight == null) + this.lightweight = true; + return lightweight; + } + + public void setLightweight(Boolean lightweight) { + this.lightweight = lightweight; + } + + public String toSmallString() { + String rowIdString = (this.getRowId() == null) ? "" : "rowId= " + this.getRowId() + ", "; + String idString = (this.getId() == null) ? "" : "id= " + getId() + ", "; + + return "Entity[" + rowIdString + idString + "objClass= " + this.getObjectClass() + ", ownValue= " + this.getOwnValue() + "]"; + } + + @Override + public String toString() { + String rowIdString = (this.getRowId() == null) ? "" : "rowId= " + this.getRowId() + ", "; + String idString = (this.getId() == null) ? "" : "id= " + getId() + ", "; + + + + return "Entity[" + rowIdString + idString + "oc=" + this.getObjectClass() + ", ov=" + this.getOwnValue() + ", user=" + getUser() + ", sysStatus=" + this.getSystemStatus() + + ", type= " + this.getType() + " att.size= " + this.attributes.size() + "]"; + } + + public String getShortString(){ + return "[" + this.getId() + "] " + this.getOwnValue(); + } + + @Override + public int compareTo(Entity e) { + if(e == null) + return 1; + if(StringUtils.isNotEmpty(this.getOwnValue())) + return this.getOwnValue().compareTo(e.getOwnValue()); + else return 0; + } + + public Relation containsSrcRel(Relation other){ + + for(Relation ownRel : getSourceRelations()){ + if(ownRel.equalsContent(other)){ + return ownRel; + } + } + return null; + } + + public Relation containsTarRel(Relation other){ + for(Relation ownRel : getTargetRelations()){ + if(ownRel.equalsContent(other)){ + return ownRel; + } + } + return null; + } + + public boolean equalsContent(Entity other) throws Exception{ + + if(this.isLightweight() || other.isLightweight()){ + throw new Exception(" It is not possible to comparate the content of two lightWeight entities."); + } + + if(this.getAttributes().size() != other.getAttributes().size() || + this.sourceRelations.size() != other.sourceRelations.size() || + this.targetRelations.size() != other.targetRelations.size()){ + return false; + } + + for(Attribute att : this.getAttributes()){ + Attribute otherAtt = other.getAttributeByName(att.getName()); + if(!att.equalsContent(otherAtt)){ + return false; + } + } + + for(Relation rel : other.getSourceRelations()){ + if(containsSrcRel(rel) == null){ + return false; + } + } + + for(Relation rel : other.getTargetRelations()){ + if(this.containsTarRel(rel) == null){ + return false; + } + } + + + return true; + } + + public void refreshEnt(Entity other, WrapperService ws) throws Exception{ + + //Attributes + for(Attribute otherAtt : other.getAttributes()){ + Attribute rootAtt = this.getAttributeByName(otherAtt.getName()); + if(rootAtt == null){ + Attribute newAtt = new Attribute(otherAtt.getName(), otherAtt.getContentType(), otherAtt.getValue()); + this.addAttribute(newAtt); + logger.info("Adding " + newAtt); + }else if(!rootAtt.equalsContent(otherAtt)){ + String oldValue = rootAtt.getOwnValue(); + rootAtt.setValue(otherAtt.getValue()); + logger.info("Refreshing " + rootAtt + "\n" + oldValue + "\n"); + } + } + + for(Attribute rootAtt : new ArrayList<Attribute>(this.getAttributes())){ + if(other.getAttributeByName(rootAtt.getName()) == null){ + this.getAttributes().remove(rootAtt); + } + } + + //Source Relations + for(Relation otherRel : other.getSourceRelations()){ + if(this.containsSrcRel(otherRel) == null){ + //create + Relation rel = Relation.entRelation(otherRel, ws); + this.addSourceRelation(rel); + } + } + + for(Relation rootRel : new ArrayList<Relation>(this.getSourceRelations())){ + if(other.containsSrcRel(rootRel) == null){ + this.getSourceRelations().remove(rootRel); + } + } + + //Target relations + for(Relation otherRel : other.getTargetRelations()){ + if(this.containsTarRel(otherRel) == null){ + //create + Relation rel = Relation.entRelation(otherRel, ws); + this.addTargetRelation(rel); + } + } + + for(Relation rootRel : new ArrayList<Relation>(this.getTargetRelations())){ + if(other.containsTarRel(rootRel) == null){ + this.getTargetRelations().remove(rootRel); + } + } + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/Node.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,368 @@ +package org.mpi.openmind.repository.bo; + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.Normalizer; +import java.text.SimpleDateFormat; +import java.text.Normalizer.Form; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import javax.persistence.Transient; + +import org.hibernate.annotations.Type; +import org.mpi.openmind.repository.utils.ArabicNormalizerUtils; +import org.mpi.openmind.repository.utils.NormalizerUtils; +import org.mpi.openmind.repository.utils.RomanizationLoC; + +/** + * + * @author jurzua + */ +@Entity +@Table(name="node") +@Inheritance(strategy=InheritanceType.SINGLE_TABLE) +@DiscriminatorColumn(name="node_type", discriminatorType=DiscriminatorType.STRING) +@DiscriminatorValue("NODE") +public abstract class Node implements Serializable, Cloneable { + private static final long serialVersionUID = 1L; + + public static String TYPE_TBOX = "TBox"; + public static String TYPE_ABOX = "ABox"; + public static String TYPE_VARIABLE = "Variable"; + public static String TYPE_FORMULA = "Formula"; + public static String TYPE_LINK = "Link"; + + public static String SYS_STATUS_CURRENT_VERSION = "CURRENT_VERSION"; + public static String SYS_STATUS_PREVIOUS_VERSION = "PREVIOUS_VERSION"; + + private static SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss" ); + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name="row_id") + private Long rowId; + + @Transient + private Date dateModification; + + public String getTimeStamp(){ + return DateFormat.getDateTimeInstance( + DateFormat.MEDIUM, DateFormat.SHORT).format(getDateModification()); + } + + public Date getDateModification() { + dateModification = new Date(this.modificationTime); + return dateModification; + } + + public void setDateModification(Date dateModification) { + this.dateModification = dateModification; + } + + @Transient + private List<View> views = new ArrayList<View>(); + + public void addView(View view){ + if(view != null){ + this.views.add(view); + } + } + + public List<View> getViews() { + return views; + } + + public void setViews(List<View> views) { + this.views = views; + } + + @Column(name="long_value") + private Long longValue; + + @Column(name="binary_value") + private byte[] binaryValue; + + @Column(name="object_class") + private String objectClass; + + @Column(name="user") + private String user; + + //@Type(type = "org.hibernate.type.NumericBooleanType") + @Column(name="public") + private Boolean isPublic = Boolean.FALSE; + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getObjectClass() { + return objectClass; + } + + public void setObjectClass(String objectClass) { + this.objectClass = objectClass; + } + + public Long getLongValue() { + return longValue; + } + + + public void setLongValue(Long longValue) { + this.longValue = longValue; + } + + public byte[] getBinaryValue() { + return binaryValue; + } + + public void setBinaryValue(byte[] binaryValue) { + this.binaryValue = binaryValue; + } + + @Column(name="modification_time") + private Long modificationTime; + + @Column(name="id") + private Long id; + + //lenght 16777215= Medium text + @Column(name="own_value", columnDefinition="mediumtext") + private String ownValue; + + //, length=16777215 + @Column(name="normalized_own_value", columnDefinition="mediumtext") + private String normalizedOwnValue; + + //, length=16777215 + @Column(name="normalized_arabic_own_value", columnDefinition="mediumtext") + private String normalizedArabicOwnValue; + + @Column(name="version") + private Long version; + + @Column(name="status") + private String status; + + @Column(name="system_status") + private String systemStatus; + + @Column(name="type") + private String type; + + @Column(name="content_type") + private String contentType; + + /** + * Returns true if the entity was made persistent. + * It means the fields 'id' and 'rowId' are not null. + * @return + */ + public boolean isPersistent(){ + if(this.getId() == null || this.getRowId() == null) + return false; + else + return true; + } + + public void increaseVersion(){ + if(this.version == null){ + this.version = new Long(1); + }else{ + this.version++; + } + } + + public String getOwnValue() { + return ownValue; + } + + public void setOwnValue(String ownValue) { + this.ownValue = ownValue; + this.normalizedOwnValue = NormalizerUtils.normalize(ownValue); + this.normalizedArabicOwnValue = ArabicNormalizerUtils.normalize(ownValue); + } + + public void autoNormalize(){ + this.normalizedOwnValue = NormalizerUtils.normalize(ownValue); + this.normalizedArabicOwnValue = ArabicNormalizerUtils.normalize(ownValue); + } + + public String getRomanizationLoC(){ + return RomanizationLoC.convert(this.ownValue); + } + + public String getNormalizedOwnValue() { + return normalizedOwnValue; + } + + public void setNormalizedOwnValue(String normalizedOwnValue) { + this.normalizedOwnValue = normalizedOwnValue; + } + + public String getNormalizedArabicOwnValue() { + return normalizedArabicOwnValue; + } + + public void setNormalizedArabicOwnValue(String normalizedArabicOwnValue) { + this.normalizedArabicOwnValue = normalizedArabicOwnValue; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public Long getRowId() { + return rowId; + } + + public void setRowId(Long rowId) { + this.rowId = rowId; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getModificationTime() { + return modificationTime; + } + + public void setModificationTime(Long modificationTime) { + this.modificationTime = modificationTime; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getSystemStatus() { + return systemStatus; + } + + public void setSystemStatus(String systemStatus) { + this.systemStatus = systemStatus; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } + + /* + @Override + public int hashCode() { + int hash = 0; + hash += (rowId != null ? rowId.hashCode() : 0); + return hash; + }*/ + @Override + public int hashCode() { + if(this.rowId == null){ + return super.hashCode(); + }else{ + return rowId.hashCode(); + } + } + + /** + * Clones this object and every object contained by it. + * This method was implemented because the cache. The cache must return a cloned version of a object, + * otherwise when the object is modified, it becomes dirty and the cache could not recognize the change of the state. + */ + @Override + public Object clone() { + try { + Node clone = (Node)super.clone(); + clone.setViews(new ArrayList<View>()); + for(View view : this.views){ + clone.addView((View)view.clone()); + } + return clone; + } + catch (CloneNotSupportedException e) { + throw new InternalError(e.toString()); + } + } + + public Boolean getIsPublic() { + return isPublic; + } + + public void setIsPublic(Boolean isPublic) { + this.isPublic = isPublic; + } + + public String getPrivacity(){ + if(isPublic != null) + return (isPublic) ? "public" : "private"; + return "private"; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof Node)) { + return false; + } + Node other = (Node) object; + + if(this.rowId != null && other.rowId != null && this.rowId.longValue() == other.rowId.longValue()) + return true; + + /* + if ((this.rowId == null && other.rowId != null) || (this.rowId != null && !this.rowId.equals(other.rowId))) { + return false; + }*/ + return false; + } + + @Override + public String toString() { + String rowIdString = (this.getRowId() == null) ? "" : "rowId= " + this.getRowId() + ", "; + String idString = (this.getId() == null) ? "" : "id= " + getId() + ", "; + + return "Node[" + rowIdString + idString + "name= " + this.getObjectClass() + ", ownValue= " + this.getOwnValue() + ", sysStatus= " + this.getSystemStatus() + "]"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/Relation.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,278 @@ +package org.mpi.openmind.repository.bo; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Transient; + +import org.apache.commons.lang.StringUtils; +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.event.def.OnUpdateVisitor; +import org.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.repository.utils.OMUtils; + +import cl.maps.penta.PentaKey; +import cl.maps.utils.RelKey; + +/** + * + * @author jurzua + */ +@javax.persistence.Entity +@DiscriminatorValue("RELATION") +@Cache(usage=CacheConcurrencyStrategy.READ_ONLY) +public class Relation extends Node implements Serializable, Comparable<Relation> { + + public Relation(){ + this.setSystemStatus(SYS_STATUS_CURRENT_VERSION); + } + + + public static Relation defRelation(Relation other, WrapperService ws) throws Exception{ + + Relation rel = new Relation(); + + rel.setOwnValue(other.getOwnValue()); + rel.setObjectClass(other.getObjectClass()); + rel.setType(other.getType()); + + Entity src = ws.getDefinitionById(other.getSourceId()); + Entity tar = ws.getDefinitionById(other.getTargetId()); + if(src == null || tar == null){ + throw new Exception("src and/or target not found!"); + } + rel.setSource(src); + rel.setTarget(tar); + return rel; + } + + public static Relation entRelation(Relation other, WrapperService ws) throws Exception{ + + Relation rel = new Relation(); + + rel.setOwnValue(other.getOwnValue()); + rel.setObjectClass(other.getOwnValue()); + rel.setType(Node.TYPE_ABOX); + + Entity src = ws.getEntityById(other.getSourceId()); + Entity tar = ws.getEntityById(other.getTargetId()); + + if(src == null || tar == null){ + throw new Exception("src and/or target not found!"); + } + + rel.setSource(src); + rel.setTarget(tar); + return rel; + } + + public Relation(Entity source, Entity target){ + this.setSource(source); + this.setTarget(target); + source.addSourceRelation(this); + target.addTargetRelation(this); + this.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + } + + public Relation(Entity srcNonLW, Entity tarNonLW, String ownValue){ + this.setSource(srcNonLW); + this.setTarget(tarNonLW); + srcNonLW.addSourceRelation(this); + tarNonLW.addTargetRelation(this); + this.setOwnValue(ownValue); + this.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + } + + @Column(name="source_id") + private Long sourceId; + + @Column(name="source_modif") + private Long sourceModif; + + @Column(name="source_obj_class") + private String sourceObjectClass; + + @Column(name="target_id") + private Long targetId; + + @Column(name="target_modif") + private Long targetModif; + + @Column(name="target_obj_class") + private String targetObjectClass; + + @Transient + private Entity source; + + @Transient + private Entity target; + + @Transient + private List<Attribute> attributes = new ArrayList<Attribute>(); + + public Attribute getAttributeByName(String name) { + for (Attribute attribute : this.getAttributes()) { + if (attribute.getObjectClass().equals(name)) { + return attribute; + } + } + return null; + } + + public void addAttribute(Attribute att){ + if(att != null){ + att.setSourceId(this.getId()); + att.setSourceModif(this.getModificationTime()); + att.setSystemStatus(this.getSystemStatus()); + this.attributes.add(att); + } + } + + @Override + public void setSystemStatus(String status) { + super.setSystemStatus(status); + if(this.attributes != null){ + for(Attribute att : this.attributes){ + att.setSystemStatus(status); + } + } + + } + + public List<Attribute> getAttributes() { + return attributes; + } + + public void setAttributes(List<Attribute> attributes) { + this.attributes = attributes; + } + + public String getSourceObjectClass() { + return sourceObjectClass; + } + + public void setSourceObjectClass(String sourceObjectClass) { + this.sourceObjectClass = sourceObjectClass; + } + + public String getTargetObjectClass() { + return targetObjectClass; + } + + public void setTargetObjectClass(String targetObjectClass) { + this.targetObjectClass = targetObjectClass; + } + + public Long getSourceModif() { + return sourceModif; + } + + public void setSourceModif(Long sourceModif) { + this.sourceModif = sourceModif; + } + + public Long getTargetModif() { + return targetModif; + } + + public void setTargetModif(Long targetModif) { + this.targetModif = targetModif; + } + + public Entity getSource() { + return source; + } + + public void setSource(Entity src) { + this.source = src; + if(src != null){ + this.setSourceId(src.getId()); + this.setSourceModif(src.getModificationTime()); + this.setSourceObjectClass(src.getObjectClass()); + } + } + + public Entity getTarget() { + return target; + } + + public void setTarget(Entity target) { + this.target = target; + if(target != null){ + this.setTargetId(target.getId()); + this.setTargetModif(target.getModificationTime()); + this.setTargetObjectClass(target.getObjectClass()); + } + } + + + public Long getSourceId() { + return sourceId; + } + + public void setSourceId(Long sourceId) { + this.sourceId = sourceId; + } + + public Long getTargetId() { + return targetId; + } + + public void setTargetId(Long targetId) { + this.targetId = targetId; + } + + public boolean equalsContent(Relation other){ + if(other == null){ + return false; + } + + if(StringUtils.equals(getOwnValue(), other.getOwnValue()) && + OMUtils.equals(getSourceId(), other.getSourceId()) && + OMUtils.equals(getTargetId(), other.getTargetId())){ + return true; + } + + return false; + } + + + @Override + public String toString() { + String source = ", Src[id=" + sourceId; + if(getSource() != null) + source += ", src=" + getSource().getOwnValue(); + source +="] "; + String target = ", Tar[id=" + targetId; + if(getTarget() != null) + target += ", tar=" + getTarget().getOwnValue(); + target +="] "; + return "Relation[" + + "rowId=" + this.getRowId() + ", " + + "id=" + getId() +", " + + "ownValue=" + getOwnValue() + source + target + ", " + + "sysStatus=" + this.getSystemStatus() + "]"; + } + + @Override + public int compareTo(Relation e) { + if(e == null) + return 1; + if(StringUtils.isNotEmpty(this.getOwnValue())) + return this.getOwnValue().compareTo(e.getOwnValue()); + else return 0; + } + + public PentaKey<RelKey, Long, Long, String, Long> getKey(){ + return new PentaKey<RelKey, Long, Long, String, Long>( + new RelKey(this.getSourceId(), this.getTargetId(), this.getOwnValue()), + this.getSourceId(), + this.getTargetId(), + this.getOwnValue(), + this.getId()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/View.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,55 @@ +package org.mpi.openmind.repository.bo; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +/** + * + * @author jurzua + */ +@Entity +@DiscriminatorValue("VIEW") +public class View extends Node implements Serializable{ + + @Column(name="source_id") + private Long sourceId; + + @Column(name="source_modif") + private Long sourceModif; + + @Column(name="source_obj_class") + private String sourceObjectClass; + + public Long getSourceId() { + return sourceId; + } + + public void setSourceId(Long sourceId) { + this.sourceId = sourceId; + } + + public Long getSourceModif() { + return sourceModif; + } + + public void setSourceModif(Long sourceModif) { + this.sourceModif = sourceModif; + } + + public String getSourceObjectClass() { + return sourceObjectClass; + } + + public void setSourceObjectClass(String sourceObjectClass) { + this.sourceObjectClass = sourceObjectClass; + } + + @Override + public String toString() { + return "View[rowId=" + this.getRowId() + "]"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/ViewerAttribute.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,196 @@ +package org.mpi.openmind.repository.bo; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; + +import cl.maps.duplex.DuplexKey; + + +/** + * + * This class is used to generate automatically view pages. + * + * possible values for term: + * WITNESS|source:is_part_of|CODEX:identifier + * + * @author jurzua + */ +@Entity +@Table(name="viewer_attribute") +public class ViewerAttribute implements Cloneable, Comparable<ViewerAttribute>{ + + public static Integer SHOW_ALWAYS = 0; + public static Integer SHOW_WITH_CONTENT = 1; + public static Integer HIDE = 2; + + public static String ALIGN_LEFT = "left"; + public static String ALIGN_RIGHT = "right"; + public static String ALIGN_CENTER = "center"; + + public static Integer CONTENT_TEXT = 0; + public static Integer CONTENT_DATE = 1; + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name="id") + private Long id; + + @Column(name="page", columnDefinition="varchar(255)") + private Long page; + + @Column(name="label", columnDefinition="varchar(255)") + private String label; + + @Column(name="display_mode") + private Integer displayMode = SHOW_WITH_CONTENT; + + @Column(name="text_align") + private String textAlign = ALIGN_RIGHT; + + @Column(name="query", columnDefinition="varchar(255)") + private String query; + + @Column(name="position") + private Integer index; + + @Column(name="user") + private String user; + + @Column(name="content_type") + private Integer contentType; + + + @Temporal(TemporalType.TIMESTAMP) + @Column(name="created") + private Date created; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name="updated") + private Date updated; + + public boolean isPersistent(){ + return (this.id != null); + } + + public DuplexKey<Long, Long> getKey(){ + return new DuplexKey<Long, Long>(this.page, this.id); + } + + @PrePersist + protected void onCreate() { + updated = created = new Date(); + } + + @PreUpdate + protected void onUpdate() { + updated = new Date(); + } + + public Long getPage() { + return page; + } + + public void setPage(Long page) { + this.page = page; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public Integer getDisplayMode() { + return displayMode; + } + + public void setDisplayMode(Integer displayMode) { + this.displayMode = displayMode; + } + + public String getQuery() { + return query; + } + + public void setQuery(String query) { + this.query = query; + } + + public Integer getIndex() { + return index; + } + + public void setIndex(Integer index) { + this.index = index; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public int compareTo(ViewerAttribute o) { + return this.index - o.index; + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public Date getUpdated() { + return updated; + } + + public void setUpdated(Date updated) { + this.updated = updated; + } + + public String getTextAlign() { + return textAlign; + } + + public void setTextAlign(String textAlign) { + this.textAlign = textAlign; + } + + public Integer getContentType() { + return contentType; + } + + public void setContentType(Integer contentType) { + this.contentType = contentType; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/ViewerPage.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,111 @@ +package org.mpi.openmind.repository.bo; + +import java.util.Date; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; + + +@Entity +@Table(name="viewer_page") +public class ViewerPage implements Cloneable{ + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name="id") + private Long id; + + @Column(name="label") + private String label; + + @Column(name="definition") + private String definition; + + @Column(name="user") + private String user; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name="created") + private Date created; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name="updated") + private Date updated; + + public boolean isPersistent(){ + return id != null; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + @PrePersist + protected void onCreate() { + updated = created = new Date(); + } + + @PreUpdate + protected void onUpdate() { + updated = new Date(); + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public Date getUpdated() { + return updated; + } + + public void setUpdated(Date updated) { + this.updated = updated; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getDefinition() { + return definition; + } + + public void setDefinition(String definition) { + this.definition = definition; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/utils/EntitySortByNormalizedOwnValue.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,63 @@ +package org.mpi.openmind.repository.bo.utils; + +import java.util.Comparator; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.utils.TransliterationUtil; + +public class EntitySortByNormalizedOwnValue implements Comparator<Entity>{ + + public int compare(Entity o1, Entity o2) { + return EntitySortByNormalizedOwnValue.compare0(o1, o2); + } + + public static int compare0(Entity o1, Entity o2){ + if(o1 == null && o2 == null){ + return 0; + }else if(o1 == null){ + return 1; + }else if(o2 == null){ + return -1; + } + + try{ + String s1 = o1.getNormalizedOwnValue(); + String s2 = o2.getNormalizedOwnValue(); + + if(StringUtils.isBlank(s1) && StringUtils.isBlank(s2)){ + return 0; + }else if(StringUtils.isBlank(s1)){ + return 1; + }else if(StringUtils.isBlank(s2)){ + return -1; + }else{ + s1 = s1.replace("#", ""); + s1 = s1.replace("-", ""); + s1 = s1.replace("(", ""); + s1 = s1.replace(")", ""); + s1 = s1.replace("[", ""); + s1 = s1.replace("]", ""); + s1 = s1.replace("_", ""); + + + s2 = s2.replace("#", ""); + s2 = s2.replace("-", ""); + s2 = s2.replace("(", ""); + s2 = s2.replace(")", ""); + s2 = s2.replace("[", ""); + s2 = s2.replace("]", ""); + s2 = s2.replace("_", ""); + + s1 = TransliterationUtil.getTransliteration(s1); + s2 = TransliterationUtil.getTransliteration(s2); + + int value = s1.compareTo(s2); + return value; + } + }catch(Exception e){ + e.printStackTrace(); + } + return 0; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/utils/EntitySortByVersion.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,24 @@ +package org.mpi.openmind.repository.bo.utils; + +import java.util.Comparator; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.repository.bo.Entity; + +public class EntitySortByVersion implements Comparator<Entity>{ + + public int compare(Entity e1, Entity e2){ + int diff = 0; + + if(e1.getVersion() != null && e2.getVersion() != null){ + diff = new Long(e1.getVersion() - e2.getVersion()).intValue(); + } + + if(diff == 0 && StringUtils.isNotEmpty(e1.getOwnValue()) && StringUtils.isNotEmpty(e2.getOwnValue())){ + diff = e1.getOwnValue().compareTo(e2.getOwnValue()); + } + + return diff; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/utils/RelationSortBySourceOW.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,28 @@ +package org.mpi.openmind.repository.bo.utils; + +import java.util.Comparator; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.repository.bo.Relation; + +public class RelationSortBySourceOW implements Comparator<Relation>{ + + public int compare(Relation r1, Relation r2){ + if(r1.getSource() != null && r2.getSource() != null){ + String r1OW = r1.getSource().getOwnValue(); + String r2OW = r2.getSource().getOwnValue(); + if(StringUtils.isNotEmpty(r1OW) && StringUtils.isNotEmpty(r2OW)){ + return r1OW.compareTo(r2OW); + }else if(StringUtils.isEmpty(r1OW)){ + return -1; + }else if(StringUtils.isEmpty(r1OW)){ + return 1; + } + }else if(r1.getSource() == null){ + return -1; + }else if(r2.getSource() == null){ + return 1; + } + return 0; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/utils/RelationSortByTargetOW.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,21 @@ +package org.mpi.openmind.repository.bo.utils; + +import java.util.Comparator; + +import org.mpi.openmind.repository.bo.Relation; + +public class RelationSortByTargetOW implements Comparator<Relation>{ + + public int compare(Relation r1, Relation r2){ + if(r1.getTarget() != null && r2.getTarget() != null){ + String r1OW = r1.getTarget().getOwnValue(); + String r2OW = r2.getTarget().getOwnValue(); + return r1OW.compareTo(r2OW); + }else if(r1.getTarget() == null){ + return -1; + }else if(r2.getTarget() == null){ + return 1; + } + return 0; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/bo/utils/Sequence.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,92 @@ +package org.mpi.openmind.repository.bo.utils; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * + * @author jurzua + */ +@Entity +@Table(name="row_sequence") +public class Sequence implements Serializable { + private static final long serialVersionUID = 1L; + + public Sequence(){} + + public Sequence(String name, Long value){ + this.lastValue = value; + this.name = name; + } + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Column(name="name") + private String name; + + @Column(name="last_value") + private Long lastValue; + + public Long generateId(){ + return this.lastValue++; + } + + public Long getLastValue() { + return lastValue; + } + + public void setLastValue(Long lastValue) { + this.lastValue = lastValue; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof Sequence)) { + return false; + } + Sequence other = (Sequence) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "org.mpi.openmind.ontology.joins.Sequence[id=" + id + "]"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,1590 @@ +package org.mpi.openmind.repository.services; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.impl.SessionFactoryImpl; +import org.mpi.openmind.configuration.ConfigurationService; +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.bo.View; +import org.mpi.openmind.repository.bo.utils.Sequence; +import org.mpi.openmind.repository.utils.ArabicNormalizerUtils; +import org.mpi.openmind.repository.utils.HibernateUtil; +import org.mpi.openmind.repository.utils.NormalizerUtils; +import org.mpi.openmind.repository.utils.OwnValueGenerator; + +/** + * + * @author jurzua + */ +public abstract class AbstractPersistenceService { + + private ConfigurationService configurationService; + + private OwnValueGenerator ownValueGenerator; + + private final static String NODE_SEQUENCE = "nodeSequence"; + + private static Logger logger = Logger + .getLogger(AbstractPersistenceService.class); + + private boolean importModus = false; + + /* + * static { logger.setLevel(Level.DEBUG); PatternLayout layout = new + * PatternLayout( "%d{ABSOLUTE} %5p %c{1}:%L - %m%n"); Appender stdout = new + * ConsoleAppender(layout, "System.out"); logger.addAppender(stdout); } + */ + + public AbstractPersistenceService() { + } + + public String generateOwnValue(Entity entity) { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + String ownValue = null; + + try { + session.getTransaction().begin(); + ownValue = this.ownValueGenerator.generateOwnValue(entity, session); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } finally { + session.getTransaction().commit(); + } + + return ownValue; + } + + /** + * Performs the actual disabling of the Hibernate second-level cache. + */ + /* + protected void disableSecondLevelCache() { + Map<Object, Cache> cacheRegionsMap = ((SessionFactoryImpl) HibernateUtil + .getSessionFactory()).getAllSecondLevelCacheRegions(); + Collection<Cache> cacheRegions = cacheRegionsMap.values(); + for (Cache cache : cacheRegions) { + cache.clear(); + } + }*/ + + public int dropAssertions() { + int row = 0; + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.beginTransaction(); + String sql = "DELETE FROM Node WHERE type = 'ABox'"; + Query query = session.createQuery(sql); + row = query.executeUpdate(); + + logger.info("Drop assertions - rows deleted= " + row); + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + return row; + } + + public int dropDefinitions() { + int row = 0; + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.beginTransaction(); + String sql = "DELETE FROM Node WHERE type = 'TBox'"; + Query query = session.createQuery(sql); + row = query.executeUpdate(); + + logger.info("Drop definitions - rows deleted= " + row); + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + return row; + } + + public void saveOrUpdateObject(Object obj) { + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + session.saveOrUpdate(obj); + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + private void saveNode0(Session session, Node node, Sequence idSequence){ + node.autoNormalize(); + node.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + + if(node.getModificationTime() == null){ + node.setModificationTime(System.currentTimeMillis()); + } + if(node.getVersion() == null){ + node.increaseVersion(); + } + + if (node.getId() == null){ + node.setId(idSequence.generateId()); + } + + if (node.getRowId() == null){ + session.save(node); + }else{ + session.merge(node); + } + + logger.info("Saved node\t" + node); + } + + public void saveNode(Node node) throws Exception { + + logger.info("Saving node\t" + node); + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + Sequence idSequence = this.getIdSequence(session); + + this.saveNode0(session, node, idSequence); + + session.save(idSequence); + + session.getTransaction().commit(); + logger.info("Node saved\t" + node); + } + + public void saveNodeList(List<Node> nodeList) throws Exception{ + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + Sequence idSequence = this.getIdSequence(session); + /* + long start = System.currentTimeMillis(); + DecimalFormat df = new DecimalFormat("#.##"); + int counter = 0; + */ + logger.info("##### Saving Node List: " + nodeList.size() + " #####"); + + for (Node node : nodeList) { + this.saveNode0(session, node, idSequence); + /* + double percent = ((double) counter / (double) nodeList.size()) * 100.0; + long diff = System.currentTimeMillis() - start; + double min = (double) diff / (double) (60 * 1000); + + if ((percent % 10) < 0.005) { + logger.debug("\n[" + df.format(percent) + " %] counter: " + + counter + " / " + nodeList.size()); + + logger.debug("Tempo: " + (double) counter / min); + } + counter++; + */ + } + + session.save(idSequence); + session.getTransaction().commit(); + } + + /** + * <p> + * This method delete the nodes and its history. + * </p> + * + * @param nodeList + */ + public void removeNode(Node node) { + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + this.removeNodesById(node.getId(), session); + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + } + + /** + * <p> + * This method delete the nodes and its history. + * </p> + * + * @param nodeList + */ + public void removeNodeList(List<Node> nodeList) { + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + logger.debug("##### Deleting Node List: " + nodeList.size() + + " #####"); + + for (Node node : nodeList) { + this.removeNodesById(node.getId(), session); + } + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + } + + /** + * Save every part of the entity but not generate new versions nor ownValue + * + * @param nodeList + */ + public void saveEntityListAsNode(List<Entity> entList) { + int entitiesSize = 0; + int nodeSize = 0; + long start = System.currentTimeMillis(); + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + Sequence idSequence = this.getIdSequence(session); + + DecimalFormat df = new DecimalFormat("#.##"); + int counter = 0; + + List<Node> nodeList = generateNodeList(entList); + logger.debug("##### Saving Node List #####"); + logger.debug("Entities: " + entList.size() + " #####"); + logger.debug("Nodes: " + nodeList.size() + " #####"); + + entitiesSize = entList.size(); + nodeSize = nodeList.size(); + + for (Node node : nodeList) { + + node.autoNormalize(); + + if (node.getId() == null) + node.setId(idSequence.generateId()); + if (node.getRowId() == null) + session.save(node); + else + session.merge(node); + + double percent = ((double) counter / (double) nodeList.size()) * 100.0; + long diff = System.currentTimeMillis() - start; + double min = (double) diff / (double) (60 * 1000); + + if ((percent % 10) < 0.005) { + logger.debug("\n[" + df.format(percent) + " %] counter: " + + counter + " / " + nodeList.size()); + + logger.debug("Tempo: " + (double) counter / min); + } + counter++; + } + + session.save(idSequence); + session.getTransaction().commit(); + + StringBuilder sb = new StringBuilder(); + sb.append("\n\t#### saveEntityListAsNode ####\n"); + sb.append("\tentitiesSize=\t" + entitiesSize + "\n"); + sb.append("\tnodeSize=\t" + nodeSize + "\n"); + sb.append("\ttime[ms]=\t" + (System.currentTimeMillis() - start) + + "\n"); + logger.info(sb.toString()); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + e.printStackTrace(); + } + } + + private List<Node> generateNodeList(List<Entity> list) { + List<Node> list0 = new ArrayList<Node>(); + for (Entity ent : list) { + list0.add(ent); + for (Attribute att : ent.getAttributes()) { + list0.add(att); + } + for (Relation rel : ent.getSourceRelations()) { + list0.add(rel); + } + for (Relation rel : ent.getTargetRelations()) { + list0.add(rel); + } + } + return list0; + } + + /** + * @param nodeList + */ + public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList) + throws Exception { + long start = System.currentTimeMillis(); + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + Sequence idSequence = this.getIdSequence(session); + DecimalFormat df = new DecimalFormat("#.##"); + int counter = 0; + + logger.debug("##### Saving Node List: " + nodeList.size() + " #####"); + + for (Entity node : nodeList) { + node.autoNormalize(); + if (node.getId() == null) + node.setId(idSequence.generateId()); + if (node.getRowId() == null) + session.save(node); + else + session.merge(node); + double percent = ((double) counter / (double) nodeList.size()) * 100.0; + long diff = System.currentTimeMillis() - start; + double min = (double) diff / (double) (60 * 1000); + + if ((percent % 10) < 0.005) { + logger.debug("\n[" + df.format(percent) + " %] counter: " + + counter + " / " + nodeList.size()); + + logger.debug("Tempo: " + (double) counter / min); + } + counter++; + } + + session.save(idSequence); + session.getTransaction().commit(); + } + + public void deleteEntityList(List<Entity> entities) { + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + for (Entity ent : entities) { + this.removePersistenceEntity(ent, session); + } + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + } + + public void saveEntityList(List<Entity> entities) throws Exception { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + for (Entity entity : entities) { + saveEntity0(session, entity); + } + + session.getTransaction().commit(); + } + + public Map<Long, Long> saveEntityListAsNew(List<Entity> entities, + Map<Long, Long> idMap) throws Exception { + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + Sequence idSequence = this.getIdSequence(session); + + for (Entity entity : entities) { + + Long oldId = entity.getId(); + entity.resetId(); + entity.resetRowId(); + this.saveEntity0(session, entity); + idMap.put(oldId, entity.getId()); + } + + session.save(idSequence); + session.getTransaction().commit(); + return idMap; + } + + /** + * <p> + * This method deletes all entities by type (TBox/ABox), it means the method + * deletes either all concepts or all assertions. + * </p> + * <p> + * The history of every deleted entity will be removed too. + * </p> + * + * @param type + * Node.TYPE_TBOX or Node.TYPE_ABOX + */ + @Deprecated + public void deleteEntities(Long id, String type, Boolean deleteHistory) { + if (!(Node.TYPE_ABOX.equals(type) || Node.TYPE_TBOX.equals(type))) { + throw new IllegalArgumentException( + "The parameter 'type' should be either: " + Node.TYPE_ABOX + + " or " + Node.TYPE_TBOX + ", but was: " + type); + } + + List<Entity> entList = this.getEntities(id, + Node.SYS_STATUS_CURRENT_VERSION, type, null); + + // loading previous versions + List<Entity> historyEntList = new ArrayList<Entity>(); + if (deleteHistory) { + for (Entity ent : entList) { + historyEntList.addAll(this.getEntities(ent.getId(), + Node.SYS_STATUS_PREVIOUS_VERSION, type, null)); + } + } + + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + for (Entity ent : entList) { + this.removePersistenceEntity(ent, session); + } + if (deleteHistory) { + for (Entity ent : historyEntList) { + this.removePersistenceEntity(ent, session); + } + } + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + } + + /** + * This deleting is made using JDBC and not hibernate. The history is too + * removed by this method. TODO test what could happen with the hibernate + * cache + * + * @param ent + * @param session + */ + private void removePersistenceEntity(Entity ent, Session session) { + for (Attribute att : ent.getAttributes()) { + // session.delete(att); + removeNodesById(att.getId(), session); + for (View view : att.getViews()) { + // session.delete(view); + removeNodesById(view.getId(), session); + } + } + for (Relation rel : ent.getSourceRelations()) { + // session.delete(rel); + removeNodesById(rel.getId(), session); + for (View view : rel.getViews()) { + // session.delete(view); + removeNodesById(view.getId(), session); + } + } + for (Relation rel : ent.getTargetRelations()) { + // session.delete(rel); + removeNodesById(rel.getId(), session); + for (View view : rel.getViews()) { + // session.delete(view); + removeNodesById(view.getId(), session); + } + } + for (View view : ent.getViews()) { + // session.delete(view); + removeNodesById(view.getId(), session); + } + // session.delete(ent); + removeNodesById(ent.getId(), session); + } + + private int removeNodesById(Long id, Session session) { + String sql = "DELETE FROM Node WHERE id = :id"; + Query query = session.createQuery(sql); + query.setLong("id", id); + return query.executeUpdate(); + } + + /** + * <p> + * Changes the system status of the entity from CURRENT_VERSION to + * PREVIOUS_VERSION. + * </p> + * + * <p> + * It means, the entity will stay in the DB, but the it will not be visible + * by the ordinary methods. + * </p> + * + * @param entity + */ + public void removeEntCurrentVersion(Long entId, String type) + throws Exception { + logger.info("Deleting entity [ID=" + entId + ", type=" + type + + "]. But keeping history in DB."); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + if (entId != null) { + //this method get the current version. Related to the relation, the time_modification is not consider, + //because it it producing problems. + List<Entity> previousEntityList = + this.getEntities(session, entId, + Node.SYS_STATUS_CURRENT_VERSION, + type, null, false); + if (previousEntityList.size() > 0) { + if (previousEntityList.size() > 1) { + System.err + .println("[PersistenceService.saveEntity] found more than one current entities!"); + } + + Entity previousEntity = previousEntityList.get(0); + logger.info("Saving previous entity: " + previousEntity); + this.savePreviousEntity(session, previousEntity); + } + } + session.getTransaction().commit(); + } + + /* + * public void removeEntity(Long entId, String type) { + * logger.info("Deleting entity [ID=" + entId + ", type=" + type + + * "]. But keeping history in DB."); Session session = + * HibernateUtil.getSessionFactory().getCurrentSession(); + * session.getTransaction().begin(); + * + * if (entId != null) { List<Entity> previousEntityList = + * this.getEntities(session, entId, Node.SYS_STATUS_CURRENT_VERSION, type, + * null); if (previousEntityList.size() > 0) { if (previousEntityList.size() + * > 1) { System.err .println( + * "[PersistenceService.saveEntity] found more than one current entities!"); + * } + * + * Entity previousEntity = previousEntityList.get(0); + * logger.info("Saving previous entity: " + previousEntity); + * this.savePreviousEntity(session, previousEntity); } } } + */ + + public void savePreviousEntity(Entity entity) throws Exception { + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + if (entity.getId() != null) { + List<Entity> previousEntityList = this.getEntities(session, + entity.getId(), Node.SYS_STATUS_CURRENT_VERSION, + entity.getType(), null, false); + if (previousEntityList.size() > 0) { + if (previousEntityList.size() > 1) { + logger.error("[PersistenceService.saveEntity] found more than one current entities!"); + } + Entity previousEntity = previousEntityList.get(0); + logger.debug("Saving previous entity: " + previousEntity); + this.savePreviousEntity(session, previousEntity); + } + } + this.saveCurrentEntity(session, entity, null); + session.getTransaction().commit(); + } + + /** + * + * @param entity + * @return + */ + public void saveEntity(Entity entity) throws Exception { + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + saveEntity0(session, entity); + + session.getTransaction().commit(); + + } + + private void saveEntity0(Session session, Entity entity) throws Exception { + if (entity.getId() != null) { + List<Entity> previousEntityList = this.getEntities(session, entity.getId(), Node.SYS_STATUS_CURRENT_VERSION, entity.getType(), null, false); + if (previousEntityList.size() > 0) { + if (previousEntityList.size() > 1) { + logger.error("[PersistenceService.saveEntity] found more than one current entities!"); + } + + Entity previousEntity = previousEntityList.get(0); + logger.info("Saving previous entity: " + previousEntity); + this.savePreviousEntity(session, previousEntity); + } + } + this.saveCurrentEntity(session, entity, null); + } + + private void saveCurrentEntity(Session session, Entity entity, + Sequence idSequence) throws Exception { + Long time = System.currentTimeMillis(); + entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + entity.resetRowId(); + entity.increaseVersion(); + entity.setObjectClass(entity.getObjectClass()); + entity.setModificationTime(time); + entity.setType(entity.getType()); + entity.setUser(entity.getUser()); + entity.autoNormalize(); + // generating of id, connecting rels, atts and views to the entity + this.prepareEntity(session, entity, idSequence); + this.saveEntity(session, entity); + } + + /** + * Here the modification time will be propagated from the entity to its + * relations an attributes. + * + * @param session + * @param entity + * @return + */ + private Entity prepareEntity(Session session, Entity entity, + Sequence idSequence) { + + if (entity.getId() == null) { + if (idSequence == null) + entity.setId(this.generateId(session)); + else + entity.setId(idSequence.generateId()); + } + + for (Attribute att : entity.getAttributes()) { + if (att.getId() == null) { + if (idSequence == null) + att.setId(this.generateId(session)); + else + att.setId(idSequence.generateId()); + } + att.setSourceId(entity.getId()); + att.setSourceModif(entity.getModificationTime()); + att.setSourceObjectClass(entity.getObjectClass()); + att.autoNormalize(); + } + + // TODO normalize the name of the relations?? + for (Relation rel : entity.getSourceRelations()) { + if (rel.getId() == null) { + if (idSequence == null) + rel.setId(this.generateId(session)); + else + rel.setId(idSequence.generateId()); + } + rel.setSourceId(entity.getId()); + rel.setSourceModif(entity.getModificationTime()); + rel.setSourceObjectClass(entity.getObjectClass()); + rel.autoNormalize(); + if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)){ + rel.setObjectClass(rel.getOwnValue()); + }else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)){ + rel.setObjectClass(Node.TYPE_TBOX); + } + + // new Attributes for relations + for (Attribute att : rel.getAttributes()) { + if (att.getId() == null) { + if (idSequence == null) + att.setId(this.generateId(session)); + else + att.setId(idSequence.generateId()); + } + att.setSourceId(rel.getId()); + att.setSourceModif(rel.getModificationTime()); + // TODO this should be analyzed + att.setSourceObjectClass(rel.getOwnValue()); + att.autoNormalize(); + } + } + + for (Relation rel : entity.getTargetRelations()) { + if (rel.getId() == null) { + if (idSequence == null) + rel.setId(this.generateId(session)); + else + rel.setId(idSequence.generateId()); + } + rel.setTargetId(entity.getId()); + rel.setTargetModif(entity.getModificationTime()); + rel.setTargetObjectClass(entity.getObjectClass()); + rel.autoNormalize(); + if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)){ + rel.setObjectClass(rel.getOwnValue()); + }else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)){ + rel.setObjectClass(Node.TYPE_TBOX); + } + // new Attributes for relations + for (Attribute att : rel.getAttributes()) { + if (att.getId() == null) { + if (idSequence == null) + att.setId(this.generateId(session)); + else + att.setId(idSequence.generateId()); + } + att.setSourceId(rel.getId()); + att.setSourceModif(rel.getModificationTime()); + // TODO this should be analyzed + att.setSourceObjectClass(rel.getOwnValue()); + att.autoNormalize(); + } + } + for (View view : entity.getViews()) { + if (view.getId() == null) { + if (idSequence == null) + view.setId(this.generateId(session)); + else + view.setId(idSequence.generateId()); + } + view.setSourceId(entity.getId()); + view.setSourceModif(entity.getModificationTime()); + view.setSourceObjectClass(entity.getObjectClass()); + view.autoNormalize(); + } + return entity; + } + + /** + * Saves an entity setting its system state as previous_version + * + * @param session + * @param entity + */ + private void savePreviousEntity(Session session, Entity entity) { + entity.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); + session.save(entity); + for (Attribute attribute : entity.getAttributes()) { + session.save(attribute); + } + for (Relation rel : entity.getSourceRelations()) { + session.save(rel); + for (Attribute att : rel.getAttributes()) { + session.save(att); + } + } + for (Relation rel : entity.getTargetRelations()) { + session.save(rel); + } + for (View view : entity.getViews()) { + session.save(view); + } + } + + private void saveEntity(Session session, Entity entity) throws Exception { + logger.info("Saving current entity: " + entity); + session.save(entity); + for (Attribute attribute : entity.getAttributes()) { + session.save(attribute); + } + for (Relation rel : entity.getSourceRelations()) { + rel.setSource(entity); + if (rel.getSourceId() == null || rel.getTargetId() == null) { + throw new IllegalStateException( + "Either the sourceId or the targetId was not initialized to the source relation: " + + rel.getOwnValue()); + } + session.save(rel); + for (Attribute att : rel.getAttributes()) { + session.save(att); + } + } + for (Relation rel : entity.getTargetRelations()) { + rel.setTarget(entity); + if (rel.getSourceId() == null || rel.getTargetId() == null) { + throw new IllegalStateException( + "Either the sourceId or the targetId was not initialized to the source relation: " + + rel.getOwnValue()); + } + session.save(rel); + } + + // Call of ownValue Generator. + // TODO: the method should be used always? what would happen if the + // ownValue returns null? + if (!isImportModus()) { + String ownValue = this.getOwnValueGenerator().generateOwnValue( + entity, session); + if (StringUtils.isNotEmpty(ownValue)) { + entity.setOwnValue(ownValue); + entity.autoNormalize(); + session.save(entity); + } + } + } + + public OwnValueGenerator getOwnValueGenerator() { + return ownValueGenerator; + } + + public void setOwnValueGenerator(OwnValueGenerator ownValueGenerator) { + this.ownValueGenerator = ownValueGenerator; + } + + /** + * <p> + * Returns entities with their corresponded contain. + * </p> + * + * @param session + * @param id + * @param systemStatus + * @param type + * @param ownValue + * @return + */ + private List<Entity> getEntities(Session session, Long id, + String systemStatus, String type, String ownValue, boolean considerTimeModif) { + + if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) || systemStatus + .equals(Node.SYS_STATUS_CURRENT_VERSION))) { + throw new IllegalArgumentException("Invalid input systemStatus: " + + systemStatus); + } + + if (StringUtils.isNotEmpty(type) + && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { + throw new IllegalArgumentException("Invalid input type: " + type); + } + + List<Entity> entities; + + String hqlEntities = "from Entity where systemStatus = :systemStatus"; + + if (StringUtils.isNotEmpty(type)) { + hqlEntities += " AND type = :type"; + } + + if (id != null) { + hqlEntities += " AND id = :id"; + } + + if (StringUtils.isNotEmpty(ownValue)) { + hqlEntities += " AND ownValue = :ownValue"; + } + + Query queryEntities = session.createQuery(hqlEntities); + queryEntities.setString("systemStatus", systemStatus); + + if (StringUtils.isNotEmpty(type)) { + queryEntities.setString("type", type); + } + + if (StringUtils.isNotEmpty(ownValue)) { + queryEntities.setString("ownValue", ownValue); + } + + if (id != null) { + queryEntities.setLong("id", id); + } + entities = queryEntities.list(); + + for (Entity entity : entities) { + entity.setLightweight(true); + entity = this.getEntityContent(session, entity, considerTimeModif); + } + + return entities; + } + + /** + * <p> + * Returns entities with their corresponded contain. + * </p> + * + * @param id + * @param systemStatus + * @param type + * @param ownValue + * @return + */ + public List<Entity> getEntities(Long id, String systemStatus, String type, + String ownValue) { + logger.debug("GET ENTITIES Entities [id=" + id + ", type=" + type + + ", ownValue=" + ownValue + "]"); + + if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) || systemStatus + .equals(Node.SYS_STATUS_CURRENT_VERSION))) { + throw new IllegalArgumentException("Invalid input systemStatus: " + + systemStatus); + } + + if (StringUtils.isNotEmpty(type) + && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { + throw new IllegalArgumentException("Invalid input type: " + type); + } + + List<Entity> entities = null; + Session session = null; + try { + long start = System.currentTimeMillis(); + session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + entities = this.getEntities(session, id, systemStatus, type, + ownValue, true); + + long dif = System.currentTimeMillis() - start; + + String s = "Found=\n"; + for (Entity e : entities) { + s += e.toString() + "\n"; + } + s += "time used= " + dif + "[ms]\n\n"; + logger.debug(s); + + // session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } finally { + session.getTransaction().commit(); + } + return entities; + } + + private String whereUpdate(String where) { + if (StringUtils.isEmpty(where)) + where = " where "; + else + where += " AND "; + return where; + } + + public List<Node> getNodes(Session session, Long id, String systemStatus, + Long sourceId, Long srcModif, Long targetId, Long tarModif) { + List<Node> nodes = null; + + String from = "from Node "; + String where = ""; + + if (id != null) { + where = this.whereUpdate(where); + where += " id = :id "; + + } + if (StringUtils.isNotEmpty(systemStatus)) { + where = this.whereUpdate(where); + where += " systemStatus = :systemStatus "; + } + + // source's attributes + if (sourceId != null) { + where = this.whereUpdate(where); + where += " sourceId = :sourceId "; + } + if (srcModif != null) { + where = this.whereUpdate(where); + where += " sourceModif = :sourceModif "; + } + + if (targetId != null) { + where = this.whereUpdate(where); + where += " targetId = :targetId "; + } + if (tarModif != null) { + where = this.whereUpdate(where); + where += " targetModif = :targetModif "; + } + + String hql = from + where; + Query query = session.createQuery(hql); + + if (id != null) + query.setLong("id", id); + if (StringUtils.isNotEmpty(systemStatus)) + query.setString("systemStatus", systemStatus); + + if (sourceId != null) + query.setLong("sourceId", sourceId); + if (srcModif != null) + query.setLong("sourceModif", srcModif); + + if (targetId != null) + query.setLong("targetId", targetId); + if (tarModif != null) + query.setLong("targetModif", tarModif); + + nodes = query.list(); + + return nodes; + } + + private Entity getEntityContent(Session session, Entity entity, boolean considerTimeModif) { + if (entity != null && entity.isLightweight()) { + entity.setLightweight(false); + entity.setAttributes(new ArrayList<Attribute>()); + entity.setSourceRelations(new ArrayList<Relation>()); + entity.setTargetRelations(new ArrayList<Relation>()); + + // getting Attributes and SourceRelations + List<Node> nodes = null; + + if(considerTimeModif){ + nodes = this.getNodes(session, null, + entity.getSystemStatus(), entity.getId(), + entity.getModificationTime(), null, null); + }else{ + nodes = this.getNodes(session, null, + entity.getSystemStatus(), entity.getId(), + null, null, null); + } + + + for (Node node : nodes) { + if (node instanceof Attribute) { + entity.addAttribute((Attribute) node); + } else if (node instanceof Relation) { + Relation rel = (Relation) node; + // new attr for relations + List<Node> attrs = this.getNodes(session, null, + rel.getSystemStatus(), rel.getId(), + rel.getModificationTime(), null, null); + for (Node attNode : attrs) { + if (attNode instanceof Attribute) { + rel.addAttribute((Attribute) attNode); + } + } + if(considerTimeModif){ + entity.addSourceRelation(rel); + }else{ + entity.getSourceRelations().add(rel); + } + } else if (node instanceof View) { + entity.getViews().add((View) node); + } else { + throw new IllegalArgumentException("Invalid node found: " + + node); + } + } + + // getting TargetRelations + List<Node> tarRels = null; + + if(considerTimeModif){ + tarRels = this.getNodes(session, null, + entity.getSystemStatus(), null, null, entity.getId(), + entity.getModificationTime()); + }else{ + tarRels = this.getNodes(session, null, + entity.getSystemStatus(), null, null, entity.getId(), + null); + } + + for (Node node : tarRels) { + if (node instanceof Relation) { + Relation rel = (Relation) node; + // new attr for relations + List<Node> attrs = this.getNodes(session, null, + rel.getSystemStatus(), rel.getId(), + rel.getModificationTime(), null, null); + for (Node attNode : attrs) { + if (attNode instanceof Attribute) { + rel.addAttribute((Attribute) attNode); + } + } + if(considerTimeModif){ + entity.addTargetRelation(rel); + }else{ + entity.getTargetRelations().add(rel); + } + } else { + throw new IllegalArgumentException("Invalid node found: " + + node); + } + } + } + return entity; + } + + public Entity getEntityContent(Entity entity) { + if (entity != null && entity.isLightweight()) { + try { + logger.debug("GET ENTITY CONTENT [objClass=" + + entity.getObjectClass() + ", id=" + entity.getId() + + ", ownValue=" + entity.getOwnValue() + "]"); + long start = System.currentTimeMillis(); + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + entity = this.getEntityContent(session, entity, true); + long diff = System.currentTimeMillis() - start; + logger.debug("Time to get content=" + diff + "(s)"); + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + } + return entity; + } + + public List<String> getObjecClassSuggestion(String objectClass, + Class nodeClass, int maxResults) { + objectClass += "%"; + try { + + List<String> suggestions = new ArrayList<String>(); + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + String hql = "from "; + if (nodeClass.equals(Entity.class)) { + hql += "Entity "; + } + if (nodeClass.equals(Relation.class)) { + hql += "Relation "; + } + if (nodeClass.equals(Attribute.class)) { + hql += "Attribute "; + } + if (nodeClass.equals(Node.class)) { + hql += "Node "; + } + if (nodeClass.equals(View.class)) { + hql += "View "; + } + hql += " where objectClass like :objectClass " + + "group by objectClass"; + + Query query = session.createQuery(hql); + query.setString("objectClass", objectClass); + query.setMaxResults(maxResults); + List<Node> list = query.list(); + for (Node node : list) { + suggestions.add(node.getObjectClass()); + } + return suggestions; + + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + return null; + } + + public List<String> getOwnValueSuggestion(String ownValue, Class nodeClass, + int maxResults) { + ownValue += "%"; + try { + + List<String> suggestions = new ArrayList<String>(); + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + String hql = "from "; + if (nodeClass.equals(Entity.class)) { + hql += "Entity "; + } + if (nodeClass.equals(Relation.class)) { + hql += "Relation "; + } + if (nodeClass.equals(Attribute.class)) { + hql += "Attribute "; + } + if (nodeClass.equals(Node.class)) { + hql += "Node "; + } + if (nodeClass.equals(View.class)) { + hql += "View "; + } + hql += " where ownValue like :ownValue " + "group by ownValue"; + + Query query = session.createQuery(hql); + query.setString("ownValue", ownValue); + query.setMaxResults(maxResults); + List<Node> list = query.list(); + for (Node node : list) { + suggestions.add(node.getOwnValue()); + } + return suggestions; + + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + return null; + } + + public Entity loadEntitiesForTargetRelation(Entity entity) { + try { + // where parent_id = :parent_id AND type = :type + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + for (Relation rel : entity.getTargetRelations()) { + List<Entity> entities = this.getEntities(session, + rel.getSourceId(), Node.SYS_STATUS_CURRENT_VERSION, + null, null, true); + if (entities.size() > 0) { + Entity source = entities.get(0); + rel.setSource(source); + } + } + + session.getTransaction().commit(); + return entity; + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + return entity; + } + + protected List<Entity> getLightweightEntities(Session session, + String systemStatus, Long id, String type, String objectClass, + String ownValue, boolean ownValueSubString, int maxResult) { + + String hqlEntities = "from Entity where "; + if (StringUtils.isNotEmpty(ownValue)) { + if (ownValueSubString) + hqlEntities += "ownValue like :ownValue AND "; + else + hqlEntities += "ownValue = :ownValue AND "; + } + if (id != null) { + hqlEntities += "id = :id AND "; + } + if (StringUtils.isNotEmpty(objectClass)) { + hqlEntities += "objectClass = :objectClass AND "; + } + if (StringUtils.isNotEmpty(type)) { + hqlEntities += "type = :type AND "; + } + + hqlEntities += "systemStatus = :systemStatus "; + + // it will be now sorted by openmind + // + "order by ownValue"; + + Query queryEntities = session.createQuery(hqlEntities); + queryEntities.setString("systemStatus", systemStatus); + if (StringUtils.isNotEmpty(ownValue)) { + if (ownValueSubString) { + queryEntities.setString("ownValue", "%" + ownValue + "%"); + logger.info("ownValue=%" + ownValue + "%"); + } else { + queryEntities.setString("ownValue", ownValue); + logger.info("ownValue=" + ownValue); + } + } + if (maxResult > 0) { + queryEntities.setMaxResults(maxResult); + } + + if (StringUtils.isNotEmpty(type)) { + queryEntities.setString("type", type); + } + if (id != null) { + queryEntities.setLong("id", id); + } + if (StringUtils.isNotEmpty(objectClass)) { + queryEntities.setString("objectClass", objectClass); + } + List<Entity> entities = queryEntities.list(); + + for(Entity ent : entities){ + ent.setLightweight(true); + } + + return entities; + } + + /** + * Own value is searched as substring + * + * @param systemStatus + * @param id + * @param type + * @param objectClass + * @param ownValue + * @param maxResult + * @return + */ + public List<Entity> getLightweightEntities(String systemStatus, Long id, + String type, String objectClass, String ownValue, + boolean ownValueSubString, int maxResult) { + logger.debug("GET LW ENTITIES [type=" + type + " id=" + id + + ", objectClass=" + objectClass + ", ownValue=" + ownValue + + "]"); + + List<Entity> entities = new ArrayList<Entity>(); + if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) || systemStatus + .equals(Node.SYS_STATUS_CURRENT_VERSION))) { + throw new IllegalArgumentException("Invalid input systemStatus: " + + systemStatus); + } + + if (StringUtils.isNotEmpty(type) + && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { + throw new IllegalArgumentException("Invalid input type: " + type); + } + + try { + // where parent_id = :parent_id AND type = :type + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + entities = this.getLightweightEntities(session, systemStatus, id, + type, objectClass, ownValue, ownValueSubString, maxResult); + + + session.getTransaction().commit(); + return entities; + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return entities; + } + + private Sequence getIdSequence(Session session) { + Sequence sequence = null; + String hqlJoin = "from Sequence where name = :name"; + Query query = session.createQuery(hqlJoin); + query.setString("name", NODE_SEQUENCE); + List<Sequence> sequences = query.list(); + if (sequences.size() > 0) + sequence = sequences.get(0); + if (sequence == null) { + sequence = new Sequence(NODE_SEQUENCE, new Long(0)); + } + return sequence; + } + + protected Long generateId(Session session) { + Long id = null; + Sequence sequence = null; + String hqlJoin = "from Sequence where name = :name"; + Query query = session.createQuery(hqlJoin); + query.setString("name", NODE_SEQUENCE); + List<Sequence> sequences = query.list(); + if (sequences.size() > 0) { + sequence = sequences.get(0); + } else { + sequence = new Sequence(NODE_SEQUENCE, new Long(0)); + } + id = sequence.generateId(); + session.save(sequence); + return id; + } + + public ConfigurationService getConfigurationService() { + return configurationService; + } + + public void setConfigurationService( + ConfigurationService configurationService) { + this.configurationService = configurationService; + } + + public boolean isImportModus() { + return importModus; + } + + public void setImportModus(boolean importModus) { + this.importModus = importModus; + } + + // ################################################################# + // ################################################################# + // ################################################################# + + public List<Relation> getDefRelations() { + List<Relation> list = new ArrayList<Relation>(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + try { + session.getTransaction().begin(); + + String hql = "from Relation where systemStatus = :systemStatus and type = :type"; + Query query = session.createQuery(hql); + + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("type", Node.TYPE_TBOX); + + list = query.list(); + /* + * for(Relation rel : list){ List<Node> attrs = + * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), + * rel.getModificationTime(), null, null); for(Node attNode : + * attrs){ if(attNode instanceof Attribute){ + * rel.addAttribute((Attribute)attNode); } } } + */ + + } catch (Exception e) { + logger.error(e.getMessage(), e); + } finally { + session.getTransaction().commit(); + } + return list; + } + + public List<Attribute> getDefAttributes() { + List<Attribute> list = new ArrayList<Attribute>(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + try { + session.getTransaction().begin(); + + String hql = "from Attribute where systemStatus = :systemStatus and type = :type"; + Query query = session.createQuery(hql); + + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("type", Node.TYPE_TBOX); + + list = query.list(); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + } finally { + session.getTransaction().commit(); + } + return list; + } + + public List<Entity> getLWDefinitions() throws Exception { + List<Entity> list = new ArrayList<Entity>(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + + session.getTransaction().begin(); + + String hql = "from Entity where systemStatus = :systemStatus and type = :type"; + Query query = session.createQuery(hql); + + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("type", Node.TYPE_TBOX); + + list = query.list(); + for (Entity def : list) { + def.setLightweight(true); + } + session.getTransaction().commit(); + + return list; + } + + // ################################################################# + // ################################################################# + // ################################################################# + + public Long getEntityCount(String objectClass) { + Long count = null; + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "select count(*) from Entity where "; + + if (StringUtils.isNotEmpty(objectClass)) { + hql += "objectClass = :objectClass AND "; + } else { + hql += "objectClass != :objectClass AND "; + } + hql += "systemStatus = :systemStatus "; + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + if (StringUtils.isNotEmpty(objectClass)) { + query.setString("objectClass", objectClass); + } else { + query.setString("objectClass", Node.TYPE_TBOX); + } + count = (Long) query.uniqueResult(); + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return count; + } + + /** + * + * @param objectClass + * if it is null, it will be returned all entities (no + * definition). To get the definitions objectClass should be: + * Node.TYPE_TBOX + * @param startRecord + * @param endRecord + * @return + */ + public List<Entity> getEntityPage(String objectClass, + final int startRecord, final int endRecord) { + + List<Entity> entities = new ArrayList<Entity>(); + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Entity where "; + + if (StringUtils.isNotEmpty(objectClass)) { + hql += "objectClass = :objectClass AND "; + } else { + hql += "objectClass != :objectClass AND "; + } + + hql += "systemStatus = :systemStatus order by ownValue"; + + Query query = session.createQuery(hql); + query.setFirstResult(startRecord); + query.setMaxResults(endRecord); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + if (StringUtils.isNotEmpty(objectClass)) { + query.setString("objectClass", objectClass); + } else { + query.setString("objectClass", Node.TYPE_TBOX); + } + entities = query.list(); + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + // e.printStackTrace(); + } + return entities; + } + + public static void main(String[] args) { + /* + * ServiceRegistry sr = new ServiceRegistry(); Long count = + * sr.getPersistenceService().getEntityCount("CODEX"); + * logger.info("count codex " + count); + * + * List<Entity> list = sr.getPersistenceService().getEntityPage("CODEX", + * count.intValue() - 100, count.intValue() - 50); for(Entity e : list){ + * logger.info(e.toString()); } + */ + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/PersistenceService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,2239 @@ +package org.mpi.openmind.repository.services; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.hibernate.Query; +import org.hibernate.Session; +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.bo.ViewerAttribute; +import org.mpi.openmind.repository.bo.ViewerPage; +import org.mpi.openmind.repository.bo.utils.RelationSortBySourceOW; +import org.mpi.openmind.repository.bo.utils.RelationSortByTargetOW; +import org.mpi.openmind.repository.services.utils.AttributeFilter; +import org.mpi.openmind.repository.services.utils.RelationFilter; +import org.mpi.openmind.repository.services.utils.RelationResultEntry; +import org.mpi.openmind.repository.utils.HibernateUtil; +import org.mpi.openmind.repository.utils.JDBCUtils; + +/** + * + * @author jurzua + */ +public class PersistenceService extends AbstractPersistenceService { + + private static Logger logger = Logger.getLogger(PersistenceService.class); + public static String GROUP_BY_SOURCE = "group_by_source"; + public static String GROUP_BY_TARGET = "group_by_target"; + public static String GROUP_BY_RELATION = "group_by_relation"; + + public Long getSourceRelationsCount(Entity entity, String relationName, + String tarObjClass) { + Long count = null; + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "select count(*) from Relation rel " + "where " + + "rel.sourceId = :id AND " + + "rel.sourceModif = :modif AND " + + "rel.ownValue = :relationName AND " + + "rel.systemStatus = :systemStatus "; + + if (StringUtils.isNotEmpty(tarObjClass)) { + hql += " AND rel.targetObjectClass = :tarObjClass "; + } + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entity.getId()); + query.setLong("modif", entity.getModificationTime()); + query.setString("relationName", relationName); + if (StringUtils.isNotEmpty(tarObjClass)) { + query.setString("tarObjClass", tarObjClass); + } + + count = (Long) query.uniqueResult(); + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return count; + } + + public List<Relation> getRelationBySrcOCFromDB(String srcOC, int maxResult) + throws Exception { + List<Relation> relations = new ArrayList<Relation>(); + long start = System.currentTimeMillis(); + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Relation rel " + "where " + + "rel.systemStatus = :systemStatus AND " + + "rel.sourceObjectClass = :srcOC "; + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("srcOC", srcOC); + + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Relation> list = query.list(); + + // TODO loading attributes + /* + * for(Relation rel : relations){ List<Node> attrs = + * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), + * rel.getModificationTime(), null, null); for(Node attNode : attrs){ + * if(attNode instanceof Attribute){ + * rel.addAttribute((Attribute)attNode); } } } + */ + + session.getTransaction().commit(); + + logger.info("getRelationBySrcOC [" + srcOC + "] - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return relations; + } + + public List<Relation> getRelationByTarOCFromDB(String tarOC, int maxResult) { + List<Relation> relations = new ArrayList<Relation>(); + long start = System.currentTimeMillis(); + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Relation rel " + "where " + + "rel.systemStatus = :systemStatus AND " + + "rel.targetObjectClass = :tarOC "; + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("tarOC", tarOC); + + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Relation> list = query.list(); + + // TODO loading attributes + /* + * for(Relation rel : relations){ List<Node> attrs = + * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), + * rel.getModificationTime(), null, null); for(Node attNode : attrs){ + * if(attNode instanceof Attribute){ + * rel.addAttribute((Attribute)attNode); } } } + */ + + session.getTransaction().commit(); + + logger.info("getRelationByTarOC [" + tarOC + "] - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return relations; + } + + /** + * Returns relation by name, where the parameter name is not substring. + * + * @param name + * @param maxResult + * @return + */ + public List<Relation> getRelationByNameDB(String name, int maxResult) + throws Exception { + List<Relation> relations = new ArrayList<Relation>(); + long start = System.currentTimeMillis(); + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Relation rel " + "where " + + "rel.systemStatus = :systemStatus " + + " AND rel.ownValue = :name "; + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("name", name); + + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + relations = query.list(); + + // loading attributes + /* + * for(Relation rel : relations){ List<Node> attrs = + * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(), + * rel.getModificationTime(), null, null); for(Node attNode : attrs){ + * if(attNode instanceof Attribute){ + * rel.addAttribute((Attribute)attNode); } } } + */ + + session.getTransaction().commit(); + + logger.debug("getRelationByName - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return relations; + } + + public List<Relation> getSourceRelationsJDBCFromDB(Entity entity, + String relationName, String tarObjClass, int maxResult, + boolean includeEntities) { + + List<Relation> relations = null; + long start = System.currentTimeMillis(); + try { + + Connection conn = JDBCUtils.getConn(); + + String sql = "select * from node " + "where " + "source_id = '" + + entity.getId() + "' AND " + "source_modif = '" + + entity.getModificationTime() + "' AND " + + "system_status = '" + Node.SYS_STATUS_CURRENT_VERSION + + "' "; + + if (StringUtils.isNotEmpty(relationName)) { + sql += " AND own_value = '" + relationName + "' "; + } + if (StringUtils.isNotEmpty(tarObjClass)) { + sql += " AND target_obj_class = '" + tarObjClass + "' "; + } + + // logger.info(sql); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql); + relations = JDBCUtils.rs2Rels(rs); + + conn.close(); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + e.printStackTrace(); + } + + logger.debug("getSourceRelations JDBC - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return relations; + } + + public List<Relation> getSourceRelationsFromDB(Entity entity, + String relationName, String tarObjClass, int maxResult, + boolean includeEntities) throws Exception { + + List<Relation> relations = new ArrayList<Relation>(); + long start = System.currentTimeMillis(); + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Relation rel " + "where " + + "rel.sourceId = :id AND " + + "rel.sourceModif = :modif AND " // TODO: previous versions are same without considering the modif + + "rel.systemStatus = :systemStatus "; + + if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ + hql += " AND rel.sourceModif = :modif "; + } + if (StringUtils.isNotEmpty(relationName)) { + hql += " AND rel.ownValue = :relationName "; + } + if (StringUtils.isNotEmpty(tarObjClass)) { + hql += " AND rel.targetObjectClass = :tarObjClass "; + } + + Query query = session.createQuery(hql); + query.setLong("id", entity.getId()); + query.setLong("modif", entity.getModificationTime()); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + + + //if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ + // query.setLong("modif", entity.getModificationTime()); + //} + if (StringUtils.isNotEmpty(relationName)) { + query.setString("relationName", relationName); + } + if (StringUtils.isNotEmpty(tarObjClass)) { + query.setString("tarObjClass", tarObjClass); + } + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Relation> list = query.list(); + if (includeEntities) { + for (Relation rel : list) { + List<Entity> tarList = getLightweightEntities(session, + Node.SYS_STATUS_CURRENT_VERSION, rel.getTargetId(), + null, null, null, false, 1); + if (tarList.size() > 0) { + rel.setSource(entity); + rel.setTarget(tarList.get(0)); + relations.add(rel); + } + } + + Collections.sort(relations, new RelationSortByTargetOW()); + } else { + relations = list; + } + + // loading attributes + for (Relation rel : relations) { + List<Node> attrs = this.getNodes(session, null, + rel.getSystemStatus(), rel.getId(), + rel.getModificationTime(), null, null); + for (Node attNode : attrs) { + if (attNode instanceof Attribute) { + rel.addAttribute((Attribute) attNode); + } + } + } + + session.getTransaction().commit(); + + logger.debug("getSourceRelations - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return relations; + } + + /** + * <p> + * Returns the relations directly for the given entity from the DB, which + * conforms the given input. + * </p> + * <p> + * Only the variable tarObjClass can be null. + * </p> + * <p> + * If its expected all existing element from the DB, the variable maxResult + * should be -1, otherwise this value limits the returned result-set. + * </p> + * + * @param entity + * @param relationName + * @param tarObjClass + * @param maxResult + * @return + */ + public List<Relation> getSourceRelationsError(Entity entity, + String relationName, String tarObjClass, int maxResult) + throws Exception { + + List<Relation> relations = new ArrayList<Relation>(); + + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Relation rel, Entity tar " + "where " + + "rel.sourceId = :id AND " + "rel.sourceModif = :mod AND " + + "rel.ownValue = :relationName AND " + + "rel.systemStatus = :systemStatus AND " + + "tar.systemStatus = :systemStatus AND " + + "tar.id = rel.targetId "; + + if (StringUtils.isNotEmpty(tarObjClass)) { + hql += " AND rel.targetObjectClass = :tarObjClass "; + } + + hql += " order by tar.ownValue "; + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entity.getId()); + query.setLong("mod", entity.getModificationTime()); + query.setString("relationName", relationName); + if (StringUtils.isNotEmpty(tarObjClass)) { + query.setString("tarObjClass", tarObjClass); + } + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Object> list = query.list(); + + for (Object o : list) { + Object[] array = (Object[]) o; + if (array.length > 0) { + Relation rel = (Relation) array[0]; + Entity tar = (Entity) array[1]; + tar.setLightweight(true); + rel.setSource(entity); + rel.setTarget(tar); + relations.add(rel); + } + } + session.getTransaction().commit(); + + return relations; + } + + public Long getTargetRelationsCount(Entity entity, String relationName, + String srcObjClass) { + Long count = null; + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "select count(*) from Relation rel " + "where " + + "rel.targetId = :id AND " + + "rel.targetModif = :modif AND " + + "rel.ownValue = :relationName AND " + + "rel.systemStatus = :systemStatus "; + + if (StringUtils.isNotEmpty(srcObjClass)) { + hql += " AND rel.sourceObjectClass = :srcObjClass "; + } + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entity.getId()); + query.setLong("modif", entity.getModificationTime()); + if (StringUtils.isNotEmpty(relationName)) { + query.setString("relationName", relationName); + } + if (StringUtils.isNotEmpty(srcObjClass)) { + query.setString("srcObjClass", srcObjClass); + } + + count = (Long) query.uniqueResult(); + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return count; + } + + public List<Relation> getTargetRelationsJDBCFromDB(Entity entity, + String relationName, String srcObjClass, int maxResult, + boolean includeEntities) { + + List<Relation> relations = null; + long start = System.currentTimeMillis(); + try { + + Connection conn = JDBCUtils.getConn(); + + String sql = "select * from node " + "where " + "target_id = '" + + entity.getId() + "' AND " + "target_modif = '" + + entity.getModificationTime() + "' AND " + + "system_status = '" + Node.SYS_STATUS_CURRENT_VERSION + + "' "; + + if (StringUtils.isNotEmpty(relationName)) { + sql += " AND own_value = '" + relationName + "' "; + } + if (StringUtils.isNotEmpty(srcObjClass)) { + sql += " AND source_obj_class = '" + srcObjClass + "' "; + } + + // logger.info(sql); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql); + relations = JDBCUtils.rs2Rels(rs); + + conn.close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + e.printStackTrace(); + } + + logger.debug("GetTargetRelations JDBC - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return relations; + } + + public List<Relation> getTargetRelationsFromDB(Entity entity, + String relationName, String srcObjClass, int maxResult, + boolean includeEntities) { + List<Relation> relations = new ArrayList<Relation>(); + long start = System.currentTimeMillis(); + try { + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Relation rel " + "where " + + "rel.targetId = :id AND " + + "rel.targetModif = :modif AND " + + "rel.systemStatus = :systemStatus "; + + if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ + hql += " AND rel.targetModif = :modif "; + } + if (StringUtils.isNotEmpty(relationName)) { + hql += " AND rel.ownValue = :relationName "; + } + if (StringUtils.isNotEmpty(srcObjClass)) { + hql += " AND rel.sourceObjectClass = :srcObjClass "; + } + + Query query = session.createQuery(hql); + query.setLong("id", entity.getId()); + query.setLong("modif", entity.getModificationTime()); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + + //if(StringUtils.equals(entity.getSystemStatus(), Node.SYS_STATUS_PREVIOUS_VERSION)){ + // query.setLong("modif", entity.getModificationTime()); + //} + if (StringUtils.isNotEmpty(relationName)) { + query.setString("relationName", relationName); + } + if (StringUtils.isNotEmpty(srcObjClass)) { + query.setString("srcObjClass", srcObjClass); + } + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Relation> list = query.list(); + if (includeEntities) { + for (Relation rel : list) { + List<Entity> srcList = getLightweightEntities(session, + Node.SYS_STATUS_CURRENT_VERSION, rel.getSourceId(), + null, null, null, false, 1); + if (srcList.size() > 0) { + rel.setSource(srcList.get(0)); + rel.setTarget(entity); + relations.add(rel); + } + } + + Collections.sort(relations, new RelationSortBySourceOW()); + } else { + relations = list; + } + // loading attributes + for (Relation rel : relations) { + List<Node> attrs = this.getNodes(session, null, + rel.getSystemStatus(), rel.getId(), + rel.getModificationTime(), null, null); + for (Node attNode : attrs) { + if (attNode instanceof Attribute) { + rel.addAttribute((Attribute) attNode); + } + } + } + + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + e.printStackTrace(); + } + long end = System.currentTimeMillis(); + if (end - start > 500) { + logger.debug("GetTargetRelations - execution time[ms]: " + + (end - start)); + } + return relations; + } + + /** + * <p> + * Returns the relations directly for the given entity from the DB, which + * conforms the given input. + * </p> + * <p> + * Only the variable srcObjClass can be null. + * </p> + * <p> + * If every existing element from the DB is expected, the variable maxResult + * should be -1, otherwise this value limits the returned result-set. + * </p> + * + * @param entity + * @param relationName + * @param tarObjClass + * @param maxResult + * @return + */ + public List<Relation> getTargetRelationsError(Entity entity, + String relationName, String srcObjClass, int maxResult) { + List<Relation> relations = new ArrayList<Relation>(); + try { + logger.debug("### get Target Relations ###"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Relation rel, Entity src " + "where " + + "rel.targetId = :id AND " + "rel.targetModif = :mod AND " + + "rel.ownValue = :relationName AND " + + "rel.systemStatus = :systemStatus AND " + + "src.systemStatus = :systemStatus AND " + + "src.id = rel.sourceId "; + + if (StringUtils.isNotEmpty(srcObjClass)) { + hql += " AND rel.sourceObjectClass = :srcObjClass "; + } + + hql += " order by src.ownValue "; + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entity.getId()); + query.setLong("mod", entity.getModificationTime()); + query.setString("relationName", relationName); + if (StringUtils.isNotEmpty(srcObjClass)) { + query.setString("srcObjClass", srcObjClass); + } + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Object> list = query.list(); + + for (Object o : list) { + Object[] array = (Object[]) o; + if (array.length > 0) { + Relation rel = (Relation) array[0]; + Entity src = (Entity) array[1]; + src.setLightweight(true); + rel.setSource(src); + rel.setTarget(entity); + relations.add(rel); + } + } + + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return relations; + } + + /** + * <p> + * Returns the entities of class "tarObjClass" directly from the DB, which + * are connected to the given entity by a relation of name "relationName". + * </p> + * <p> + * Only the variable tarObjClass can be null. + * </p> + * <p> + * If its expected all existing element from the DB, the variable maxResult + * should be -1, otherwise this value limits the returned result-set. + * </p> + * + * @param entity + * @param relationName + * @param tarObjClass + * @param maxResult + * @return + */ + public List<Entity> getTargetsForSourceRelation(Entity entity, + String relationName, String tarObjClass, int maxResult) { + List<Entity> entities = new ArrayList<Entity>(); + try { + logger.debug("### get Targets For Source Relation ###"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from Relation rel, Entity tar " + "where " + + "rel.sourceId = :id AND " + "rel.sourceModif = :mod AND " + + "rel.ownValue = :relationName AND " + + "rel.systemStatus = :systemStatus AND " + + "tar.systemStatus = :systemStatus AND " + + "tar.id = rel.targetId "; + + if (StringUtils.isNotEmpty(tarObjClass)) { + hql += " AND rel.targetObjectClass = :tarObjClass "; + } + + hql += " order by tar.ownValue "; + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entity.getId()); + query.setLong("mod", entity.getModificationTime()); + query.setString("relationName", relationName); + if (StringUtils.isNotEmpty(tarObjClass)) { + query.setString("tarObjClass", tarObjClass); + } + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Object> list = query.list(); + + for (Object o : list) { + Object[] array = (Object[]) o; + if (array.length > 0) { + Entity ent = (Entity) array[1]; + ent.setLightweight(true); + entities.add(ent); + } + } + + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.debug("Target For SourceRelation - Time execution: " + diff + / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + + "[min] " + diff / 1000 + "[sec]"); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return entities; + } + + /** + * <p> + * Returns the entities of class "srcObjClass" directly from the DB, which + * are connected to the given entity by a relation of name "relationName". + * </p> + * <p> + * Only the variable srcObjClass can be null. + * </p> + * <p> + * If its expected all existing element from the DB, the variable maxResult + * should be -1, otherwise this value limits the returned result-set. + * </p> + * + * @param entity + * @param relationName + * @param tarObjClass + * @param maxResult + * @return + */ + public List<Entity> getSourcesForTargetRelation(Entity entity, + String relationName, String srcObjClass, int maxResult) { + List<Entity> entities = new ArrayList<Entity>(); + try { + logger.debug("### get Sources For TargetRelation ###"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from Relation rel, Entity src " + "where " + + "rel.targetId = :id AND " + "rel.targetModif = :mod AND " + + "rel.ownValue = :relationName AND " + + "rel.systemStatus = :systemStatus AND " + + "src.systemStatus = :systemStatus AND " + + "src.id = rel.sourceId "; + + if (StringUtils.isNotEmpty(srcObjClass)) { + hql += " AND rel.sourceObjectClass = :srcObjClass "; + } + + hql += " order by src.ownValue "; + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entity.getId()); + query.setLong("mod", entity.getModificationTime()); + query.setString("relationName", relationName); + if (StringUtils.isNotEmpty(srcObjClass)) { + query.setString("srcObjClass", srcObjClass); + } + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Object> list = query.list(); + + for (Object o : list) { + Object[] array = (Object[]) o; + if (array.length > 0) { + Entity ent = (Entity) array[1]; + ent.setLightweight(true); + entities.add(ent); + } + } + + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.debug("Sources For TargetRelation - Time execution: " + diff + / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + + "[min] " + diff / 1000 + "[sec]"); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return entities; + } + + public ViewerAttribute saveViewerAttribute(ViewerAttribute att){ + try { + logger.debug("### saveViewerAttribute ###"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + session.saveOrUpdate(att); + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return att; + } + + public ViewerPage saveViewerPage(ViewerPage page){ + try { + logger.debug("### saveViewerPage ###"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + session.saveOrUpdate(page); + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return page; + } + + public int removeViewerAttribute(Long id){ + int rows = 0; + try { + logger.debug("$$$ removeViewerAttribute [ID=" + id + "]"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.beginTransaction(); + String sql = "DELETE FROM ViewerAttribute WHERE id = :id"; + Query query = session.createQuery(sql); + query.setLong("id", id); + + rows = query.executeUpdate(); + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return rows; + } + + public int removeViewerPage(Long id){ + int rows = 0; + try { + logger.debug("$$$ removeViewerPage [ID=" + id + "]"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.beginTransaction(); + String sql = "DELETE FROM ViewerPage WHERE id = :id"; + Query query = session.createQuery(sql); + query.setLong("id", id); + + rows = query.executeUpdate(); + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return rows; + } + + public List<ViewerAttribute> getViewerAttributes(){ + List<ViewerAttribute> list = new ArrayList<ViewerAttribute>(); + try { + logger.debug("$$$ getViewerAttributes"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from ViewerAttribute"; + Query query = session.createQuery(hql); + list = query.list(); + + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.debug("getViewerAttributes - Time execution: " + diff + / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + + "[min] " + diff / 1000 + "[sec]"); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + + return list; + } + + public List<ViewerPage> getViewerPages(){ + List<ViewerPage> list = new ArrayList<ViewerPage>(); + try { + logger.info("$$$$ getViewerPages"); + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from ViewerPage"; + Query query = session.createQuery(hql); + list = query.list(); + + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.info("getViewerPages - Time execution: " + diff + / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + + "[min] " + diff / 1000 + "[sec]"); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + + return list; + } + + /* + * public List<Entity> searchEntityStartingBy(String objectClass, + * List<String> ownValueList , int maxResult){ List<Entity> entities = new + * ArrayList<Entity>(); try { + * logger.debug("### Starting Search Entity Starting by " + ownValueList + + * " ###"); Long start = System.currentTimeMillis(); + * + * Session session = HibernateUtil.getSessionFactory() .getCurrentSession(); + * session.getTransaction().begin(); + * + * String hql = "from Entity " + "where " + + * "objectClass = :objectClass AND " + "ownValue REGEXP '^[0-9]' AND " + + * "systemStatus = :systemStatus "; + * + * + * if(ownValueList.size() > 0){ hql += " AND ( "; } for(int i=0; i < + * ownValueList.size(); i++){ if( i > 0) hql += " OR "; hql += + * "ownValue like :ownValue" + i + " "; } if(ownValueList.size() > 0){ hql + * += " ) "; } hql += "GROUP by id " + "ORDER by ownValue "; + * + * Query query = session.createQuery(hql); if(maxResult > 0){ + * query.setMaxResults(maxResult); } query.setString("systemStatus", + * Node.SYS_STATUS_CURRENT_VERSION); query.setString("objectClass", + * objectClass); + * + * for(int i=0; i < ownValueList.size(); i++){ query.setString("ownValue" + + * i, ownValueList.get(i)); } + * + * entities = query.list(); + * + * Long end = System.currentTimeMillis(); Long diff = end - start; + * logger.info("Search Entity Starting By - Time execution: " + diff + * / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + "[min] " + diff / + * 1000 + "[sec]"); + * + * session.getTransaction().commit(); } catch (Exception e) { + * logger.error(e.getMessage()); e.printStackTrace(); } return entities; } + */ + + /** + * <p> + * This method returns a list of entities with same attributes and same own + * value than the given entity + * </p> + * + * @param entity + * @param substring + * @param maxResults + * @return + */ + public List<Entity> searchEntity(Entity entity, boolean substring, + int maxResult) { + List<Entity> entities = new ArrayList<Entity>(); + try { + logger.debug("### Starting Search Entity ###"); + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String div = (substring) ? " like " : " = "; + + String hql = "from Entity ent "; + + for (int i = 0; i < entity.getAttributes().size(); i++) { + hql += ", Attribute att" + i + " "; + } + + hql += " where " + "ent.systemStatus = :systemStatus AND " + + "ent.objectClass = :objectClass AND " + "ent.ownValue " + + div + " :ownValue "; + + if (entity.getAttributes().size() > 0) + hql += "AND "; + + for (int i = 0; i < entity.getAttributes().size(); i++) { + hql += "att" + i + ".systemStatus = :systemStatus AND "; + hql += "att" + i + ".sourceId = ent.id AND "; + hql += "att" + i + ".objectClass = :objectClass" + i + " AND "; + hql += "att" + i + ".ownValue " + div + " :ownValue" + i + " "; + if (i < (entity.getAttributes().size() - 1)) + hql += "AND "; + } + hql += " GROUP by ent.id"; + + Query query = session.createQuery(hql); + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("objectClass", entity.getObjectClass()); + query.setString( + "ownValue", + (substring) ? "%" + entity.getOwnValue() + "%" : entity + .getOwnValue()); + + int i = 0; + for (Attribute att : entity.getAttributes()) { + query.setString("objectClass" + i, att.getObjectClass()); + query.setString( + "ownValue" + i, + (substring) ? "%" + att.getOwnValue() + "%" : att + .getOwnValue()); + i++; + } + + List<Object> list = query.list(); + + for (Object o : list) { + Object[] array = (Object[]) o; + if (array.length > 0) { + Entity ent = (Entity) array[0]; + ent.setLightweight(true); + entities.add(ent); + } + } + + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.info("Search Entity - Time execution: " + diff + / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + + "[min] " + diff / 1000 + "[sec]"); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return entities; + } + + public List<Entity> searchEntity(String objectClass, String ownValue, + int maxResult) { + try { + logger.info("### Starting Search Entity ###"); + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from Entity " + "where " + + "systemStatus = :systemStatus AND " + + "objectClass like :objectClass "; + + if (StringUtils.isNotEmpty(ownValue)) { + hql += "AND ownValue like :ownValue "; + } + + Query query = session.createQuery(hql); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("objectClass", objectClass); + if (StringUtils.isNotEmpty(ownValue)) { + query.setString("ownValue", "%" + ownValue + "%"); + } + + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + + List<Entity> list = query.list(); + + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.info("Search Entity - Time execution: " + diff + / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + + "[min] " + diff / 1000 + "[sec]"); + return list; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + public List<Attribute> getAttributeByDefByName(String def, String name, + int maxResults) { + List<Attribute> list = null; + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Attribute " + "where " + + "objectClass = :name AND " + + "systemStatus = :systemStatus AND " + + "sourceObjectClass = :sourceObjectClass "; + + Query query = session.createQuery(hql); + query.setString("name", name); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("sourceObjectClass", def); + if (maxResults > 0) { + query.setMaxResults(maxResults); + } + + list = query.list(); + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return list; + } + + /** + * Returns an attribute directly from the DB no implemented yet! + * + * @param entity + * @param name + * @return + */ + public Attribute getAttributeByName(Entity entity, String name) { + Attribute att = null; + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Attribute " + "where " + + "objectClass = :name AND " + + "systemStatus = :systemStatus AND " + "sourceId = :id "; + + Query query = session.createQuery(hql); + query.setMaxResults(1); + query.setString("name", name); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entity.getId()); + + List<Attribute> list = query.list(); + if (list.size() > 0) { + att = (Attribute) list.get(0); + } + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return att; + } + + public List<Attribute> getAllAttributesJDBC(Long entId, int maxResult) { + List<Attribute> atts = null; + long start = System.currentTimeMillis(); + try { + Connection conn = JDBCUtils.getConn(); + + String sql = "select * from node " + "where " + + "system_status = '" + Node.SYS_STATUS_CURRENT_VERSION + + "' AND " + "source_id = '" + entId + "' " + + "order by object_class"; + + if (maxResult > 0) { + // query.setMaxResults(maxResult); + } + + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql); + atts = JDBCUtils.rs2Atts(rs); + + conn.close(); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + + logger.debug("getAllAttributes JDBC - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return atts; + } + + /** + * Returns a list of all attribute for an entity directly from the DB no + * implemented yet! + * + * @param entity + * @param name + * @return + */ + public List<Attribute> getAllAttributes(Long entId, int maxResult) { + List<Attribute> atts = null; + long start = System.currentTimeMillis(); + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Attribute " + "where " + + "systemStatus = :systemStatus AND " + "sourceId = :id " + + "order by objectClass"; + + Query query = session.createQuery(hql); + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("id", entId); + + atts = query.list(); + + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + e.printStackTrace(); + } + logger.debug("getAllAttributes - execution time[ms]: " + + (System.currentTimeMillis() - start)); + return atts; + } + + /** + * Here the entities can more than one time in the result set. + * + * @param filters + * @param maxResult + * @return + */ + public Map<Attribute, Entity> searchAttEntityByAttributeFilter( + List<AttributeFilter> filters, int maxResult) { + Map<Attribute, Entity> result = new HashMap<Attribute, Entity>(); + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Entity as ent, Attribute as att " + "where " + + "att.systemStatus = :systemStatus AND " + + "ent.systemStatus = :systemStatus AND " + + "ent.id = att.sourceId AND ("; + + int count = 0; + for (AttributeFilter filter : filters) { + hql += " ( "; + if (StringUtils.isNotEmpty(filter.getEntObjectClass())) { + hql += "att.sourceObjectClass = :sourceObjectClass" + count + + " "; + if (StringUtils.isNotEmpty(filter.getName()) + || StringUtils.isNotEmpty(filter.getOwnValue())) + hql += " AND "; + } + if (StringUtils.isNotEmpty(filter.getName())) { + hql += "att.objectClass = :name" + count + " "; + if (StringUtils.isNotEmpty(filter.getOwnValue())) + hql += " AND "; + } + if (StringUtils.isNotEmpty(filter.getOwnValue())) { + if (filter.isNormalize()) { + hql += "att.normalizedOwnValue like :ownValue" + count + + " "; + } else { + hql += "att.ownValue like :ownValue" + count + " "; + } + } + + hql += " ) "; + count++; + if (filters.size() > count) + hql += " OR "; + } + + hql += " ) group by att.id "; + // I removed this line because the buffer of MyISAM is to + // small and it could be a reason to return lesser rows. I hope so! + // order by ent.ownValue + + Query query = session.createQuery(hql); + if (maxResult > 0) + query.setMaxResults(maxResult); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + + count = 0; + for (AttributeFilter filter : filters) { + if (StringUtils.isNotEmpty(filter.getName())) + query.setString("name" + count, filter.getName()); + if (StringUtils.isNotEmpty(filter.getOwnValue())) + query.setString("ownValue" + count, + "%" + filter.getOwnValue() + "%"); + if (StringUtils.isNotEmpty(filter.getEntObjectClass())) + query.setString("sourceObjectClass" + count, + filter.getEntObjectClass()); + count++; + } + + List<Object> listO = query.list(); + for (Object o : listO) { + Object[] array = (Object[]) o; + if (array.length > 1) { + Entity ent = (Entity) array[0]; + ent.setLightweight(true); + Attribute att = (Attribute) array[1]; + result.put(att, ent); + } + } + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return result; + } + + /** + * here the entities not repeated + * + * @param filters + * @param maxResult + * @return + */ + public Map<Entity, Attribute> searchEntityByAttributeFilter( + List<AttributeFilter> filters, int maxResult) { + Map<Entity, Attribute> result = new HashMap<Entity, Attribute>(); + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Entity as ent, Attribute as att " + "where " + + "att.systemStatus = :systemStatus AND " + + "ent.systemStatus = :systemStatus AND " + + "ent.id = att.sourceId AND ("; + + int count = 0; + for (AttributeFilter filter : filters) { + hql += " ( "; + if (StringUtils.isNotEmpty(filter.getEntObjectClass())) { + hql += "att.sourceObjectClass = :sourceObjectClass" + count + + " "; + if (StringUtils.isNotEmpty(filter.getName()) + || StringUtils.isNotEmpty(filter.getOwnValue())) + hql += " AND "; + } + if (StringUtils.isNotEmpty(filter.getName())) { + hql += "att.objectClass = :name" + count + " "; + if (StringUtils.isNotEmpty(filter.getOwnValue())) + hql += " AND "; + } + if (StringUtils.isNotEmpty(filter.getOwnValue())) { + if (filter.isNormalize()) { + hql += "att.normalizedOwnValue like :ownValue" + count + + " "; + } else { + hql += "att.ownValue like :ownValue" + count + " "; + } + } + + hql += " ) "; + count++; + if (filters.size() > count) + hql += " OR "; + } + + hql += " ) group by att.sourceId order by ent.ownValue"; + + Query query = session.createQuery(hql); + if (maxResult > 0) + query.setMaxResults(maxResult); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + + count = 0; + for (AttributeFilter filter : filters) { + if (StringUtils.isNotEmpty(filter.getName())) + query.setString("name" + count, filter.getName()); + if (StringUtils.isNotEmpty(filter.getOwnValue())) + query.setString("ownValue" + count, + "%" + filter.getOwnValue() + "%"); + if (StringUtils.isNotEmpty(filter.getEntObjectClass())) + query.setString("sourceObjectClass" + count, + filter.getEntObjectClass()); + count++; + } + + List<Object> listO = query.list(); + for (Object o : listO) { + Object[] array = (Object[]) o; + if (array.length > 1) { + Entity ent = (Entity) array[0]; + ent.setLightweight(true); + Attribute att = (Attribute) array[1]; + result.put(ent, att); + } + } + + session.getTransaction().commit(); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return result; + } + + /** + * + * @param filters + * @param maxResults + * @return + */ + public List<Attribute> searchAttribute(List<AttributeFilter> filters, + int maxResult) { + try { + // logger.info(""); + // logger.info("### Starting Search Attribute [entObjClass="+entObjClass+", name="+name+", ownValue="+ownValue+", contentType="+contentType+"]###"); + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from Attribute " + "where " + + "systemStatus = :systemStatus AND ("; + + int count = 0; + for (AttributeFilter filter : filters) { + hql += " ( "; + if (StringUtils.isNotEmpty(filter.getEntObjectClass())) { + hql += "sourceObjectClass = :sourceObjectClass" + count + + " "; + if (StringUtils.isNotEmpty(filter.getName()) + || StringUtils.isNotEmpty(filter.getOwnValue())) + hql += " AND "; + } + if (StringUtils.isNotEmpty(filter.getName())) { + hql += "objectClass like :name" + count + " "; + if (StringUtils.isNotEmpty(filter.getOwnValue())) + hql += " AND "; + } + if (StringUtils.isNotEmpty(filter.getOwnValue())) { + if (filter.isNormalize()) { + hql += "normalizedOwnValue like :ownValue" + count + + " "; + } else { + hql += "ownValue like :ownValue" + count + " "; + } + } + + hql += " ) "; + count++; + if (filters.size() > count) + hql += " OR "; + } + + hql += " ) group by sourceId"; + + Query query = session.createQuery(hql); + if (maxResult > 0) + query.setMaxResults(maxResult); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + + count = 0; + for (AttributeFilter filter : filters) { + if (StringUtils.isNotEmpty(filter.getName())) + query.setString("name" + count, filter.getName() + "%"); + if (StringUtils.isNotEmpty(filter.getOwnValue())) + query.setString("ownValue" + count, + "%" + filter.getOwnValue() + "%"); + if (StringUtils.isNotEmpty(filter.getEntObjectClass())) + query.setString("sourceObjectClass" + count, + filter.getEntObjectClass()); + count++; + } + + List<Attribute> list = query.list(); + + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.info("Found " + list.size() + " elements"); + logger.info("Search Attribute - Time execution: " + diff + / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + + "[min] " + diff / 1000 + "[sec]\n"); + return list; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p> + * Returns only an attribute searched by its name, its value and by an + * attribute which belong to the same entity + * </p> + * <p> + * For example to find all Country by which contains in its value "Germa" + * the input should be so: + * </p> + * <p> + * firstName = "name" firstValue = "Germ" secondName = "type" secondValue = + * "country" internally both attributes are binding by the unique entity of + * type "PLACE". Only the attribute of the first-name and -value will be + * returned. + * </p> + * + * @param name + * @param ownValue + * @param entObjClass + * @param maxResults + * @return + */ + public List<Attribute> searchAttribute(String firstName, String firstValue, + String secondName, String secondValue, String entObjClass, + int maxResults) { + List<Attribute> list = new ArrayList<Attribute>(); + Session session = null; + try { + logger.info("Starting Search Attribute " + "[entObjClass=" + + entObjClass + ", firstName=" + firstName + + ", firstValue=" + firstValue + ", secondName=" + + secondName + ", secondValue=" + secondValue + "]"); + + session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from Attribute as a1, Attribute as a2 " + "where " + + + "a1.sourceId = a2.sourceId AND " + + + "a1.systemStatus = :systemStatus AND " + + "a2.systemStatus = :systemStatus AND " + + + "a1.objectClass like :firstName AND " + + "a2.objectClass like :secondName AND " + + + "a1.ownValue like :firstValue AND " + + "a2.ownValue like :secondValue AND " + + + "a1.sourceObjectClass = :entObjClass AND " + + "a2.sourceObjectClass = :entObjClass "; + + hql += "group by a1.sourceId "; + + Query query = session.createQuery(hql); + query.setString("firstName", "%" + firstName + "%"); + query.setString("firstValue", "%" + firstValue + "%"); + query.setString("secondName", "%" + secondName + "%"); + query.setString("secondValue", "%" + secondValue + "%"); + query.setString("entObjClass", entObjClass); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + if (maxResults > 0) { + query.setMaxResults(maxResults); + } + + List<Object> listO = query.list(); + + for (Object o : listO) { + Object[] array = (Object[]) o; + if (array.length > 0) { + Attribute firstAtt = (Attribute) array[0]; + list.add(firstAtt); + } + } + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } finally { + session.getTransaction().commit(); + } + return list; + } + + /** + * <p> + * only the ownValue of the attribute and of the entity can be null + * </p> + * <p> + * The ownValue, the attName and the attOwnValue are searched as substring. + * </p> + * + * @param objectClass + * @param ownValue + * @param attName + * @param attOwnValue + * @param maxResult + * @return + */ + public List<Entity> searchEntityByAttribute(String objectClass, + String ownValue, String attName, String attOwnValue, int maxResult) { + List<Entity> entities = new ArrayList<Entity>(); + try { + + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from Entity as ent, Attribute as att " + "where " + + "ent.systemStatus = :systemStatus AND " + + "att.systemStatus = :systemStatus AND " + + "ent.objectClass = :objectClass AND "; + + if (StringUtils.isNotEmpty(ownValue)) { + hql += "ent.ownValue like :ownValue AND "; + } + + if (StringUtils.isNotEmpty(attOwnValue)) { + hql += "att.ownValue like :attOwnValue AND "; + } + + hql += "att.objectClass like :attName AND " + + "ent.id = att.sourceId " + "group by ent.id "; + + Query query = session.createQuery(hql); + if (maxResult > 0) { + query.setMaxResults(maxResult); + } + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("objectClass", objectClass); + if (StringUtils.isNotEmpty(ownValue)) + query.setString("ownValue", "%" + ownValue + "%"); + query.setString("attName", "%" + attName + "%"); + if (StringUtils.isNotEmpty(attOwnValue)) + query.setString("attOwnValue", "%" + attOwnValue + "%"); + + List<Object> listO = query.list(); + + for (Object o : listO) { + Object[] array = (Object[]) o; + if (array.length > 0) { + Entity ent = (Entity) array[0]; + ent.setLightweight(true); + entities.add(ent); + } + } + + session.getTransaction().commit(); + + return entities; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p> + * This method searches attributes, which match the input's parameters. + * </p> + * <p> + * Any parameter must be empty, excepted contentType. + * </p> + * + * @param name + * @param ownValue + * @param entObjClass + * @param maxResults + * @return list of attributes filtered by input's parameters + */ + public List<Attribute> searchAttribute(String name, String ownValue, + String entObjClass, String contentType, int maxResults) { + List<Attribute> list = new ArrayList<Attribute>(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + try { + // logger.info(""); + // logger.info("### Starting Search Attribute [entObjClass=" + // + entObjClass + ", name=" + name + ", ownValue=" + ownValue + // + ", contentType=" + contentType + "]###"); + + session.getTransaction().begin(); + // Long start = System.currentTimeMillis(); + + String hql = "from Attribute " + "where " + + "systemStatus = :systemStatus AND " + + "objectClass like :name AND "; + if (StringUtils.isNotEmpty(ownValue)) { + hql += "ownValue like :ownValue AND "; + } + if (StringUtils.isNotEmpty(contentType)) { + hql += "contentType like :contentType AND "; + } + hql += "sourceObjectClass = :sourceObjectClass "; + hql += "group by id order by ownValue"; + + Query query = session.createQuery(hql); + if (maxResults > 0) { + query.setMaxResults(maxResults); + } + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setString("name", name + "%"); + if (StringUtils.isNotEmpty(ownValue)) { + query.setString("ownValue", "%" + ownValue + "%"); + } + query.setString("sourceObjectClass", entObjClass); + if (StringUtils.isNotEmpty(contentType)) { + query.setString("contentType", contentType); + } + + list = query.list(); + + // Long end = System.currentTimeMillis(); + // Long diff = end - start; + // logger.info("Found " + list.size() + " elements"); + // logger.info("Search Attribute - Time execution: " + diff + // / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + // + "[min] " + diff / 1000 + "[sec]\n"); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } finally { + session.getTransaction().commit(); + } + return list; + } + + /** + * + * @param objClass + * @param relName + * @param objClassSrc + * @param attName + * @param attValue + * @param maxResults + * @return + */ + public List<Entity> searchEntityByAttributeOfTarRelation(String objClass, + String relName, String objClassSrc, String attName, + String attValue, int maxResults) { + List<Entity> entityList = new ArrayList<Entity>(); + Session session = null; + try { + + if (StringUtils.isEmpty(objClass) || StringUtils.isEmpty(relName) + || StringUtils.isEmpty(objClassSrc) + || StringUtils.isEmpty(attName) + || StringUtils.isEmpty(attValue)) { + throw new Exception( + "At least one of these parameters was empty: objClass, relName, objClassSrc, attName, attValue."); + } + + session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from Relation as rel, Attribute as att " + + "where " + + "att.sourceObjectClass = :objClassSrc AND " + + + // "att.sourceObjectClass = rel.sourceObjectClass AND " + + "rel.targetObjectClass = :objClass AND " + + "rel.ownValue = :relName AND " + + "att.ownValue like :attValue AND " + + "att.objectClass = :attName AND " + + "att.systemStatus = :systemStatus AND " + + "rel.systemStatus = :systemStatus AND " + + "rel.sourceId = att.sourceId " + "group by att.sourceId"; + + Query query = session.createQuery(hql); + if (maxResults > 0) { + query.setMaxResults(maxResults); + } + + query.setString("objClass", objClass); + query.setString("relName", relName); + query.setString("objClassSrc", objClassSrc); + query.setString("attName", attName); + query.setString("attValue", "%" + attValue + "%"); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + + List<Object> list = query.list(); + List<Long> idList = new ArrayList<Long>(); + for (Object o : list) { + Object[] array = (Object[]) o; + Relation rel = (Relation) array[0]; + idList.add(rel.getTargetId()); + } + + for (Long id : idList) { + List<Entity> l = getLightweightEntities(session, + Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null, + true, -1); + if (l.size() > 0) { + entityList.add(l.get(0)); + } + } + + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.info("searchEntityByAttributeOfTarRelation - Time execution: " + + diff + / (60 * 60 * 1000) + + "[hours] " + + diff + / (60 * 1000) + "[min] " + diff / 1000 + "[sec]"); + } catch (Exception e) { + logger.error(e.getMessage()); + } finally { + session.getTransaction().commit(); + } + return entityList; + } + + /** + * returns the attribute found and the corresponding entity + * + * @param objClass + * @param relName + * @param objClassSrc + * @param attName + * @param attValue + * @param maxResults + * @return + */ + public Map<Entity, Attribute> searchEntityByAttributeOfTarRelation2( + String objClass, String relName, String objClassSrc, + String attName, String attValue, int maxResults) { + Map<Entity, Attribute> entityMap = new HashMap<Entity, Attribute>(); + Session session = null; + try { + + if (StringUtils.isEmpty(objClass) || StringUtils.isEmpty(relName) + || StringUtils.isEmpty(objClassSrc) + || StringUtils.isEmpty(attName) + || StringUtils.isEmpty(attValue)) { + throw new Exception( + "At least one of these parameters was empty: objClass, relName, objClassSrc, attName, attValue."); + } + + session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from Relation as rel, Attribute as att " + + "where " + + "att.sourceObjectClass = :objClassSrc AND " + + + // "att.sourceObjectClass = rel.sourceObjectClass AND " + + "rel.targetObjectClass = :objClass AND " + + "rel.ownValue = :relName AND " + + "att.ownValue like :attValue AND " + + "att.objectClass = :attName AND " + + "att.systemStatus = :systemStatus AND " + + "rel.systemStatus = :systemStatus AND " + + "rel.sourceId = att.sourceId " + "group by att.sourceId"; + + Query query = session.createQuery(hql); + if (maxResults > 0) { + query.setMaxResults(maxResults); + } + + query.setString("objClass", objClass); + query.setString("relName", relName); + query.setString("objClassSrc", objClassSrc); + query.setString("attName", attName); + query.setString("attValue", "%" + attValue + "%"); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + + List<Object> list = query.list(); + Map<Long, Attribute> map = new HashMap<Long, Attribute>(); + for (Object o : list) { + Object[] array = (Object[]) o; + Relation rel = (Relation) array[0]; + Attribute att = (Attribute) array[1]; + map.put(rel.getTargetId(), att); + } + + for (Long id : map.keySet()) { + List<Entity> l = getLightweightEntities(session, + Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null, + true, -1); + if (l.size() > 0) { + entityMap.put(l.get(0), map.get(id)); + } + } + + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.info("searchEntityByAttributeOfTarRelation - Time execution: " + + diff + / (60 * 60 * 1000) + + "[hours] " + + diff + / (60 * 1000) + "[min] " + diff / 1000 + "[sec]"); + } catch (Exception e) { + logger.error(e.getMessage()); + } finally { + session.getTransaction().commit(); + } + return entityMap; + } + + /** + * TODO + * + * @param objClass + * @param relName + * @param objClassTar + * @param attName + * @param attValue + * @param maxResults + * @return + */ + public List<Entity> searchEntityByAttributeOfSrcRelation(String objClass, + String relName, String objClassTar, String attName, + String attValue, int maxResults) { + try { + + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * This method search a + * + * @param filter + * RelationFilter + * @param maxResults + * @return + */ + public List<RelationResultEntry> searchByRelation(RelationFilter filter, + String grouping, int maxResults) { + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hql = "from "; + if (filter.srcInclude) { + if (filter.srcAttInclude) { + hql += "Attribute as srcAtt, "; + hql += "NodeJoin as joinSrcAtt, "; + } + hql += "Entity as src, "; + } + hql += "Relation as rel "; + if (filter.tarInclude) { + hql += ", "; + hql += "Entity as tar "; + if (filter.tarAttInclude) { + hql += ", "; + hql += "NodeJoin as joinTarAtt, "; + hql += "Attribute as tarAtt "; + } + } + + hql += "where "; + if (filter.srcInclude) { + if (filter.srcAttInclude) { + hql += "srcAtt.systemStatus = :systemStatus AND "; + hql += "joinSrcAtt.sourceSystemStatus = :systemStatus AND joinSrcAtt.targetSystemStatus = :systemStatus AND "; + hql += "joinSrcAtt.sourceId = src.id AND joinSrcAtt.targetId = srcAtt.id AND "; + hql += "srcAtt.ownValue like :srcAttOwnValue AND "; + hql += "srcAtt.objectClass like :srcAttName AND "; + } + hql += "src.systemStatus = :systemStatus AND "; + hql += "rel.sourceId = src.id AND "; + hql += "src.ownValue like :srcOwnValue AND "; + hql += "src.objectClass like :srcObjectClass AND "; + } + if (filter.tarInclude) { + if (filter.tarAttInclude) { + hql += "tarAtt.systemStatus = :systemStatus AND "; + hql += "joinTarAtt.sourceSystemStatus = :systemStatus AND joinTarAtt.targetSystemStatus = :systemStatus AND "; + hql += "joinTarAtt.sourceId = tar.id AND joinTarAtt.targetId = tarAtt.id AND "; + hql += "tarAtt.ownValue like :tarAttOwnValue AND "; + hql += "tarAtt.objectClass like :tarAttName AND "; + } + hql += "tar.systemStatus = :systemStatus AND "; + hql += "rel.targetId = tar.id AND "; + hql += "tar.ownValue like :tarOwnValue AND "; + hql += "tar.objectClass like :tarObjectClass AND "; + } + hql += "rel.systemStatus = :systemStatus AND "; + hql += "rel.ownValue like :relOwnValue AND "; + hql += "rel.objectClass like :relObjectClass "; + + hql += ") "; + + // setup of the field, which is used to group the result entries. + if (GROUP_BY_SOURCE.equals(grouping) && filter.srcInclude) { + hql += " group by src.id"; + } else { + if (GROUP_BY_TARGET.equals(grouping) && filter.tarInclude) { + hql += " group by tar.id"; + } else { + hql += " group by rel.id"; + } + } + + Query query = session.createQuery(hql); + if (maxResults > 0) { + query.setMaxResults(maxResults); + } + + // setting the values of the filter in the query. + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + if (filter.srcInclude) { + if (filter.srcAttInclude) { + if (StringUtils.isNotEmpty(filter.srcAttOwnValue)) { + query.setString("srcAttOwnValue", "%" + + filter.srcAttOwnValue); + } + if (StringUtils.isNotEmpty(filter.srcAttName)) { + query.setString("srcAttName", "%" + filter.srcAttName); + } + } + if (StringUtils.isNotEmpty(filter.srcOwnValue)) { + query.setString("srcOwnValue", "%" + filter.srcOwnValue); + } + if (StringUtils.isNotEmpty(filter.srcObjectClass)) { + query.setString("srcObjectClass", "%" + + filter.srcObjectClass); + } + } + if (filter.tarInclude) { + if (filter.tarAttInclude) { + if (StringUtils.isNotEmpty(filter.tarAttOwnValue)) { + query.setString("tarAttOwnValue", filter.tarAttOwnValue); + } + if (StringUtils.isNotEmpty(filter.tarAttName)) { + query.setString("tarAttName", filter.tarAttName); + } + } + if (StringUtils.isNotEmpty(filter.tarObjectClass)) { + query.setString("tarObjectClass", filter.tarObjectClass); + } + if (StringUtils.isNotEmpty(filter.tarOwnValue)) { + query.setString("tarOwnValue", filter.tarOwnValue); + } + } + if (StringUtils.isNotEmpty(filter.relOwnValue)) { + query.setString("relOwnValue", filter.relOwnValue); + } + if (StringUtils.isNotEmpty(filter.relObjectClass)) { + query.setString("relObjectClass", filter.relObjectClass); + } + + List<Object> list = query.list(); + List<RelationResultEntry> resultSet = new ArrayList<RelationResultEntry>(); + RelationResultEntry entry; + for (Object o : list) { + entry = new RelationResultEntry(); + int index = 0; + Object[] array = (Object[]) o; + // Object item = (Object) array[0]; + if (filter.srcInclude) { + if (filter.srcAttInclude) { + entry.srcAttribute = (Attribute) array[index]; + index++; + index++; + // hql += "Attribute as srcAtt, "; + // hql += "NodeJoin as joinSrcAtt, "; + } + entry.source = (Entity) array[index]; + index++; + // hql += "Entity as src, "; + } + // hql += "Relation as rel "; + entry.relation = (Relation) array[index]; + index++; + if (filter.tarInclude) { + // hql += "Entity as tar "; + entry.target = (Entity) array[index]; + index++; + if (filter.tarAttInclude) { + index++; + entry.tarAttribute = (Attribute) array[index]; + // hql += "NodeJoin as joinTarAtt, "; + // hql += "Attribute as tarAtt "; + } + } + resultSet.add(entry); + } + // ////////////////////////////////////////// + // ////////////////////////////////////////// + // ////////////////////////////////////////// + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.info("GetLightweightEntities BY RELATION - Time execution: " + + diff + / (60 * 60 * 1000) + + "[hours] " + + diff + / (60 * 1000) + "[min] " + diff / 1000 + "[sec]"); + return resultSet; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p> + * Returns a list of entities filtered by the following rules: + * </p> + * if <b>ownValue</b> is no empty, the entities returned have into ownValue + * the given substring. + * + * @param ownValue + * @param objectClass + * @param filters + * @return + */ + + /** + * TODO: what will be returned if it is searching a null value eg: + * identifier=null? + * + * @param ownValue + * @param objectClass + * @param attObjClass + * @param attOwnValue + * @param assertions + * @param maxResults + * @return + */ + public List<Entity> getLightweightEntitiesByAttribute(String ownValue, + String objectClass, String attObjClass, String attOwnValue, + Boolean assertions, int maxResults, boolean substring) { + logger.debug("Get LW Entities By Attribute [maxResults: " + maxResults + + "]"); + + List<Entity> entities = new ArrayList<Entity>(); + + if (substring) { + ownValue = (StringUtils.isEmpty(ownValue)) ? "" : "%" + ownValue + + "%"; + objectClass = (StringUtils.isEmpty(objectClass)) ? "" : "%" + + objectClass + "%"; + attOwnValue = (StringUtils.isEmpty(attOwnValue)) ? "" : "%" + + attOwnValue + "%"; + attObjClass = (StringUtils.isEmpty(attObjClass)) ? "" : "%" + + attObjClass + "%"; + } + + String type = (assertions) ? Node.TYPE_ABOX : Node.TYPE_TBOX; + String equalityOperator = (substring) ? " like " : " = "; + + try { + Session session = HibernateUtil.getSessionFactory() + .getCurrentSession(); + session.getTransaction().begin(); + Long start = System.currentTimeMillis(); + + String hqlEntities = "from Entity as ent, Attribute as att " + + "where ent.systemStatus = :systemStatus AND att.systemStatus = :systemStatus AND " + + "att.sourceId = ent.id AND "; + + if (StringUtils.isNotEmpty(ownValue)) { + hqlEntities += "ent.ownValue " + equalityOperator + + " :ownValue AND "; + } + if (StringUtils.isNotEmpty(objectClass)) { + hqlEntities += "ent.objectClass " + equalityOperator + + " :objectClass AND "; + } + if (StringUtils.isNotEmpty(attOwnValue)) { + hqlEntities += "att.ownValue " + equalityOperator + + " :attOwnValue AND "; + } + if (StringUtils.isNotEmpty(attObjClass)) { + hqlEntities += "att.objectClass " + equalityOperator + + " :attObjClass AND "; + } + hqlEntities += "ent.type = :type " + + "group by ent.id order by ent.ownValue"; + + Query queryEntities = session.createQuery(hqlEntities); + queryEntities.setString("systemStatus", + Node.SYS_STATUS_CURRENT_VERSION); + queryEntities.setString("type", type); + if (StringUtils.isNotEmpty(ownValue)) { + queryEntities.setString("ownValue", ownValue); + } + if (StringUtils.isNotEmpty(objectClass)) { + queryEntities.setString("objectClass", objectClass); + } + if (StringUtils.isNotEmpty(attOwnValue)) { + queryEntities.setString("attOwnValue", attOwnValue); + } + if (StringUtils.isNotEmpty(attObjClass)) { + queryEntities.setString("attObjClass", attObjClass); + } + if (maxResults > 0) { + queryEntities.setMaxResults(maxResults); + } + + entities = new ArrayList<Entity>(); + List<Object> list = queryEntities.list(); + for (Object o : list) { + Object[] array = (Object[]) o; + Object item = (Object) array[0]; + if (item instanceof Entity) { + // logger.info("Entity= " + item); + Entity ent = (Entity) item; + ent.setLightweight(true); + entities.add(ent); + } + } + + session.getTransaction().commit(); + Long end = System.currentTimeMillis(); + Long diff = end - start; + logger.debug("GetLightweightEntitiesByAttribute - Time execution: " + + diff / (60 * 60 * 1000) + "[hours] " + diff / (60 * 1000) + + "[min] " + diff / 1000 + "[sec]"); + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return entities; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/ServiceRegistry.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,79 @@ +package org.mpi.openmind.repository.services; + +import java.io.Serializable; + +import org.apache.log4j.Logger; +import org.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.configuration.ConfigurationService; +import org.mpi.openmind.search.SearchService; +import org.mpi.openmind.security.SecurityService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.scheduling.quartz.CronTriggerBean; + +/** + * + * @author jurzua + */ +public class ServiceRegistry implements Serializable { + + private static final long serialVersionUID = 3883885508641723220L; + + public static final String PERSISTENCE_SERVICE = "persistenceService"; + public static final String ONTOLOGY_SERVICE = "ontologyService"; + public static final String IMPORT_SERVICE = "importService"; + public static final String SECURITY_SERVICE = "securityService"; + public static final String CONFIGURATION_SERVICE = "configurationService"; + public static final String SEARCH_SERVICE = "searchService"; + public static final String WRAPPER = "wrapper"; + public static final String CRON = "schedulingCronTrigger"; + + private static Logger logger = Logger.getLogger(ServiceRegistry.class); + + private transient ApplicationContext context; + private transient WrapperService ws = null; + + public ServiceRegistry() { + logger.info("Initialize Service Registry"); + context = new ClassPathXmlApplicationContext( + new String[] { "openmind-context.xml" }); + + } + + public WrapperService getWrapper(){ + if(this.ws == null){ + logger.info("Initializing WrapperService"); + this.ws = (WrapperService) this.context.getBean(WRAPPER); + this.ws.initCache(); + } + return ws; + } + + /* + public OntologyService getOntologyService() { + return (OntologyService) this.context.getBean(ONTOLOGY_SERVICE); + }*/ + + public PersistenceService getPS() { + return (PersistenceService) context.getBean(PERSISTENCE_SERVICE); + } + + public SecurityService getSecurityService() { + return (SecurityService) context.getBean(SECURITY_SERVICE); + } + + public ConfigurationService getConfigurationService(){ + return (ConfigurationService) context.getBean(CONFIGURATION_SERVICE); + } + + + public SearchService getSearchService(){ + return (SearchService) context.getBean(SEARCH_SERVICE); + } + + public CronTriggerBean getCron(){ + return (CronTriggerBean) context.getBean(CRON); + } + + public static void main(String[] args) {} +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/utils/AttributeFilter.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,36 @@ +package org.mpi.openmind.repository.services.utils; + + +/** + * + * @author jurzua + */ +public class AttributeFilter extends Filter{ + private String name; + private String entObjectClass; + + public AttributeFilter(){} + + public AttributeFilter(String name, String value, String entObjectClass){ + this.setEntObjectClass(entObjectClass); + this.setOwnValue(value); + this.setName(name); + } + + public String getEntObjectClass() { + return entObjectClass; + } + + public void setEntObjectClass(String entObjectClass) { + this.entObjectClass = entObjectClass; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/utils/AttributeResultEntry.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,26 @@ +package org.mpi.openmind.repository.services.utils; + +import org.mpi.openmind.repository.bo.Attribute; +import org.mpi.openmind.repository.bo.Entity; + +public class AttributeResultEntry { + public Attribute attribute; + public Entity entity; + + + @Override + public String toString(){ + String s; + if(entity != null){ + s = "entity[id="+ entity.getId() + ", ownValue=" + entity.getOwnValue() + "]"; + }else{ + s = "entity[null]"; + } + if(attribute != null){ + s += ", attribute[id=" + attribute.getId() + ", name="+ attribute.getObjectClass() + ", value= " + attribute.getOwnValue() + "]"; + }else{ + s += "attribute[null]"; + } + return "AttributeResultEntry= " + s; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/utils/EntityFilter.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,18 @@ +package org.mpi.openmind.repository.services.utils; + + +/** + * + * @author jurzua + */ +public class EntityFilter extends Filter{ + private String objectClass; + + public String getObjectClass() { + return objectClass; + } + + public void setObjectClass(String objectClass) { + this.objectClass = objectClass; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/utils/Filter.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,29 @@ +package org.mpi.openmind.repository.services.utils; + +import java.text.Normalizer; + +/** + * + * @author jurzua + */ +public abstract class Filter { + + private String ownValue; + private boolean normalize = false; + + public boolean isNormalize() { + return normalize; + } + + public void setNormalize(boolean normalize) { + this.normalize = normalize; + } + + public String getOwnValue() { + return ownValue; + } + + public void setOwnValue(String value) { + this.ownValue = value; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/utils/RelationFilter.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,38 @@ +package org.mpi.openmind.repository.services.utils; + +public class RelationFilter extends Filter{ + + public String srcAttOwnValue; + public String srcAttName; + public Boolean srcAttNormalizedOwnValue = false; + //the value of this variable + //means if the Attribute of the Source should be included in the searching. + public Boolean srcAttInclude = false; + + public String srcOwnValue; + public String srcObjectClass; + public Boolean srcNormalizedOwnValue = false; + public Boolean srcInclude = true; + + public String relOwnValue; + public String relObjectClass; + public String relNormalizedOwnValue; + + public String tarOwnValue; + public String tarObjectClass; + public Boolean tarNormaliedOwnValue; + public Boolean tarInclude = true; + + public String tarAttOwnValue; + public String tarAttName; + public Boolean tarAttNormalizedOwnValue; + //the value of this variable + //means if the Attribute of the Target should be included in the searching. + public Boolean tarAttInclude = false; + + public RelationFilter(String srcOwnValue, String relOwnValue, String tarOwnValue){ + this.srcOwnValue = srcOwnValue; + this.relOwnValue = relOwnValue; + this.tarOwnValue = tarOwnValue; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/services/utils/RelationResultEntry.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,14 @@ +package org.mpi.openmind.repository.services.utils; + +import org.mpi.openmind.repository.bo.Attribute; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Relation; + +public class RelationResultEntry { + public Attribute srcAttribute; + public Entity source; + public Relation relation; + public Entity target; + public Attribute tarAttribute; + public Filter filter; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/ArabicNormalizerUtils.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,100 @@ +package org.mpi.openmind.repository.utils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; + +public class ArabicNormalizerUtils { + + + public static List<Character> ignoreList; + + static{ + Character[] array = { + 0x064B, 0x064C, 0x064D, 0x064E, + 0x064F, 0x0650, 0x0651, 0x0652, + 0x0670, 0x0671, 0x06E4, 0x06E4, + 0xE818, 0xE820, 0xE821, 0xE822, + 0xE823, 0xE824, 0xE825, 0xE826, + 0xE827, 0xE828, 0xE829, 0xE82A, + 0xE82B, 0xE82C, 0xE82D, 0xE832, + 0xE833, 0xE834, 0xE835, 0xE836, + 0xFB50, 0xFB51, 0xFC5E, 0xFC5F, + 0xFC60, 0xFC61, 0xFC62, 0xFE70, + 0xFE72, 0xFE74, 0xFE76, 0xFE78, + 0xFE7A, 0xFE7C, 0xFE7E + }; + ignoreList = Arrays.asList(array); + } + + public static Map<String, Character[]> wildCardCharMap = new HashMap<String, Character[]>(); + //public static Map<String, List<String>> wildCardStringMap = new HashMap<String, List<String>>(); + + static{ + + Character[] apostrophes = { + 0x22, 0x60, 0x2032, 0x2018, 0x2019, 0x201B, 0x27, 0x2BB, 0x2BC, 0x2BD, 0x2CB, 0x2BE, 0x2BF + }; + wildCardCharMap.put("", apostrophes); + + Character[] array1 = { + 0x0622, 0x0623, 0x0625, 0x0627 }; + wildCardCharMap.put("1", array1); + + Character[] array2 = { + 0x0626, 0x0649, 0x064A, 0x0649, 0x064A }; + wildCardCharMap.put("2", array2); + + Character[] array3 = { + 0x0648, 0x0624, 0x0648}; + wildCardCharMap.put("3", array3); + + Character[] array4 = { + 0x067E, 0x0628, 0x0628 }; + wildCardCharMap.put("4", array4); + + Character[] array5 = { + 0x0686, 0x062C, 0x062C}; + wildCardCharMap.put("5", array5); + + Character[] array6 = { + 0x0698, 0x0632, 0x0632}; + wildCardCharMap.put("6", array6); + + Character[] array7 = { + 0x06A4, 0x0641, 0x0641}; + wildCardCharMap.put("7", array7); + + Character[] array8 = { + 0x0643, 0x06A9, 0x06AF, 0x0643}; + wildCardCharMap.put("8", array8); + } + + public static String normalize(String w){ + if(StringUtils.isEmpty(w)) + return w; + + + /* + * Replacing combination of vowels + + for(String key : wildCardStringMap.keySet()){ + List<String> list = wildCardStringMap.get(key); + for(String term : list){ + w = w.replace(term, key); + } + }*/ + + for(String key : wildCardCharMap.keySet()){ + Character[] list = wildCardCharMap.get(key); + for(int i=0; i< list.length; i++){ + w = w.replace(list[i] + "", key); + } + } + return w; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/AttributeMap.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,92 @@ +package org.mpi.openmind.repository.utils; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +import org.mpi.openmind.repository.bo.Attribute; +import org.mpi.openmind.repository.bo.Entity; + +public class AttributeMap implements Map { + + private Entity entity; + + public AttributeMap(Entity entity) { + this.entity=entity; + } + + + @Override + public void clear() { + // TODO Auto-generated method stub + + } + + @Override + public boolean containsKey(Object key) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean containsValue(Object value) { + // TODO Auto-generated method stub + return false; + } + + @Override + public Set entrySet() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object get(Object key) { + Attribute attr = entity.getAttributeByName((String)key); + return attr.getValue(); + } + + + + @Override + public boolean isEmpty() { + // TODO Auto-generated method stub + return false; + } + + @Override + public Set keySet() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object put(Object key, Object value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void putAll(Map m) { + // TODO Auto-generated method stub + + } + + @Override + public Object remove(Object key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public Collection values() { + // TODO Auto-generated method stub + return null; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/CustomMysqlDialect.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,9 @@ +package org.mpi.openmind.repository.utils; + +public class CustomMysqlDialect extends org.hibernate.dialect.MySQLDialect { + + public String getTableTypeString() { + return " ENGINE=InnoDB DEFAULT CHARSET=utf8"; + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/EntityUtils.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,31 @@ +package org.mpi.openmind.repository.utils; + +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Node; +import org.mpi.openmind.repository.bo.Relation; + +public class EntityUtils { + + public static Entity createAssertion(String objectClass){ + Entity ent = new Entity(Node.TYPE_ABOX, objectClass, false); + return ent; + } + + public static Entity createAssertion(String objectClass, String ownValue){ + Entity ent = new Entity(Node.TYPE_ABOX, objectClass, false); + ent.setOwnValue(ownValue); + return ent; + } + + public static Relation createRelationForAssertions(Entity source, Entity target, String ownValue){ + Relation rel = new Relation(source, target, ownValue); + rel.setType(Node.TYPE_ABOX); + return rel; + } + + public static Relation createRelationForDefinitions(Entity source, Entity target, String ownValue){ + Relation rel = new Relation(source, target, ownValue); + rel.setType(Node.TYPE_TBOX); + return rel; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/ExportOM4Util.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,234 @@ +package org.mpi.openmind.repository.utils; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +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.Relation; +import org.mpi.openmind.repository.bo.View; +import org.mpi.openmind.repository.services.ServiceRegistry; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +/** + * <p>Exports assertions and concepts in a format compatible with openmind 4</p> + * + * @author Jorge Urzua + */ +public class ExportOM4Util { + + private static Logger logger = Logger.getLogger(WrapperService.class); + + /** + * <p>Generates a xml file, + * which contains all concepts of the system (called definition by OM3)</p> + * @param ontology running ontology service + * @param fileOutput path to save the output + */ + public static void exportConcepts(WrapperService ontology, String fileOutput){ + try{ + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.newDocument(); + + Element rootElement = document.createElement(XMLUtil.META_DATA); + document.appendChild(rootElement); + + Element conElement = document.createElement(XMLUtil.CONCEPTS); + rootElement.appendChild(conElement); + + List<Relation> relList = new ArrayList<Relation>(); + for(Entity concept : ontology.getConcepts()){ + System.out.println("Processing: " + concept.getOwnValue()); + insertEntity(concept, conElement, document, XMLUtil.CONCEPT); + for(Relation srcRel : concept.getSourceRelations()){ + relList.add(srcRel); + } + } + + + Element relElement = document.createElement(XMLUtil.RELATIONS); + rootElement.appendChild(relElement); + System.out.println("\nRelation:\n"); + for(Relation rel : relList){ + System.out.println("inserting relation: " + rel.getOwnValue()); + insertRelation(rel, relElement, document); + } + + XMLUtil.saveXMLDocument(fileOutput, document); + }catch(Exception e){ + logger.error(e.getMessage()); + e.printStackTrace(); + } + } + + public static void insertRelation(Relation rel, Element rootElement, Document document){ + Element relElement = document.createElement(XMLUtil.RELATION); + rootElement.appendChild(relElement); + + if(rel.getId() != null) + relElement.setAttribute(XMLUtil.ID, rel.getId().toString()); + if(StringUtils.isNotEmpty(rel.getOwnValue())) + relElement.setAttribute(XMLUtil.OWN_VALUE, rel.getOwnValue()); + if(StringUtils.isNotEmpty(rel.getObjectClass())) + relElement.setAttribute(XMLUtil.OBJECT_CLASS, rel.getObjectClass()); + if(StringUtils.isNotEmpty(rel.getContentType())) + relElement.setAttribute(XMLUtil.CONTENT_TYPE, rel.getContentType()); + if(rel.getSourceId() != null) + relElement.setAttribute(XMLUtil.RELATION_SOURCE_ID, rel.getSourceId().toString()); + if(rel.getTargetId() != null) + relElement.setAttribute(XMLUtil.RELATION_TARGET_ID, rel.getTargetId().toString()); + if(rel.getVersion() != null) + relElement.setAttribute(XMLUtil.VERSION, rel.getVersion().toString()); + if(rel.getModificationTime() != null) + relElement.setAttribute(XMLUtil.MODIFICATION_TIME, rel.getModificationTime().toString()); + + //TODO: adding the attribute to the dom. Firstly is needed the attributes in the relations. + //if(rel.getAttributes().size() > 0) + //insertAttributes(rel.getAttributes(), relElement, document); + + //adding the views to the dom + if(rel.getViews().size() > 0) + insertViews(rel.getViews(), relElement, document); + + } + + public static void insertEntity(Entity entity, Element rootElement, Document document, String type){ + Element entElement = document.createElement(type); + rootElement.appendChild(entElement); + + if(entity.getId() != null) + entElement.setAttribute(XMLUtil.ID, entity.getId().toString()); + if(StringUtils.isNotEmpty(entity.getOwnValue())) + entElement.setAttribute(XMLUtil.OWN_VALUE, entity.getOwnValue()); + if(StringUtils.isNotEmpty(entity.getObjectClass())) + entElement.setAttribute(XMLUtil.OBJECT_CLASS, entity.getObjectClass()); + if(StringUtils.isNotEmpty(entity.getContentType())) + entElement.setAttribute(XMLUtil.CONTENT_TYPE, entity.getContentType()); + if(entity.getVersion() != null) + entElement.setAttribute(XMLUtil.VERSION, entity.getVersion().toString()); + if(entity.getModificationTime() != null) + entElement.setAttribute(XMLUtil.MODIFICATION_TIME, entity.getModificationTime().toString()); + //adding the attribute to the dom + if(entity.getAttributes().size() > 0) + insertAttributes(entity.getAttributes(), entElement, document); + + //adding the views to the dom + if(entity.getViews().size() > 0) + insertViews(entity.getViews(), entElement, document); + } + + private static void insertAttributes(List<Attribute> atts, Element rootElement, Document document){ + Element attsElement = document.createElement(XMLUtil.ATTRIBUTES); + rootElement.appendChild(attsElement); + for(Attribute att : atts){ + insertAttribute(att, attsElement, document); + } + } + + private static void insertAttribute(Attribute att, Element rootElement, Document document){ + Element attElement = document.createElement(XMLUtil.ATTRIBUTE); + rootElement.appendChild(attElement); + + if(att.getId() != null) + attElement.setAttribute(XMLUtil.ID, att.getId().toString()); + if(StringUtils.isNotEmpty(att.getValue())) + attElement.setAttribute(XMLUtil.ATTRIBUTE_VALUE, att.getValue()); + if(StringUtils.isNotEmpty(att.getName())) + attElement.setAttribute(XMLUtil.ATTRIBUTE_NAME, att.getName()); + if(StringUtils.isNotEmpty(att.getContentType())) + attElement.setAttribute(XMLUtil.CONTENT_TYPE, att.getContentType()); + if(att.getVersion() != null) + attElement.setAttribute(XMLUtil.VERSION, att.getVersion().toString()); + if(att.getModificationTime() != null) + attElement.setAttribute(XMLUtil.MODIFICATION_TIME, att.getModificationTime().toString()); + + if(att.getViews().size() > 0) + insertViews(att.getViews(), attElement, document); + } + + private static void insertViews(List<View> views, Element rootElement, Document document){ + Element viewsElement = document.createElement(XMLUtil.VIEWS); + rootElement.appendChild(viewsElement); + for(View view : views){ + insertView(view, viewsElement, document); + } + } + + private static void insertView(View view, Element rootElement, Document document){ + Element viewElement = document.createElement(XMLUtil.VIEW); + rootElement.appendChild(viewElement); + + if(view.getId() != null) + viewElement.setAttribute(XMLUtil.ID, view.getId().toString()); + if(StringUtils.isNotEmpty(view.getOwnValue())) + viewElement.setAttribute(XMLUtil.OWN_VALUE, view.getOwnValue()); + if(StringUtils.isNotEmpty(view.getObjectClass())) + viewElement.setAttribute(XMLUtil.OBJECT_CLASS, view.getObjectClass()); + if(StringUtils.isNotEmpty(view.getContentType())) + viewElement.setAttribute(XMLUtil.CONTENT_TYPE, view.getContentType()); + if(view.getVersion() != null) + viewElement.setAttribute(XMLUtil.VERSION, view.getVersion().toString()); + if(view.getModificationTime() != null) + viewElement.setAttribute(XMLUtil.MODIFICATION_TIME, view.getModificationTime().toString()); + } + + /** + * <p>Generates a xml file, + * which contains all assertions of the system (called entities by OM3)</p> + * @param ontology running ontology service + * @param fileOutput path to save the output + */ + public static void exportAssertions(WrapperService ontology, String fileOutput, String insertionMode){ + try{ + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.newDocument(); + + Element rootElement = document.createElement(XMLUtil.OPENMIND_DATA); + //merge, replace, insert + rootElement.setAttribute(XMLUtil.INSERTION_MODE, insertionMode); + document.appendChild(rootElement); + + Element conElement = document.createElement(XMLUtil.ASSERTIONS); + rootElement.appendChild(conElement); + + List<Relation> relList = new ArrayList<Relation>(); + for(Entity assertion : ontology.getAssertion()){ + System.out.println("Processing: " + assertion.getOwnValue()); + insertEntity(assertion, conElement, document, XMLUtil.ASSERTION); + for(Relation srcRel : assertion.getSourceRelations()){ + if(!WrapperService.IS_TYPE_OF.equals(srcRel.getOwnValue())){ + relList.add(srcRel); + } + } + } + + Element relElement = document.createElement(XMLUtil.RELATIONS); + rootElement.appendChild(relElement); + System.out.println("\nRelation:\n"); + for(Relation rel : relList){ + System.out.println("inserting relation: " + rel.getOwnValue()); + insertRelation(rel, relElement, document); + } + + XMLUtil.saveXMLDocument(fileOutput, document); + }catch(Exception e){ + logger.error(e.getMessage()); + e.printStackTrace(); + } + } + + public static void main(String[] args) { + ServiceRegistry serviceRegistry = new ServiceRegistry(); + //ExportOM4Util.exportConcepts(serviceRegistry.getOntologyService(), "C:/concepts.xml"); + ExportOM4Util.exportAssertions(serviceRegistry.getWrapper(), "/Users/jurzua/assertions.xml", XMLUtil.INSERT); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/HibernateUtil.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,38 @@ +package org.mpi.openmind.repository.utils; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import org.hibernate.*; +import org.hibernate.cfg.*; + + +/** + * @author Administrator + * + */ +public class HibernateUtil { + + private static final SessionFactory sessionFactory; + + static { + try { + // Create the SessionFactory from hibernate.cfg.xml + AnnotationConfiguration cfg = new AnnotationConfiguration().configure("hibernate.cfg.xml"); + if(System.getProperty("hibernate.hbm2ddl.auto") != null && System.getProperty("hibernate.hbm2ddl.auto").equals("create")){ + cfg.setProperty("hibernate.hbm2ddl.auto", "create"); + } + sessionFactory = cfg.buildSessionFactory(); + } catch (Throwable ex) { + // Make sure you log the exception, as it might be swallowed + System.err.println("Initial SessionFactory creation failed." + ex); + ex.printStackTrace(); + throw new ExceptionInInitializerError(ex); + } + } + + public static SessionFactory getSessionFactory() { + return sessionFactory; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/ImportOM3Util.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,509 @@ +package org.mpi.openmind.repository.utils; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +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.Relation; +import org.mpi.openmind.repository.services.ServiceRegistry; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * + * @author jurzua + */ +public class ImportOM3Util { + + private static Map<String, Entity> assertions; + + public static void importAssertions(WrapperService ontologyService, + String fileName, Boolean dropAssertions) { + Long start = System.currentTimeMillis(); + System.out.println("Importing Assertions: "); + assertions = new HashMap<String, Entity>(); + int entitiesInput = 0; + int entitiesSaved = 0; + DecimalFormat df = new DecimalFormat("#.##"); + + List<Entity> assertions = new ArrayList<Entity>(); + List<ImportEntityRelation> relations = new ArrayList<ImportEntityRelation>(); + Map<String, Entity> conceptCache = new HashMap<String, Entity>(); + Map<Long, Entity> assertionCache = new HashMap<Long, Entity>(); + + try { + if (dropAssertions) { + ontologyService.getPS().dropAssertions(); + } + Document doc = XMLUtil.getDocument(fileName); + NodeList list = doc.getElementsByTagName(XMLUtil.OPENMIND_DATA); + if (list.getLength() > 0) { + Node rootElement = list.item(0); + + // importing entities + Node entsNode = XMLUtil.getNodeByName(rootElement + .getChildNodes(), XMLUtil.ENTITIES); + if (entsNode != null) { + int counter = 0; + for (int i = 0; i < entsNode.getChildNodes().getLength() ; i++) { + counter++; + double percent = ((double) counter / (double) entsNode + .getChildNodes().getLength()) * 100.0; +// if ((counter % 20) == 0) { +// System.out.print("*"); +// } +// if ((percent % 10) < 0.005) { +// System.out.println("\n[" + df.format(percent) +// + " %] counter: " + counter + " / " +// + entsNode.getChildNodes().getLength()); +// } + + Node entNode = entsNode.getChildNodes().item(i); + if (entNode.getNodeName().equals(XMLUtil.ENTITY)) { + entitiesInput++; + Entity assertion = getEntityFromNode( + ontologyService, entNode, conceptCache, + assertionCache); + + if (assertion != null) { + // System.out.println("Assertion built: " + + // ontologyService.saveAssertion(assertion)); + // ontologyService.saveAssertion(assertion); + assertions.add(assertion); + //now the relations come an the end of the file + //getRelationsOfAssertion(entNode, relations); + entitiesSaved++; + // concepts.put(concept.getOwnValue(), concept); + } + } + } +// System.out.println("---------------------------"); + // this call improves the performace of the insertion, + // cause it is executed by a single transaction. + ontologyService.saveEntityList(assertions, "import-om3"); + } + + Node relsNode = XMLUtil.getNodeByName(rootElement + .getChildNodes(), XMLUtil.RELATIONS); + getRelationsOfAssertion(relsNode, relations); + + List<org.mpi.openmind.repository.bo.Node> relationList = + new ArrayList<org.mpi.openmind.repository.bo.Node>(); + for (ImportEntityRelation importRel : relations) { + // iff there exist the target and the source entity, this + // relation will be saved + if (assertionCache.get(importRel.getSourceId()) != null + && assertionCache.get(importRel.getTargetId()) != null) { + Relation relation = new Relation(); + + relation.setOwnValue(importRel.getLabel()); + relation.setSystemStatus(org.mpi.openmind.repository.bo.Node.SYS_STATUS_CURRENT_VERSION); + + Entity src = assertionCache.get(importRel.getSourceId()); + relation.setSourceId(src.getId()); + relation.setSourceModif(src.getModificationTime()); + relation.setSourceObjectClass(src.getObjectClass()); + + Entity tar = assertionCache.get(importRel.getTargetId()); + relation.setTargetId(tar.getId()); + relation.setTargetModif(tar.getModificationTime()); + relation.setTargetObjectClass(tar.getObjectClass()); + relation.setType(org.mpi.openmind.repository.bo.Node.TYPE_ABOX); + + relationList.add(relation); + //System.out.println("Saving: " + importRel); + } else { + System.err + .println("ImportEntityRelation could not be saved." + + importRel); + } + + } + // this second save will make persistent the associations. + //ontologyService.saveEntityList(assertions); + ontologyService.saveNodeListOnlyForScripts(relationList); + } else { + throw new IllegalArgumentException( + "The document is not well formed"); + } + } catch (Exception e) { + e.printStackTrace(); + } + Long end = System.currentTimeMillis(); + Long diff = end - start; + System.out.println("\n###################################"); + System.out.println("Summary Entities Importing:"); + System.out.println("Entities input: " + entitiesInput); + System.out.println("Entities saved: " + entitiesSaved); + System.out.println("Time execution: " + diff / (60 * 60 * 1000) + + "[hours] " + diff / (60 * 1000) + "[min] " + diff / 1000 + + "[sec]"); + System.out.println("###################################\n"); + } + + private static Entity getEntityFromNode(WrapperService ontologyService, + Node node, Map<String, Entity> conceptCache, + Map<Long, Entity> assertionCache) throws Exception { + Entity assertion = null; + Long id = null; + if (node.getAttributes() != null) { + assertion = new Entity( + org.mpi.openmind.repository.bo.Node.TYPE_ABOX, false); + String objectClass = node.getAttributes().getNamedItem( + XMLUtil.OBJECT_CLASS).getNodeValue(); + if (StringUtils.isNotEmpty(objectClass)) { + assertion.setObjectClass(objectClass); + /* + Entity concept = conceptCache.get(objectClass); + if (concept == null) { + concept = ontologyService.getConceptByOwnValue(objectClass); + conceptCache.put(objectClass, concept); + assertion.setObjectClass(concept.getOwnValue()); + } + // concept.setOwnValue(node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS).getNodeValue()); + if (concept == null) { + System.out.println("Concept no found: " + + node.getAttributes().getNamedItem( + XMLUtil.OBJECT_CLASS)); + } else { + assertion.setConcept(concept); + } + */ + } + + String ownValue = node.getAttributes().getNamedItem( + XMLUtil.OWN_VALUE).getNodeValue(); + assertion.setOwnValue(ownValue); + + if (node.getAttributes().getNamedItem("id") != null) { + String stringId = node.getAttributes().getNamedItem("id") + .getNodeValue(); + id = new Long(stringId); + } + + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node nodeAtt = node.getChildNodes().item(i); + if (nodeAtt.getNodeName().equals(XMLUtil.ATTRIBUTES)) { + addAttributesToAssertion(assertion, nodeAtt); + } + // TODO ask MPI, if instance an insertion of ownvalue, here will + // be created a new att. + if (nodeAtt.getNodeName().equals(XMLUtil.ATTRIBUTE_VALUE)) { + assertion.setOwnValue(nodeAtt.getFirstChild() + .getNodeValue()); + } + } + } + if (id != null) { + assertionCache.put(id, assertion); + } + return assertion; + } + + private static void getRelationsOfAssertion(Node node, + List<ImportEntityRelation> relations) { + if (node.getAttributes() != null) { + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node relsNode = node.getChildNodes().item(i); + if (relsNode.getNodeName().equals(XMLUtil.RELATIONS)) { + for (int j = 0; j < relsNode.getChildNodes().getLength(); j++) { + Node relNode = relsNode.getChildNodes().item(j); + String label = null; + String sourceId = null; + String targetId = null; + + if (relNode.getAttributes().getNamedItem(XMLUtil.LABEL) != null) { + label = relNode.getAttributes().getNamedItem( + XMLUtil.LABEL).getNodeValue(); + } + if (relNode.getAttributes().getNamedItem( + XMLUtil.RELATION_SOURCE_ID) != null) { + sourceId = relNode.getAttributes().getNamedItem( + XMLUtil.RELATION_SOURCE_ID).getNodeValue(); + } + if (relNode.getAttributes().getNamedItem( + XMLUtil.RELATION_TARGET_ID) != null) { + targetId = relNode.getAttributes().getNamedItem( + XMLUtil.RELATION_TARGET_ID).getNodeValue(); + } + if (StringUtils.isNotEmpty(label) + && StringUtils.isNotEmpty(sourceId) + && StringUtils.isNotEmpty(targetId)) { + try { + ImportEntityRelation r = new ImportEntityRelation(); + r.setSourceId(new Long(sourceId)); + r.setTargetId(new Long(targetId)); + r.setLabel(label); + relations.add(r); + } catch (Exception e) { + System.err.println(e.getMessage()); + } + } + } + } + } + } + } + + private static Entity addAttributesToAssertion(Entity assertion, + Node nodeAtts) { + if (nodeAtts.getAttributes() != null) { + for (int i = 0; i < nodeAtts.getChildNodes().getLength(); i++) { + Node attNode = nodeAtts.getChildNodes().item(i); + Attribute att = new Attribute(); + + //if(attNode.getFirstChild() != null) + // att.setOwnValue(attNode.getFirstChild().getNodeValue()); + + if(attNode.getAttributes() != null){ + if (attNode.getAttributes().getNamedItem(XMLUtil.ATTRIBUTE_VALUE) != null) { + att.setOwnValue(attNode.getAttributes().getNamedItem( + XMLUtil.ATTRIBUTE_VALUE).getNodeValue()); + } + if (attNode.getAttributes().getNamedItem(XMLUtil.CONTENT_TYPE) != null) { + att.setContentType(attNode.getAttributes().getNamedItem( + XMLUtil.CONTENT_TYPE).getNodeValue()); + } + if (attNode.getAttributes() + .getNamedItem(XMLUtil.ATTRIBUTE_NAME) != null) { + att.setObjectClass(attNode.getAttributes().getNamedItem( + XMLUtil.ATTRIBUTE_NAME).getNodeValue()); + } + } + + assertion.addAttribute(att); + } + } + return assertion; + } + + private static Entity getConceptFromNode(Node node) { + Entity concept = null; + if (node.getAttributes() != null) { + concept = new Entity(); + concept.setType(org.mpi.openmind.repository.bo.Node.TYPE_TBOX); + concept.setObjectClass(org.mpi.openmind.repository.bo.Node.TYPE_TBOX); + if (node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS) != null) { + concept.setOwnValue(node.getAttributes().getNamedItem( + XMLUtil.OBJECT_CLASS).getNodeValue()); + } + /* + if (node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null) { + concept.setOwnValue(node.getAttributes().getNamedItem( + XMLUtil.OWN_VALUE).getNodeValue()); + } + */ + /* + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node nodeAtt = node.getChildNodes().item(i); + if (nodeAtt.getNodeName().equals(XMLUtil.ATTRIBUTES)) { + addAttributesToAssertion(concept, nodeAtt); + } + }*/ + + // if(atts.getNamedItem(XMLUtil.MAIN_LABEL) != null){ + // def.setObjectClass(atts.getNamedItem(XMLUtil.OBJECT_CLASS).getNodeValue()); + // } + + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node labelNode = node.getChildNodes().item(i); + if (labelNode.getNodeName().equals(XMLUtil.LABEL)) { + addAttributeToConcept(concept, labelNode); + } + } + } + return concept; + } + + // ##################################################### + // ##################################################### + // ##################################################### + // ##################################################### + // ##################################################### + // ##################################################### + // ##################################################### + + private static Map<String, Entity> concepts; + + static public void importConcepts(WrapperService ontologyService, + String fileName, Boolean dropConcepts) { + Long start = System.currentTimeMillis(); + System.out.println("Importing Concepts:"); + concepts = new HashMap<String, Entity>(); + + int conceptsInput = 0; + int conceptsSaved = 0; + try { + if (dropConcepts) { + ontologyService.getPS().dropDefinitions(); + } + Document doc = XMLUtil.getDocument(fileName); + NodeList list = doc.getElementsByTagName(XMLUtil.META_DATA); + if (list.getLength() > 0) { + Node rootElement = list.item(0); + + // importing definitions + Node defsNode = XMLUtil.getNodeByName(rootElement + .getChildNodes(), XMLUtil.DEFINITIONS); + if (defsNode != null) { + // conceptsInput = defsNode.getChildNodes().getLength(); + for (int i = 0; i < defsNode.getChildNodes().getLength(); i++) { + Node defNode = defsNode.getChildNodes().item(i); + // System.out.println("\t" + defNode.getNodeName()); + if (defNode.getNodeName().equals(XMLUtil.DEFINITION)) { + conceptsInput++; + Entity concept = getConceptFromNode(defNode); + if (concept != null) { + // System.out.println("Concept built: " + + // ontologyService.saveConcept(concept)); + //ontologyService.saveConcept(concept); + concepts.put(concept.getOwnValue(), concept); + conceptsSaved++; + } + } + } + for(Entity e : concepts.values()){ + ontologyService.saveConcept(e); + } + } + + + // importing relation between defintions + // note: only if definitions were found, has to be imported the + // relations. + + List<org.mpi.openmind.repository.bo.Node> relationList = new ArrayList<org.mpi.openmind.repository.bo.Node>(); + Node relsNode = XMLUtil.getNodeByName(rootElement + .getChildNodes(), XMLUtil.RELATIONS); + if (relsNode != null) { + for (int j = 0; j < relsNode.getChildNodes().getLength(); j++) { + Node nodeRel = relsNode.getChildNodes().item(j); + Relation relation = getRelationFromNode(nodeRel); + if(relation != null) + relationList.add(relation); + } + ontologyService.saveNodeListOnlyForScripts(relationList, "import"); + } + + } else { + throw new IllegalArgumentException( + "The document is not well formed"); + } + } catch (Exception e) { + e.printStackTrace(); + } + Long end = System.currentTimeMillis(); + Long diff = end - start; + System.out.println("###################################"); + System.out.println("Summary Concepts Importing:"); + System.out.println("Concepts input: " + conceptsInput); + System.out.println("Concepts saved: " + conceptsSaved); + System.out.println("Time execution: " + diff / (60 * 60 * 1000) + + "[hours] " + diff / (60 * 1000) + "[min] " + diff / 1000 + + "[sec]"); + System.out.println("###################################"); + } + + private static Relation getRelationFromNode(Node node) { + Relation relation = null; + if (node.getAttributes() != null) { + Entity source = null; + Entity target = null; + String ownValue = null; + + if (node.getAttributes().getNamedItem(XMLUtil.RELATION_SOURCE) != null) { + source = concepts.get(node.getAttributes().getNamedItem( + XMLUtil.RELATION_SOURCE).getNodeValue()); + } + if (node.getAttributes().getNamedItem(XMLUtil.RELATION_TARGET) != null) { + target = concepts.get(node.getAttributes().getNamedItem( + XMLUtil.RELATION_TARGET).getNodeValue()); + } + if (node.getAttributes().getNamedItem(XMLUtil.RELATION_LABEL) != null) { + ownValue = node.getAttributes().getNamedItem( + XMLUtil.RELATION_LABEL).getNodeValue(); + } + + if (source != null && target != null + && StringUtils.isNotEmpty(ownValue)) { + relation = new Relation(); + relation.setSource(source); + relation.setTarget(target); + relation.setOwnValue(ownValue); + source.addSourceRelation(relation); + } + + } + return relation; + } + + private static Entity addAttributeToConcept(Entity concept, Node label) { + if (label.getAttributes() != null) { + Attribute att = new Attribute(); + if (label.getAttributes().getNamedItem(XMLUtil.LABEL_NAME) != null) { + att.setOwnValue(label.getAttributes().getNamedItem( + XMLUtil.LABEL_NAME).getNodeValue()); + } + if (label.getAttributes().getNamedItem(XMLUtil.CONTENT_TYPE) != null) { + att.setContentType(label.getAttributes().getNamedItem( + XMLUtil.CONTENT_TYPE).getNodeValue()); + } + concept.addAttribute(att); + } + return concept; + } + + public static class ImportEntityRelation { + String label; + Long sourceId; + Long targetId; + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public Long getSourceId() { + return sourceId; + } + + public void setSourceId(Long sourceId) { + this.sourceId = sourceId; + } + + public Long getTargetId() { + return targetId; + } + + public void setTargetId(Long targetId) { + this.targetId = targetId; + } + + @Override + public String toString() { + return sourceId + " <" + label + "> " + targetId; + } + } + + public static void main(String[] args) { + ServiceRegistry registry = new ServiceRegistry(); + registry.getPS().setImportModus(true); + //importAssertions(registry.getOntologyService(), "/Users/jurzua/Projects/max-planck/openmind4/trunk/data/ismi-data-100414/country-colated-om4.xml", false); + //importAssertions(registry.getOntologyService(), "/Users/jurzua/Projects/max-planck/openmind4/trunk/data/ismi-data-100414/output2.xml", false); + importConcepts(registry.getWrapper(), "/Users/jurzua/Projects/max-planck/openmind4/trunk/data/ismi-data-100414/definitions-om3.xml", true); + //importAssertions(registry.getOntologyService(), "/Users/jurzua/2010.10.14[06.00]-ENT.xml", true); + registry.getPS().setImportModus(false); + System.exit(0); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/ImportOM4Util.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,725 @@ +package org.mpi.openmind.repository.utils; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Appender; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; +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.Relation; +import org.mpi.openmind.repository.bo.View; +import org.mpi.openmind.repository.services.ServiceRegistry; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * <p> + * Import assertions and concepts from a file, which should be compatible with + * openmind 4 + * </p> + * + * @author Jorge Urzua + */ +public class ImportOM4Util { + + private static Logger logger = Logger.getLogger(ImportOM4Util.class); + + static{ + logger.setLevel(Level.DEBUG); + PatternLayout layout = new PatternLayout("%m%n"); + Appender stdout = new ConsoleAppender(layout, "System.out"); + logger.addAppender(stdout); + } + + public static void importAssertions(WrapperService ontology, + String fileName, Boolean dropAssertions) { + + DecimalFormat df = new DecimalFormat("#.##"); + Long start = System.currentTimeMillis(); + String insertionMode = XMLUtil.INSERT; + int newRels = 0; + int newAsser = 0; + try { + if (dropAssertions) { + ontology.getPS().dropAssertions(); + // this.om.dropAllDefinitions(); + } + + Document doc = XMLUtil.getDocument(fileName); + NodeList list = doc.getElementsByTagName(XMLUtil.OPENMIND_DATA); + + if (list.getLength() > 0) { + Node rootElement = list.item(0); + if(rootElement.getAttributes() != null){ + //insertionMode = (rootElement.getAttributes().getNamedItem(XMLUtil.INSERTION_MODE).getNodeValue() != null) ? + // rootElement.getAttributes().getNamedItem(XMLUtil.INSERTION_MODE).getNodeValue() : insertionMode; + } + // importing entities + + //Map<old ID, persistent Entity> + Map<Long, Entity> persistentAssertions = new HashMap<Long, Entity>(); + //Map<old ID, transient Entity> + Map<Long, Entity> transientAssertions = new HashMap<Long, Entity>(); + //Map<Long, Entity> xmlAssertions = new HashMap<Long, Entity>(); + + Node assertionsNode = XMLUtil.getNodeByName(rootElement.getChildNodes(), /*XMLUtil.ASSERTIONS*/XMLUtil.ENTITIES); + if (assertionsNode != null) { + + //assertions generated from xml input + Map<Long, Entity> xmlAssertions = getAllAssertionsFromNode(assertionsNode); + + //relations generated from xml input + List<org.mpi.openmind.repository.bo.Node> xmlRelations = getAllRelationFromNode(XMLUtil.getNodeByName(rootElement.getChildNodes(), XMLUtil.RELATIONS)); + + int counter = 0; + for(Entity xmlAssert : xmlAssertions.values()){ + counter++; + System.out.println("[" + counter + "/" + xmlAssertions.size() + "]\tInserting= " + xmlAssert); + double percent = ((double) counter / (double) xmlAssertions.size()) * 100.0; + + List<Entity> foundAssertions = new ArrayList<Entity>(); + if(xmlAssert.getOwnValue().equals("unknown city is part of unknown country")){ + System.out.println("unknown city is part of unknown country"); + } + if(xmlAssert.getObjectClass().equals("REPOSITORY")){ + foundAssertions = ontology.getLightweightAssertionsByExactOwnValue("REPOSITORY", xmlAssert.getOwnValue(), 1); + }else if(xmlAssert.getObjectClass().equals("PLACE")){ + foundAssertions = ontology.getLightweightAssertionsByExactOwnValue("PLACE", xmlAssert.getOwnValue(), 1); + }else if(xmlAssert.getAttributes().size() < 2){ + //this call damages the performance depending on the attributes amount + //therefore its att's amount is limited to 3 + //foundAssertions = ontology.getPS().searchEntity(xmlAssert, false, 1); + } + + boolean foundSameEnt = false; + Entity foundAssertion = null; + + if(foundAssertions.size() > 0){ + foundAssertion = foundAssertions.get(0); + + //############################################################################################ + //its necessary specific rules for the entities: CODEX, COLLECTION and PLACE(type=city) + //the following code is valid only for ISMI's Data Model + + if(xmlAssert.getObjectClass().equals("CODEX")){ + //testing CODEX -> is_part_of -> COLLECTION -> is_in -> PLACE(city) + Relation codexToColl = getSourceRelationByName(xmlAssert.getId(), "is_part_of", xmlRelations); + Entity coll = (codexToColl == null) ? null : xmlAssertions.get(codexToColl.getTargetId()); + + List<Entity> codexList0 = ontology.getPS().getTargetsForSourceRelation(foundAssertion, "is_part_of", "COLLECTION", 1); + Entity coll0 = (codexList0.size() > 0) ? codexList0.get(0) : null; + + if(coll != null && coll0 != null && coll.getOwnValue().equals(coll0.getOwnValue())){ + Relation collToRepo = getSourceRelationByName(coll.getId(), "is_part_of", xmlRelations); + Entity repo = (collToRepo == null) ? null : xmlAssertions.get(collToRepo.getTargetId()); + + List<Entity> repoList0 = ontology.getPS().getTargetsForSourceRelation(coll0, "is_part_of", "REPOSITORY", 1); + Entity repo0 = (repoList0.size() > 0) ? repoList0.get(0) : null; + + if(repo != null && repo0 != null && repo.getOwnValue().equals(repo0.getOwnValue())){ + Relation repoToCity = getSourceRelationByName(repo.getId(), "is_in", xmlRelations); + Entity city = (repoToCity == null) ? null : xmlAssertions.get(repoToCity.getTargetId()); + Attribute type = (city == null) ? null : city.getAttributeByName("type"); + + if(type != null && type.getValue().equals("city")){ + List<Entity> cityList0 = ontology.getPS().getTargetsForSourceRelation(repo0, "is_in", "PLACE", 1); + Entity city0 = (cityList0.size() > 0 ) ? cityList0.get(0) : null; + Attribute type0 = (city0 == null) ? null : ontology.getPS().getAttributeByName(city0, "type"); + + if(type0 != null && type0.getValue().equals("city") && city.getOwnValue().equals(city0.getOwnValue())){ + foundSameEnt = true; + } + } + } + } + + } else if(xmlAssert.getObjectClass().equals("COLLECTION")){ + // testing COLLECTION -> is_part_of -> REPOSITORY -> is_in -> PLACE(city) -> is_part_of -> PLACE (country) :s + Relation collToRepo = getSourceRelationByName(xmlAssert.getId(), "is_part_of", xmlRelations); + Entity repo = (collToRepo == null) ? null : xmlAssertions.get(collToRepo.getTargetId()); + + List<Entity> repoList0 = ontology.getPS().getTargetsForSourceRelation(foundAssertion, "is_part_of", "REPOSITORY", 1); + Entity repo0 = (repoList0.size() > 0) ? repoList0.get(0) : null; + + if(repo != null && repo0 != null && repo.getOwnValue().equals(repo0.getOwnValue())){ + Relation repoToCity = getSourceRelationByName(repo.getId(), "is_in", xmlRelations); + Entity city = (repoToCity == null) ? null : xmlAssertions.get(repoToCity.getTargetId()); + Attribute type = (city == null) ? null : city.getAttributeByName("type"); + + if(type != null && type.getValue().equals("city")){ + List<Entity> cityList0 = ontology.getPS().getTargetsForSourceRelation(repo0, "is_in", "PLACE", 1); + Entity city0 = (cityList0.size() > 0 ) ? cityList0.get(0) : null; + Attribute type0 = (city0 == null) ? null : ontology.getPS().getAttributeByName(city0, "type"); + + if(type0 != null && type0.getValue().equals("city") && city.getOwnValue().equals(city0.getOwnValue())){ + foundSameEnt = true; + } + } + } + + }else{ + foundSameEnt = true; + } + //############################################################################################ + //############################################################################################ + } + + if(foundSameEnt){ + if(xmlAssert.getId().intValue() == 302 || xmlAssert.getId().intValue() == 301) + System.out.println(xmlAssert.getId()); + foundAssertion = ontology.getEntityContent(foundAssertion); + persistentAssertions.put(xmlAssert.getId(), foundAssertion); + logger.debug("[" + df.format(percent) + "%]found: " + foundAssertions.size() + " for " + xmlAssert); + }else { + newAsser++; + transientAssertions.put(new Long(xmlAssert.getId()), xmlAssert); + } + } + + //The loop through the xml's assertion produced to list: + // a list with persistent assertions, because similar assertion were found in the DB + // a list of transient assertions, which were not found in DB + // to save the relations, the transient entities should be done persistent before + + List<Entity> transientList = new ArrayList<Entity>(); + for(Entity ent : transientAssertions.values()){ + ent.resetId(); + transientList.add(ent); + } + + + System.out.println("##################################"); + System.out.println("Starting Entities Persistence"); + System.out.println("##################################"); + + ontology.saveEntityList(transientList, "imported"); + + //now all transient assertions are put into the persistent list + for(Long id : transientAssertions.keySet()){ + Entity ent = transientAssertions.get(id); + persistentAssertions.put(id, ent); + } + + System.out.println("##################################"); + System.out.println("Entities Saved, starting relations import"); + System.out.println("##################################"); + + //loading the relations + Map<Long, org.mpi.openmind.repository.bo.Node> relToDel = new HashMap<Long, org.mpi.openmind.repository.bo.Node>(); + List<org.mpi.openmind.repository.bo.Node> transientRels = new ArrayList<org.mpi.openmind.repository.bo.Node>(); + for(org.mpi.openmind.repository.bo.Node node : xmlRelations){ + Relation rel = (Relation)node; + Entity src = persistentAssertions.get(rel.getSourceId()); + Entity tar = persistentAssertions.get(rel.getTargetId()); + + if(src.getOwnValue().equals("unknown city is part of unknown country")){ + System.out.println("unknown city is part of unknown country"); + } + + boolean existRelation = false; + if(src.getObjectClass().equals("CODEX")){ + //looking at the DB + List<Entity> l0 = ontology.getPS().getTargetsForSourceRelation(src, "is_part_of", "COLLECTION", 1); + existRelation = (l0.size() > 0) ? true : false; + //looking at the xml relations + if(!existRelation){ + if(getSourceRelationByName(src.getId(), "is_part_of", transientRels) != null) + existRelation = true; + } + }else if(src.getObjectClass().equals("COLLECTION")){ + List<Entity> l0 = ontology.getPS().getTargetsForSourceRelation(src, "is_part_of", "REPOSITORY", 1); + existRelation = (l0.size() > 0) ? true : false; + //looking at the xml relations + if(!existRelation){ + if(getSourceRelationByName(src.getId(), "is_part_of", transientRels) != null) + existRelation = true; + } + }else if(src.getObjectClass().equals("REPOSITORY")){ + List<Relation> l0 = ontology.getPS().getSourceRelationsFromDB(src, "is_in", "PLACE", 1, true); + //existRelation = (l0.size() > 0) ? true : false; + if(l0.size() > 0) + relToDel.put(l0.get(0).getId(), l0.get(0)); + //looking at the xml relations + if(!existRelation){ + if(getSourceRelationByName(src.getId(), "is_in", transientRels) != null) + existRelation = true; + } + }else if(src.getObjectClass().equals("PLACE")){ + Attribute att = ontology.getPS().getAttributeByName(src, "type"); + if(att != null && att.getValue().equals("city")){ + List<Relation> l0 = ontology.getPS().getSourceRelationsFromDB(src, "is_part_of", "PLACE", 1, true); + //existRelation = (l0.size() > 0) ? true : false; + if(l0.size() > 0) + relToDel.put(l0.get(0).getId(), l0.get(0)); + //looking at the xml relations + if(!existRelation){ + if(getSourceRelationByName(src.getId(), "is_part_of", transientRels) != null) + existRelation = true; + } + } + } + + //TODO a general method to detect repeated relations. Until now there is a special method for the ismi data model. + /* + Relation foundRelation = null; + for(Relation srcRel : src.getSourceRelations()){ + if(srcRel.getOwnValue().equals(rel.getOwnValue()) && srcRel.getTargetId().compareTo(tar.getId()) == 0){ + existRel = true; + foundRelation = srcRel; + break; + } + }*/ + + if(src != null && tar != null){ + if(!existRelation/*insertionMode.equals(XMLUtil.INSERT) || (!existRel && insertionMode.equals(XMLUtil.MERGE)) */){ + newRels++; + rel.setSystemStatus(org.mpi.openmind.repository.bo.Node.SYS_STATUS_CURRENT_VERSION); + + rel.setSource(src); + rel.setSourceId(src.getId()); + rel.setSourceModif(src.getModificationTime()); + rel.setSourceObjectClass(src.getObjectClass()); + + rel.setTarget(tar); + rel.setTargetId(tar.getId()); + rel.setTargetModif(tar.getModificationTime()); + rel.setTargetObjectClass(tar.getObjectClass()); + rel.setType(org.mpi.openmind.repository.bo.Node.TYPE_ABOX); + + long time = (src.getModificationTime() > tar.getModificationTime()) ? src.getModificationTime() : tar.getModificationTime(); + rel.setModificationTime(time); + src.addSourceRelation(rel); + tar.addTargetRelation(rel); + transientRels.add(rel); + //logger.debug(rel); + //logger.debug("Source[" + rel.getSourceId() + "] " + src); + //logger.debug("Target[" + rel.getTargetId() + "] " + tar); + System.out.println(); + } + } + } + + System.out.println("##################################"); + System.out.println("Starting Relation Persistence"); + System.out.println("##################################"); + + long start0 = System.currentTimeMillis(); + ontology.saveNodeListOnlyForScripts(transientRels, "imported"); + System.out.println("rel saved! time=" + (System.currentTimeMillis() - start0)); + List<org.mpi.openmind.repository.bo.Node> listToDel = new ArrayList<org.mpi.openmind.repository.bo.Node>(); + for(org.mpi.openmind.repository.bo.Node nodeToDel : relToDel.values()){ + listToDel.add(nodeToDel); + } + ontology.removeNodeList(listToDel); + + System.out.println("##################################"); + System.out.println("The End"); + System.out.println("##################################"); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("\n###################################"); + System.out.println("Summary Entities Importing:"); + System.out.println("New Assertions: " + newAsser); + System.out.println("New Relations: " + newRels); + long diff = System.currentTimeMillis() - start; + int hours = (int)((double) diff / (double) (60 * 60 * 1000)); + int min = (int)((double) diff / (double) (60 * 1000)); + double sec = diff / 1000; + int diffMin = min - (hours * 60); + int diffSec = (int)sec - (min * 60); + + System.out.println("Time execution: : " + hours + ":" + diffMin + ":" + diffSec + " [hh:min:sec]"); + System.out.println("###################################\n"); + } + + private static Map<Long, Entity> getAllAssertionsFromNode(Node assertionsNode){ + Map<Long, Entity> entities = new HashMap<Long, Entity>(); + if (assertionsNode != null) { + for (int i = 0; i < assertionsNode.getChildNodes().getLength() ; i++) { + Node assertionNode = assertionsNode.getChildNodes().item(i); + if(assertionNode instanceof Element){ + if (assertionNode.getNodeName().equals(/*XMLUtil.ASSERTION*/XMLUtil.ENTITY)) { + Entity assertion = getAssertionFromNode(assertionNode); + entities.put(assertion.getId(), assertion); + } + } + } + } + return entities; + } + + private static Relation getTargetRelationByName(Entity ent, String name, List<Relation> allRels){ + for(Relation rel : allRels){ + if(rel.getTargetId().equals(ent.getId()) && rel.getOwnValue().equals(name)){ + return rel; + } + } + return null; + } + + private static Relation getSourceRelationByName(Long oldId, String name, List<org.mpi.openmind.repository.bo.Node> allRels){ + for(org.mpi.openmind.repository.bo.Node node : allRels){ + Relation rel = (Relation)node; + if(rel.getSourceId().equals(oldId) && rel.getOwnValue().equals(name)){ + return rel; + } + } + return null; + } + + private static List<org.mpi.openmind.repository.bo.Node> getAllRelationFromNode(Node relsNode){ + List<org.mpi.openmind.repository.bo.Node> relList = new ArrayList<org.mpi.openmind.repository.bo.Node>(); + if(relsNode != null){ + for (int i = 0; i < relsNode.getChildNodes().getLength() ; i++) { + Node relNode = relsNode.getChildNodes().item(i); + if(relNode instanceof Element){ + if (relNode.getNodeName().equals(XMLUtil.RELATION)) { + Relation rel = getRelationFromNode(relNode); + relList.add((org.mpi.openmind.repository.bo.Node)rel); + } + } + } + } + return relList; + } + + private static Relation getRelationFromNode(Node node){ + Relation rel = null; + if(node.getAttributes() != null){ + String ownValue = null; + String srcId = null; + String tarId = null; + + if(node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null) + ownValue = node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE).getNodeValue(); + if(node.getAttributes().getNamedItem(XMLUtil.RELATION_SOURCE_ID) != null) + srcId = node.getAttributes().getNamedItem(XMLUtil.RELATION_SOURCE_ID).getNodeValue(); + if(node.getAttributes().getNamedItem(XMLUtil.RELATION_TARGET_ID) != null) + tarId = node.getAttributes().getNamedItem(XMLUtil.RELATION_TARGET_ID).getNodeValue(); + + if(StringUtils.isEmpty(ownValue) || StringUtils.isEmpty(tarId) || StringUtils.isEmpty(srcId)){ + return null; + } + + rel = new Relation(); + rel.setOwnValue(ownValue); + rel.setSourceId(new Long(srcId)); + rel.setTargetId(new Long(tarId)); + + } + return rel; + } + + private static Entity getAssertionFromNode(Node node/*, Map<Long, Entity> xmlAssertions*/){ + Entity assertion = null; + if (node.getAttributes() != null) { + + if(node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS) != null){ + assertion = new Entity(org.mpi.openmind.repository.bo.Node.TYPE_ABOX, false); + assertion.setObjectClass( node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS).getNodeValue()); + + if(node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null){ + assertion.setOwnValue(node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE).getNodeValue()); + } + + if(node.getAttributes().getNamedItem("id") != null) { + Long id = new Long(node.getAttributes().getNamedItem("id").getNodeValue()); + assertion.setId(id); + //xmlAssertions.put(id, assertion); + } + + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node nodeAtt = node.getChildNodes().item(i); + if (nodeAtt.getNodeName().equals(XMLUtil.ATTRIBUTES)) { + addAttributesToAssertion(assertion, nodeAtt); + } + } + } + } + + return assertion; + } + + /** + * TODO add Views + * @param assertion + * @param nodeAtts + * @return + */ + private static Entity addAttributesToAssertion(Entity assertion, Node nodeAtts) { + if (nodeAtts.getAttributes() != null) { + for (int i = 0; i < nodeAtts.getChildNodes().getLength(); i++) { + Node attNode = nodeAtts.getChildNodes().item(i); + if(attNode instanceof Element){ + Attribute att = new Attribute(); + + if (attNode.getAttributes().getNamedItem(XMLUtil.CONTENT_TYPE) != null) { + att.setContentType(attNode.getAttributes().getNamedItem(XMLUtil.CONTENT_TYPE).getNodeValue()); + } + if (attNode.getAttributes().getNamedItem(XMLUtil.ATTRIBUTE_NAME) != null) { + att.setName(attNode.getAttributes().getNamedItem(XMLUtil.ATTRIBUTE_NAME).getNodeValue()); + } + if (attNode.getAttributes().getNamedItem(XMLUtil.ATTRIBUTE_VALUE) != null) { + att.setValue(attNode.getAttributes().getNamedItem(XMLUtil.ATTRIBUTE_VALUE).getNodeValue()); + } + assertion.addAttribute(att); + } + } + } + return assertion; + } + + + public static void importConcepts(WrapperService ontologyService, + String fileOutput, Boolean dropConcepts) { + Long start = System.currentTimeMillis(); + + System.out.println("Importing Concepts:"); + Map<String, Entity> conceptMap = new HashMap<String, Entity>(); + int conceptsInput = 0; + int conceptsSaved = 0; + try { + if (dropConcepts) { + ontologyService.deleteAllConcepts(); + } + Document doc = XMLUtil.getDocument(fileOutput); + NodeList list = doc.getElementsByTagName(XMLUtil.META_DATA); + if (list.getLength() > 0) { + Node rootElement = list.item(0); + + // importing definitions + Node defsNode = XMLUtil.getNodeByName(rootElement.getChildNodes(), /*XMLUtil.CONCEPTS*/ XMLUtil.DEFINITIONS); + if (defsNode != null) { + for (int i = 0; i < defsNode.getChildNodes().getLength(); i++) { + Node conceptNode = defsNode.getChildNodes().item(i); + if (conceptNode.getNodeName().equals(/*XMLUtil.CONCEPT*/XMLUtil.DEFINITION)) { + conceptsInput++; + Entity concept = getConceptFromNode(conceptNode, + conceptMap); + if (concept != null) { + ontologyService.saveConcept(concept); + conceptsSaved++; + } + } + } + } + + // importing relation between defintions + // note: only if definitions were found, has to be imported the + // relations. + Node relsNode = XMLUtil.getNodeByName(rootElement.getChildNodes(), XMLUtil.RELATIONS); + if (relsNode != null) { + for (int j = 0; j < relsNode.getChildNodes().getLength(); j++) { + Node nodeRel = relsNode.getChildNodes().item(j); + if (nodeRel.getNodeName().equals(XMLUtil.RELATION)) { + Relation rel = getRelationFromNode(nodeRel, conceptMap); + ontologyService.saveNodeOnlyForScripts(rel, "restore"); + } + } + } + + // this new makePersistent is needing, because the relations + // were + // associated to the corresponding entities, which have to be + // updated. + for (Entity entity : conceptMap.values()) { + ontologyService.saveConcept(entity); + } + } else { + throw new IllegalArgumentException( + "The document is not well formed"); + } + } catch (Exception e) { + e.printStackTrace(); + } + Long end = System.currentTimeMillis(); + Long diff = end - start; + System.out.println("###################################"); + System.out.println("Summary Concepts Importing:"); + System.out.println("Concepts input: " + conceptsInput); + System.out.println("Concepts saved: " + conceptsSaved); + System.out.println("Time execution: " + diff / (60 * 60 * 1000) + + "[hours] " + diff / (60 * 1000) + "[min] " + diff / 1000 + + "[sec]"); + System.out.println("###################################"); + } + + private static Relation getRelationFromNode(Node node, Map<String, Entity> conceptMap){ + /// + Relation rel = null; + if(node.getAttributes() != null){ + rel = new Relation(); + + //rel.setType(org.mpi.openmind.repository.bo.Node.TYPE_TBOX); + // concept.setObjectClass(org.mpi.openmind.repository.bo.Node.TYPE_TBOX); + + if(node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null && + node.getAttributes().getNamedItem(XMLUtil.RELATION_SOURCE_ID) != null && + node.getAttributes().getNamedItem(XMLUtil.RELATION_TARGET_ID) != null){ + String ownValue = node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE).getNodeValue(); + String sourceId = node.getAttributes().getNamedItem(XMLUtil.RELATION_SOURCE_ID).getNodeValue(); + String targetId = node.getAttributes().getNamedItem(XMLUtil.RELATION_TARGET_ID).getNodeValue(); + + rel = new Relation(conceptMap.get(sourceId), conceptMap.get(targetId), ownValue); + + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node childNode = node.getChildNodes().item(i); +// TODO +// if (childNode.getNodeName().equals(XMLUtil.ATTRIBUTES)) { +// rel.setAttributes(getAttributesFromNode(childNode)); +// } + if (childNode.getNodeName().equals(XMLUtil.VIEWS)) { + rel.setViews(getViewsFromNode(childNode)); + } + } + } + + /* + if (node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null) + rel.setOwnValue(node.getAttributes().getNamedItem( + XMLUtil.OWN_VALUE).getNodeValue()); + + if (node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS) != null) + rel.setObjectClass(node.getAttributes().getNamedItem( + XMLUtil.OBJECT_CLASS).getNodeValue()); + + + if (node.getAttributes().getNamedItem(XMLUtil.RELATION_SOURCE_ID) != null){ + String sourceId = node.getAttributes().getNamedItem(XMLUtil.RELATION_SOURCE_ID).getNodeValue(); + rel.setSourceId(conceptMap.get(sourceId).getId()); + //here is added the relation into the map, so when this map is made persistence, the relation will be saved. + conceptMap.get(sourceId).addSourceRelation(rel); + } + + if (node.getAttributes().getNamedItem(XMLUtil.RELATION_TARGET_ID) != null){ + String targetId = node.getAttributes().getNamedItem(XMLUtil.RELATION_TARGET_ID).getNodeValue(); + rel.setTargetId(conceptMap.get(targetId).getId()); + }*/ + + + } + return rel; + } + + private static Entity getConceptFromNode(Node node, + Map<String, Entity> conceptMap) { + Entity concept = null; + if (node.getAttributes() != null) { + concept = new Entity(); + + concept.setType(org.mpi.openmind.repository.bo.Node.TYPE_TBOX); + // concept.setObjectClass(org.mpi.openmind.repository.bo.Node.TYPE_TBOX); + + // the concept and its id are saved in the map. + // It is necessary to the creation of relations between concepts + if (node.getAttributes().getNamedItem(XMLUtil.ID) != null) + conceptMap.put(node.getAttributes().getNamedItem( + XMLUtil.ID).getNodeValue(), concept); + + if (node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null) + concept.setOwnValue(node.getAttributes().getNamedItem( + XMLUtil.OWN_VALUE).getNodeValue()); + + if (node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS) != null) + concept.setObjectClass(node.getAttributes().getNamedItem( + XMLUtil.OBJECT_CLASS).getNodeValue()); + + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node childNode = node.getChildNodes().item(i); + if (childNode.getNodeName().equals(XMLUtil.ATTRIBUTES)) { + concept.setAttributes(getAttributesFromNode(childNode)); + } + if (childNode.getNodeName().equals(XMLUtil.VIEWS)) { + concept.setViews(getViewsFromNode(childNode)); + } + } + } + return concept; + } + + private static List<Attribute> getAttributesFromNode(Node node) { + List<Attribute> list = new ArrayList<Attribute>(); + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node childNode = node.getChildNodes().item(i); + if (childNode.getNodeName().equals(XMLUtil.ATTRIBUTE)) { + list.add(getAttributeFromNode(childNode)); + } + } + return list; + } + + private static List<View> getViewsFromNode(Node node){ + List<View> list = new ArrayList<View>(); + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node childNode = node.getChildNodes().item(i); + if (childNode.getNodeName().equals(XMLUtil.VIEW)) { + list.add(getViewFromNode(childNode)); + } + } + return list; + } + + private static Attribute getAttributeFromNode(Node node) { + if (node.getAttributes() != null) { + Attribute att = new Attribute(); + if (node.getAttributes().getNamedItem(XMLUtil.VALUE) != null) + att.setOwnValue(node.getAttributes().getNamedItem( + XMLUtil.VALUE).getNodeValue()); + + if (node.getAttributes().getNamedItem(XMLUtil.NAME) != null) + att.setObjectClass(node.getAttributes().getNamedItem( + XMLUtil.NAME).getNodeValue()); + + for (int i = 0; i < node.getChildNodes().getLength(); i++) { + Node childNode = node.getChildNodes().item(i); + if (childNode.getNodeName().equals(XMLUtil.VIEWS)) { + att.addView(getViewFromNode(childNode)); + } + } + return att; + } + return null; + } + + private static View getViewFromNode(Node node) { + if (node.getAttributes() != null) { + View view = new View(); + if (node.getAttributes().getNamedItem(XMLUtil.OWN_VALUE) != null) + view.setOwnValue(node.getAttributes().getNamedItem( + XMLUtil.OWN_VALUE).getNodeValue()); + + if (node.getAttributes().getNamedItem(XMLUtil.OBJECT_CLASS) != null) + view.setObjectClass(node.getAttributes().getNamedItem( + XMLUtil.OBJECT_CLASS).getNodeValue()); + return view; + } + return null; + } + + public static void main(String[] args) { + // BACKUP FROM EULER SHOULD BE HERE IMPORTED!!!! + ServiceRegistry registry = new ServiceRegistry(); + registry.getPS().setImportModus(true); + //importAssertions(registry.getOntologyService(), "/Users/jurzua/Projects/max-planck/openmind4/trunk/data/ismi-data-100414/country-colated-om4.xml", false); + //importAssertions(registry.getOntologyService(), "/Users/jurzua/Projects/max-planck/openmind4/trunk/data/ismi-data-100414/output2.xml", false); + importConcepts(registry.getWrapper(), "/Users/jurzua/Projects/max-planck/openmind4/trunk/data/ismi-data-100414/2010.11.10[06.00]-DEF.xml", true); + //importAssertions(registry.getOntologyService(), "/Users/jurzua/2010.10.14[06.00]-ENT.xml", true); + registry.getPS().setImportModus(false); + System.exit(0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/JDBCUtils.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,154 @@ +package org.mpi.openmind.repository.utils; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.cfg.AnnotationConfiguration; +import org.mpi.openmind.repository.bo.Attribute; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Relation; + +public class JDBCUtils { + + public static Connection getConn() throws SQLException, + IllegalAccessException, ClassNotFoundException, + java.lang.InstantiationException { + AnnotationConfiguration cfg = new AnnotationConfiguration() + .configure("hibernate.cfg.xml"); + String userName = (String) cfg.getProperties().get( + "hibernate.connection.username"); + String pwd = (String) cfg.getProperties().get( + "hibernate.connection.password"); + String url = (String) cfg.getProperties().get( + "hibernate.connection.url"); + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + return DriverManager.getConnection(url, userName, pwd); + } + + /* +rs.getString("node_type"); + rs.getString("row_id"); + rs.getString("binary_value"); + rs.getString("content_type"); + rs.getString("id"); + rs.getString("long_value"); + rs.getString("modification_time"); + rs.getString("normalized_own_value"); + rs.getString("object_class"); + rs.getString("own_value"); + rs.getString("status"); + rs.getString("system_status"); + rs.getString("type"); + rs.getString("version"); + rs.getString("source_id"); + rs.getString("source_modif"); + rs.getString("source_obj_class"); + rs.getString("target_id"); + rs.getString("target_modif"); + rs.getString("target_obj_class"); + rs.getString("user"); + rs.getString("possible_value"); + rs.getString("public"); + */ + + public static List<Relation> rs2Rels(ResultSet rs) throws SQLException { + List<Relation> list = new ArrayList<Relation>(); + while (rs.next()) { + Relation rel = new Relation(); + //rs.getString("node_type") discriminator + rel.setRowId(rs.getLong("row_id")); + rel.setBinaryValue(rs.getBytes("binary_value")); + rel.setContentType(rs.getString("content_type")); + rel.setId(rs.getLong("id")); + rel.setLongValue(rs.getLong("long_value")); + rel.setModificationTime(rs.getLong("modification_time")); + rel.setNormalizedOwnValue(rs.getString("normalized_own_value")); + rel.setObjectClass(rs.getString("object_class")); + rel.setOwnValue(rs.getString("own_value")); + rel.setStatus(rs.getString("status")); + rel.setSystemStatus(rs.getString("system_status")); + rel.setType(rs.getString("type")); + rel.setVersion(rs.getLong("version")); + rel.setSourceId(rs.getLong("source_id")); + rel.setSourceModif(rs.getLong("source_modif")); + rel.setSourceObjectClass(rs.getString("source_obj_class")); + rel.setTargetId(rs.getLong("target_id")); + rel.setTargetModif(rs.getLong("target_modif")); + rel.setTargetObjectClass(rs.getString("target_obj_class")); + rel.setUser(rs.getString("user")); + //rel.setPossibleValues(rs.getString("possible_value")); + rel.setIsPublic(rs.getBoolean("public")); + } + return list; + } + + + public static List<Attribute> rs2Atts(ResultSet rs) throws SQLException { + List<Attribute> list = new ArrayList<Attribute>(); + while (rs.next()) { + Attribute att = new Attribute(); + //rs.getString("node_type") discriminator + att.setRowId(rs.getLong("row_id")); + att.setBinaryValue(rs.getBytes("binary_value")); + att.setContentType(rs.getString("content_type")); + att.setId(rs.getLong("id")); + att.setLongValue(rs.getLong("long_value")); + att.setModificationTime(rs.getLong("modification_time")); + att.setNormalizedOwnValue(rs.getString("normalized_own_value")); + att.setObjectClass(rs.getString("object_class")); + att.setOwnValue(rs.getString("own_value")); + att.setStatus(rs.getString("status")); + att.setSystemStatus(rs.getString("system_status")); + att.setType(rs.getString("type")); + att.setVersion(rs.getLong("version")); + att.setSourceId(rs.getLong("source_id")); + att.setSourceModif(rs.getLong("source_modif")); + att.setSourceObjectClass(rs.getString("source_obj_class")); + //rs.getString("target_id"); + //rs.getString("target_modif"); + //rs.getString("target_obj_class"); + att.setUser(rs.getString("user")); + att.setPossibleValues(rs.getString("possible_value")); + att.setIsPublic(rs.getBoolean("public")); + } + return list; + } + + public static List<Entity> rs2Ents(ResultSet rs) throws SQLException { + List<Entity> list = new ArrayList<Entity>(); + while (rs.next()) { + Entity ent = new Entity(); + ent.setLightweight(true); + //rs.getString("node_type") discriminator + ent.setRowId(rs.getLong("row_id")); + ent.setBinaryValue(rs.getBytes("binary_value")); + ent.setContentType(rs.getString("content_type")); + ent.setId(rs.getLong("id")); + ent.setLongValue(rs.getLong("long_value")); + ent.setModificationTime(rs.getLong("modification_time")); + ent.setNormalizedOwnValue(rs.getString("normalized_own_value")); + ent.setObjectClass(rs.getString("object_class")); + ent.setOwnValue(rs.getString("own_value")); + ent.setStatus(rs.getString("status")); + ent.setSystemStatus(rs.getString("system_status")); + ent.setType(rs.getString("type")); + ent.setVersion(rs.getLong("version")); + //rs.getString("source_id") + //rs.getString("source_modif"); + //rs.getString("source_obj_class"); + //rs.getString("target_id"); + //rs.getString("target_modif"); + //rs.getString("target_obj_class"); + ent.setUser(rs.getString("user")); + //rs.getString("possible_value"); + ent.setIsPublic(rs.getBoolean("public")); + } + return list; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/NormalizerUtils.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,288 @@ +package org.mpi.openmind.repository.utils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; + +public class NormalizerUtils { + + public static Map<String, List<String>> wildCardStringMap = new HashMap<String, List<String>>(); + + static{ + List<String> list; + StringBuilder sb = new StringBuilder(); + + list = new ArrayList<String>(); + Character c = 0x1E6F; + sb.append(c); + list.add(sb.toString());//ṯ + list.add("th"); + wildCardStringMap.put("T", list); + + list = new ArrayList<String>(); + c = 0x1E2b; + list.add(c + "");//ḫ + list.add("kh"); + wildCardStringMap.put("H", list); + + list = new ArrayList<String>(); + c = 0x1E0f; + list.add(c + "");//ḏ + list.add("dh"); + wildCardStringMap.put("D", list); + + list = new ArrayList<String>(); + c = 0x0161; + list.add(c + "");//š + list.add("sh"); + wildCardStringMap.put("S", list); + + list = new ArrayList<String>(); + c = 0x0121; + list.add(c + "");//ġ + list.add("gh"); + wildCardStringMap.put("G", list); + + list = new ArrayList<String>(); + c = 0x1E97; + list.add("a" + c + " ");//aẗSPACE + list.add("at "); + list.add("ah "); + list.add("a "); + wildCardStringMap.put("A ", list); + + list = new ArrayList<String>(); + c = 0x1ef3; + list.add(c + "");//ỳ + c = 0x00E1; + list.add(c + "");//á + c = 0x0101; + list.add(c + "");//ā + c = 0x00E0; + list.add(c + "");//à + /* + //Chantal list for A + c = 0x0065; + list.add(c + "");//e + c = 0x0101; + list.add(c + "");//ā + c = 0x00E2; + list.add(c + "");//â + */ + wildCardStringMap.put("A", list); + + /* + list = new ArrayList<String>(); + c = 0x0062; + list.add(c + "");//b + c = 0x0070; + list.add(c + "");//p + wildCardStringMap.put("B", list); + */ + } + + public static Map<String, Character[]> wildCardCharMap = new HashMap<String, Character[]>(); + + // " ` ′ ‘ ’ ‛ ' ʻ ʼ ʽ ˋ ʾ ʿ + public static Character[] apostrophes = { + 0x22, 0x60, 0x2032, 0x2018, 0x2019, 0x201B, 0x27, 0x2BB, 0x2BC, 0x2BD, 0x2CB, 0x2BE, 0x2BF }; + //IN: Aa Áá Àà Ââ Ǎǎ Ăă Ãã Ảả Ȧȧ Ạạ Ää Åå Ḁḁ Āā Ąą + //OUT: ᶏ Ⱥⱥ Ȁȁ Ấấ Ầầ Ẫẫ Ẩẩ Ậậ Ắắ Ằằ Ẵẵ Ẳẳ Ặặ Ǻǻ Ǡǡ Ǟǟ Ȁȁ Ȃȃ + public static Character[] AList = { + 0x41, 0x61, 0xC1, 0xE1, 0xC0, 0xE0, 0xC2, 0xE2, 0x1CD, + 0x1CE, 0x102, 0x103, 0xC3, 0xE3, 0x1EA2, 0x1EA3, 0x226, + 0x227, 0x1EA0, 0x1EA1, 0xC4, 0xE4, 0xC5, 0xE5, 0x1E00, + 0x1E01, 0x100, 0x101, 0x104, 0x105 }; + + static{ + + wildCardCharMap.put("", apostrophes); + wildCardCharMap.put("A", AList); + + //IN: Bb Ḃḃ Ḅḅ Ḇḇ Ɓɓ ʙ Bb + //OUT: Ƃƃ ᵬ ᶀ ʙ Bb ȸ Ƀƀ + Character[] BList = { + 0x42, 0x62, 0x1E02, 0x1E03, 0x1E04, 0x1E05, 0x1E06, + 0x1E07, 0x181, 0x253, 0x299, 0xFF22, 0xFF42, + }; + wildCardCharMap.put("B", BList); + + //Ćć Ĉĉ Čč Ċċ C̄c̄ Ç(ç problem with this) Ḉḉ Ȼȼ Ƈƈ ɕ ᴄ Cc + Character[] CList = { + 0x43, 0x63, 0x106, 0x107, 0x108, 0x109, 0x10C, 0x10D, + 0x10A, 0x10B, 0x43, 0xC7, 0xE7, 0x1E08, 0x1E09, 0x23B, + 0x23C, 0x187, 0x188, 0x255, 0x1D04, 0xFF23, 0xFF43 + }; + wildCardCharMap.put("C", CList); + + //IN: Dd Ďď Ḋḋ Ḑḑ Ḍḍ Ḓḓ Ḏḏ Dd + //OUT: Đđ D̦d̦ Ɖɖ Ɗɗ Ƌƌ ᵭ ᶁ ᶑ ȡ ᴅ + Character[] DList = { + 0x44, 0x64, 0x10E, 0x10F, 0x1E0A, 0x1E0B, 0x1E10, + 0x1E11, 0x1E0C, 0x1E0D, 0x1E12, 0x1E13, 0x1E0E, + 0x1E0F, 0xFF24, 0xFF44 + }; + wildCardCharMap.put("D", DList); + + //IN: Ee Éé Èè Êê Ḙḙ Ěě Ĕĕ Ẽẽ Ḛḛ Ẻẻ Ėė Ëë Ēē Ȩȩ Ęę Ȅȅ Ếế Ềề Ễễ Ểể Ḝḝ Ḗḗ Ḕḕ Ȇȇ Ẹẹ Ệệ ᴇ Ee + //OUT: Ææ Ǽǽ Ǣǣ Œœ ᶒ Ɇɇ + Character[] EList = { + 0x45, 0x65, 0xC9, 0xE9, 0xC8, 0xE8, 0xCA, 0xEA, + 0x1E18, 0x1E19, 0x11A, 0x11B, 0x114, 0x115, + 0x1EBC, 0x1EBD, 0x1E1A, 0x1E1B, 0x1EBA, 0x1EBB, + 0x116, 0x117, 0xCB, 0xEB, 0x112, 0x113, 0x228, + 0x229, 0x118, 0x119, 0x204, 0x205, 0x1EBE, 0x1EBF, + 0x1EC0, 0x1EC1, 0x1EC4, 0x1EC5, 0x1EC2, 0x1EC3, + 0x1E1C, 0x1E1D, 0x1E16, 0x1E17, 0x1E14, 0x1E15, + 0x206, 0x207, 0x1EB8, 0x1EB9, 0x1EC6, 0x1EC7, + 0x1D07, 0xFF25, 0xFF45 + }; + wildCardCharMap.put("E", EList); + + //Ii Íí Ìì Ĭĭ Îî Ǐǐ Ïï Ḯḯ Ĩĩ Įį Īī Ỉỉ Ȉȉ Ȋȋ Ịị Ḭḭ + Character[] IList = { + 0x49, 0x69, 0xCD, 0xED, 0xCC, 0xEC, 0x12C, 0x12D, 0xCE, + 0xEE, 0x1CF, 0x1D0, 0xCF, 0xEF, 0x1E2E, 0x1E2F, 0x128, + 0x129, 0x12E, 0x12F, 0x12A, 0x12B, 0x1EC8, 0x1EC9, 0x208, + 0x209, 0x20A, 0x20B, 0x1ECA, 0x1ECB, 0x1E2C, 0x1E2D + }; + wildCardCharMap.put("I", IList); + + //IN: Gg Ǵǵ Ğğ Ĝĝ Ǧǧ Ġġ Ģģ Ḡḡ Ǥǥ Gg + //OUT: Ɠɠ ᶃ ɢ + Character[] GList = { + 0x47, 0x67, 0x1F4, 0x1F5, 0x11E, 0x11F, 0x11C, 0x11D, + 0x1E6, 0x1E7, 0x120, 0x121, 0x122, 0x123, 0x1E20, 0x1E21, + 0x1E4, 0x1E5, 0xFF27, 0xFF47 + }; + wildCardCharMap.put("G", GList); + + //Nn Ńń Ǹǹ Ňň Ññ Ṅṅ Ņņ Ṇṇ Ṋṋ Ṉṉ + Character[] NList = { + 0x4E, 0x6E, 0x143, 0x144, 0x1F8, 0x1F9, 0x147, 0x148, + 0xD1, 0xF1, 0x1E44, 0x1E45, 0x145, 0x146, 0x1E46, + 0x1E47, 0x1E4A, 0x1E4B, 0x1E48, 0x1E49 + }; + wildCardCharMap.put("N", NList); + + //H h Ĥ ĥ Ȟ ȟ Ḧ ḧ Ḣ ḣ Ḩ ḩ Ḥ ḥ Ḫ ḫ H ̱ ẖ Ħ ħ Ⱨ ⱨ + Character[] HList = { + 0x48, 0x68, 0x124, 0x125, 0x21E, 0x21F, 0x1E26, 0x1E27, + 0x1E22, 0x1E23, 0x1E28, 0x1E29, 0x1E24, 0x1E25, 0x1E2A, + 0x1E2B, 0x48, 0x1E96, 0x126, 0x127, 0x2C67, 0x2C68 + }; + wildCardCharMap.put("H", HList); + + //Oo Óó Òò Ŏŏ Ôô Ốố Ồồ Ỗỗ Ổổ Ǒǒ Öö Ȫȫ Őő Õõ Ṍṍ Ṏṏ Ȭȭ Ȯȯ Ȱȱ Øø Ǿǿ Ǫǫ Ǭǭ Ōō Ṓṓ Ṑṑ Ỏỏ Ȍȍ Ȏȏ Ơơ Ớớ Ờờ Ỡỡ Ởở Ợợ Ọọ Ộộ + Character[] OLIST = { + 0x4F, 0x6F, 0xD3, 0xF3, 0xD2, 0xF2, 0x14E, 0x14F, 0xD4, + 0xF4, 0x1ED0, 0x1ED1, 0x1ED2, 0x1ED3, 0x1ED6, 0x1ED7, + 0x1ED4, 0x1ED5, 0x1D1, 0x1D2, 0xD6, 0xF6, 0x22A, 0x22B, + 0x150, 0x151, 0xD5, 0xF5, 0x1E4C, 0x1E4D, 0x1E4E, 0x1E4F, + 0x22C, 0x22D, 0x22E, 0x22F, 0x230, 0x231, 0xD8, 0xF8, 0x1FE, + 0x1FF, 0x1EA, 0x1EB, 0x1EC, 0x1ED, 0x14C, 0x14D, 0x1E52, + 0x1E53, 0x1E50, 0x1E51, 0x1ECE, 0x1ECF, 0x20C, 0x20D, + 0x20E, 0x20F, 0x1A0, 0x1A1, 0x1EDA, 0x1EDB, 0x1EDC, 0x1EDD, + 0x1EE0, 0x1EE1, 0x1EDE, 0x1EDF, 0x1EE2, 0x1EE3, 0x1ECC, + 0x1ECD, 0x1ED8, 0x1ED9 + }; + wildCardCharMap.put("O", OLIST); + + Character[] RList = { + 0x52, 0x72, 0x154, 0x155, 0x158, 0x159, 0x1E58, 0x1E59, + 0x156, 0x157, 0x210, 0x211, 0x212, 0x213, 0x1E5A, 0x1E5B, + 0x1E5C, 0x1E5D, 0x1E5E, 0x1E5F, 0x27C, 0x27E, 0x280, 0xFF32, 0xFF52 + }; + wildCardCharMap.put("R", RList); + + + //IN: Ss Śś Ṥṥ Ŝŝ Šš Ṧṧ Ṡṡẛ Şş Ṣṣ Ṩṩ Șș S̩̩ + //OUT: ᵴ ᶊ ʂ ȿ ꜱ Ss s + Character[] SList = { + 0x53, 0x73, 0x15A, 0x15B, 0x1E64, 0x1E65, 0x15C, 0x15D, + 0x160, 0x161, 0x1E66, 0x1E67, 0x1E60, 0x1E61, 0x15E, 0x15F, + 0x1E62, 0x1E63, 0x1E68, 0x1E69, 0x218, 0x219, 0x53 + }; + wildCardCharMap.put("S", SList); + + + //IN: Tt Ťť Ṫṫ Ţţ Ṭṭ Țț Ṱṱ Ṯṯ Tt + //OUT: Ŧŧ Ⱦⱦ Ƭƭ Ʈʈ T̈ẗ ᵵ ƫ ȶ ᶙ ᴛ + Character[] TList = { + 0x54, 0x74, 0x164, 0x165, 0x1E6A, 0x1E6B, 0x162, 0x163, + 0x1E6C, 0x1E6D, 0x21A, 0x21B, 0x1E70, 0x1E71, 0x1E6E, + 0x1E6F, 0xFF34, 0xFF54 + }; + wildCardCharMap.put("T", TList); + + //IN: Uu Úú Ùù Ŭŭ Ûû Ǔǔ Ůů Üü Ǘǘ Ǜǜ Ǚǚ Ǖǖ Űű Ũũ Ṹṹ Ųų Ūū + //OUT: Ṻṻ Ủủ Ȕȕ Ȗȗ Ưư Ứứ Ừừ Ữữ Ửử Ựự Ụụ Ṳṳ Ṷṷ Ṵṵ Ʉʉ ᵾ ᶙ ᴜ Uu + Character[] UList ={ + 0x55, 0x75, 0xDA, 0xFA, 0xD9, 0xF9, 0x16C, 0x16D, 0xDB, 0xFB, 0x1D3, + 0x1D4, 0x16E, 0x16F, 0xDC, 0xFC, 0x1D7, 0x1D8, 0x1DB, 0x1DC, 0x1D9, + 0x1DA, 0x1D5, 0x1D6, 0x170, 0x171, 0x168, 0x169, 0x1E78, 0x1E79, + 0x172, 0x173, 0x16A, 0x16B + }; + wildCardCharMap.put("U", UList); + + Character[] VList = { + 0x1E7C, 0x1E7D, 0x1E7E, 0x1E7F, 0x1B2, + 0x28B, 0x1D20, 0xFF36, 0xFF56 + }; + wildCardCharMap.put("V", VList); + + //IN: Zz Źź Ẑẑ Žž Żż Ẓẓ Ẕẕ Ƶƶ Ȥȥ + //OUT: Ⱬⱬ ᵶ ᶎ ʐ ʑ ɀ ᴢ Zz + Character[] ZList = { + 0x5A, 0x7A, 0x179, 0x17A, 0x1E90, 0x1E91, 0x17D, + 0x17E, 0x17B, 0x17C, 0x1E92, 0x1E93, 0x1E94, + 0x1E95, 0x1B5, 0x1B6, 0x1D22, 0xFF3A, 0xFF5A + }; + wildCardCharMap.put("Z", ZList); + } + + + public static String normalize(String w){ + if(StringUtils.isEmpty(w)) + return w; + + w = w.toLowerCase(); + /* + * Replacing combination of vowels + */ + for(String key : wildCardStringMap.keySet()){ + List<String> list = wildCardStringMap.get(key); + for(String term : list){ + w = w.replace(term, key); + } + } + + for(String key : wildCardCharMap.keySet()){ + Character[] list = wildCardCharMap.get(key); + for(int i=0; i< list.length; i++){ + w = w.replace(list[i] + "", key); + } + } + return w.toLowerCase(); + } + + public static String normalizedToCompare(String s1){ + s1 = s1.replace("#", ""); + s1 = s1.replace("-", ""); + s1 = s1.replace("(", ""); + s1 = s1.replace(")", ""); + s1 = s1.replace("[", ""); + s1 = s1.replace("]", ""); + s1 = s1.replace("_", ""); + + return s1; + } + + public static void main(String[] args){ + String s = NormalizerUtils.normalize("ṯ"); + System.out.println(s); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,203 @@ +package org.mpi.openmind.repository.utils; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javolution.xml.stream.XMLOutputFactory; +import javolution.xml.stream.XMLStreamException; +import javolution.xml.stream.XMLStreamWriter; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +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.PersistenceService; +import org.mpi.openmind.scheduling.utils.Scheduling; + +public class OM4StreamWriter { + + private static Logger logger = Logger.getLogger(OM4StreamWriter.class); + + private static int itemsPerPage = 500; + + public static void backupEntities(String fileName, PersistenceService ps){ + generateEntities(fileName, ps, Node.TYPE_ABOX); + } + + public static void backupDefinitions(String fileName, PersistenceService ps){ + generateEntities(fileName, ps, Node.TYPE_TBOX); + } + + private static void generateEntities(String fileName, PersistenceService ps, String type) { + OutputStreamWriter out; + try { + FileOutputStream fileOut = new FileOutputStream (fileName); + out = new OutputStreamWriter (fileOut, "UTF-8"); + + XMLOutputFactory factory = XMLOutputFactory.newInstance(); + factory.setProperty(XMLOutputFactory.INDENTATION, "\t"); + + XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(out); + + int entitiesCount = 0; + + xmlStreamWriter.writeStartDocument("UTF-8", "1.0"); + + if(type.equals(Node.TYPE_ABOX)){ + xmlStreamWriter.writeStartElement(XMLUtil.OPENMIND_DATA); + entitiesCount = ps.getEntityCount(null).intValue(); + }else{ + xmlStreamWriter.writeStartElement(XMLUtil.META_DATA); + entitiesCount = ps.getEntityCount(Node.TYPE_TBOX).intValue(); + } + + int numberOfPages = entitiesCount / itemsPerPage; + int counter = 0; + long start = System.currentTimeMillis(); + DecimalFormat df = new DecimalFormat("#.##"); + + List<Relation> relList = new ArrayList<Relation>(); + + //(type.equals(Node.TYPE_TBOX)) ? "" : "" + + xmlStreamWriter.writeStartElement((type.equals(Node.TYPE_TBOX)) ? XMLUtil.DEFINITIONS : XMLUtil.ENTITIES); + for(int currentPage = 0; currentPage <= numberOfPages; currentPage++){ + int startRecord = currentPage * itemsPerPage; + List<Entity> entities; + + if(type.equals(Node.TYPE_ABOX)){ + entities = ps.getEntityPage(null, startRecord, itemsPerPage); + }else{ + entities = ps.getEntityPage(Node.TYPE_TBOX, startRecord, itemsPerPage); + } + + for(Entity ent : entities){ + insertEntity(ent, xmlStreamWriter, ps); + relList.addAll(ent.getSourceRelations()); + counter++; + if ((counter % 50) == 0) { + System.out.print("*"); + } + } + long diff = System.currentTimeMillis() - start; + double min = (double) diff / (double) (60 * 1000); + double percent = ((double) counter / (double) entitiesCount) * 100.0; + System.out.print("\n(" + df.format(percent) + "%) \t[" + counter + "/" + entitiesCount + "]\t"); + logger.info("Speed[ents/min]: " + (double) counter / min); + xmlStreamWriter.flush(); + } + xmlStreamWriter.writeEndElement(); + + xmlStreamWriter.writeStartElement(XMLUtil.RELATIONS); + for(Relation rel : relList){ + insertRelation(rel, xmlStreamWriter); + } + xmlStreamWriter.writeEndElement(); + + + //end file. + xmlStreamWriter.writeEndElement(); + + xmlStreamWriter.flush(); + xmlStreamWriter.close(); + + logger.info("END Stream Writer"); + } catch (IOException e) { + e.printStackTrace(); + } catch (XMLStreamException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private static void insertRelation(Relation rel, XMLStreamWriter writer) throws XMLStreamException{ + writer.writeStartElement(XMLUtil.RELATION); + + if(rel.getId() != null) + writer.writeAttribute(XMLUtil.ID, rel.getId().toString()); + if(StringUtils.isNotEmpty(rel.getOwnValue())) + writer.writeAttribute(XMLUtil.OWN_VALUE, rel.getOwnValue()); + if(StringUtils.isNotEmpty(rel.getObjectClass())) + writer.writeAttribute(XMLUtil.OBJECT_CLASS, rel.getObjectClass()); + if(StringUtils.isNotEmpty(rel.getContentType())) + writer.writeAttribute(XMLUtil.CONTENT_TYPE, rel.getContentType()); + if(rel.getSourceId() != null) + writer.writeAttribute(XMLUtil.RELATION_SOURCE_ID, rel.getSourceId().toString()); + if(rel.getTargetId() != null) + writer.writeAttribute(XMLUtil.RELATION_TARGET_ID, rel.getTargetId().toString()); + if(rel.getVersion() != null) + writer.writeAttribute(XMLUtil.VERSION, rel.getVersion().toString()); + if(rel.getModificationTime() != null) + writer.writeAttribute(XMLUtil.MODIFICATION_TIME, rel.getModificationTime().toString()); + + writer.writeEndElement(); + } + + private static void insertEntity(Entity entity, XMLStreamWriter writer, PersistenceService ps) throws XMLStreamException{ + + writer.writeStartElement((entity.getType().equals(Node.TYPE_TBOX)) ? XMLUtil.DEFINITION : XMLUtil.ENTITY); + + if(entity.isLightweight()){ + entity = ps.getEntityContent(entity); + } + + if(StringUtils.isNotEmpty(entity.getObjectClass())) + writer.writeAttribute(XMLUtil.OBJECT_CLASS, entity.getObjectClass()); + if(entity.getId() != null) + writer.writeAttribute(XMLUtil.ID, entity.getId().toString()); + if(StringUtils.isNotEmpty(entity.getOwnValue())) + writer.writeAttribute(XMLUtil.OWN_VALUE, entity.getOwnValue()); + if(StringUtils.isNotEmpty(entity.getContentType())) + writer.writeAttribute(XMLUtil.CONTENT_TYPE, entity.getContentType()); + if(entity.getVersion() != null) + writer.writeAttribute(XMLUtil.VERSION, entity.getVersion().toString()); + if(entity.getModificationTime() != null) + writer.writeAttribute(XMLUtil.MODIFICATION_TIME, entity.getModificationTime().toString()); + + + if(entity.getAttributes().size() > 0){ + writer.writeStartElement(XMLUtil.ATTRIBUTES); + for(Attribute att : entity.getAttributes()){ + insertEntity(att, writer); + } + writer.writeEndElement(); + } + + writer.writeEndElement(); + } + + private static void insertEntity(Attribute att, XMLStreamWriter writer) throws XMLStreamException{ + writer.writeStartElement(XMLUtil.ATTRIBUTE); + + if(StringUtils.isNotEmpty(att.getName())) + writer.writeAttribute(XMLUtil.ATTRIBUTE_NAME, att.getName()); + if(att.getId() != null) + writer.writeAttribute(XMLUtil.ID, att.getId().toString()); + if(StringUtils.isNotEmpty(att.getValue())) + writer.writeAttribute(XMLUtil.ATTRIBUTE_VALUE, att.getValue()); + if(StringUtils.isNotEmpty(att.getContentType())) + writer.writeAttribute(XMLUtil.CONTENT_TYPE, att.getContentType()); + if(att.getVersion() != null) + writer.writeAttribute(XMLUtil.VERSION, att.getVersion().toString()); + if(att.getModificationTime() != null) + writer.writeAttribute(XMLUtil.MODIFICATION_TIME, att.getModificationTime().toString()); + + writer.writeEndElement(); + } + + + + public static void main(String[] args) { + //ServiceRegistry srvReg = new ServiceRegistry(); + //OM4StreamWriter.backupEntities("/Users/jurzua/ent.xml", srvReg.getPersistenceService()); + logger.info(DateFormat.getInstance().format(new Date())); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/OMUtils.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,126 @@ +package org.mpi.openmind.repository.utils; + +import java.text.DecimalFormat; +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.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.repository.bo.Attribute; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.ViewerAttribute; +import org.mpi.openmind.repository.utils.ismi.ISMICalendar; + +public class OMUtils { + + private static String SOURCE = "source"; + private static String TARGET = "target"; + + + public static List<String> resolveQuery(Long entBaseId, String query, WrapperService ws, Integer contentType) throws Exception{ + + List<String> rs = new ArrayList<String>(); + String[] array = query.split("&"); + + for(String element : array){ + String[] array0 = element.split("\\|"); + List<String> terms = new ArrayList<String>(Arrays.asList(array0)); + + rs.addAll(new ArrayList<String>(entityQuery(entBaseId, terms.remove(0), terms, ws, contentType))); + } + + return rs; + } + + private static Set<String> entityQuery(Long currentEntId, String currentTerm, List<String> terms, WrapperService ws, Integer contentType) throws Exception{ + Set<String> rs; + + if(terms.size() == 0){ + rs = new HashSet<String>(); + + String[] array = currentTerm.split(":"); + if(array.length > 1){ + if(StringUtils.equals(array[1], "id")){ + rs.add(currentEntId.toString()); + }else{ + //returns attribute + Attribute att = ws.getAttributeByName(currentEntId, array[1]); + if(att != null) + rs.add(att.getValue()); + } + }else{ + //returns ownvalue + Entity currentEntity = ws.getEntityById(currentEntId); + String ownValue = currentEntity.getOwnValue(); + if(contentType == null || contentType.equals(ViewerAttribute.CONTENT_TEXT)){ + //nothing + }else if(contentType.equals(ViewerAttribute.CONTENT_DATE)){ + ISMICalendar cal = new ISMICalendar(ownValue); + ownValue = cal.getCalendarAsHtml(); + } + rs.add(ownValue); + + } + }else{ + + rs = relationQuery(currentEntId, terms.remove(0), new ArrayList<String>(terms), ws, contentType); + } + return rs; + } + + private static Set<String> relationQuery(Long currentEntId, String currentTerm, List<String> terms, WrapperService ws, Integer contentType) throws Exception{ + Set<String> rs = new HashSet<String>(); + + String[] array = currentTerm.split(":"); + if(array.length != 2 && + (!SOURCE.equals(array[0]) || !TARGET.equals(array[0]))){ + throw new Exception("Relation item has not valid format: " + currentTerm); + } + + if(terms.size() == 0){ + throw new Exception("ViewerAttribute ended with a relation term: " + currentTerm); + } + + String nextTerm = terms.get(0); + String classNextTerm = nextTerm.split(":")[0]; + + terms.remove(0); + List<Entity> nextList; + if(SOURCE.equals(array[0])){ + nextList = ws.getTargetsForSourceRelation(currentEntId, array[1], classNextTerm, -1); + + }else{ + nextList = ws.getSourcesForTargetRelation(currentEntId, array[1], classNextTerm, -1); + } + + for(Entity ent : nextList){ + Set<String> rs0 = entityQuery(ent.getId(), nextTerm, new ArrayList<String>(terms), ws, contentType); + rs.addAll(rs0); + } + + return rs; + } + + public static boolean equals(Long one, Long two){ + if(one == null && two == null){ + return true; + } + + if(one == null || two == null){ + return false; + } + + return one.equals(two); + } + + + public static String percentage(int index, int total) { + double p = (100 * (double) index) / (double) total; + DecimalFormat df = new DecimalFormat("#.#"); + return df.format(p); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/OwnValueGenerator.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,355 @@ +package org.mpi.openmind.repository.utils; + +import java.util.Formatter; +import java.util.List; +import java.util.Locale; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.hibernate.Query; +import org.hibernate.Session; +import org.mpi.openmind.configuration.ConfigurationService; +import org.mpi.openmind.configuration.ownvalue.NodeOwnValueRule; +import org.mpi.openmind.configuration.ownvalue.ORElement; +import org.mpi.openmind.configuration.ownvalue.OROwnValueRule; +import org.mpi.openmind.configuration.ownvalue.OwnValueAttribute; +import org.mpi.openmind.configuration.ownvalue.OwnValueEntity; +import org.mpi.openmind.configuration.ownvalue.OwnValueNode; +import org.mpi.openmind.configuration.ownvalue.OwnValueRelation; +import org.mpi.openmind.configuration.ownvalue.OwnValueRule; +import org.mpi.openmind.configuration.ownvalue.PrintRule; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Node; + +public class OwnValueGenerator { + + private ConfigurationService configurationService; + + private static Logger logger = Logger.getLogger(OwnValueGenerator.class); + + /** + * This method generates an ownValue for the given Entity looking at the ownValue rule from the Configuration service. + * The parameter session should be a opened session. + * @param entity + * @param session + * @return + */ + public String generateOwnValue(Entity entity, Session session) { + + logger.info("### Generating Own Value for= " + entity.getId() + " ###"); + + String ownValue = null; + List<PrintRule> printRules = getConfigurationService().getPrintRules(entity + .getObjectClass()); + for(PrintRule rule : printRules){ + ownValue = this.generatePrintValue(rule, entity, session); + break; + } + + if(StringUtils.isNotBlank(ownValue)){ + ownValue = ownValue.replace("__", "_"); + } + + return ownValue; + } + + private String generatePrintValue(PrintRule rule, Entity entity, Session session){ + String ownValue = null; + String formatPattern = rule.getFormat(); + Object[] generatedOwnValueArrayObjects = new Object[rule + .getOwnValueRuleList().size()]; + + int count = 0; + for (String ownValueRuleId : rule.getOwnValueRuleList()) { + formatPattern = formatPattern.replace("%" + ownValueRuleId + + "%", "%" + (count + 1) + "$1s"); + OwnValueRule ownValueRule = configurationService + .getOwnValueRule(ownValueRuleId); + String s = generateOwnValue(ownValueRule, entity, session); + + + //if the word none appears in the own_value it should be deleted. + if(entity.getObjectClass().equals("WITNESS") && StringUtils.isNotBlank(s)){ + s = s.replace("None", ""); + s = s.replace("NONE", ""); + s = s.replace("none", ""); + } + + + //System.out.println("For Rule= " + ownValueRuleId + " were found ownValue= " + s); + generatedOwnValueArrayObjects[count] = s; + count++; + + if(rule.getPrintModus().equals(PrintRule.MODUS_PRINT_IF_NO_NULLS) && StringUtils.isEmpty(s)){ + return null; + } + } + + if(rule.getPrintModus().equals(PrintRule.MODUS_PRINT_IF_SOME_OW_NO_NULL)){ + boolean thereIsSomeOwnValueNoNull = false; + for(int i=0; i < generatedOwnValueArrayObjects.length; i++){ + if(StringUtils.isNotEmpty((String)generatedOwnValueArrayObjects[i])){ + thereIsSomeOwnValueNoNull = true; + break; + } + } + + if(!thereIsSomeOwnValueNoNull) + return null; + } + + /* + logger.debug(""); + logger.debug("Vector: " + generatedOwnValueArrayObjects); + logger.debug("FormatPattern: " + formatPattern); + logger.debug(""); + */ + StringBuilder sb = new StringBuilder(); + Formatter formatter = new Formatter(sb, Locale.US); + formatter.format(formatPattern, generatedOwnValueArrayObjects); + + //logger.info("***** Result OwnValue ****** = " + sb.toString()); + //logger.debug("-----------------------------------------------"); + ownValue = sb.toString(); + return ownValue; + } + + private String generateOwnValue(OwnValueRule rule, Entity entity, Session session) { + if(rule instanceof NodeOwnValueRule){ + return generateNodeOwnValue((NodeOwnValueRule)rule, entity, session); + }else if(rule instanceof OROwnValueRule){ + return generateOROwnValue((OROwnValueRule)rule, entity, session); + }else{ + return null; + } + } + + private String generateNodeOwnValue(NodeOwnValueRule rule, Entity entity, Session session){ + return getOwnValueFromDB(((NodeOwnValueRule)rule).getNodeList(), entity, session); + } + + private String generateOROwnValue(OROwnValueRule rule, Entity entity, Session session){ + String ownValue = null; + for(ORElement orElement : rule.getOrElementList()){ + if(StringUtils.isNotEmpty(orElement.getRef())){ + OwnValueRule ownValueRule = getConfigurationService().getOwnValueRule(orElement.getRef()); + if(ownValueRule instanceof OROwnValueRule){ + ownValue = generateOROwnValue((OROwnValueRule)ownValueRule, entity, session); + }else if(ownValueRule instanceof NodeOwnValueRule){ + ownValue = generateNodeOwnValue((NodeOwnValueRule)ownValueRule, entity, session); + } + }else if(XMLUtil.CONSTANT.equals(orElement.getType())){ + ownValue = orElement.getValue(); + return ownValue; + } + if(StringUtils.isNotEmpty(ownValue)){ + return ownValue; + } + } + return null; + } + + private String getOwnValueFromDB(List<OwnValueNode> ruleList, Entity entity, + Session session) { + String ownValue = null; + + String hql = "from Entity as ent, "; + + int count = 0; + for (OwnValueNode node : ruleList) { + if (count > 0) + hql += ", "; + if (node instanceof OwnValueRelation) { + hql += " Relation as rel" + count + ""; + } else if (node instanceof OwnValueEntity) { + hql += " Entity as ent" + count + ""; + } else if (node instanceof OwnValueAttribute) { + hql += " Attribute as att" + count + ""; + } + count++; + } + + hql += " where ent.id = :entId "; + + count = 0; + for (OwnValueNode node : ruleList) { + // if(count > 0) + String element = null; + if (node instanceof OwnValueRelation) { + element = " AND rel" + count + ""; + if (StringUtils.isNotEmpty(((OwnValueRelation) node).getName())) + hql += element + ".ownValue = :name" + count; + } else if (node instanceof OwnValueEntity) { + element = " AND ent" + count + ""; + if (StringUtils.isNotEmpty(((OwnValueEntity) node) + .getObjectClass())) + hql += element + ".objectClass = :objectClass" + count; + if (StringUtils.isNotEmpty(((OwnValueEntity) node) + .getOwnValue())) + hql += element + ".ownValue = :ownValue" + count; + } else if (node instanceof OwnValueAttribute) { + element = " AND att" + count + ""; + if (StringUtils + .isNotEmpty(((OwnValueAttribute) node).getName())) + hql += element + ".objectClass = :name" + count; + } + hql += element + ".systemStatus = :systemStatus "; + count++; + } + + JoinResponse resp = generateOwnValueJoin(ruleList, 0, new JoinResponse(new String(), false)); + + hql += " AND " + resp.hql; + + Query query = session.createQuery(hql); + // query.setMaxResults(1); + query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); + query.setLong("entId", entity.getId()); + if(resp.usedModif){ + query.setLong("entModif", entity.getModificationTime()); + } + + count = 0; + for (OwnValueNode node : ruleList) { + if (node instanceof OwnValueRelation) { + if (StringUtils.isNotEmpty(((OwnValueRelation) node).getName())) + query.setString("name" + count, ((OwnValueRelation) node) + .getName()); + } else if (node instanceof OwnValueEntity) { + if (StringUtils.isNotEmpty(((OwnValueEntity) node) + .getObjectClass())) + query.setString("objectClass" + count, + ((OwnValueEntity) node).getObjectClass()); + if (StringUtils.isNotEmpty(((OwnValueEntity) node) + .getOwnValue())) + query.setString("ownValue" + count, ((OwnValueEntity) node) + .getOwnValue()); + } else if (node instanceof OwnValueAttribute) { + if (StringUtils + .isNotEmpty(((OwnValueAttribute) node).getName())) + query.setString("name" + count, ((OwnValueAttribute) node) + .getName()); + } + count++; + } + /* + System.out.println("QUERY: " + query.getQueryString()); + System.out.println(); + System.out.println("TO STRING: " + query.toString()); + System.out.println(); + System.out.println("PARA: " + query.getNamedParameters()); + System.out.println();*/ + + List<Object> list = query.list(); + //System.out.println("OwnValues found: " + list.size()); + //System.out.println(); + for (Object o : list) { + Object[] array = (Object[]) o; + for (int i = 0; i < array.length; i++) { + //System.out.println(array[i]); + } + //System.out.println("-------"); + ownValue = ((Node) array[array.length - 1]).getOwnValue(); + break; + } + + //System.out.println("Own Value Found: " + ownValue); + return ownValue; + } + + /** + * This method generates the hql piece, where each element of a ownValueRule + * is connected. It works recursively. + * + * @param nodes + * @param hql + * @param index + * @return + */ + private JoinResponse generateOwnValueJoin(List<OwnValueNode> nodes, + int index, JoinResponse resp) { + OwnValueNode node = nodes.get(index); + if (node instanceof OwnValueRelation) { + if (((OwnValueRelation) node).getType().equals( + OwnValueNode.SOURCE_RELATION)) { + if (index == 0) { + resp.hql += " rel" + index + ".sourceId = :entId "; + resp.hql += " AND "; + resp.hql += " rel" + index + ".sourceModif = :entModif "; + resp.usedModif = true; + } else { + resp.hql += " AND "; + resp.hql += " rel" + index + ".sourceId = ent" + (index - 1) + + ".id "; + resp.hql += " AND "; + resp.hql += " rel" + index + ".sourceModif = ent" + (index - 1) + + ".modificationTime "; + } + } else if (((OwnValueRelation) node).getType().equals( + OwnValueNode.TARGET_RELATION)) { + if (index == 0) { + resp.hql += " rel" + index + ".targetId = :entId "; + resp.hql += " AND "; + resp.hql += " rel" + index + ".targetModif = :entModif "; + resp.usedModif = true; + } else { + resp.hql += " AND "; + resp.hql += " rel" + index + ".targetId = ent" + (index - 1) + + ".id "; + resp.hql += " AND "; + resp.hql += " rel" + index + ".targetModif = ent" + (index - 1) + + ".modificationTime "; + } + } + } else if (node instanceof OwnValueEntity) { + // if index is zero and the first node is an entity, must throw a + // exception, it must not be possible! + if (((OwnValueEntity) node).getType().equals( + OwnValueNode.SOURCE_ENTITY)) { + resp.hql += " AND "; + resp.hql += " ent" + index + ".id = rel" + (index - 1) + + ".sourceId "; + } else if (((OwnValueEntity) node).getType().equals( + OwnValueNode.TARGET_ENTITY)) { + resp.hql += " AND "; + resp.hql += " ent" + index + ".id = rel" + (index - 1) + + ".targetId "; + } + } else if (node instanceof OwnValueAttribute) { + if (index == 0) { + resp.hql += " att" + index + ".sourceId = :entId "; + } else { + resp.hql += " AND "; + resp.hql += " att" + index + ".sourceId = ent" + (index - 1) + + ".id "; + } + // an attribute is always an 'endpoint' + //logger.info("index=" + index + ", hql=" + hql); + return resp; + } + //logger.info("index=" + index + ", hql=" + hql); + if (node.isEndNode()) { + return resp; + } + resp = generateOwnValueJoin(nodes, index + 1, resp); + return resp; + } + + public ConfigurationService getConfigurationService() { + return configurationService; + } + + public void setConfigurationService( + ConfigurationService configurationService) { + this.configurationService = configurationService; + } + + class JoinResponse{ + public String hql; + public boolean usedModif; + public JoinResponse(String hql, boolean usedModif){ + this.hql = hql; + this.usedModif = usedModif; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/RomanizationLoC.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,418 @@ +package org.mpi.openmind.repository.utils; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang.StringUtils; + +/** + * See document: romanized_arabic_into_arabic.doc + * http://www.loc.gov/catdir/cpso/romanization/arabic.pdf + * @author jurzua + * + */ +public class RomanizationLoC { + + private static boolean debug = false; + + + private static Map<String, String> CONVERSIONMAP = new HashMap<String, String>(); + // \w = A word character: [a-zA-Z_0-9] + private static String T = "ẗ"; + private static String regex_words = "[\\w|ā|ī|’|ā|š|ṭ|ẗ]+"; + private static String regex_words_empty = "[\\w|ā|ī|’|ā|š]*"; + private static String rule_5_a_init = "at-t|aṯ-ṯ|ad-d|aḏ-ḏ|ar-r|az-z|as-s|aš-š|aṣ-ṣ|aḍ-ḍ|aṭ-ṭ|aẓ-ẓ|al-l|an-n"; + private static String rule_5_a_letters = "t|ṯ|d|ḏ|r|z|s|š|ṣ|ḍ|ṭ|ẓ|l|n"; + private static String begin_space = "(^|.*\\s)"; + private static String begin_space0 = "^|.*\\s"; + + + static{ + + char[] aaa = {'t', 'ṯ', 'd', 'ḏ', 'r', 'z', 's', 'š', 'ṣ', 'ḍ', 'ṭ', 'ẓ', 'l', 'n'}; + + + //rules 1.a to 1.f + CONVERSIONMAP.put("\u1E6F", "\u0074\u0068");//ṯ -> th + CONVERSIONMAP.put("\u1E6E", "\u0054\u0068");//Ṯ -> Th + + CONVERSIONMAP.put("\u1E2B", "\u006B\u0068");//ḫ -> kh + CONVERSIONMAP.put("\u1E2A", "\u004B\u0068");//Ḫ -> Kh + + CONVERSIONMAP.put("\u1E0F", "\u0064\u0068");//ḏ -> dh + CONVERSIONMAP.put("\u1E0E", "\u0044\u0068");//Ḏ -> Dh + + CONVERSIONMAP.put("\u0161", "\u0073\u0068");//š -> sh + CONVERSIONMAP.put("\u0160", "\u0053\u0068");//Š -> Sh + + CONVERSIONMAP.put("\u0121", "\u0067\u0068");//ġ -> gh + CONVERSIONMAP.put("\u0120", "\u0047\u0068");//Ġ -> Gh + + CONVERSIONMAP.put("\u1EF3", "\u00E1");//ỳ -> á + //CONVERSIONMAP.put("\u1EF2", "\u00C1");//Ỳ -> Á + + } + + public static char APOSTROPHE = 0x27; + public static String apostrophesNormalization(String text){ + String result = text; + for(Character apostrophe : NormalizerUtils.apostrophes){ + result = result.replace(apostrophe, APOSTROPHE); + } + return result; + } + + public static char a = 0x61; + public static String aNormalization(String text){ + String result = text; + for(Character item : NormalizerUtils.AList){ + result = result.replace(item, a); + } + return result; + } + + + public static String ruleGroup7(String text){ + + String rule_7_1_allah = "illāh|ullāh|allah|allāh|-Allāh|Allah|ullah|illah"; + + int count = 0; + while(text.matches("(.*)(\\S+)(" + rule_7_1_allah + ")(.*)") && count<10){ + if(debug)System.out.println("ruleGroup7"); + text = text.replaceAll("(.*)(\\S+)(" + rule_7_1_allah + ")(.*)", "$1$2 Allāh$4"); + if(debug) System.out.println(text); + count++; + } + + return text; + + } + + public static String ruleGroup6(String text){ + + String rule_6_consonants = "t|k|d|s|g"; + + if(text.matches("("+regex_words_empty+")("+ rule_6_consonants + ")h("+regex_words_empty+")")){ + text = text.replaceAll("("+regex_words_empty+")("+ rule_6_consonants + ")h("+regex_words_empty+")", "$1$2’h$3"); + } + + return text; + } + + public static String ruleGroup5(String text){ + + //wa-ʾl-nahār + //wa al-nahār + //5A + while(text.matches("(.*)(-ʾl-)(.*)")){ + if(debug)System.out.println("5A(a)"); + text = text.replaceAll("(.*)(-ʾl-)(.*)", "$1 al-$3"); + if(debug) System.out.println(text); + } + + /* + while(text.matches("(.*)(" + begin_space0 + ")(ʾl-)(.*)")){ + if(debug)System.out.println("5A"); + text = text.replaceAll("(.*)(" + begin_space0 + ")(ʾl-)(.*)", "$1$2al-$4"); + }*/ + + + //5.B + text = rule5B(text); + + return text; + } + + public static String rule5B(String text){ + //'t', 'ṯ', 'd', 'ḏ', 'r', 'z', 's', 'š', 'ṣ', 'ḍ', 'ṭ', 'ẓ', 'l', 'n' + + String regex0 = "(t-t|ṯ-ṯ|d-d|ḏ-ḏ|r-r|z-z|s-s|š-š|ṣ-ṣ|ḍ-ḍ|ṭ-ṭ|ẓ-ẓ|l-l|n-n)"; + + String regex = begin_space + "(a|A)" + regex0 + "(\\S+)(.*)"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(text); + if(matcher.find()){ + if(debug) System.out.println("5.B"); + String g1 = matcher.group(1); + String g2 = matcher.group(2); + String g3 = matcher.group(3); + String g4 = matcher.group(4); + String g5 = matcher.group(5); + + text = g1 + g2 + "l-" + g3.charAt(0) + g4 + g5; + + if(debug) System.out.println(text); + matcher = pattern.matcher(text); + } + + return text; + } + + + public static String ruleGroup4(String text){ + + String gr_4b = "li al-|li’l-|li-’l-|li-l-"; + //4.B + while(text.matches(begin_space + "(" + gr_4b + ")(.*)")){ + if(debug) System.out.println("4.B"); + text = text.replaceAll(begin_space + "(" + gr_4b + ")(.*)", "$1lil-$3"); + } + + //4.A + while(text.matches(begin_space + "(li )(.*)")){ + if(debug) System.out.println("4.A"); + text = text.replaceAll(begin_space + "(li )(.*)", "$1li-$3"); + } + + return text; + } + + + public static String ruleGroup3(String text){ + + //3.A + while(text.matches(begin_space + "(bi|wa|ka)(\\s+)(al-)(.*)")){ + if(debug) System.out.println("3.A"); + text = text.replaceAll(begin_space + "(bi|wa|ka)(\\s+)(al-)(.*)", "$1$2-$4$5"); + //if(debug) System.out.println(text); + } + + // 3.B + while(text.matches(begin_space + "(bi|wa|ka)(\\s+)(.*)")){ + if(debug)System.out.println("3.B"); + text = text.replaceAll(begin_space + "(bi|wa|ka)(\\s+)(.*)", "$1$2-$4"); + } + + return text; + } + + public static String ruleGroup2(String text){ + + //2.C: al-XXXXẗ -> al-XXXXh + while(text.matches(begin_space + "(al-)(\\S+)ẗ(\\s+|$)(.*)")){ + if(debug) System.out.println("2.C"); + //System.out.println(text.replaceAll(begin_space + "(al-)(" + regex_words + ")ẗ(.*)", "$2$3ẗ")); + text = text.replaceAll(begin_space + "(al-)(\\S+)ẗ(\\s+|$)(.*)", "$1$2$3h$4$5"); + if(debug) System.out.println(text); + } + + //Other XXXXẗ al-XXXXẗ -> XXXXt al-XXXXh + if(text.matches("(" + regex_words + ")(ẗ)(\\s*)(al-)(" + regex_words + ")(ẗ)")){ + if(debug) System.out.println("2.Other"); + text = text.replaceAll("(" + regex_words + ")(ẗ)(\\s*)(al-)(" + regex_words + ")(ẗ)", "$1t al-$5h"); + if(debug) System.out.println(text); + } + + //2.D XXXXẗan -> XXXXtan + while(text.matches("(.*)(ẗan)(\\s+|$)(.*)")){ + if(debug) System.out.println("2.D"); + text = text.replaceAll("(.*)(ẗan)(\\s+|$)(.*)", "$1tan$3$4"); + if(debug) System.out.println(text); + } + + //2A + text = rule2A(text); + + //2B + text = rule2B(text); + + return text; + } + + public static String rule2B(String text){ + + String regex = "(.*)(ẗ)(\\s+|(?!al-)\\S*)(.*)"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(text); + int count = 0; + while(matcher.find() && count < 10){ + if(debug) System.out.println("2.B"); + String g1 = matcher.group(1); + String g2 = matcher.group(2); + String g3 = matcher.group(3); + String g4 = matcher.group(4); + text = g1 + "h" + g3 + g4; + if(debug) System.out.println(text); + matcher = pattern.matcher(text); + count++; + } + return text; + } + + public static String rule2A(String text){ + + //2.A + //String regex2A = "(.*)(\\s++)(.*)ẗ(\\s++)(al-)(.*)"; + //String regex2A = "(.*)(\\s++)(?<!(al-))(.*)ẗ(\\s++)(al-)(.*)"; + String regex = begin_space + "((?!al-)\\S+)(ẗ)(\\s+)(al-)(.*)"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(text); + + while(matcher.find()){ + + if(debug) System.out.println("2.A"); + + String g1 = matcher.group(1); + String g2 = matcher.group(2); + String g3 = matcher.group(3); + String g4 = matcher.group(4); + String g5 = matcher.group(5); + String g6 = matcher.group(6); + + + //System.out.println(g1 +" # "+ g2 + " #3 " + g3 + " #4 " + g4 + " #5 " + g5 + " # " + g6); + text = g1 + g2 + "t" + g4 + g5 + g6; + if(debug) System.out.println(text); + + matcher = pattern.matcher(text); + } + + return text; + } + + public static String convert(final String text) { + + if(StringUtils.isEmpty(text)) + return text; + + String replacementText = new String(text); + + replacementText = ruleGroup2(replacementText); + replacementText = ruleGroup4(replacementText); + replacementText = ruleGroup6(replacementText); //6 must be executed before 5 + replacementText = ruleGroup5(replacementText); + replacementText = ruleGroup3(replacementText); + replacementText = ruleGroup7(replacementText); + + for (String ar : CONVERSIONMAP.keySet()) { + String lat = CONVERSIONMAP.get(ar); + if (replacementText.contains(ar)) { + replacementText = replacementText.replace(ar, lat); + } + } + + return replacementText; + } + + public static void test(String s){ + System.out.println("--------------\n" + s + " ->\n" + convert(s) + "\n"); + + } + + + public static void main(String[] args){ + + + //test("li’l-Shirbīnī"); + //test("li-'l-Shirbīnī"); + //test("’Abdullāh"); + //test("’Abd allāh"); + + //test("ʿAli b. ʿAbdullah"); + //test("ʿAbdullah"); + //test("Risālaẗ"); + //test("Risālaẗ fī"); + //test("Risālaẗ fī qismaẗ"); + //test("Risālaẗ fī qismaẗ al-handasaẗ al-qabbān bi ṭarīq al-handasaẗ bi ṭarīq wa'l-misāḥaẗ wa'l-ḥisāb bi'l-nisab al-arbaʿ"); + + //test("ʿAli b. ʿAbdullah"); + //test("Yusuf b. ʿAbdullah"); + + + + //test("fī-'l-kitāb"); + + //test("Risālaẗ (Nukat) fīmā yaṣiḥḥu min aḥkām al-nujūm = Kitāb al-taḏākīr (Risālaẗ) fī ibṭāl aḥkām al-nujūm"); + + /* + //Rules Group 2 + test("al-risalaẗ"); + test("risalaẗ al-kabir"); + test("risalaẗ al-kabir"); + test("risalaẗ al-kabiraẗ"); // ????? + test("risalaẗ"); + test("risalaẗan"); + test("Risālaẗ fī al-ʿamal bi-rubʿ al-muqanṭarāt al-šamālīyaẗ"); + + //Rules Group 3 + test("bi al-tamām̄"); + test("wa al-kamāl"); + test("bi tarīq"); + + //Group 4 + test("li al-shirbini"); + test("li’l-Shirbīnī"); + test("li-’l-Shirbīnī"); + test("li tajrīd"); + + + //Group 5 + test("aš-šams"); + test("aḏ-ḏams"); + + + + //Group 6 + test("Adham"); + + //Group 7 + test("’Abd allāh"); + + test("fi’l-kitāb"); + test("fi-’l-kitāb"); + */ + + + + + //test("al-Abyāt fī al-Ṭāliʿ wa al-Ġārib wa al-Mutawassiṭ wa al-Watad"); + //test("Al-tuḥfaẗ al-šāhiyyaẗ fī al-āḥkām al-falakiyyaẗ"); + + //char ch = 'Á'; + //System.out.println(String.format("%04x", (int) ch)); + //test("Al-Futūḥāt al-Wahbīyaẗ fī Ỳarḥ al-Risālaẗ al-Fatḥīyaẗ fī al-ʿamal bi-al-rubʿ al-mujayyab"); + + //test("wa-ʾl-nahār"); + //test("li-l-ʿIlm"); + //test("al-Jawharaẗ al-bahiyyaẗ fī maʿrifaẗ al-awqāt fī maʿrifaẗ al-awqāt al-layliyyaẗ wa-ʾl-nahāriyyaẗ"); + //test("al-Jawharaẗ al-bahiyyaẗ fī al-maʿrifaẗ al-awqāt al-layliyyaẗ wa-ʾl-nahāriyyaẗ"); + + //String text = "fī maʿrifaẗan al-awqāt al-layliyyaẗ wa-ʾl-nahāriyyaẗ"; + + //test("Natījaẗ al-afkār fī aʿmāl al-layl wa-ʾl-nahār"); + + + //test("al-ʿAqīda as-silālajīya dfsdssdf"); + test("Muḫtaṣaraẗ fī ṣanʿaẗ baʿḍ al-ālāt al-raṣadiyyaẗ wa-ʾl-ʿamal bi-hā"); + + + + + /* + String text = "Natījaẗ al-afkār fī aʿmāl al-layl wa-ʾl-nahār"; + String regex = begin_space + "((?!al-)\\S+)(ẗ)(\\s+)(al-)(.*)"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(text); + + while(matcher.find()){ + + if(debug) System.out.println("2.A"); + } + */ + /* + //String regex = "(.*)(\\s+)((?!al-)\\S+)(ẗ)(\\s+)(al-)(.*)"; + String regex = "(.*)(ẗ)(\\s+|(?!al-)\\S*)(.*)"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(text); + + while(matcher.find()){ + System.out.println(matcher.groupCount()); + + System.out.println(matcher.group(1) + " # " + matcher.group(2) + " # " + matcher.group(3) + " # " + matcher.group(4)); + } + */ + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/TransliterationUtil.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,267 @@ +package org.mpi.openmind.repository.utils; + +import java.io.UnsupportedEncodingException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; + +public class TransliterationUtil { + + private static Logger logger = Logger.getLogger(TransliterationUtil.class); + + private static Map<Character, Character> CHARMAP = new HashMap<Character, Character>(); + private static Map<String, String> CONVERSIONMAP = new HashMap<String, String>(); + private static List<String> CONVERSIONLIST = new ArrayList<String>(); + + static { + // Arabic / Latin + addToCharMap('\u0623', '\u0061'); + addToCharMap('\u0627', '\u0101'); + addToCharMap('\u0622', '\u0101'); + addToCharMap('\u0625', '\u0069'); + addToCharMap('\u0628', '\u0062'); + addToCharMap('\u062A', '\u0074'); + addToCharMap('\u062B', '\u1E6F'); + addToCharMap('\u062C', '\u006A'); + addToCharMap('\u062D', '\u1E25'); + addToCharMap('\u062E', '\u1E2B'); + addToCharMap('\u062F', '\u0064'); + addToCharMap('\u0630', '\u1E0F'); + addToCharMap('\u0631', '\u0072'); + addToCharMap('\u0632', '\u007A'); + addToCharMap('\u0633', '\u0073'); + addToCharMap('\u0634', '\u0161'); + addToCharMap('\u0635', '\u1E63'); + addToCharMap('\u0636', '\u1E0D'); + addToCharMap('\u0637', '\u1E6D'); + addToCharMap('\u0638', '\u1E93'); + addToCharMap('\u0639', '\u02BF'); + addToCharMap('\u063A', '\u0121'); + addToCharMap('\u0641', '\u0066'); + addToCharMap('\u0642', '\u0071'); + addToCharMap('\u0643', '\u006B'); + addToCharMap('\u0644', '\u006C'); + addToCharMap('\u0645', '\u006D'); + addToCharMap('\u0646', '\u006E'); + addToCharMap('\u0647', '\u0068'); + addToCharMap('\u0648', '\u0077'); + addToCharMap('\u064A', '\u0079'); + addToCharMap('\u0621', '\u02BE'); + addToCharMap('\u0626', '\u02BE'); + + //Chantal said replace this + //addToCharMap('\u0629', '\u0068'); + addToCharMap('\u0629', '\u1E97'); + + addToCharMap('\u064E', '\u0061'); + addToCharMap('\u0650', '\u0069'); + // addToCharMap('\u0652',''); + addToCharMap('\u064F', '\u0075'); + + + //chantal start + addToStringList("\u0020\u0627\u0644", "\u0020\u0061\u006C\u002D"); + addToStringList("\u0629\u0020\u0627\u0644","\u0074\u0020\u0061\u006C\u002D"); + addToStringList("\u0623\u064F\u0648\u0652", "\u016B"); + addToStringList("\u064F\u0648\u0652", "\u016B"); + addToStringList("\u0650\u064A\u0652", "\u012B"); + //chantal end + + addToStringList("\u064F\u0648", "\u016B"); + //addToStringList("\u0650\u064A", "\u012B"); no in the table table 2014.04 + addToStringList("\u064E\u064A", "\u0061\u0079"); + addToStringList("\u064E\u0648", "\u0061\u0077"); + addToStringList("\u0652\u064A", "\u0079"); + addToStringList("\u0652\u0648", "\u0077"); + addToStringList("\u0020\u0627\u0644", "\u0020\u0061\u006C\u002D"); + addToStringList("\u0629\u0020\u0627\u0644", + "\u0074\u0020\u0061\u006C\u002D"); + + /* + * addToCharMap('\u1E0d','\u0064');//ḍ -> d + * addToCharMap('\u1E25','\u0068');//ḥ -> h + * addToCharMap('\u012B','\u0069');//ī -> i + * addToCharMap('\u1E63','\u0073');//ṣ -> s + * addToCharMap('\u1E6D','\u0074');//ṭ -> t + * addToCharMap('\u016b','\u0075');//ū -> u + * addToCharMap('\u0101','\u0061');//ā -> a + */ + + // Chantal Recommendations: + addToCharMap('\u0649', '\u1EF3');// ى->ỳ + addToCharMap('\u0624', '\u02BE');// ؤ->ʾ + addToCharMap('\u0670', '\u0101'); // َٰ -> ā + + + addToStringList("\u0623\u064E", "\u0061"); + addToStringList("\u0625\u0650", "\u0069"); + addToStringList("\u064E\u0649", "\u1EF3"); + addToStringList("\u0623\u064F", "\u0075"); // أُ -> i + addToStringList("\u064E\u0627", "\u0101"); // َا -> ā + addToStringList("\u064B", "\u0061\u006E"); // ًَٰ-> an + addToStringList("\u064D", "\u0069\u006E"); // ٍَٰ-> in + addToStringList("\u064C", "\u0075\u006E"); // ٍَٰ-> un + addToStringList("\u0652", ""); + + } + + private static String stringToUnicode(String s){ + StringBuilder sb = new StringBuilder(); + for(char ch : s.toCharArray()){ + sb.append(toUnicode(ch)); + } + return sb.toString(); + } + + private static String toUnicode(char ch) { + return String.format("\\u%04x", (int) ch); + } + + private static void addToCharMap(char arabCh, char latCh) { + CHARMAP.put(arabCh, latCh); + } + + private static void addToStringList(String arabStr, String latStr) { + CONVERSIONMAP.put(arabStr, latStr); + CONVERSIONLIST.add(arabStr); + } + + public static String getTransliteration(final String text) { + + String replacementText = new String(text); + replacementText = duplication(replacementText); + /* + * for (int i = 0; i < replacementText.length(); i++) { + * replacementText.charAt(i); replacementText.codePointAt(i); } + */ + //System.out.println("^^^^^^^^^^^^^^^^"); + for (String ar : CONVERSIONLIST) { + String lat = CONVERSIONMAP.get(ar); + /*System.out.println( + stringToUnicode(ar) + + "\n" + + stringToUnicode(lat)); + */ + if (replacementText.contains(ar)) { + //System.out.println("ok"); + replacementText = replacementText.replace(ar, lat); + } + //System.out.println("--------------"); + } + //System.out.println("^^^^^^^^^^^"); + for (char ar : CHARMAP.keySet()) { + char lat = CHARMAP.get(ar); + if (replacementText.indexOf(ar) > -1) { + replacementText = replacementText.replace(ar, lat); + } + } + + //logger.debug("Transliteration from: " + text + ", to: " + // + replacementText); + return replacementText; + } + + private static final Map<String, String> duplicationConsonantVowelMap; + private static final Map<String, String> duplicationVowelConsonantMap; + + static { + duplicationConsonantVowelMap = new HashMap<String, String>(); + duplicationConsonantVowelMap.put("(.)\u0650\u0651", "\u0650"); + duplicationConsonantVowelMap.put("(.)\u064B\u0651", "\u064B"); + duplicationConsonantVowelMap.put("(.)\u064C\u0651", "\u064B"); + duplicationConsonantVowelMap.put("(.)\u064D\u0651", "\u064D"); + duplicationConsonantVowelMap.put("(.)\u064E\u0651", "\u064E"); + duplicationConsonantVowelMap.put("(.)\u064F\u0651", "\u064F"); + + duplicationVowelConsonantMap = new HashMap<String, String>(); + duplicationVowelConsonantMap.put("\u0650(.)\u0651", "\u0650"); + duplicationVowelConsonantMap.put("\u064B(.)\u0651", "\u064B"); + duplicationVowelConsonantMap.put("\u064C(.)\u0651", "\u064B"); + duplicationVowelConsonantMap.put("\u064D(.)\u0651", "\u064D"); + duplicationVowelConsonantMap.put("\u064E(.)\u0651", "\u064E"); + duplicationVowelConsonantMap.put("\u064F(.)\u0651", "\u064F"); + + } + + private static String duplication(String text) { + // for(String duplicationTerm : duplicationMap){ + // text = text.replaceAll("(.)" + duplicationTerm, "$1$1"); + // } + // text = text.replaceAll("(.)(.)\u0651", "$1$1$2"); + for (String key : duplicationConsonantVowelMap.keySet()) { + text = text.replaceAll(key, + "$1$1" + duplicationConsonantVowelMap.get(key)); + } + + for (String key : duplicationVowelConsonantMap.keySet()) { + text = text.replaceAll(key, duplicationVowelConsonantMap.get(key) + + "$1$1"); + } + + return text; + } + + public static void printHexCharacters(String s) { + for (char ch : s.toCharArray()) { + String hex = String.format("%04x", (int) ch); + System.out.println(hex); + } + + } + + private static String test(String term) { + TransliterationUtil.printHexCharacters(term); + String s = TransliterationUtil.getTransliteration(term); + System.out.println(s); + TransliterationUtil.printHexCharacters(s); + return s; + } + + public static String changeCharInPosition(int position, char ch, String str) { + char[] charArray = str.toCharArray(); + charArray[position] = ch; + return new String(charArray); + } + + /** + * @param args + * @throws UnsupportedEncodingException + */ + public static void main(String[] args) throws UnsupportedEncodingException { + // TransliterationUtil.getTransliteration("ṣīṭūāḥḍ"); + // System.out.println(TransliterationUtil.getTransliteration("رسالة اسطرلاب")); + // System.out.println(TransliterationUtil.getTransliteration("\u0101"+"رسالة اسطرلاب")); + // System.out.println(TransliterationUtil.getTransliteration("أُصُوْل")); + // TransliterationUtil.printHexCharacters("أُصُوْل"); + + // System.out.println(TransliterationUtil.duplication("abcd11")); + + // System.out.println(TransliterationUtil.getTransliteration("حُجَّة")); + // TransliterationUtil.printHexCharacters("حُجَّة"); + + // System.out.println(TransliterationUtil.getTransliteration("مُحَمَّد")); + // TransliterationUtil.printHexCharacters("مُحَمَّد"); + // TransliterationUtil.test("مُسَمَّة"); + // TransliterationUtil.test("حُبّ"); + // TransliterationUtil.test("حُبٌّ"); + // TransliterationUtil.test("الرَشِيْد"); + + /* + * String s = TransliterationUtil.test("مَكْتُوب"); + * TransliterationUtil.printHexCharacters(s); + * System.out.println("******"); + * TransliterationUtil.printHexCharacters("makْtūb"); + */ + + //TransliterationUtil.test("أُوْلَى"); + //TransliterationUtil.test("قِيَامَة"); + //TransliterationUtil.printHexCharacters("قِيَامَة"); + + System.out.println("\u7831"); + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,280 @@ +package org.mpi.openmind.repository.utils; + +//JAXP +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; +import java.io.*; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + + + +/** + * + * @author Jorge Urzúa + */ +public class XMLUtil { + + private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + + public static String OWN_VALUE_CONFIG = "own-value-configuration"; + public static String OWN_VALUE_RULES = "own-value-rules"; + public static String OWN_VALUE_RULE = "own-value-rule"; + public static String TARGET_RELATION = "target-relation"; + public static String SOURCE_RELATION = "source-relation"; + public static String SUBSTRING = "substring"; + public static String NAME = "name"; + public static String SOURCE = "source"; + public static String TARGET = "target"; + public static String END_NODE = "end-node"; + public static String XOR = "xor"; + public static String RULE = "rule"; + public static String REF = "ref"; + public static String VALUE = "value"; + public static String TYPE = "type"; + public static String CONSTANT = "constant"; + + public static String PRINT_RULES = "print-rules"; + public static String PRINT_RULE = "print-rule"; + + + public static String OPENMIND_DATA = "openmind-data"; + public static String ENTITIES = "entities"; + public static String ENTITY = "entity"; + public static String ENTITY_ID = "id"; + public static String ATTRIBUTES = "attributes"; + public static String ATTRIBUTE = "attribute"; + public static String ATTRIBUTE_NAME = "name"; + public static String ATTRIBUTE_VALUE = "value"; + public static String RELATION_SOURCE_ID = "source-id"; + public static String RELATION_TARGET_ID = "target-id"; + public static String RELATION_ID = "id"; + public static String VIEW = "view"; + public static String VIEWS = "views"; + public static String MODIFICATION_TIME = "modification-time"; + public static String VERSION = "version"; + public static String ID = "id"; + public static String CONCEPTS = "concepts"; + public static String CONCEPT = "concept"; + public static String ASSERTIONS = "assertions"; + public static String ASSERTION = "assertion"; + + //names used by the previous version of ismi. + public static String META_DATA = "openmind-meta"; + public static String DEFINITIONS = "definitions"; + public static String DEFINITION = "definition"; + public static String LABEL = "label"; + public static String MAIN_LABEL = "main-label"; + public static String OBJECT_CLASS = "object-class"; + public static String OWN_VALUE = "own-value"; + public static String LABEL_NAME = "name"; + public static String RELATIONS = "relations"; + public static String RELATION = "relation"; + public static String RELATION_LABEL = "label"; + public static String RELATION_SOURCE = "source-class"; + public static String RELATION_TARGET = "target-class"; + public static String CONTENT_TYPE = "content-type"; + + public static String INSERTION_MODE = "insertion-mode"; + public static String MERGE = "merge"; + public static String REPLACE = "replace"; + public static String INSERT = "insert"; + + public static Node getNodeByName(NodeList nodeList, String name){ + for(int i=0; i < nodeList.getLength(); i++){ + Node tmp = nodeList.item(i); + if(tmp.getNodeName().equals(name)) + return tmp; + } + return null; + } + + /** Parses XML file and returns XML document. + * @param fileName XML file to parse + * @return XML document or <B>null</B> if error occured + */ + public static Document getDocument(String fileName) { + //System.out.println("Parsing XML file... " + fileName); + DocumentBuilder docBuilder; + Document doc = null; + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + docBuilderFactory.setIgnoringElementContentWhitespace(true); + try { + docBuilder = docBuilderFactory.newDocumentBuilder(); + } + catch (ParserConfigurationException e) { + System.out.println("Wrong parser configuration: " + e.getMessage()); + e.printStackTrace(); + return null; + } + File sourceFile = new File(fileName); + try { + doc = docBuilder.parse(sourceFile); + } + catch (SAXException e) { + System.out.println("Wrong XML file structure: " + e.getMessage()); + e.printStackTrace(); + return null; + } + catch (IOException e) { + System.out.println("Could not read source file: " + e.getMessage()); + e.printStackTrace(); + } + //System.out.println("XML file parsed"); + return doc; + } + + /** + * This method looks in the class-path for a file with the name given in input. + * @param fileName + * @return + */ + public static Document getDocumentFromPathContext(String fileName) { + //System.out.println("Parsing XML file... " + fileName); + DocumentBuilder docBuilder; + Document doc = null; + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + docBuilderFactory.setIgnoringElementContentWhitespace(true); + try { + docBuilder = docBuilderFactory.newDocumentBuilder(); + } + catch (ParserConfigurationException e) { + System.out.println("Wrong parser configuration: " + e.getMessage()); + e.printStackTrace(); + return null; + } + + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + InputStream stream = classLoader.getResourceAsStream( fileName ); + try { + doc = docBuilder.parse(stream); + } + catch (SAXException e) { + System.out.println("Wrong XML file structure: " + e.getMessage()); + e.printStackTrace(); + return null; + } + catch (IOException e) { + System.out.println("Could not read source file: " + e.getMessage()); + e.printStackTrace(); + } + //System.out.println("XML file parsed"); + return doc; + } + + /** Saves XML Document into XML file. + * @param fileName XML file name + * @param doc XML document to save + * @return <B>true</B> if method success <B>false</B> otherwise + */ + static public boolean saveXMLDocument(String fileName, Document doc) { + + File xmlOutputFile = new File(fileName); + FileOutputStream fos; + Transformer transformer; + try { + fos = new FileOutputStream(xmlOutputFile); + } + catch (FileNotFoundException e) { + System.out.println("Error occured: " + e.getMessage()); + return false; + } + // Use a Transformer for output + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + try { + transformer = transformerFactory.newTransformer(); + } + catch (TransformerConfigurationException e) { + System.out.println("Transformer configuration error: " + e.getMessage()); + return false; + } + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(fos); + // transform source into result will do save + try { + transformer.transform(source, result); + } + catch (TransformerException e) { + System.out.println("Error transform: " + e.getMessage()); + } + System.out.println("XML file saved."); + return true; + } + + public static String transformateDocumentToString(Document doc){ + try{ + DOMSource domSource = new DOMSource(doc); + StringWriter writer = new StringWriter(); + StreamResult result = new StreamResult(writer); + + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.transform(domSource, result); + return writer.toString(); + }catch(Exception e){ + e.printStackTrace(); + } + return null; + } + + public String ptransformateDocumentToString(Document doc){ + try{ + DOMSource domSource = new DOMSource(doc); + StringWriter writer = new StringWriter(); + StreamResult result = new StreamResult(writer); + + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.transform(domSource, result); + return writer.toString(); + }catch(Exception e){ + e.printStackTrace(); + } + return null; + } + + public static Document transformateStringToDocument(String text){ + try{ + DocumentBuilder db = dbf.newDocumentBuilder(); + InputStream stream = new ByteArrayInputStream(text.getBytes("UTF-8")); + return db.parse(stream); + }catch(Exception e){ + e.printStackTrace(); + } + return null; + } + + /** + * <p>This method transforms the file + * input using the a stylesheet giving as input</p> + * + * @param xsl is the stylesheet file used to transformate the input document. + * @param input is the document, which will be transformated. + * @return the document transformed by the xsl stylesheet. + */ + public static Document xmlToXmlTransformation(Document xsl, Document input){ + try{ + TransformerFactory factory = TransformerFactory.newInstance(); + + InputStream stream = new ByteArrayInputStream(XMLUtil.transformateDocumentToString(xsl).getBytes("UTF-8")); + //Transformer transformer = factory.newTransformer(new DOMSource(xsl)); + //Transformer transformer = factory.newTransformer(new StreamSource("transformatorAxel1.xsl")); + Transformer transformer = factory.newTransformer(new StreamSource(stream)); + DOMResult result = new DOMResult(); + //transformer.transform(new DOMSource(input), new StreamResult(new FileOutputStream("mi.xml"))); + transformer.transform(new DOMSource(input), result); + + return (Document)result.getNode(); + }catch(Exception e){ + e.printStackTrace(); + } + return null; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/ismi/ISMICalendar.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,564 @@ +package org.mpi.openmind.repository.utils.ismi; + +import java.io.Serializable; + +import org.apache.commons.lang.StringUtils; +import org.joda.time.DateTime; +import org.joda.time.chrono.GregorianChronology; +import org.joda.time.chrono.IslamicChronology; +import org.joda.time.chrono.JulianChronology; +import org.json.JSONException; +import org.json.JSONObject; + + +public class ISMICalendar implements Serializable { + private static final long serialVersionUID = 1L; + + public static String FROM = "from"; + public static String UNTIL = "until"; + public static String YEAR = "year"; + public static String ADD_INF = "additional_info"; + public static String CALENDAR_TYPE = "calendar_type"; + public static String INPUT_FORM = "input_form"; + public static String STATE = "state"; + public static String DATE = "date"; + public static String DATE_IN_TEXT = "date_in_text"; + + public static String INPUT_FORM_YEAR = "Year"; + public static String INPUT_FORM_DATE = "Date"; + public static String INPUT_FORM_FULL_DATE = "Full date"; + public static String INPUT_FORM_RANGE = "Range"; + public static String INPUT_FORM_FULL_RANGE = "Full range"; + + public static String TYPE_GREGORIAN = "Gregorian"; + public static String TYPE_ISLAMIC = "Islamic"; + public static String TYPE_JULIAN = "Julian"; + + public static String STATE_UNKNOWN = "unknown"; + public static String STATE_KNOWN = "known"; + public static String STATE_NOT_CHECKED = "not checked"; + + private String inputForm; + private String calendarType; + + private ISMIDate fromGregorian = new ISMIDate(); + private ISMIDate untilGregorian = new ISMIDate(); + private String additionalInfo; + + private Integer currentYear; + private ISMIDate currentFrom = new ISMIDate(); + private ISMIDate currentUntil = new ISMIDate(); + + private String dateInText; + + private String state; + + public ISMICalendar(){ + this.calendarType = TYPE_GREGORIAN; + this.inputForm = INPUT_FORM_YEAR; + this.state = STATE_NOT_CHECKED; + } + + public ISMICalendar(String jsonString){ + if(StringUtils.isNotEmpty(jsonString)){ + try { + JSONObject json = new JSONObject(jsonString); + this.state = json.getString(STATE); + if(StringUtils.isNotEmpty(state)){ + if(state.equals(STATE_KNOWN)){ + + this.additionalInfo = json.getString(ADD_INF); + this.calendarType = json.getString(CALENDAR_TYPE); + this.inputForm = json.getString(INPUT_FORM); + + if(inputForm.equals(INPUT_FORM_YEAR)){ + this.fromGregorian = new ISMIDate(json.getJSONObject(FROM)); + this.untilGregorian = new ISMIDate(json.getJSONObject(UNTIL)); + this.currentYear = json.getInt(YEAR); + }else if(inputForm.equals(INPUT_FORM_RANGE)){ + this.fromGregorian = new ISMIDate(json.getJSONObject(FROM)); + this.untilGregorian = new ISMIDate(json.getJSONObject(UNTIL)); + if(calendarType.equals(TYPE_GREGORIAN)){ + this.currentFrom = new ISMIDate(json.getJSONObject(FROM)); + this.currentUntil = new ISMIDate(json.getJSONObject(UNTIL)); + }else if(calendarType.equals(TYPE_ISLAMIC)){ + this.currentFrom = new ISMIDate(this.fromGregorian.getIslamicDateTime()); + this.currentUntil = new ISMIDate(this.untilGregorian.getIslamicDateTime()); + }else if(calendarType.equals(TYPE_JULIAN)){ + this.currentFrom = new ISMIDate(this.fromGregorian.getJulianDateTime()); + this.currentUntil = new ISMIDate(this.untilGregorian.getJulianDateTime()); + } + }else if(inputForm.equals(INPUT_FORM_DATE)){ + this.fromGregorian = new ISMIDate(json.getJSONObject(DATE)); + if(calendarType.equals(TYPE_GREGORIAN)){ + this.currentFrom = new ISMIDate(json.getJSONObject(DATE)); + }else if(calendarType.equals(TYPE_ISLAMIC)){ + this.currentFrom = new ISMIDate(this.fromGregorian.getIslamicDateTime()); + }else if(calendarType.equals(TYPE_JULIAN)){ + this.currentFrom = new ISMIDate(this.fromGregorian.getJulianDateTime()); + } + } + + if(json.has("dayOfWeekFrom")){ + this.currentFrom.setDayOfWeek(json.getInt("dayOfWeekFrom")); + } + if(json.has("dayOfWeekUntil")){ + this.currentUntil.setDayOfWeek(json.getInt("dayOfWeekUntil")); + } + if(json.has("dayOfWeek")){ + this.currentFrom.setDayOfWeek(json.getInt("dayOfWeek")); + } + + }else if(state.equals(STATE_NOT_CHECKED) || state.equals(STATE_UNKNOWN)){ + if(json.has(DATE_IN_TEXT)){ + this.dateInText = json.getString(DATE_IN_TEXT); + } + this.calendarType = TYPE_GREGORIAN; + this.inputForm = INPUT_FORM_YEAR; + } + } + + } catch (Exception e) { + e.printStackTrace(); + this.state = STATE_NOT_CHECKED; + this.calendarType = TYPE_GREGORIAN; + this.inputForm = INPUT_FORM_YEAR; + } + }else{ + this.calendarType = TYPE_GREGORIAN; + this.inputForm = INPUT_FORM_YEAR; + this.state = STATE_NOT_CHECKED; + } + } + + public void update(){ + + if(inputForm.equals(INPUT_FORM_RANGE)){ + if(isUntilOlderThanFrom()){ + if(calendarType.equals(TYPE_GREGORIAN)){ + this.inputGregorianRange(); + }else if(calendarType.equals(TYPE_ISLAMIC)){ + this.inputIslamicRange(); + }else if(calendarType.equals(TYPE_JULIAN)){ + this.inputJulianRange(); + } + } + + }else if(inputForm.equals(INPUT_FORM_YEAR)){ + if(calendarType.equals(TYPE_GREGORIAN)){ + this.inputGregorianYear(); + }else if(calendarType.equals(TYPE_ISLAMIC)){ + this.inputIslamicYear(); + }else if(calendarType.equals(TYPE_JULIAN)){ + this.inputJulianYear(); + } + }else if(inputForm.equals(INPUT_FORM_DATE)){ + if(calendarType.equals(TYPE_GREGORIAN)){ + this.inputGregorianFrom(); + }else if(calendarType.equals(TYPE_ISLAMIC)){ + this.inputIslamicFrom(); + }else if(calendarType.equals(TYPE_JULIAN)){ + this.inputJulianFrom(); + } + } + } + + public boolean isDeployable(){ + return (!this.state.equals(STATE_NOT_CHECKED) && !this.state.equals(STATE_UNKNOWN)); + } + + public String getFormattedRange(){ + if(inputForm != null){ + String from = this.fromGregorian.toString(); + if(this.inputForm.equals(INPUT_FORM_DATE)){ + return "[" + from + "]"; + }else{ + + String until = this.untilGregorian.toString(); + if(StringUtils.isNotEmpty(from) && StringUtils.isNotEmpty(until)){ + return "[FROM: " + from + " TO: " + until + "]"; + } + } + } + return null; + } + + public String getFormattedIslamicRange(){ + if(inputForm != null){ + String from = this.fromGregorian.toIslamicString(); + if(this.inputForm.equals(INPUT_FORM_DATE)){ + return "[" + from + "]"; + }else{ + String until = this.untilGregorian.toIslamicString(); + if(StringUtils.isNotEmpty(from) && StringUtils.isNotEmpty(until)){ + return "[FROM: " + from + " TO: " + until + "]"; + } + } + } + return null; + } + + public String getFormattedJulianRange(){ + if(inputForm != null){ + String from = this.fromGregorian.toJulianString(); + if(this.inputForm.equals(INPUT_FORM_DATE)){ + return "[" + from + "]"; + }else{ + String until = this.untilGregorian.toJulianString(); + if(StringUtils.isNotEmpty(from) && StringUtils.isNotEmpty(until)){ + return "[FROM: " + from + " TO: " + until + "]"; + } + } + } + return null; + } + + public String getCalendarAsHtml(){ + StringBuilder sb = new StringBuilder(); + + if(isDeployable()){ + sb.append("<table align=\"left\">"); + + sb.append("<tr>"); + sb.append("<th align=\"left\">Gregorian:</th>"); + sb.append("<th align=\"left\">" + this.getFormattedRange() + "</th>"); + sb.append("</tr>"); + + sb.append("<tr>"); + sb.append("<th align=\"left\">Islamic:</th>"); + sb.append("<th align=\"left\">" + this.getFormattedIslamicRange() + "</th>"); + sb.append("</tr>"); + + sb.append("<tr>"); + sb.append("<th align=\"left\">Julian:</th>"); + sb.append("<th align=\"left\">" + this.getFormattedJulianRange() + "</th>"); + sb.append("</tr>"); + + sb.append("</table>"); + }else if(this.state.equals(STATE_UNKNOWN)){ + sb.append("Unkknown"); + }else{ + sb.append("Not checked"); + if(StringUtils.isNotEmpty(this.dateInText)){ + sb.append("=" + this.dateInText); + } + } + + return sb.toString(); + } + + + private void inputGregorianRange(){ + this.inputGregorianFrom(); + try{ + DateTime gregrorian = + new DateTime(currentUntil.getYear(), currentUntil.getMonth(), currentUntil.getDayOfMonth(), 0, 0, 0, 0, GregorianChronology.getInstance()); + this.untilGregorian = new ISMIDate(gregrorian.withChronology(GregorianChronology.getInstance())); + if(isDayInWeek(currentUntil.getDayOfWeek())){ + this.untilGregorian.setDayOfWeek(currentUntil.getDayOfWeek()); + } + }catch (Exception e) { + //this.addGeneralMsg("In until date - " + e.getMessage()); + e.printStackTrace(); + } + } + + private void inputGregorianFrom(){ + try{ + DateTime gregorian = + new DateTime(currentFrom.getYear(), currentFrom.getMonth(), currentFrom.getDayOfMonth(), 0, 0, 0, 0, GregorianChronology.getInstance()); + this.fromGregorian = new ISMIDate(gregorian); + if(isDayInWeek(currentFrom.getDayOfWeek())){ + this.fromGregorian.setDayOfWeek(currentFrom.getDayOfWeek()); + } + }catch (Exception e) { + //this.addGeneralMsg("In from date - " + e.getMessage()); + e.printStackTrace(); + } + } + + private void inputIslamicRange(){ + this.inputIslamicFrom(); + try{ + DateTime islamic = + new DateTime(currentUntil.getYear(), currentUntil.getMonth(), currentUntil.getDayOfMonth(), 0, 0, 0, 0, IslamicChronology.getInstance()); + DateTime gregorian = islamic.withChronology(GregorianChronology.getInstance()); + this.untilGregorian = new ISMIDate(gregorian); + if(isDayInWeek(currentUntil.getDayOfWeek())){ + this.untilGregorian.setDayOfWeek(gregorian.getDayOfWeek()); + }else{ + this.untilGregorian.setAmbiguity(2); + } + }catch (Exception e) { + //this.addGeneralMsg("In until date - " + e.getMessage()); + e.printStackTrace(); + } + } + + private void inputIslamicFrom(){ + try{ + DateTime islamic = + new DateTime(currentFrom.getYear(), currentFrom.getMonth(), currentFrom.getDayOfMonth(), 0, 0, 0, 0, IslamicChronology.getInstance()); + DateTime gregorian = islamic.withChronology(GregorianChronology.getInstance()); + this.fromGregorian = new ISMIDate(gregorian); + if(isDayInWeek(currentFrom.getDayOfWeek())){ + this.fromGregorian.setDayOfWeek(gregorian.getDayOfWeek()); + }else{ + this.fromGregorian.setAmbiguity(2); + } + }catch (Exception e) { + //this.addGeneralMsg("In from date - " + e.getMessage()); + e.printStackTrace(); + } + } + + + private void inputJulianRange(){ + this.inputJulianFrom(); + DateTime julian = null; + try{ + julian = + new DateTime(currentUntil.getYear(), currentUntil.getMonth(), currentUntil.getDayOfMonth(), 0, 0, 0, 0, JulianChronology.getInstance()); + DateTime gregorian = julian.withChronology(GregorianChronology.getInstance()); + this.untilGregorian = new ISMIDate(gregorian); + if(isDayInWeek(currentUntil.getDayOfWeek())){ + this.untilGregorian.setDayOfWeek(gregorian.getDayOfWeek()); + } + }catch (Exception e) { + //this.addGeneralMsg("In until date - " + e.getMessage()); + e.printStackTrace(); + } + } + + private void inputJulianFrom(){ + DateTime julian = null; + try{ + julian = + new DateTime(currentFrom.getYear(), currentFrom.getMonth(), currentFrom.getDayOfMonth(), 0, 0, 0, 0, JulianChronology.getInstance()); + DateTime gregorian = julian.withChronology(GregorianChronology.getInstance()); + this.fromGregorian = new ISMIDate(gregorian); + if(isDayInWeek(currentFrom.getDayOfWeek())){ + this.fromGregorian.setDayOfWeek(gregorian.getDayOfWeek()); + } + }catch (Exception e) { + //addGeneralMsg("In from date - " + e.getMessage()); + e.printStackTrace(); + } + } + + public void inputGregorianYear(){ + if(inputForm.equals(INPUT_FORM_YEAR) && currentYear != null){ + this.fromGregorian = new ISMIDate(new DateTime(currentYear, 1, 1, 0, 0, 0, 0, GregorianChronology.getInstance())); + this.untilGregorian = new ISMIDate(new DateTime(currentYear, 12, 31, 0, 0, 0, 0, GregorianChronology.getInstance())); + } + } + + public void inputIslamicYear(){ + if(this.currentYear < 1 || this.currentYear > 292271022){ + //this.addGeneralMsg("year must be in the range [1,292271022]"); + }else { + DateTime islamic = + new DateTime(currentYear, 1, 1, 0, 0, 0, 0, IslamicChronology.getInstance()); + DateTime gregorian = islamic.withChronology(GregorianChronology.getInstance()); + this.fromGregorian = new ISMIDate(gregorian); + this.fromGregorian.setAmbiguity(2); + this.fromGregorian.setDayOfWeek(null); + + islamic = + new DateTime(currentYear, 12, 29, 0, 0, 0, 0, IslamicChronology.getInstance()); + gregorian = islamic.withChronology(GregorianChronology.getInstance()); + this.untilGregorian = new ISMIDate(gregorian); + this.untilGregorian.setAmbiguity(2); + this.untilGregorian.setDayOfWeek(null); + } + } + + public void inputJulianYear(){ + if(this.currentYear < 1){ + //this.addGeneralMsg("Value 0 for year is not supported"); + }else{ + DateTime julian = + new DateTime(currentYear, 1, 1, 0, 0, 0, 0, JulianChronology.getInstance()); + DateTime gregorian = julian.withChronology(GregorianChronology.getInstance()); + this.fromGregorian = new ISMIDate(gregorian); + this.fromGregorian.setDayOfWeek(null); + + julian = + new DateTime(currentYear, 12, 31, 0, 0, 0, 0, JulianChronology.getInstance()); + gregorian = julian.withChronology(GregorianChronology.getInstance()); + this.untilGregorian = new ISMIDate(gregorian); + this.untilGregorian.setDayOfWeek(null); + } + } + + + public String toJSONString(){ + this.update(); + JSONObject json = this.toJSON(); + if(json != null){ + return json.toString(); + } + return ""; + } + + public JSONObject toJSON(){ + JSONObject json = new JSONObject(); + if(state.equals(STATE_KNOWN)){ + try { + + if(inputForm.equals(INPUT_FORM_YEAR)){ + json.put(YEAR, this.currentYear); + json.put(FROM, this.fromGregorian.toJSON()); + json.put(UNTIL, this.untilGregorian.toJSON()); + }else if(inputForm.equals(INPUT_FORM_RANGE)){ + json.put(FROM, this.fromGregorian.toJSON()); + json.put(UNTIL, this.untilGregorian.toJSON()); + if(isDayInWeek(currentFrom.getDayOfWeek())){ + json.put("dayOfWeekFrom", currentFrom.getDayOfWeek()); + } + if(isDayInWeek(currentUntil.getDayOfWeek())){ + json.put("dayOfWeekUntil", currentUntil.getDayOfWeek()); + } + }else if(inputForm.equals(INPUT_FORM_DATE)){ + json.put(DATE, this.fromGregorian.toJSON()); + if(isDayInWeek(currentFrom.getDayOfWeek())){ + json.put("dayOfWeek", currentFrom.getDayOfWeek()); + } + } + + json.put(STATE, this.state); + json.put(ADD_INF, additionalInfo); + json.put(CALENDAR_TYPE, this.calendarType); + json.put(INPUT_FORM, this.inputForm); + + } catch (JSONException e) { + e.printStackTrace(); + } + return json; + }else if(state.equals(STATE_UNKNOWN)){ + try { + json.put(STATE, this.state); + } catch (JSONException e) { + e.printStackTrace(); + } + return json; + }else if(state.equals(STATE_NOT_CHECKED) && StringUtils.isNotEmpty(dateInText)){ + try { + json.put(STATE, this.state); + json.put(DATE_IN_TEXT, this.dateInText); + } catch (JSONException e) { + e.printStackTrace(); + } + return json; + } + return null; + } + + public boolean isUntilOlderThanFrom(){ + if(this.currentFrom.getYear() != null && this.currentUntil.getYear() != null){ + if(this.currentFrom.getYear() < this.currentUntil.getYear()){ + return true; + }else if(this.currentFrom.getYear().equals(this.currentUntil.getYear())){ + if(this.currentFrom.getMonth() != null && this.currentUntil.getMonth() != null){ + if(this.currentFrom.getMonth() < this.currentUntil.getMonth()){ + return true; + }else if(this.currentFrom.getMonth().equals(this.currentUntil.getMonth())){ + if(this.currentFrom.getDayOfMonth() != null && this.currentUntil.getDayOfMonth() != null){ + if(this.currentFrom.getDayOfMonth() < this.currentUntil.getDayOfMonth()){ + return true; + } + } + } + } + } + } + return false; + } + + private boolean isDayInWeek(Integer day){ + if(day == null) + return false; + if(day >= 1 && day <= 7){ + return true; + } + return false; + } + + public ISMIDate getFromGregorian() { + return fromGregorian; + } + public void setFromGregorian(ISMIDate fromGregorian) { + this.fromGregorian = fromGregorian; + } + public ISMIDate getUntilGregorian() { + return untilGregorian; + } + public void setUntilGregorian(ISMIDate untilGregorian) { + this.untilGregorian = untilGregorian; + } + + public String getInputForm() { + return inputForm; + } + + public void setInputForm(String inputForm) { + this.inputForm = inputForm; + } + + public String getCalendarType() { + return calendarType; + } + + public void setCalendarType(String calendarType) { + this.calendarType = calendarType; + } + + public String getAdditionalInfo() { + return additionalInfo; + } + + public void setAdditionalInfo(String additionalInfo) { + this.additionalInfo = additionalInfo; + } + + public Integer getCurrentYear() { + return currentYear; + } + + public void setCurrentYear(Integer currentYear) { + this.currentYear = currentYear; + } + + public ISMIDate getCurrentFrom() { + return currentFrom; + } + + public void setCurrentFrom(ISMIDate currentFrom) { + this.currentFrom = currentFrom; + } + + public ISMIDate getCurrentUntil() { + return currentUntil; + } + + public void setCurrentUntil(ISMIDate currentUntil) { + this.currentUntil = currentUntil; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getDateInText() { + return dateInText; + } + + public void setDateInText(String dateInText) { + this.dateInText = dateInText; + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/repository/utils/ismi/ISMIDate.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,306 @@ +package org.mpi.openmind.repository.utils.ismi; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +import org.joda.time.DateTime; +import org.joda.time.chrono.GregorianChronology; +import org.joda.time.chrono.IslamicChronology; +import org.joda.time.chrono.JulianChronology; +import org.json.JSONException; +import org.json.JSONObject; + + +public class ISMIDate implements Serializable{ + private static final long serialVersionUID = -6470414082851873885L; + + public static String AMBIGUITY = "2"; + + private static Map<Integer, String> gregorianMonths = new HashMap<Integer, String>(12); + private static Map<Integer, String> islamicMonths = new HashMap<Integer, String>(12); + private static Map<Integer, String> julianMonths = new HashMap<Integer, String>(12); + + static{ + + gregorianMonths.put(1, "January (1)"); + gregorianMonths.put(2, "February (2)"); + gregorianMonths.put(3, "March (3)"); + gregorianMonths.put(4, "April (4)"); + gregorianMonths.put(5, "May (5)"); + gregorianMonths.put(6, "June (6)"); + gregorianMonths.put(7, "July (7)"); + gregorianMonths.put(8, "August (8)"); + gregorianMonths.put(9, "September (9)"); + gregorianMonths.put(10, "October (10)"); + gregorianMonths.put(11, "November (11)"); + gregorianMonths.put(12, "December (12)"); + + islamicMonths.put(1, hex("Muḥarram") + " (1)"); + islamicMonths.put(2, hex("Ṣafar") + " (2)"); + islamicMonths.put(3, hex("Rabīʿ I") + " (3)"); + islamicMonths.put(4, hex("Rabīʿ II") + " (4)"); + islamicMonths.put(5, hex("Jumādỳ I") + " (5)"); + islamicMonths.put(6, hex("Jumādỳ II") + " (6)"); + islamicMonths.put(7, "Rajab (7)"); + islamicMonths.put(8, hex("Šaʿbān") + " (8)"); + islamicMonths.put(9, hex("Ramaḍān") + " (9)"); + islamicMonths.put(10, hex("Šawwāl") + " (10)"); + islamicMonths.put(11, hex("Ḏu al-Qaʿdaẗ") + " (11)"); + islamicMonths.put(12, hex("Ḏu al-Ḥijjaẗ") + " (12)"); + + julianMonths.put(1, "Ianuarius (1)"); + julianMonths.put(2, "Februarius (2)"); + julianMonths.put(3, "Martius (3)"); + julianMonths.put(4, "Aprilis (4)"); + julianMonths.put(5, "Maius (5)"); + julianMonths.put(6, "Iunius (6)"); + julianMonths.put(7, "Quintilis (Iulius) (7)"); + julianMonths.put(8, "Sextilis (Augustus) (8)"); + julianMonths.put(9, "September (9)"); + julianMonths.put(10, "October (10)"); + julianMonths.put(11, "November (11)"); + julianMonths.put(12, "December (12)"); + } + + public static String hex(String s){ + + Character c = 0x1e25; + s = s.replace("ḥ", c + ""); + c = 0x1e62; + s = s.replace("Ṣ", c + ""); + c = 0x12b; + s = s.replace("ī", c + ""); + c = 0x2bf; + s = s.replace("ʿ", c + ""); + c = 0x101; + s = s.replace("ā", c + ""); + c = 0x1ef3; + s = s.replace("ỳ", c + ""); + c = 0x160; + s = s.replace("Š", c + ""); + c = 0x1e0d; + s = s.replace("ḍ", c + ""); + c = 0x1e0e; + s = s.replace("Ḏ", c + ""); + c = 0x1e24; + s = s.replace("Ḥ", c + ""); + c = 0x1e97; + s = s.replace("ẗ", c + ""); + + return s; + } + + protected boolean isDayInWeek(Integer day){ + if(day == null) + return false; + if(day >= 1 && day <= 7){ + return true; + } + return false; + } + + + private Integer dayOfMonth; + private Integer dayOfWeek; + private Integer dayOfYear; + private Integer month; + private Integer year; + private Integer century; + private Integer ambiguity; + + public ISMIDate(){} + + public ISMIDate(DateTime dateTime){ + this.setTime(dateTime); + this.ambiguity = 0; + } + + /* + public Date(Integer year, Integer month, Integer dayOfMonth){ + this.year = year; + this.month = month; + this.dayOfMonth = dayOfMonth; + }*/ + + public ISMIDate(JSONObject json) { + try{ + dayOfMonth = json.getInt("dayOfMonth"); + month = json.getInt("month"); + year = json.getInt("year"); + //century = json.getInt("century"); + if(json.has("century")){ + century = json.getInt("century"); + } + if(json.has("dayOfWeek")){ + dayOfWeek = json.getInt("dayOfWeek"); + } + if(json.has("dayOfYear")){ + dayOfYear = json.getInt("dayOfYear"); + } + if(json.has("ambiguity")){ + ambiguity = json.getInt("ambiguity"); + } + }catch (Exception e) { + e.printStackTrace(); + } + } + + public void setTime(DateTime dateTime){ + //TODO is gregorian chronology + this.dayOfMonth = dateTime.getDayOfMonth(); + this.dayOfYear = dateTime.getDayOfYear(); + //this.dayOfWeek = dateTime.getDayOfWeek(); + this.month = dateTime.getMonthOfYear(); + this.year = dateTime.getYear(); + this.century = dateTime.getCenturyOfEra(); + } + + public JSONObject toJSON(){ + JSONObject json = new JSONObject(); + try { + json.put("dayOfMonth", dayOfMonth); + json.put("dayOfYear", dayOfYear); + json.put("month", month); + json.put("year", year); + json.put("century", century); + json.put("dayOfWeek", dayOfWeek); + json.put("ambiguity", ambiguity); + } catch (JSONException e) { + e.printStackTrace(); + } + return json; + } + + public String toString(){ + if(year == null || month == null || dayOfMonth == null) + return null; + StringBuilder sb = new StringBuilder(); + sb.append(this.dayOfMonth + "."); + sb.append(ISMIDate.gregorianMonths.get(this.month) + "."); + sb.append(this.year); + if(this.ambiguity != null && this.ambiguity > 0){ + sb.append(" [+-" + this.ambiguity + "]"); + } + return sb.toString(); + } + + public String toIslamicString(){ + try{ + if(year == null || month == null || dayOfMonth == null) + return null; + + DateTime gr = + new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); + + DateTime islamic = new DateTime(gr.withChronology(IslamicChronology.getInstance())); + + StringBuilder sb = new StringBuilder(); + sb.append(islamic.getDayOfMonth() + "."); + sb.append(ISMIDate.islamicMonths.get(islamic.getMonthOfYear()) + "."); + sb.append(islamic.getYear()); + if(!isDayInWeek(this.dayOfWeek) && !hasAmbiguity()){ + sb.append(" [+-" + AMBIGUITY + "]"); + } + return sb.toString(); + }catch (Exception e) {} + return "no valid"; + } + + public boolean hasAmbiguity(){ + if(this.getAmbiguity() == null) + return false; + if(this.getAmbiguity() > 0) + return true; + return false; + } + + public String toJulianString(){ + try{ + if(year == null || month == null || dayOfMonth == null) + return null; + + DateTime gr = + new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); + + DateTime julian = new DateTime(gr.withChronology(JulianChronology.getInstance())); + + StringBuilder sb = new StringBuilder(); + sb.append(julian.getDayOfMonth() + "."); + sb.append(ISMIDate.julianMonths.get(julian.getMonthOfYear()) + "."); + sb.append(julian.getYear()); + if(this.ambiguity != null && this.ambiguity > 0){ + sb.append(" [+-" + this.ambiguity + "]"); + } + return sb.toString(); + }catch (Exception e) {} + return "no valid"; + } + + public DateTime getIslamicDateTime(){ + if(year == null || month == null || dayOfMonth == null) + return null; + try{ + DateTime gr = + new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); + + return new DateTime(gr.withChronology(IslamicChronology.getInstance())); + }catch (Exception e) {} + return null; + } + + public DateTime getJulianDateTime(){ + if(year == null || month == null || dayOfMonth == null) + return null; + try{ + DateTime gr = + new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); + + return new DateTime(gr.withChronology(JulianChronology.getInstance())); + }catch (Exception e) {} + return null; + } + + public Integer getDayOfMonth() { + return dayOfMonth; + } + public void setDayOfMonth(Integer dayOfMonth) { + this.dayOfMonth = dayOfMonth; + } + public Integer getDayOfWeek() { + return dayOfWeek; + } + public void setDayOfWeek(Integer dayOfWeek) { + this.dayOfWeek = dayOfWeek; + } + public Integer getDayOfYear() { + return dayOfYear; + } + public void setDayOfYear(Integer dayOfYear) { + this.dayOfYear = dayOfYear; + } + public Integer getMonth() { + return month; + } + public void setMonth(Integer month) { + this.month = month; + } + public Integer getYear() { + return year; + } + public void setYear(Integer year) { + this.year = year; + } + public Integer getCentury() { + return century; + } + public void setCentury(Integer century) { + this.century = century; + } + public Integer getAmbiguity() { + return ambiguity; + } + public void setAmbiguity(Integer ambiguity) { + this.ambiguity = ambiguity; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scheduling/utils/ExampleJob.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,29 @@ +package org.mpi.openmind.scheduling.utils; + +import java.util.Date; + +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.springframework.scheduling.quartz.QuartzJobBean; + +public class ExampleJob extends QuartzJobBean { + + private int timeout; + + /** + * Setter called after the ExampleJob is instantiated + * with the value from the JobDetailBean (5) + */ + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + protected void executeInternal(JobExecutionContext ctx) + throws JobExecutionException { + System.out.println(); + System.out.println("Hola Jorge"); + System.out.println(new Date()); + System.out.println(); + } + } +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scheduling/utils/RefreshDBConnection.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,24 @@ +package org.mpi.openmind.scheduling.utils; + +import org.mpi.openmind.repository.services.PersistenceService; + +public class RefreshDBConnection { + private PersistenceService persistenceService; + + public synchronized void dailyKickOff(){ + try { + this.persistenceService.getLWDefinitions(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public PersistenceService getPersistenceService() { + return persistenceService; + } + + public void setPersistenceService(PersistenceService persistenceService) { + this.persistenceService = persistenceService; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scheduling/utils/Scheduling.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,60 @@ +package org.mpi.openmind.scheduling.utils; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.configuration.ConfigurationService; +import org.mpi.openmind.repository.services.PersistenceService; +import org.mpi.openmind.repository.utils.OM4StreamWriter; + + +public class Scheduling { + + private static Logger logger = Logger.getLogger(Scheduling.class); + + private PersistenceService persistenceService; + private ConfigurationService configurationService; + + public synchronized void dailyKickOff(){ + if(configurationService.isSchedulingEnable() && StringUtils.isNotEmpty(configurationService.getSchedulingPath())){ + + String schedulingPath = configurationService.getSchedulingPath(); + if(schedulingPath.charAt(schedulingPath.length() - 1) != '/'){ + schedulingPath += "/"; + } + + + Date now = new Date(); + DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd[HH.mm]"); + + logger.info("Backup Definitions as: " + schedulingPath + formatter.format(now) + "-DEF.xml"); + OM4StreamWriter.backupDefinitions(schedulingPath + formatter.format(now) + "-DEF.xml" , persistenceService); + logger.info("Backup Entities as: " + schedulingPath + formatter.format(now) + "-ENT.xml"); + OM4StreamWriter.backupEntities(schedulingPath + formatter.format(now) + "-ENT.xml", persistenceService); + + } + } + + + public ConfigurationService getConfigurationService() { + return configurationService; + } + public void setConfigurationService(ConfigurationService configurationService) { + this.configurationService = configurationService; + } + + public PersistenceService getPersistenceService() { + return persistenceService; + } + + + public void setPersistenceService(PersistenceService persistenceService) { + this.persistenceService = persistenceService; + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/Biography.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,33 @@ +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 Biography { + + public static void execute(WrapperService wrapper){ + List<Entity> rs = wrapper.getEntitiesByAtt("PERSON", "url", "http://islamsci.mcgill.ca/RASI/BEA", -1, true); + + for(Entity ent : rs){ + Attribute att = wrapper.getAttributeByName(ent.getId(), "url"); + System.out.println(/*"[" + ent.getId() + "] " +*/ ent.getNormalizedOwnValue() + " - " + att.getOwnValue()); + } + + System.out.println("RS: " + rs.size()); + } + + public static void main(String[] args){ + ServiceRegistry services = new ServiceRegistry(); + Biography.execute(services.getWrapper()); + System.exit(0); + } + + + + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/ChangeEntityPrivacity.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,63 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +import org.apache.commons.lang.StringUtils; + +public class ChangeEntityPrivacity { + + public static void execute(boolean privacity) { + try { + + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "ismi", "ismipw"); + + + Statement st = conn.createStatement(); + //((privacity)? "1" : "0") + String sql = "UPDATE node SET public =" + privacity + ""; + System.out.println("Executing:"); + System.out.println(sql); + int n = st.executeUpdate(sql); + //System.out.println(n + " rows were modified"); + + conn.close(); + + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(); + } + } + + public static void main(String[] args) { + String arg = args[0]; + System.out.println("arg: " + arg); + boolean privacity = false; + if(StringUtils.isNotEmpty(arg)){ + try{ + privacity = new Boolean(arg); + }catch (Exception e) { + e.printStackTrace(); + } + } + + ChangeEntityPrivacity.execute(privacity); + System.exit(0); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/CodexOwnValueGenerator.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,89 @@ +package org.mpi.openmind.scripts; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; +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.services.PersistenceService; +import org.mpi.openmind.repository.services.ServiceRegistry; +import org.mpi.openmind.repository.utils.OMUtils; + +public class CodexOwnValueGenerator { + + private static Logger logger = Logger.getLogger(CodexOwnValueGenerator.class); + + private WrapperService ot; + private PersistenceService ss; + + public CodexOwnValueGenerator(WrapperService ot){ + this.ot = ot; + this.ss = ot.getPS(); + } + + + public void execute(){ + try { + logger.info("Starting CodexOwnValueGenerator"); + List<Entity> codexList = ot.getLightweightAssertions("CODEX", null, -1); + logger.info("Codices="+ codexList.size()); + List<Node> dirtyEntities = new ArrayList<Node>(); + int count = 0; + int countCodicesNonCollection = 0; + for(Entity codex : codexList){ + + Attribute codexIdAtt = ss.getAttributeByName(codex, "identifier"); + if(codexIdAtt == null){ + throw new Exception("identifier is null for " + codex); + } + String ov = new String(); + + List<Entity> list0 = this.ss.getTargetsForSourceRelation(codex, "is_part_of", "COLLECTION", 1); + + + Entity collection; + if(list0.size() > 0){ + collection = list0.get(0); + if(collection != null){ + ov = collection.getOwnValue() + "_" + codexIdAtt.getValue(); + }else{ + ov = "empty_" + codexIdAtt.getValue(); + countCodicesNonCollection++; + } + if(count % 100 == 0){ + int size = codexList.size(); + + logger.info("* " + OMUtils.percentage(count, size) + " %"); + } + count++; + codex.setOwnValue(ov); + dirtyEntities.add(codex); + } + } + ot.saveNodeListOnlyForScripts(dirtyEntities); + + logger.info("Summary"); + logger.info("Codices without collection=" + countCodicesNonCollection); + logger.info("Total=" + count); + + + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + ServiceRegistry services = new ServiceRegistry(); + CodexOwnValueGenerator script = new CodexOwnValueGenerator(services.getWrapper()); + script.execute(); + System.exit(0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/CurrentVersionForRelations.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,109 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class CurrentVersionForRelations { + + private static String querySrc; + private static String queryTar; + + static{ + querySrc = + "select " + + "entity.id as entId, entity.own_value as entOW, entity.modification_time as entModif, entity.version as entVersion, " + + "relation.id as relId, relation.modification_time as relModif, relation.version as relVersion, relation.own_value as relOW " + + + "from node as entity, node as relation " + + + "where " + + "relation.node_type = 'RELATION' AND " + + "entity.node_type = 'RELATION' AND " + + "relation.system_status = 'CURRENT_VERSION' AND " + + "entity.system_status = 'CURRENT_VERSION' AND " + + "relation.source_id != entity.id " + + "group by entity.id"; + + } + + + public static ResultSet select(Connection conn, String arg){ + ResultSet rs = null; + try{ + + System.out.println("EntId \tentOW \tentModif \tentVersion \trelId \trelOW \trelModif \trelVersion"); + Statement st = conn.createStatement(); + rs = st.executeQuery(querySrc); + int count =0; + while (rs.next()) + { + StringBuilder sb = new StringBuilder(); + + sb.append( + rs.getString("entId") + + "\t"); + sb.append( + rs.getString("entOW") + + "\t"); + sb.append( + rs.getString("entModif") + + "\t"); + sb.append( + rs.getString("entVersion") + + "\t"); + sb.append( + rs.getString("relId") + + "\t"); + sb.append( + rs.getString("relOW") + + "\t"); + sb.append( + rs.getString("relModif") + + "\t"); + sb.append( + rs.getString("relVersion") + + "\t"); + + System.out.println(sb.toString()); + } + }catch (Exception e) { + e.printStackTrace(); + } + return rs; + } + + + public static void execute(String arg) { + try { + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "ismi", "ismipw"); + + ResultSet rs = select(conn, arg); + + conn.close(); + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + + } + + + + public static void main(String[] args) { + String arg = args[0]; + CurrentVersionForRelations.execute(arg); + System.exit(0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/CurrentVersionSrcRelation.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,324 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.StringUtils; + +/** + * Some relation, which are marked as CURRENT_VERSION, + * has not the correct srcTimeModif, + * which is necessary to be found from the corresponding entity associated. + * This script does the following: + * -Find the relation with incorrect srcModifTime + * - + * @author jurzua + * + */ +public class CurrentVersionSrcRelation { + + public static String WITNESS = "WITNESS"; + public static String TEXT = "TEXT"; + public static String CODEX = "CODEX"; + public static String PLACE = "PLACE"; + public static String PERSON = "PERSON"; + public static String REPOSITORY = "REPOSITORY"; + public static String COPY_EVENT = "COPY_EVENT"; + public static String COLLECTION = "COLLECTION"; + public static String REFERENCE = "REFERENCE"; + public static String ROLE = "ROLE"; + public static String STUDY_EVENT = "STUDY_EVENT"; + public static String SUBJECT = "SUBJECT"; + public static String TRANSFER_EVENT = "TRANSFER_EVENT"; + + private static String query; + private static List<Relation> uniqueRelations; + + static{ + query = + "select " + + "entity.id as entId, entity.own_value as entOW, entity.modification_time as entModif, entity.version as entVersion, " + + "relation.id as relId, relation.source_modif as relSrcModif, relation.version as relVersion, relation.own_value as relOW, " + + "relation.source_obj_class as relSrcOC, relation.target_obj_class as relTarOC, " + + "relation.source_id as relSrcId, " + + "relation.target_id as relTarId, " + + "relation.row_id as relRowId " + + "from node as entity, node as relation " + + "where " + + "relation.node_type = 'RELATION' AND " + + "entity.node_type = 'ENTITY' AND " + + "relation.system_status = 'CURRENT_VERSION' AND " + + "entity.system_status = 'CURRENT_VERSION' AND " + + "relation.source_id = entity.id AND " + + "relation.source_modif != entity.modification_time ";// + + //"group by entity.id "; + + CurrentVersionSrcRelation tthis = new CurrentVersionSrcRelation(); + uniqueRelations = new ArrayList<CurrentVersionSrcRelation.Relation>(); + uniqueRelations.add(tthis.new Relation(WITNESS, "is_exemplar_of", TEXT)); + uniqueRelations.add(tthis.new Relation(WITNESS, "is_part_of", CODEX)); + uniqueRelations.add(tthis.new Relation(WITNESS, "was_copied_by", PERSON)); + uniqueRelations.add(tthis.new Relation(WITNESS, "was_copied_in", PLACE)); + uniqueRelations.add(tthis.new Relation(WITNESS, "was_created_by", PERSON)); + uniqueRelations.add(tthis.new Relation(WITNESS, "was_created_in", PLACE)); + + uniqueRelations.add(tthis.new Relation(PLACE, "is_part_of", PLACE)); + + uniqueRelations.add(tthis.new Relation(REPOSITORY, "is_in", PLACE)); + + uniqueRelations.add(tthis.new Relation(COLLECTION, "is_part_of", REPOSITORY)); + + uniqueRelations.add(tthis.new Relation(CODEX, "is_part_of", COLLECTION)); + uniqueRelations.add(tthis.new Relation(CODEX, "is_part_of", REPOSITORY)); + + uniqueRelations.add(tthis.new Relation(PERSON, "was_born_in", PLACE)); + uniqueRelations.add(tthis.new Relation(PERSON, "died_in", PLACE)); + + uniqueRelations.add(tthis.new Relation(COPY_EVENT, "is_a_copy_of", WITNESS)); + uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_in", PLACE)); + uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_in", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_for", PERSON)); + + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "is_a_study_of", WITNESS)); + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_advised_by", PERSON)); + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_by", PERSON)); + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_in", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_in", PLACE)); + + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_new_location", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_new_location", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_original_location", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_original_location", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_from", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_from", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_in", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_in", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_to", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_to", REPOSITORY)); + + uniqueRelations.add(tthis.new Relation(TEXT, "is_version_of", TEXT)); + uniqueRelations.add(tthis.new Relation(TEXT, "was_created_by", PERSON)); + uniqueRelations.add(tthis.new Relation(TEXT, "was_created_in", PLACE)); + uniqueRelations.add(tthis.new Relation(TEXT, "is_translation_of", TEXT)); + uniqueRelations.add(tthis.new Relation(TEXT, "was_dedicated_to", TEXT)); + uniqueRelations.add(tthis.new Relation(TEXT, "is_commentary_on", TEXT)); + } + + public static ResultSet select(Connection conn, String arg){ + CurrentVersionSrcRelation tthis = new CurrentVersionSrcRelation(); + ResultSet rs = null; + try{ + Statement st = conn.createStatement(); + rs = st.executeQuery(query); + int count = 0; + int case1 =0; + int case2 =0; + int case3 =0; + while (rs.next()){ + Entity ent = tthis.new Entity(rs); + Relation rel = tthis.new Relation(rs); + + System.out.println(rel.toString()); + + if(rel.srcId.equals(new Long(51270))){ + System.out.println("*****************"); + } + + Relation otherRel = getCurrentRelation(ent, rel, conn); + if(otherRel == null){ + System.out.println("\tCASE1"); + case1++; + makeRelationCurrentVersion(conn, rel, ent); + }else if(shouldBeRelationUniqueForMetadaten(rel)){ + System.out.println("\tCASE2"); + case2++; + makeRelationPreviuosVersion(conn, rel); + }else{ + System.out.println("\tCASE3"); + case3++; + makeRelationCurrentVersion(conn, rel, ent); + } + count++; + } + System.out.println( "*****************"); + System.out.println("CASE1=" + case1 + " no exist other similar relation, the old should be refreshed"); + System.out.println("CASE2=" + case2 + " this relation should be unique, but there is more than one (DELETING)."); + System.out.println("CASE3=" + case3 + " this relation can exist more tha one instance"); + System.out.println("TOTAL=" + count); + System.out.println("*****************"); + }catch (SQLException ex){ + ex.printStackTrace(); + } + return rs; + } + + + public static void makeRelationPreviuosVersion(Connection conn, Relation rel) throws SQLException{ + System.out.println("Making PREVIOUS..."); + Statement st = conn.createStatement(); + //st.executeUpdate("DELETE FROM node WHERE row_id = '"+ rel.rowId + "'"); + st.executeUpdate("UPDATE node SET system_status='PREVIOUS_VERSION' WHERE row_id='"+ rel.rowId +"'"); + } + + public static void makeRelationCurrentVersion(Connection conn, Relation rel, Entity ent) throws SQLException{ + System.out.println("Making Current Version..."); + Statement st = conn.createStatement(); + st.executeUpdate("UPDATE node SET source_modif='" + ent.modif + "' WHERE row_id='"+ rel.rowId +"'"); + } + + public static boolean shouldBeRelationUniqueForMetadaten(Relation rel){ + if(uniqueRelations.contains(rel)){ + return true; + } + return false; + } + + public static Relation getCurrentRelation(Entity ent, Relation rel, Connection conn){ + ResultSet rs = null; + CurrentVersionSrcRelation tthis = new CurrentVersionSrcRelation(); + + String relQuery = + "select " + + "relation.id as relId, " + + "relation.source_modif as relSrcModif, " + + "relation.version as relVersion, " + + "relation.own_value as relOW, " + + "relation.source_id as relSrcId, " + + "relation.target_id as relTarId, " + + "relation.source_obj_class as relSrcOC, " + + "relation.target_obj_class as relTarOC, " + + "relation.row_id as relRowId " + + "from node as relation " + + "where " + + "relation.node_type = 'RELATION' AND " + + "relation.system_status = 'CURRENT_VERSION' AND " + + "relation.source_id = '" + rel.srcId + "' AND " + + //"relation.target_id = '" + rel.tarId + "' AND " + + "relation.source_modif = '" + ent.modif + "' "; + + try{ + Statement st = conn.createStatement(); + rs = st.executeQuery(relQuery); + Relation otherRel = null; + while(rs.next()){ + otherRel = tthis.new Relation(rs); + break; + } + return otherRel; + }catch (SQLException ex){ + ex.printStackTrace(); + } + + return null; + } + + + + public static void execute() { + try { + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "ismi", "ismipw"); + + ResultSet rs = select(conn, ""); + + conn.close(); + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + } + + public static void main(String[] args) { + + CurrentVersionSrcRelation.execute(); + System.exit(0); + } + + private class Entity{ + public Long id; + public Long modif; + public String ow; + public Entity(ResultSet rs){ + try { + //"entity.id as entId, entity.own_value as entOW, entity.modification_time as entModif, entity.version as entVersion, " + + this.modif = rs.getLong("entModif"); + this.id = rs.getLong("entId"); + this.ow = rs.getString("entOW"); + } catch (SQLException e) { + System.out.println(); + e.printStackTrace(); + System.out.println(); + } + } + } + + private class Relation{ + public Long id; + public Long srcModif; + public Long srcId; + public Long tarId; + public String ow; + public String srcOC; + public String tarOC; + public String rowId; + + public Relation(String srcOC, String ow, String tarOC){ + this.ow = ow; + this.srcOC = srcOC; + this.tarOC = tarOC; + } + + public Relation(ResultSet rs){ + //s relId, relation.source_modif as relSrcModif, relation.version as relVersion, relation.own_value as relOW + try { + this.srcModif = rs.getLong("relSrcModif"); + this.id = rs.getLong("relId"); + this.ow = rs.getString("relOW"); + this.srcOC = rs.getString("relSrcOC"); + this.tarOC = rs.getString("relTarOC"); + this.rowId = rs.getString("relRowId"); + this.srcId = rs.getLong("relSrcId"); + this.tarId = rs.getLong("relTarId"); + } catch (SQLException e) { + System.out.println(); + e.printStackTrace(); + System.out.println(); + } + } + @Override + public boolean equals(Object obj){ + if(obj != null){ + if(obj instanceof Relation){ + Relation other = (Relation)obj; + if(StringUtils.equals(this.ow, other.ow) && + StringUtils.equals(this.srcOC, other.srcOC) && + StringUtils.equals(this.tarOC, other.tarOC)){ + return true; + } + } + } + return false; + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append("ROW_ID=" + this.rowId + "\t"); + sb.append(this.srcOC + " [" + this.ow + "] " + this.tarOC + "\t SrcID=" + this.srcId + "\t TarID=" + this.tarId); + return sb.toString(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/CurrentVersionTarRelation.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,325 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.StringUtils; + +/** + * Some relation, which are marked as CURRENT_VERSION, + * has not the correct srcTimeModif, + * which is necessary to be found from the corresponding entity associated. + * This script does the following: + * -Find the relation with incorrect srcModifTime + * - + * @author jurzua + * + */ +public class CurrentVersionTarRelation { + + public static String WITNESS = "WITNESS"; + public static String TEXT = "TEXT"; + public static String CODEX = "CODEX"; + public static String PLACE = "PLACE"; + public static String PERSON = "PERSON"; + public static String REPOSITORY = "REPOSITORY"; + public static String COPY_EVENT = "COPY_EVENT"; + public static String COLLECTION = "COLLECTION"; + public static String REFERENCE = "REFERENCE"; + public static String ROLE = "ROLE"; + public static String STUDY_EVENT = "STUDY_EVENT"; + public static String SUBJECT = "SUBJECT"; + public static String TRANSFER_EVENT = "TRANSFER_EVENT"; + + private static String query; + private static List<Relation> uniqueRelations; + + static{ + query = + "select " + + "entity.id as entId, entity.own_value as entOW, entity.modification_time as entModif, entity.version as entVersion, " + + "relation.id as relId, relation.source_modif as relSrcModif, relation.version as relVersion, relation.own_value as relOW, " + + "relation.source_obj_class as relSrcOC, relation.target_obj_class as relTarOC, " + + "relation.source_id as relSrcId, " + + "relation.target_id as relTarId, " + + "relation.row_id as relRowId " + + "from node as entity, node as relation " + + "where " + + "relation.node_type = 'RELATION' AND " + + "entity.node_type = 'ENTITY' AND " + + "relation.system_status = 'CURRENT_VERSION' AND " + + "entity.system_status = 'CURRENT_VERSION' AND " + + "relation.target_id = entity.id AND " + + "relation.target_modif != entity.modification_time " + + "group by entity.id "; + + CurrentVersionTarRelation tthis = new CurrentVersionTarRelation(); + uniqueRelations = new ArrayList<CurrentVersionTarRelation.Relation>(); + + /* + uniqueRelations.add(tthis.new Relation(WITNESS, "is_exemplar_of", TEXT)); + uniqueRelations.add(tthis.new Relation(WITNESS, "is_part_of", CODEX)); + uniqueRelations.add(tthis.new Relation(WITNESS, "was_copied_by", PERSON)); + uniqueRelations.add(tthis.new Relation(WITNESS, "was_copied_in", PLACE)); + uniqueRelations.add(tthis.new Relation(WITNESS, "was_created_by", PERSON)); + uniqueRelations.add(tthis.new Relation(WITNESS, "was_created_in", PLACE)); + + uniqueRelations.add(tthis.new Relation(PLACE, "is_part_of", PLACE)); + + uniqueRelations.add(tthis.new Relation(REPOSITORY, "is_in", PLACE)); + + uniqueRelations.add(tthis.new Relation(COLLECTION, "is_part_of", REPOSITORY)); + + uniqueRelations.add(tthis.new Relation(CODEX, "is_part_of", COLLECTION)); + uniqueRelations.add(tthis.new Relation(CODEX, "is_part_of", REPOSITORY)); + + uniqueRelations.add(tthis.new Relation(PERSON, "was_born_in", PLACE)); + uniqueRelations.add(tthis.new Relation(PERSON, "died_in", PLACE)); + + uniqueRelations.add(tthis.new Relation(COPY_EVENT, "is_a_copy_of", WITNESS)); + uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_in", PLACE)); + uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_in", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(COPY_EVENT, "was_copied_for", PERSON)); + + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "is_a_study_of", WITNESS)); + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_advised_by", PERSON)); + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_by", PERSON)); + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_in", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(STUDY_EVENT, "was_studied_in", PLACE)); + + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_new_location", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_new_location", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_original_location", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "has_original_location", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_from", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_from", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_in", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_in", REPOSITORY)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_to", PLACE)); + uniqueRelations.add(tthis.new Relation(TRANSFER_EVENT, "was_transferred_to", REPOSITORY)); + + uniqueRelations.add(tthis.new Relation(TEXT, "is_version_of", TEXT)); + uniqueRelations.add(tthis.new Relation(TEXT, "was_created_by", PERSON)); + uniqueRelations.add(tthis.new Relation(TEXT, "was_created_in", PLACE)); + uniqueRelations.add(tthis.new Relation(TEXT, "is_translation_of", TEXT)); + uniqueRelations.add(tthis.new Relation(TEXT, "was_dedicated_to", TEXT)); + uniqueRelations.add(tthis.new Relation(TEXT, "is_commentary_on", TEXT)); + */ + } + + public static ResultSet select(Connection conn, String arg){ + CurrentVersionTarRelation tthis = new CurrentVersionTarRelation(); + ResultSet rs = null; + try{ + Statement st = conn.createStatement(); + rs = st.executeQuery(query); + int count = 0; + int case1 =0; + int case2 =0; + int case3 =0; + while (rs.next()){ + Entity ent = tthis.new Entity(rs); + Relation rel = tthis.new Relation(rs); + + System.out.println(rel.toString()); + + Relation otherRel = getCurrentRelation(ent, rel, conn); + if(otherRel == null){ + System.out.println("\tCASE1"); + case1++; + //makeRelationCurrentVersion(conn, rel, ent); + }else if(shouldBeRelationUniqueForMetadaten(rel)){ + System.out.println("\tCASE2"); + case2++; + //deleteRelation(conn, rel); + }else{ + System.out.println("\tCASE3"); + case3++; + //makeRelationCurrentVersion(conn, rel, ent); + } + count++; + } + System.out.println( "*****************"); + System.out.println("CASE1=" + case1 + " no exist other similar relation, the old should be refreshed"); + System.out.println("CASE2=" + case2 + " this relation should be unique, but there is more than one (DELETING)."); + System.out.println("CASE3=" + case3 + " this relation can exist more tha one instance"); + System.out.println("TOTAL=" + count); + System.out.println("*****************"); + }catch (SQLException ex){ + ex.printStackTrace(); + } + return rs; + } + + + public static void deleteRelation(Connection conn, Relation rel) throws SQLException{ + System.out.println("Deleting..."); + Statement st = conn.createStatement(); + st.executeUpdate("DELETE FROM node WHERE row_id = '"+ rel.rowId + "'"); + } + + public static void makeRelationCurrentVersion(Connection conn, Relation rel, Entity ent) throws SQLException{ + System.out.println("Making Current Version..."); + Statement st = conn.createStatement(); + st.executeUpdate("UPDATE node SET source_modif='" + ent.modif + "' WHERE row_id='"+ rel.rowId +"'"); + } + + public static boolean shouldBeRelationUniqueForMetadaten(Relation rel){ + if(uniqueRelations.contains(rel)){ + return true; + } + return false; + } + + public static Relation getCurrentRelation(Entity ent, Relation rel, Connection conn){ + ResultSet rs = null; + CurrentVersionTarRelation tthis = new CurrentVersionTarRelation(); + + String relQuery = + "select " + + "relation.id as relId, " + + "relation.source_modif as relSrcModif, " + + "relation.version as relVersion, " + + "relation.own_value as relOW, " + + "relation.source_id as relSrcId, " + + "relation.target_id as relTarId, " + + "relation.source_obj_class as relSrcOC, " + + "relation.target_obj_class as relTarOC, " + + "relation.row_id as relRowId " + + "from node as relation " + + "where " + + "relation.node_type = 'RELATION' AND " + + "relation.system_status = 'CURRENT_VERSION' AND " + + //"relation.source_id = '" + rel.srcId + "' AND " + + "relation.target_id = '" + rel.tarId + "' AND " + + "relation.source_modif = '" + ent.modif + "' "; + + try{ + Statement st = conn.createStatement(); + rs = st.executeQuery(relQuery); + Relation otherRel = null; + while(rs.next()){ + otherRel = tthis.new Relation(rs); + break; + } + return otherRel; + }catch (SQLException ex){ + ex.printStackTrace(); + } + + return null; + } + + + + public static void execute() { + try { + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "ismi", "ismipw"); + + ResultSet rs = select(conn, ""); + + conn.close(); + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + } + + public static void main(String[] args) { + + CurrentVersionTarRelation.execute(); + System.exit(0); + } + + private class Entity{ + public Long id; + public Long modif; + public String ow; + public Entity(ResultSet rs){ + try { + //"entity.id as entId, entity.own_value as entOW, entity.modification_time as entModif, entity.version as entVersion, " + + this.modif = rs.getLong("entModif"); + this.id = rs.getLong("entId"); + this.ow = rs.getString("entOW"); + } catch (SQLException e) { + System.out.println(); + e.printStackTrace(); + System.out.println(); + } + } + } + + private class Relation{ + public Long id; + public Long srcModif; + public Long srcId; + public Long tarId; + public String ow; + public String srcOC; + public String tarOC; + public String rowId; + + public Relation(String srcOC, String ow, String tarOC){ + this.ow = ow; + this.srcOC = srcOC; + this.tarOC = tarOC; + } + + public Relation(ResultSet rs){ + //s relId, relation.source_modif as relSrcModif, relation.version as relVersion, relation.own_value as relOW + try { + this.srcModif = rs.getLong("relSrcModif"); + this.id = rs.getLong("relId"); + this.ow = rs.getString("relOW"); + + this.srcOC = rs.getString("relSrcOC"); + this.tarOC = rs.getString("relTarOC"); + + this.rowId = rs.getString("relRowId"); + + this.srcId = rs.getLong("relSrcId"); + this.tarId = rs.getLong("relTarId"); + } catch (SQLException e) { + System.out.println(); + e.printStackTrace(); + System.out.println(); + } + } + @Override + public boolean equals(Object obj){ + if(obj != null){ + if(obj instanceof Relation){ + Relation other = (Relation)obj; + if(StringUtils.equals(this.ow, other.ow) && + StringUtils.equals(this.srcOC, other.srcOC) && + StringUtils.equals(this.tarOC, other.tarOC)){ + return true; + } + } + } + return false; + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append("ROW_ID=" + this.rowId + "\t"); + sb.append(this.srcOC + " [" + this.ow + "] " + this.tarOC + "\t SrcID=" + this.srcId + "\t TarID=" + this.tarId); + return sb.toString(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/DivaImport.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,180 @@ +package org.mpi.openmind.scripts; + +import java.sql.DriverManager; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; +import org.hibernate.mapping.Array; +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; + +import cl.maps.utils.AttKey; + + +public class DivaImport { + + static{ + ConsoleAppender console = new ConsoleAppender(); //create appender + //configure the appender + String PATTERN = "%d [%p|%c|%C{1}] %m%n"; + console.setLayout(new PatternLayout(PATTERN)); + console.setThreshold(Level.INFO); + console.activateOptions(); + //add appender to any Logger (here is root) + Logger.getRootLogger().addAppender(console); + } + + public static String DIGITALIZATION = "DIGITALIZATION"; + public static String userName = "diva-import"; + + public static void execute(){ + ServiceRegistry services = new ServiceRegistry(); + createDataModel(services.getWrapper()); + importData(services.getWrapper()); + } + + private static void createDataModel(WrapperService ontology){ + + try { + + Entity digi = new Entity(Node.TYPE_TBOX, Node.TYPE_TBOX, false); + digi.setOwnValue(DIGITALIZATION); + + digi = ontology.saveLWDefinition(digi, userName); + + Attribute attName = new Attribute(Node.TYPE_TBOX, "text", "name"); + attName.setSourceId(digi.getId()); + attName.setSourceObjectClass(Node.TYPE_TBOX); + attName.setSourceModif(digi.getModificationTime()); + attName.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + + ontology.saveDefAttribute(attName, userName); + + Attribute num_files = new Attribute(Node.TYPE_TBOX, "text", "num_files"); + num_files.setSourceId(digi.getId()); + num_files.setSourceObjectClass(Node.TYPE_TBOX); + num_files.setSourceModif(digi.getModificationTime()); + num_files.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + + ontology.saveDefAttribute(num_files, userName); + + + //DIGI is_digitalization_of CODEX + Entity codex = ontology.getDefinition("CODEX"); + Relation rel = new Relation(digi, codex, "is_digitalization_of"); + + ontology.saveDefRelation(rel, userName); + + //----------- + Entity witness = ontology.getDefinition("WITNESS"); + + Attribute end_page = new Attribute(Node.TYPE_TBOX, "text", "end_page"); + end_page.setSourceId(witness.getId()); + end_page.setSourceObjectClass(Node.TYPE_TBOX); + end_page.setSourceModif(witness.getModificationTime()); + end_page.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + + ontology.saveDefAttribute(end_page, userName); + + Attribute start_page = new Attribute(Node.TYPE_TBOX, "text", "start_page"); + start_page.setSourceId(witness.getId()); + start_page.setSourceObjectClass(Node.TYPE_TBOX); + start_page.setSourceModif(witness.getModificationTime()); + start_page.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + + ontology.saveDefAttribute(start_page, userName); + + + } catch (Exception e) { + e.printStackTrace(); + } + + + } + + private static void importData(WrapperService ontology){ + try { + + Class.forName("org.postgresql.Driver"); + Connection conn = null; + conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/diva","jurzua", "221082"); + + String sql = "SELECT * FROM imageserve_manuscript"; + + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + int countNoCodex = 0; + int countYesCodex = 0; + int countIdCodex = 0; + List<String> noCodexList = new ArrayList<String>(); + + List<Entity> list = new ArrayList<Entity>(); + while(rs.next()){ + String directory = rs.getString("directory"); + Long ismi_id = rs.getLong("ismi_id"); + Integer numFiles = rs.getInt("num_files"); + Boolean hasFolioNums = rs.getBoolean("has_folio_nums"); + String folioPgs = rs.getString("folio_pgs"); + + String ov = directory.replace("/data7/srv/images/", ""); + + Entity digi = new Entity(Node.TYPE_ABOX, DIGITALIZATION, false); + digi.setOwnValue(ov); + + digi.addAttribute(new Attribute("name", "text", ov)); + digi.addAttribute(new Attribute("num_files", "text", numFiles + "")); + + + if(ismi_id != null && ismi_id != 0){ + Entity codex = ontology.getEntityByIdWithContent(ismi_id); + digi.replaceSourceRelation(codex, "CODEX", "is_digitalization_of"); + countYesCodex++; + }else{ + List<Entity> list0 = ontology.getEntityByDefAndOW("CODEX", ov, 1); + if(list0.size() > 0){ + Entity codex = ontology.getEntityByIdWithContent(list0.get(0).getId()); + digi.replaceSourceRelation(codex, "CODEX", "is_digitalization_of"); + countIdCodex++; + }else{ + noCodexList.add(ov); + countNoCodex++; + } + } + + list.add(digi); + } + + + conn.close(); + + ontology.saveEntityList(list, userName); + + System.out.println("countNoCodex: " + countNoCodex); + System.out.println("countYesCodex: " + countYesCodex); + System.out.println("countIdCodex: " + countIdCodex); + System.out.println("list.size(): " + list.size()); + System.out.println("END"); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + public static void main(String[] args){ + execute(); + System.exit(0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/DoubleEntity.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,12 @@ +package org.mpi.openmind.scripts; + +/** + * This script is used to solve the problem, + * when the db contains two (or more) entities with the same id and both have the state 'CURRENT_VERSION'. + * + * @author jurzua + * + */ +public class DoubleEntity { + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/DoubleRelations.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,258 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.repository.utils.NormalizerUtils; + +public class DoubleRelations { + + private static String query; + + static{ + query = + "select e1.id as id1, e2.id as id2, e1.own_value as own_value, e1.target_id, e1.source_id, e1.row_id as row_id1, e2.row_id as row_id2, " + + "e1.modification_time as time1, e2.modification_time as time2, " + + "e1.version as v1, e2.version as v2 " + + "from node as e1, node as e2 " + + "where " + + "e1.node_type = 'RELATION' AND " + + "e2.node_type = 'RELATION' AND " + + "e1.system_status = 'CURRENT_VERSION' AND " + + "e2.system_status = 'CURRENT_VERSION' AND " + + "e1.source_id = e2.source_id AND " + + "e1.target_id = e2.target_id AND " + + "e1.own_value = e2.own_value AND " + + //"e1.id > e2.id " + + "e1.row_id > e2.row_id " + + "group by e1.id"; + } + + public static class DoubleRelation{ + + public Long rowId1; + public Long id1; + public Integer v1; + + public Long rowId2; + public Long id2; + public Integer v2; + + public String ov; + public Long srcId; + public Long tarId; + + public Long time1; + public Long time2; + + + public DoubleRelation(ResultSet rs) throws SQLException{ + this.id1 = rs.getLong("id1"); + this.id2 = rs.getLong("id2"); + this.rowId1 = rs.getLong("row_id1"); + this.rowId2 = rs.getLong("row_id2"); + this.ov = rs.getString("own_value"); + this.srcId = rs.getLong("source_id"); + this.tarId = rs.getLong("target_id"); + this.time1 = rs.getLong("time1"); + this.time2 = rs.getLong("time2"); + this.v1 = rs.getInt("v1"); + this.v2 = rs.getInt("v2"); + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + + sb.append("["+ srcId +", " + tarId + "]\t\t\t"); + sb.append("["+ id1 +", " + id2 + "]\t"); + sb.append("["+ rowId1 +", " + rowId2 + "]\t" + ov ); + sb.append("\t["+ time1 +", " + time2 + "]"); + sb.append("\t["+ v1 +", " + v2 + "]"); + return sb.toString(); + } + } + + public static ResultSet select(Connection conn, String arg){ + ResultSet rs = null; + try + { + System.out.println("[sourceId, targetId]\t[id1, id2]\t[row_id1, row_id2]\townValue\t[time1, time2]\t[version1, version2]"); + Statement st = conn.createStatement(); + rs = st.executeQuery(query); + System.out.println(); + System.out.println(query); + System.out.println(); + int count = 0; + + while (rs.next()){ + count++; + + DoubleRelation dr = new DoubleRelation(rs); + + /* + String id1 = rs.getString("id1"); + String id2 = rs.getString("id2"); + String row_id1 = rs.getString("row_id1"); + String row_id2 = rs.getString("row_id2"); + String ownValue = rs.getString("own_value"); + String sourceId = rs.getString("source_id"); + String targetId = rs.getString("target_id"); + String stime1 = rs.getString("time1"); + String stime2 = rs.getString("time2"); + String v1 = rs.getString("v1"); + String v2 = rs.getString("v2"); + */ + System.out.println(dr.toString()); + + if(StringUtils.isNotEmpty(arg) && arg.equals("REDUCE")){ + Statement st0 = conn.createStatement(); + //st0.executeUpdate("DELETE FROM node WHERE row_id = '"+ row_id1 + "'"); + /* + Long time1 = null; + Long time2 = null; + Integer version1 = null; + Integer version2 = null; + try{ + time1 = new Long(stime1); + time2 = new Long(stime2); + }catch (Exception e) {} + + try{ + version1 = new Integer(v1); + version2 = new Integer(v2); + }catch (Exception e) {} + */ + + //the oldest row should be deleted + if(dr.time1 != null && dr.time2 != null){ + if(dr.time1 > dr.time2){ + st0.executeUpdate("DELETE FROM node WHERE row_id = '"+ dr.rowId2 + "'"); + updateRelation(conn, dr.rowId1, dr.srcId, dr.tarId); + }else{ + st0.executeUpdate("DELETE FROM node WHERE row_id = '"+ dr.rowId1 + "'"); + updateRelation(conn, dr.rowId2, dr.srcId, dr.tarId); + } + }else if(dr.v1 != null && dr.v2 != null){ + if(dr.v1 > dr.v2){ + st0.executeUpdate("DELETE FROM node WHERE row_id = '"+ dr.rowId2 + "'"); + updateRelation(conn, dr.rowId1, dr.srcId, dr.tarId); + }else{ + st0.executeUpdate("DELETE FROM node WHERE row_id = '"+ dr.rowId1 + "'"); + updateRelation(conn, dr.rowId2, dr.srcId, dr.tarId); + } + }else{ + st0.executeUpdate("DELETE FROM node WHERE row_id = '"+ dr.rowId1 + "'"); + updateRelation(conn, dr.rowId2, dr.srcId, dr.tarId); + } + } + } + System.out.println(); + System.out.println(count + " relations to reduce!"); + } + catch (SQLException ex){ + ex.printStackTrace(); + } + return rs; + } + + public static void updateRelation(Connection conn, Long relRowId, Long srcId, Long tarId) throws SQLException{ + Long srcTime = getModTimeOfEntity(conn, srcId); + Long tarTime = getModTimeOfEntity(conn, tarId); + String query = "UPDATE node SET source_modif='"+srcTime+"', target_modif='"+ tarTime +"' WHERE row_id = '"+ relRowId + "'"; + System.out.println(query); + Statement st = conn.createStatement(); + st.executeUpdate(query); + } + + public static long getModTimeOfEntity(Connection conn, Long id) throws SQLException{ + Long time = null; + String query = "select modification_time from openmind.node where node_type = 'ENTITY' and system_status = 'CURRENT_VERSION' and id = '"+id+"'"; + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); + + while (rs.next()){ + time = rs.getLong("modification_time"); + } + return time; + + } + + /* + public static void reduce(Connection conn, ResultSet rs){ + System.out.println("Reducing..."); + try { + while (rs.next()){ + Statement st = conn.createStatement(); + + Long time1 = null; + Long time2 = null; + try{ + time1 = new Long(rs.getString("time1")); + time2 = new Long(rs.getString("time2")); + }catch (Exception e) {} + + //the oldest row should be deleted + if(time1 != null && time2 != null){ + if(time1 > time2){ + st.executeUpdate("DELETE node WHERE row_id='"+ rs.getString("row_id2") + "'"); + }else{ + st.executeUpdate("DELETE node WHERE row_id='"+ rs.getString("row_id1") + "'"); + } + }else{ + st.executeUpdate("DELETE node WHERE row_id='"+ rs.getString("row_id1") + "'"); + } + + + } + } catch (SQLException e) { + e.printStackTrace(); + } + }*/ + + public static void execute(String mode, String mysqlUser, String mysqlPass) { + try { + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, mysqlUser, mysqlPass); + //conn = DriverManager.getConnection(url, "root", "admin"); + + ResultSet rs = select(conn, mode); + + conn.close(); + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + + } + + + + public static void main(String[] args) { + if(args.length == 3){ + DoubleRelations.execute(args[0], args[1], args[2]); + }else{ + System.out.println("Parameter/s no found: They should be: mode(SHOW/REDUCE), mysql_user, mysql_password"); + System.out.println("args.length= " + args.length); + System.out.println(Arrays.toString(args)); + + } + + System.exit(0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/FloruitDate.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,53 @@ +package org.mpi.openmind.scripts; + +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; + +public class FloruitDate { + + private static String FLORUIT_DATE = "FLORUIT_DATE"; + private static String userName = "floruit-script"; + + public static void execute(){ + System.out.println("*** FloruitDate ***"); + ServiceRegistry services = new ServiceRegistry(); + createDataModel(services.getWrapper()); + System.out.println("&&&&&&&&&&&&&&&&&&&"); + } + + private static void createDataModel(WrapperService ontology){ + try { + Entity floruitDate = new Entity(Node.TYPE_TBOX, Node.TYPE_TBOX, false); + floruitDate.setOwnValue(FLORUIT_DATE); + + floruitDate = ontology.saveLWDefinition(floruitDate, userName); + + + Attribute attName = new Attribute(Node.TYPE_TBOX, "date", "date"); + attName.setSourceId(floruitDate.getId()); + attName.setSourceObjectClass(Node.TYPE_TBOX); + attName.setSourceModif(floruitDate.getModificationTime()); + attName.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + + ontology.saveDefAttribute(attName, userName); + + //**** + Entity person = ontology.getDefinition("PERSON"); + Relation rel = new Relation(person, floruitDate, "has_floruit_date"); + ontology.saveDefRelation(rel, userName); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args){ + FloruitDate.execute(); + System.exit(0); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/Indexmeta.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,195 @@ +package org.mpi.openmind.scripts; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import org.apache.commons.lang.StringUtils; +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.services.ServiceRegistry; +import org.w3c.dom.Document; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathFactory; + +/** + * This script should do mainly two things: + * 1. Create an attribute called mpiwg_id + * 2. Create an attribute called indexmeta_folder + * + * + * @author jurzua + * + */ +public class Indexmeta { + + //http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Texter?fn=/permanent/library/SWE97E52/index.meta + private static String ECHO_URL = "http://echo.mpiwg-berlin.mpg.de"; + + private static String ATT_INDEXMETA_FOLDER = "indexmeta_folder"; + private static String ATT_MPIWG_ID = "mpiwg_id"; + private static String CODEX = "CODEX"; + public static String userName = "indexmeta-script"; + + + + private static void saveMpiwgIds(List<Identifier> mpiwgIdList, WrapperService ws) throws Exception{ + for(Identifier item : mpiwgIdList){ + + + System.out.println(item); + + Entity codex = ws.getEntityByIdWithContent(item.ismiId); + + if(codex == null) + throw new Exception("Codex " + item.ismiId + " was not found."); + + + //Setting indexmeta_folder + if(codex.getAttributeByName(ATT_INDEXMETA_FOLDER) == null){ + Attribute att = new Attribute(ATT_INDEXMETA_FOLDER, "text", item.indexmeta); + codex.addAttribute(att); + }else{ + codex.getAttributeByName(ATT_INDEXMETA_FOLDER).setValue(item.indexmeta); + } + + //Setting mpiwg_id + if(codex.getAttributeByName(ATT_MPIWG_ID) == null){ + Attribute att = new Attribute(ATT_MPIWG_ID, "text", item.mpiwgId); + codex.addAttribute(att); + }else{ + codex.getAttributeByName(ATT_MPIWG_ID).setValue(item.mpiwgId); + } + + ws.saveAssertion(codex, userName); + } + } + + private static void prepareDefinitions(WrapperService ws) throws Exception{ + + Entity codex = ws.getDefinition(CODEX); + + Attribute attIndexmetaFolder = ws.getDefAttributeByOwnValue(CODEX, ATT_INDEXMETA_FOLDER); + + if(attIndexmetaFolder == null){ + attIndexmetaFolder = new Attribute(Node.TYPE_TBOX, "text", ATT_INDEXMETA_FOLDER); + attIndexmetaFolder.setSourceId(codex.getId()); + attIndexmetaFolder.setSourceObjectClass(Node.TYPE_TBOX); + attIndexmetaFolder.setSourceModif(codex.getModificationTime()); + attIndexmetaFolder.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + ws.saveDefAttribute(attIndexmetaFolder, userName); + } + + Attribute attMpiwgwgId = ws.getDefAttributeByOwnValue(CODEX, ATT_MPIWG_ID); + + if(attMpiwgwgId == null){ + attMpiwgwgId = new Attribute(Node.TYPE_TBOX, "text", ATT_MPIWG_ID); + attMpiwgwgId.setSourceId(codex.getId()); + attMpiwgwgId.setSourceObjectClass(Node.TYPE_TBOX); + attMpiwgwgId.setSourceModif(codex.getModificationTime()); + attMpiwgwgId.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + ws.saveDefAttribute(attMpiwgwgId, userName); + } + } + + private static List<Identifier> readFile(){ + List<Identifier> rs = new ArrayList<Indexmeta.Identifier>(); + + + + try{ + Scanner scan = new Scanner( + new File("/Users/jurzua/Projects/ISMI/workspace/ismi-richfaces/docs/echo/archiv_id_ismi_online_2014.05.23.tab")); + + String line=""; + //int readline = Integer.parseInt(scan.nextLine());// + while (scan.hasNextLine()) + { + line = scan.nextLine(); + + Identifier id = new Identifier(line); + if(id.isValid){ + rs.add(id); + } + } + }catch(Exception e){ + e.printStackTrace(); + } + return rs; + } + + public static class Identifier{ + + public Long ismiId; + public String indexmeta; + public String mpiwgId; + public String label; + public boolean isValid = false; + + public Identifier(String line) throws Exception{ + String[] split = line.split("\t"); + if(split.length >= 3){ + this.indexmeta = split[0]; + this.label = split[1]; + this.ismiId = Long.parseLong(split[2]); + + String link0 = indexmeta.replace("/mpiwg/online", ""); + String link = "http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Texter?fn=" + link0 + "/index.meta"; + this.mpiwgId = getMpiwgId(link); + + this.isValid = (StringUtils.isNotEmpty(mpiwgId) && ismiId != null); + } + } + + @Override + public String toString(){ + return label + "\t[" + indexmeta + ", " + ismiId + ", " + mpiwgId + "]"; + } + + } + + + public static void execute(){ + ServiceRegistry sr = new ServiceRegistry(); + + try { + prepareDefinitions(sr.getWrapper()); + saveMpiwgIds(readFile(), sr.getWrapper()); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + execute(); + System.exit(0); + } + + private static Document getDocument(String link){ + try { + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(link); + return doc; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static String getMpiwgId(String link) throws Exception{ + + Document doc = getDocument(link); + + XPathFactory xPathfactory = XPathFactory.newInstance(); + XPath xpath = xPathfactory.newXPath(); + + return (String) xpath.compile("//resource/meta/dri[@type='mpiwg']").evaluate(doc, XPathConstants.STRING); + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/NormalizeOW.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,105 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.repository.utils.ArabicNormalizerUtils; +import org.mpi.openmind.repository.utils.NormalizerUtils; + +public class NormalizeOW { + public static void execute(String type) { + try { + System.out.println("Normalizing own values for all: " + type +"S."); + System.out.println("INFO: only for the CURRENT_VERSION of the mentioned nodes will be affected."); + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "ismi", "ismipw"); + + Map<Long, String> selectedMap = select(conn, type); + + System.out.println("Amount of nodes=" + selectedMap.size()); + change(conn, selectedMap); + System.out.println("End"); + + conn.close(); + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + + } + + public static void change(Connection conn, Map<Long, String> map){ + String s = new String(); + for(Long id : map.keySet()){ + try { + Statement st = conn.createStatement(); + String normalizedOW = NormalizerUtils.normalize(map.get(id)); + String normalizedArabicOW = ArabicNormalizerUtils.normalize(map.get(id)); + st.executeUpdate("UPDATE node SET normalized_own_value='" + normalizedOW + "' WHERE row_id='"+ id +"'"); + s = "UPDATE node SET normalized_arabic_own_value='" + normalizedArabicOW + "' WHERE row_id='"+ id +"'"; + //System.out.println(s); + st.executeUpdate(s); + } catch (SQLException e) { + System.err.println(s); + e.printStackTrace(); + } + } + } + + @SuppressWarnings("finally") + public static Map<Long, String> select(Connection conn, String type){ + Map<Long, String> map = new HashMap<Long, String>(); + String query = "select row_id, own_value " + + "from node " + + "where system_status = 'CURRENT_VERSION'"; + + if(type.equals("ATTRIBUTE") || type.equals("ENTITY")){ + query += " AND node_type = '"+ type +"'"; + } + + try + { + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); + while (rs.next()) + { + String id = rs.getString("row_id"); + String ow = rs.getString("own_value"); + map.put(new Long(id), ow); + } + } + catch (SQLException ex){ + ex.printStackTrace(); + //System.err.println(ex.getMessage()); + }finally{ + return map; + } + } + + public static void main(String[] args) { + String arg = args[0]; + if(StringUtils.isNotEmpty(arg)){ + if(arg.equals("ATTRIBUTE") || arg.equals("ENTITY") || arg.equals("all")){ + NormalizeOW.execute(arg); + System.exit(0); + } + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/PrintAhlwardtNr.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,40 @@ +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 PrintAhlwardtNr { + + + public static void execute(WrapperService wrapper){ + + + List<Attribute> list = wrapper.getAttributes("WITNESS", "ahlwardt_no"); + + + System.out.println("Codex\tWitness\tAhlwardt_no"); + for(Attribute att : list){ + + List<Entity> codexList = wrapper.getTargetsForSourceRelation(att.getSourceId(), "is_part_of", "CODEX", 1); + if(codexList.isEmpty()){ + //System.err.println("XXXXXXXXXXXXXXXXXX"); + }else{ + System.out.println(codexList.get(0).getId() + "\t" + att.getSourceId() + "\t" + att.getValue()); + } + + } + + } + + public static void main(String[] args){ + + ServiceRegistry sr = new ServiceRegistry(); + execute(sr.getWrapper()); + System.exit(0); + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/PrintStartPages.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,40 @@ +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 PrintStartPages { + + + public static void execute(WrapperService wrapper){ + + + List<Attribute> list = wrapper.getAttributes("WITNESS", "start_page"); + + + System.out.println("Codex\tWitness\tAhlwardt_no"); + for(Attribute att : list){ + + List<Entity> codexList = wrapper.getTargetsForSourceRelation(att.getSourceId(), "is_part_of", "CODEX", 1); + if(codexList.isEmpty()){ + //System.err.println("XXXXXXXXXXXXXXXXXX"); + }else{ + System.out.println(codexList.get(0).getId() + "\t" + att.getSourceId() + "\t" + att.getValue()); + } + + } + + } + + public static void main(String[] args){ + + ServiceRegistry sr = new ServiceRegistry(); + execute(sr.getWrapper()); + System.exit(0); + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/PublicCodices.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,206 @@ +package org.mpi.openmind.scripts; + +import java.util.ArrayList; +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.bo.Node; +import org.mpi.openmind.repository.services.ServiceRegistry; + +public class PublicCodices { + + + private static String ATT_NAME = "public"; + private static String CLASS_NAME = "CODEX"; + + + private static List<Long> codexIdList = new ArrayList<Long>(); + public static String userName = "public-codices-script"; + static{ + codexIdList.add(new Long(27543)); + codexIdList.add(new Long(36745)); + codexIdList.add(new Long(58453)); + codexIdList.add(new Long(87298)); + codexIdList.add(new Long(259646)); + codexIdList.add(new Long(35093)); + codexIdList.add(new Long(22863)); + codexIdList.add(new Long(34870)); + codexIdList.add(new Long(36882)); + codexIdList.add(new Long(101488)); + codexIdList.add(new Long(36696)); + codexIdList.add(new Long(31794)); + codexIdList.add(new Long(37240)); + codexIdList.add(new Long(35014)); + codexIdList.add(new Long(35583)); + codexIdList.add(new Long(37025)); + codexIdList.add(new Long(35960)); + codexIdList.add(new Long(172492)); + codexIdList.add(new Long(98286)); + codexIdList.add(new Long(165721)); + codexIdList.add(new Long(260111)); + codexIdList.add(new Long(90980)); + codexIdList.add(new Long(36316)); + codexIdList.add(new Long(260120)); + codexIdList.add(new Long(36241)); + codexIdList.add(new Long(260129)); + codexIdList.add(new Long(260138)); + codexIdList.add(new Long(38860)); + codexIdList.add(new Long(176694)); + codexIdList.add(new Long(72545)); + codexIdList.add(new Long(36185)); + codexIdList.add(new Long(36575)); + codexIdList.add(new Long(260146)); + codexIdList.add(new Long(31672)); + codexIdList.add(new Long(37739)); + codexIdList.add(new Long(89861)); + codexIdList.add(new Long(176778)); + codexIdList.add(new Long(180743)); + codexIdList.add(new Long(86328)); + codexIdList.add(new Long(260150)); + codexIdList.add(new Long(90658)); + codexIdList.add(new Long(58423)); + codexIdList.add(new Long(181058)); + codexIdList.add(new Long(105948)); + codexIdList.add(new Long(35526)); + codexIdList.add(new Long(74078)); + codexIdList.add(new Long(260158)); + codexIdList.add(new Long(181096)); + codexIdList.add(new Long(31606)); + codexIdList.add(new Long(31568)); + codexIdList.add(new Long(27872)); + codexIdList.add(new Long(36938)); + codexIdList.add(new Long(4836)); + codexIdList.add(new Long(34668)); + codexIdList.add(new Long(76866)); + codexIdList.add(new Long(102230)); + codexIdList.add(new Long(76888)); + codexIdList.add(new Long(74070)); + codexIdList.add(new Long(73757)); + codexIdList.add(new Long(182685)); + codexIdList.add(new Long(260162)); + codexIdList.add(new Long(260170)); + codexIdList.add(new Long(1102)); + codexIdList.add(new Long(172888)); + codexIdList.add(new Long(260174)); + codexIdList.add(new Long(34806)); + codexIdList.add(new Long(28088)); + codexIdList.add(new Long(36713)); + codexIdList.add(new Long(37323)); + codexIdList.add(new Long(34551)); + codexIdList.add(new Long(35943)); + codexIdList.add(new Long(98095)); + codexIdList.add(new Long(260178)); + codexIdList.add(new Long(260182)); + codexIdList.add(new Long(182770)); + codexIdList.add(new Long(260186)); + codexIdList.add(new Long(260190)); + codexIdList.add(new Long(260194)); + codexIdList.add(new Long(36114)); + codexIdList.add(new Long(85003)); + codexIdList.add(new Long(31630)); + codexIdList.add(new Long(157290)); + codexIdList.add(new Long(37153)); + codexIdList.add(new Long(37213)); + codexIdList.add(new Long(172952)); + codexIdList.add(new Long(86871)); + codexIdList.add(new Long(64406)); + codexIdList.add(new Long(102590)); + codexIdList.add(new Long(82615)); + codexIdList.add(new Long(58245)); + codexIdList.add(new Long(179791)); + codexIdList.add(new Long(179550)); + codexIdList.add(new Long(12419)); + codexIdList.add(new Long(95861)); + codexIdList.add(new Long(36429)); + codexIdList.add(new Long(36099)); + codexIdList.add(new Long(74237)); + codexIdList.add(new Long(36065)); + codexIdList.add(new Long(74822)); + codexIdList.add(new Long(87549)); + codexIdList.add(new Long(83765)); + codexIdList.add(new Long(36733)); + codexIdList.add(new Long(19259)); + codexIdList.add(new Long(260198)); + codexIdList.add(new Long(34986)); + codexIdList.add(new Long(88041)); + codexIdList.add(new Long(260202)); + codexIdList.add(new Long(36550)); + codexIdList.add(new Long(260206)); + codexIdList.add(new Long(37228)); + codexIdList.add(new Long(39880)); + codexIdList.add(new Long(36318)); + codexIdList.add(new Long(36597)); + codexIdList.add(new Long(35035)); + codexIdList.add(new Long(58328)); + codexIdList.add(new Long(80831)); + codexIdList.add(new Long(58354)); + codexIdList.add(new Long(74277)); + codexIdList.add(new Long(36529)); + codexIdList.add(new Long(36380)); + codexIdList.add(new Long(69450)); + codexIdList.add(new Long(200246)); + codexIdList.add(new Long(260222)); + codexIdList.add(new Long(81178)); + codexIdList.add(new Long(260226)); + codexIdList.add(new Long(199952)); + codexIdList.add(new Long(262557)); + codexIdList.add(new Long(87212)); + codexIdList.add(new Long(99059)); + codexIdList.add(new Long(64270)); + codexIdList.add(new Long(81811)); + codexIdList.add(new Long(65785)); + codexIdList.add(new Long(36645)); + } + + + public static void prepareDefinitions(WrapperService ws) throws Exception{ + + Entity codex = ws.getDefinition(CLASS_NAME); + + Attribute attPublic = ws.getDefAttributeByOwnValue(CLASS_NAME, ATT_NAME); + + if(attPublic == null){ + attPublic = new Attribute(Node.TYPE_TBOX, "boolean", ATT_NAME); + attPublic.setSourceId(codex.getId()); + attPublic.setSourceObjectClass(Node.TYPE_TBOX); + attPublic.setSourceModif(codex.getModificationTime()); + attPublic.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); + ws.saveDefAttribute(attPublic, userName); + } + } + + public static void makeCodicesPublic(WrapperService ws) throws Exception{ + + for(Long codexId : codexIdList){ + Entity codex = ws.getEntityByIdWithContent(codexId); + if(codex != null){ + if(codex.getAttributeByName(ATT_NAME) == null){ + Attribute att = new Attribute(ATT_NAME, "boolean", "true"); + codex.addAttribute(att); + }else{ + codex.getAttributeByName(ATT_NAME).setValue("true"); + } + } + ws.saveAssertion(codex, userName); + } + } + + + + public static void main(String[] args){ + ServiceRegistry services = new ServiceRegistry(); + + try { + prepareDefinitions(services.getWrapper()); + makeCodicesPublic(services.getWrapper()); + } catch (Exception e) { + e.printStackTrace(); + } + + System.exit(0); + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/RelationsWithoutSrcOrTar.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,162 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.services.ServiceRegistry; + +public class RelationsWithoutSrcOrTar { + private static String query; + + static{ + query = + "select row_id, id, source_id, target_id from node where node_type = 'RELATION' and system_status = 'CURRENT_VERSION' and type = 'ABox'"; + } + + public static ResultSet select(Connection conn, String arg, WrapperService os){ + ResultSet rs = null; + try + { + System.out.println("[rowId, id]\t[id1, id2]\t[src_id, tar_id]\t[src_exist, tar_exist]"); + Statement st = conn.createStatement(); + rs = st.executeQuery(query); + int count =0; + int allCount = 0; + while (rs.next()){ + + Relation rel = + new Relation(rs.getLong("row_id"), rs.getLong("id"), rs.getLong("source_id"), rs.getLong("target_id")); + + Entity src = os.getEntityById(rel.srcId); + if(src == null){ + rel.existSrc = false; + } + Entity tar = os.getEntityById(rel.tarId); + if(tar == null){ + rel.existTar = false; + } + + if(rel.existSrc == false || rel.existTar == false){ + System.out.println("[" + rel.rowId + ", " + rel.id + "]\t" + + "["+ rel.srcId +", "+ rel.tarId +"]\t" + + "["+ rel.existSrc +", "+ rel.existTar +"]"); + + if(StringUtils.isNotEmpty(arg) && arg.equals("REDUCE")){ + Statement st0 = conn.createStatement(); + st0.executeUpdate("DELETE FROM node WHERE row_id = '"+ rel.rowId + "'"); + } + + count++; + } + + + + if(allCount % 200 == 0){ + System.out.println(allCount); + } + allCount++; + } + System.out.println(); + System.out.println(count + " relations with problems of " + allCount); + + } + catch (SQLException ex){ + ex.printStackTrace(); + } + return rs; + } + + /* + public static void reduce(Connection conn, ResultSet rs){ + System.out.println("Reducing..."); + try { + while (rs.next()){ + Statement st = conn.createStatement(); + + Long time1 = null; + Long time2 = null; + try{ + time1 = new Long(rs.getString("time1")); + time2 = new Long(rs.getString("time2")); + }catch (Exception e) {} + + //the oldest row should be deleted + if(time1 != null && time2 != null){ + if(time1 > time2){ + st.executeUpdate("DELETE node WHERE row_id='"+ rs.getString("row_id2") + "'"); + }else{ + st.executeUpdate("DELETE node WHERE row_id='"+ rs.getString("row_id1") + "'"); + } + }else{ + st.executeUpdate("DELETE node WHERE row_id='"+ rs.getString("row_id1") + "'"); + } + + + } + } catch (SQLException e) { + e.printStackTrace(); + } + }*/ + + public static void execute(String arg, WrapperService os) { + + + try { + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "ismi", "ismipw"); + + ResultSet rs = select(conn, arg, os); + + + + conn.close(); + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + + } + + + + public static void main(String[] args) { + String arg = null; + if(args.length > 0){ + arg = args[0]; + } + ServiceRegistry sr = new ServiceRegistry(); + RelationsWithoutSrcOrTar.execute(arg, sr.getWrapper()); + System.exit(0); + } + + public static class Relation{ + + public boolean existSrc = true; + public boolean existTar = true; + public Long rowId; + public Long id; + public Long srcId; + public Long tarId; + + public Relation(Long rowId, Long id, Long srcId, Long tarId){ + this.rowId = rowId; + this.id = id; + this.srcId = srcId; + this.tarId = tarId; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/RepositoryName.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,57 @@ +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); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/RoleToRelation.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,110 @@ +package org.mpi.openmind.scripts; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +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; +import org.mpi.openmind.repository.utils.ImportOM3Util; + +public class RoleToRelation { + + private static List<String> roles = new ArrayList<String>(); + private static String USER = "script-role"; + private WrapperService ontology; + private List<Entity> roleList = new ArrayList<Entity>(); + private String filePath = new String(); + + + static{ + roles.add("Annotator"); + roles.add("Author"); + roles.add("Copyist"); + roles.add("Illuminator"); + roles.add("Illustrator"); + roles.add("Owner"); + roles.add("Patron"); + roles.add("Student"); + roles.add("Teacher"); + roles.add("Translator"); + } + + public RoleToRelation(WrapperService ot, String filePath){ + this.ontology = ot; + this.filePath = filePath; + } + + public void execute(){ + + if(StringUtils.isNotEmpty(filePath)){ + ImportOM3Util.importConcepts(ontology, filePath, true); + } + + + this.createRoles(); + + List<Entity> personList = this.ontology.getLightweightAssertions("PERSON", null,-1); + + for(Entity person : personList){ + Attribute att = this.ontology.getPS().getAttributeByName(person, "role"); + if(att != null){ + Entity role = getRole(att.getValue()); + if(role != null){ + Relation hasRole = new Relation(person, role, "has_role"); + try { + ontology.saveNodeOnlyForScripts(hasRole, USER); + } catch (Exception e) { + e.printStackTrace(); + } + ontology.removeNode(att); + }else{ + System.err.println("This role was not found: " + att.getOwnValue()); + } + } + } + } + + public Entity getRole(String name){ + if(StringUtils.isNotEmpty(name)){ + for(Entity role : roleList){ + if(role.getOwnValue().toLowerCase().equals(name.toLowerCase())){ + return role; + } + } + } + return null; + } + + public void createRoles(){ + for(String s : roles){ + try { + Entity role = new Entity(Node.TYPE_ABOX, "ROLE", false); + role.setOwnValue(s); + role.addAttribute(new Attribute("name", "text", s)); + ontology.saveAssertion(role, USER); + this.roleList.add(role); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + + public static void main(String[] args) { + + //path of the file that constains the definitions. + String filePath = (args.length > 0) ? args[0] : null; + + ServiceRegistry services = new ServiceRegistry(); + RoleToRelation script = new RoleToRelation(services.getWrapper(), filePath); + script.execute(); + System.exit(0); + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/ShowRepositoryName.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,47 @@ +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 ShowRepositoryName { + + public static String USER = "script-repo-name"; + + private WrapperService ontology; + + public ShowRepositoryName(WrapperService ot){ + this.ontology = ot; + } + + int countCorrect =0; + int countError =0; + public void execute(){ + 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())); + this.ontology.saveAssertion(repo, USER); + **/ + }else{ + countCorrect++; + } + } + System.out.println("There are " + countError + " repositories without an attribute called name."); + } + + public static void main(String[] args) { + ServiceRegistry services = new ServiceRegistry(); + ShowRepositoryName script = new ShowRepositoryName(services.getWrapper()); + script.execute(); + System.exit(0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/SicilyCase.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,122 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.services.ServiceRegistry; + +public class SicilyCase { + + //public static String USER = "script-repo-name"; + +/* + +select pl1.source_id as PL1, pl1.id, pl2.id, pl1.own_value from openmind.`node` as pl1, openmind.`node` as pl2 +where + +pl1.node_type = 'RELATION' and +pl2.node_type = 'RELATION' and +pl1.system_status = 'CURRENT_VERSION' and +pl2.system_status = 'CURRENT_VERSION' and +pl1.source_obj_class = 'PLACE' and +pl2.source_obj_class = 'PLACE' and +pl1.target_obj_class = 'PLACE' and +pl2.target_obj_class = 'PLACE' and +pl1.source_id = pl2.source_id and +pl1.id != pl2.id + + */ + + + private WrapperService ontology; + private static String sql = + "select " + + "pl1.source_id as srcId, " + + "pl1.id as rel1Id, " + + "pl2.id as rel2Id, " + + "pl1.own_value, " + + "pl1.target_id as tar1Id, " + + "pl2.target_id as tar2Id " + + "from openmind.`node` as pl1, openmind.`node` as pl2 " + + "where " + + "pl1.node_type = 'RELATION' and " + + "pl2.node_type = 'RELATION' and " + + "pl1.system_status = 'CURRENT_VERSION' and " + + "pl2.system_status = 'CURRENT_VERSION' and " + + "pl1.source_obj_class = 'PLACE' and " + + "pl2.source_obj_class = 'PLACE' and " + + "pl1.target_obj_class = 'PLACE' and " + + "pl2.target_obj_class = 'PLACE' and " + + "pl1.source_id = pl2.source_id and " + + "pl1.id != pl2.id"; + + public SicilyCase(WrapperService ot){ + this.ontology = ot; + } + + public void execute(){ + try { + + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "ismi", "ismipw"); + + + Statement st = conn.createStatement(); + + + + + System.out.println("Executing:"); + System.out.println(sql); + ResultSet rs = st.executeQuery(sql); + + System.out.println("srcId\ttar1\ttar2"); + + while(rs.next()){ + Long srcId = rs.getLong("srcId"); + Long tar1Id = rs.getLong("tar1Id"); + Long tar2Id = rs.getLong("tar2Id"); + Long rel1Id = rs.getLong("rel1Id"); + Long rel2Id = rs.getLong("rel2Id"); + + Entity src = ontology.getEntityByIdReadOnly(srcId); + Entity tar1 = ontology.getEntityByIdReadOnly(tar1Id); + Entity tar2 = ontology.getEntityByIdReadOnly(tar2Id); + + System.out.println(src.getOwnValue() + " [" +srcId + "]\t" + tar1.getOwnValue() + " [" + tar1Id + "]\t" + tar2.getOwnValue() + " [" + tar2Id + "]"); + } + + + conn.close(); + + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + ex.printStackTrace(); + } + } + + + public static void main(String[] args) { + ServiceRegistry services = new ServiceRegistry(); + SicilyCase script = new SicilyCase(services.getWrapper()); + script.execute(); + System.exit(0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/StabiCollection.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,70 @@ +package org.mpi.openmind.scripts; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +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; +import org.mpi.openmind.repository.services.utils.AttributeFilter; + +public class StabiCollection { + + public static String file = "/Users/jurzua/Projects/ISMI/workspace/ismi-richfaces/docs/canada/publicView/stabi_codices.txt"; + + public static void execute(WrapperService ws){ + + System.out.println("#### StabiCollection #### StabiCollection"); + + for(String value : getValuesFromFile()){ + List<AttributeFilter> filterList = new ArrayList<AttributeFilter>(); + filterList.add(new AttributeFilter("name", value, "DIGITALIZATION")); + Map<Entity, Attribute> resultMap = ws.searchEntityByAttributeFilter(filterList, 1); + Entity digi = resultMap.keySet().iterator().next(); + + + + List<Entity> tmp = ws.getTargetsForSourceRelation(digi, "is_digitalization_of", "CODEX", 1); + Entity codex = tmp.get(0); + //System.out.println(codex.getId() + "\t" + digi.getId()); + //System.out.println("codexIdList.add(new Long("+ codex.getId() +"));"); + System.out.print(codex.getId() + ","); + } + + } + + public static List<String> getValuesFromFile(){ + List<String> rs = new ArrayList<String>(); + + try { + FileReader a = new FileReader(file); + BufferedReader br = new BufferedReader(a); + String line; + line = br.readLine(); + + while((line = br.readLine()) != null) { + rs.add(line); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + return rs; + } + + + public static void main(String[] args) { + + ServiceRegistry sr = new ServiceRegistry(); + execute(sr.getWrapper()); + + System.exit(0); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/TimeModification.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,631 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.NotImplementedException; +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.repository.bo.Node; + +/** + * + * This script find the relation that are mark as "CURRENT_VERSION", + * however the time_modification does not meet either the time modification of the source, of target or both. + * + * This script does not work well, when there is twice entities with the state "CURRENT_VERSION" and with the same id. + * This problem should be corrected before running this script. Use before class: DoubleEntity + * + * + * @author jurzua + * + */ +public class TimeModification { + + //selects + private static String queryEntListByClass = "select * from openmind.node where object_class = '##' and system_status = 'CURRENT_VERSION'"; + private static String queryAllEntList = "select * from openmind.node where node_type = 'ENTITY' and system_status = 'CURRENT_VERSION' and type = 'ABox'"; + private static String queryTarRels = "select * from openmind.node where target_id = '##' and system_status = 'CURRENT_VERSION'"; + private static String querySrcRels = "select * from openmind.node where source_id = '##' and system_status = 'CURRENT_VERSION' and node_type = 'RELATION'"; + + //updates + private static String updateTarRel = "UPDATE openmind.node SET target_modif = '##' WHERE row_id = '**'"; + private static String updateSrcRel = "UPDATE openmind.node SET source_modif = '##' WHERE row_id = '**'"; + + private static List<ClassRelation> classRelList = new ArrayList<ClassRelation>(); + + static{ + classRelList.add(new ClassRelation("WITNESS", "src:is_part_of")); + classRelList.add(new ClassRelation("WITNESS", "src:is_exemplar_of")); + classRelList.add(new ClassRelation("CODEX", "src:is_part_of")); + classRelList.add(new ClassRelation("COLLECTION", "src:is_part_of")); + classRelList.add(new ClassRelation("REPOSITORY", "src:is_in")); + classRelList.add(new ClassRelation("CODEX", "tar:is_part_of")); + classRelList.add(new ClassRelation("COLLECTION", "tar:is_part_of")); + classRelList.add(new ClassRelation("REPOSITORY", "tar:is_part_of")); + classRelList.add(new ClassRelation("PLACE", "tar:is_in")); + classRelList.add(new ClassRelation("TEXT", "tar:is_exemplar_of")); + classRelList.add(new ClassRelation("TEXT", "src:was_created_by")); + classRelList.add(new ClassRelation("TEXT", "tar:is_prime_alias_title_of")); + classRelList.add(new ClassRelation("TEXT", "tar:is_alias_title_of")); + classRelList.add(new ClassRelation("TEXT", "tar:is_translation_of")); + classRelList.add(new ClassRelation("PERSON", "tar:was_created_by")); + classRelList.add(new ClassRelation("PERSON", "src:lived_in")); + classRelList.add(new ClassRelation("PERSON", "tar:is_reference_of")); + classRelList.add(new ClassRelation("PERSON", "tar:is_alias_name_of")); + classRelList.add(new ClassRelation("PERSON", "src:has_role")); + classRelList.add(new ClassRelation("PERSON", "tar:was_copied_by")); + classRelList.add(new ClassRelation("ALIAS", "src:is_prime_alias_title_of")); + classRelList.add(new ClassRelation("ALIAS", "src:is_alias_title_of")); + classRelList.add(new ClassRelation("ALIAS", "tar:has_title_written_as")); + } + + + /** + * + * @param conn + * @param entId + * @param entModif + * @param entOC + * @param action show/solve + * @throws SQLException + */ + public static void executeEntity(Connection conn, Long entId, Long entModif, String entOC, String relacion, String action) throws Exception{ + + if(StringUtils.isEmpty(relacion)){ + executeSources(conn, entId, entModif, entOC, null, action); + executeTargets(conn, entId, entModif, entOC, null, action); + }else{ + String[] array = relacion.split(":"); + String relLabel = array[1]; + if(array[0].equals("src")){ + executeSources(conn, entId, entModif, entOC, relLabel, action); + }else if(array[0].equals("tar")){ + executeTargets(conn, entId, entModif, entOC, relLabel, action); + }else{ + throw new Exception("The parameter relation is not valid: " + relacion); + } + } + } + + public static boolean analyseSrcRels(Connection conn, List<Relation> rels, Long entId, Long entModif, String entOC, String relLabel, String action) throws Exception{ + + boolean ok = true; + + for(Relation rel : rels){ + if(!rel.srcModif.equals(entModif)){ + ok = false; + break; + } + } + + if(!ok){ + + printRels(entId, entOC, entModif, rels, "SRC"); + + if( StringUtils.equals(action, "solve") && ( + (StringUtils.equals(entOC, "WITNESS") && (StringUtils.equals(relLabel, "is_part_of"))) || + (StringUtils.equals(entOC, "WITNESS") && (StringUtils.equals(relLabel, "is_exemplar_of"))) || + (StringUtils.equals(entOC, "COLLECTION") && (StringUtils.equals(relLabel, "is_part_of"))) || + (StringUtils.equals(entOC, "TEXT") && (StringUtils.equals(relLabel, "was_created_by"))) || + (StringUtils.equals(entOC, "ALIAS") && (StringUtils.equals(relLabel, "is_prime_alias_title_of"))) || + (StringUtils.equals(entOC, "ALIAS") && (StringUtils.equals(relLabel, "is_alias_title_of"))) + ) + ){ + solveUniqueSourceRel(conn, rels, entId, entModif); + } + + + if( StringUtils.equals(action, "solve") && ( + (StringUtils.equals(entOC, "PERSON") && (StringUtils.equals(relLabel, "lived_in"))) || + (StringUtils.equals(entOC, "PERSON") && (StringUtils.equals(relLabel, "has_role"))) + ) + ){ + solveMultipleSourceRels(conn, rels, entId, entModif); + removeDoubleRelations(conn, rels); + } + + } + return ok; + } + + public static boolean analyseTarRels(Connection conn, List<Relation> rels, Long entId, Long entModif, String entOC, String relLabel, String action) throws Exception{ + + boolean ok = true; + + for(Relation rel : rels){ + if(!rel.tarModif.equals(entModif)){ + ok = false; + break; + } + } + + if(!ok){ + + printRels(entId, entOC, entModif, rels, "TAR"); + + + if( StringUtils.equals(action, "solve") && ( + (StringUtils.equals(entOC, "CODEX") && (StringUtils.equals(relLabel, "is_part_of"))) || + (StringUtils.equals(entOC, "TEXT") && (StringUtils.equals(relLabel, "is_exemplar_of"))) || + (StringUtils.equals(entOC, "TEXT") && (StringUtils.equals(relLabel, "is_alias_title_of"))) || + (StringUtils.equals(entOC, "PERSON") && (StringUtils.equals(relLabel, "was_created_by"))) || + (StringUtils.equals(entOC, "PERSON") && (StringUtils.equals(relLabel, "is_reference_of"))) || + (StringUtils.equals(entOC, "PERSON") && (StringUtils.equals(relLabel, "is_alias_name_of"))) || + (StringUtils.equals(entOC, "PERSON") && (StringUtils.equals(relLabel, "was_copied_by"))) || + (StringUtils.equals(entOC, "COLLECTION") && (StringUtils.equals(relLabel, "is_part_of"))) || + (StringUtils.equals(entOC, "REPOSITORY") && (StringUtils.equals(relLabel, "is_part_of"))) || + (StringUtils.equals(entOC, "PLACE") && (StringUtils.equals(relLabel, "is_in"))) + ) + ){ + solveMultipleTargetRels(conn, rels, entId, entModif); + removeDoubleRelations(conn, rels); + } + + if( StringUtils.equals(action, "solve") && ( + (StringUtils.equals(entOC, "TEXT") && (StringUtils.equals(relLabel, "is_prime_alias_title_of"))) || + (StringUtils.equals(entOC, "TEXT") && (StringUtils.equals(relLabel, "is_translation_of"))) || + (StringUtils.equals(entOC, "ALIAS") && (StringUtils.equals(relLabel, "has_title_written_as"))) + ) + ){ + solveUniqueTargetRel(conn, rels, entId, entModif); + } + } + return ok; + } + + private static void printRels(Long entId, String entOC, Long entModif, List<Relation> rels, String direction){ + System.out.println("\n" + direction + ") Entity [id=" + entId + ", oc=" + entOC + ", modif=" + entModif + "] - Relation " + rels.get(0).label); + System.out.println("\trowId\tid\tlabel\tsrcModif\ttarModif"); + for(Relation rel : rels){ + System.out.println("\t" + rel.toString()); + } + } + + /** + * This method should be used only when the entity is WITNESS and from the source domain. + * @param conn + * @param rels + * @param entId + * @param entModif + * @throws SQLException + */ + private static void solveUniqueSourceRel(Connection conn, List<Relation> rels, Long entId, Long entModif) throws SQLException{ + System.out.println("\t-----------------------"); + Statement stmt = conn.createStatement() ; + + + Relation correctRel = null; + + for(Relation rel : rels){ + if(rel.srcModif.equals(entModif)){ + correctRel = rel; + break; + } + } + + // if correctRel is not null, it means that there is at least one relation with the right modification time. + // if correctRel is not, we will use the last modified relation + + if(correctRel == null){ + correctRel = getLastModifiedRel(rels); + String update = updateSrcRel.replace("##", entModif.toString()).replace("**", correctRel.rowId.toString()); + System.out.println("\t" + update); + int rows = stmt.executeUpdate( update ) ; + } + + + // All other relations will be made Node.SYS_STATUS_PREVIOUS_VERSION + if(correctRel != null){ + for(Relation rel : rels){ + if(!rel.equals(correctRel)){ + + String update = "UPDATE openmind.node SET system_status = '" + Node.SYS_STATUS_PREVIOUS_VERSION + "' WHERE row_id = '" + rel.rowId + "'"; + System.out.println("\t" + update); + int rows = stmt.executeUpdate( update ) ; + + } + } + } + + stmt.close(); + System.out.println("\t-----------------------\n"); + } + + private static void solveUniqueTargetRel(Connection conn, List<Relation> rels, Long entId, Long entModif) throws SQLException{ + + System.out.println("\t-----------------------"); + Statement stmt = conn.createStatement() ; + + Relation correctRel = null; + + for(Relation rel : rels){ + if(rel.tarModif.equals(entModif)){ + correctRel = rel; + break; + } + } + // if correctRel is not null, it means that there is at least one relation with the right modification time. + // if correctRel is not, we will use the last modified relation + if(correctRel == null){ + correctRel = getLastModifiedRel(rels); + String update = updateTarRel.replace("##", entModif.toString()).replace("**", correctRel.rowId.toString()); + System.out.println("\t" + update); + int rows = stmt.executeUpdate( update ) ; + } + + // All other relations will be made Node.SYS_STATUS_PREVIOUS_VERSION + if(correctRel != null){ + for(Relation rel : rels){ + if(!rel.equals(correctRel)){ + + String update = "UPDATE openmind.node SET system_status = '" + Node.SYS_STATUS_PREVIOUS_VERSION + "' WHERE row_id = '" + rel.rowId + "'"; + System.out.println("\t" + update); + int rows = stmt.executeUpdate( update ) ; + + } + } + } + + stmt.close(); + System.out.println("\t-----------------------\n"); + } + + private static void removeDoubleRelations(Connection conn, List<Relation> rels) throws Exception{ + Map<String, List<Relation>> doubleRelMal = new HashMap<String, List<Relation>>(); + + for(Relation rel : rels){ + String key = rel.srcId + "-" + rel.tarId; + if(!doubleRelMal.containsKey(key)){ + doubleRelMal.put(key, new ArrayList<Relation>()); + } + doubleRelMal.get(key).add(rel); + } + + for(String key : doubleRelMal.keySet()){ + + List<Relation> list = doubleRelMal.get(key); + + //found double relations + + if(list.size() > 1){ + Statement stmt = conn.createStatement() ; + System.out.println("\t-----------------------"); + System.out.println("\tFound double relations:"); + + Relation correctRel = getLastModifiedRel(list); + for(Relation rel : list){ + System.out.println("\t" + rel.toString()); + if(!rel.equals(correctRel)){ + String update = "UPDATE openmind.node SET system_status = '" + Node.SYS_STATUS_PREVIOUS_VERSION + "' WHERE row_id = '" + rel.rowId + "'"; + System.out.println("\t" + update); + int rows = stmt.executeUpdate( update ) ; + } + } + stmt.close(); + System.out.println("\t-----------------------\n"); + } + + } + + } + + private static Relation getLastModifiedRel(List<Relation> rels){ + Relation lastModified = null; + + for(Relation rel : rels){ + if(lastModified == null || lastModified.modifTime < rel.modifTime){ + lastModified = rel; + } + } + return lastModified; + } + + private static void solveMultipleTargetRels(Connection conn, List<Relation> rels, Long entId, Long entModif) throws SQLException{ + System.out.println("\t-----------------------"); + Statement stmt = conn.createStatement() ; + + + for(Relation rel : rels){ + String update = updateTarRel.replace("##", entModif.toString()).replace("**", rel.rowId.toString()); + System.out.println("\t" + update); + int rows = stmt.executeUpdate( update ) ; + } + + stmt.close(); + System.out.println("\t-----------------------\n"); + } + + private static void solveMultipleSourceRels(Connection conn, List<Relation> rels, Long entId, Long entModif) throws SQLException{ + System.out.println("\t-----------------------"); + Statement stmt = conn.createStatement() ; + + + for(Relation rel : rels){ + String update = updateSrcRel.replace("##", entModif.toString()).replace("**", rel.rowId.toString()); + System.out.println("\t" + update); + int rows = stmt.executeUpdate( update ) ; + } + + stmt.close(); + System.out.println("\t-----------------------\n"); + } + + + private static void executeSources(Connection conn, Long entId, Long entModif, String entOC, String relLabel, String action) throws Exception{ + Map<String, List<Relation>> srcRelMap = new HashMap<String, List<Relation>>(); + + ResultSet srcRels = getSrcRels(conn, entId); + try { + while (srcRels.next()) { + Relation rel = new Relation(srcRels); + if(!srcRelMap.containsKey(rel.label)){ + srcRelMap.put(rel.label, new ArrayList<Relation>()); + } + srcRelMap.get(rel.label).add(rel); + } + } finally { + srcRels.close(); + } + + if(StringUtils.isEmpty(relLabel)){ + for(String rel : srcRelMap.keySet()){ + analyseSrcRels(conn, srcRelMap.get(rel), entId, entModif, entOC, rel, action); + } + }else if(srcRelMap.containsKey(relLabel)){ + analyseSrcRels(conn, srcRelMap.get(relLabel), entId, entModif, entOC, relLabel, action); + } + } + + private static void executeTargets(Connection conn, Long entId, Long entModif, String entOC, String relLabel, String action) throws Exception{ + Map<String, List<Relation>> tarRelMap = new HashMap<String, List<Relation>>(); + + ResultSet tarRels = getTarRels(conn, entId); + try { + while (tarRels.next()) { + Relation rel = new Relation(tarRels); + if(!tarRelMap.containsKey(rel.label)){ + tarRelMap.put(rel.label, new ArrayList<Relation>()); + } + tarRelMap.get(rel.label).add(rel); + } + } finally { + tarRels.close(); + } + + if(StringUtils.isEmpty(relLabel)){ + for(String rel : tarRelMap.keySet()){ + analyseTarRels(conn, tarRelMap.get(rel), entId, entModif, entOC, rel, action); + } + }else if(tarRelMap.containsKey(relLabel)){ + analyseTarRels(conn, tarRelMap.get(relLabel), entId, entModif, entOC, relLabel, action); + } + } + + /* + public static void solveTarRels(Connection conn, List<Relation> rels, Long entId, Long entModif) throws SQLException{ + Statement stmt = conn.createStatement() ; + for(Relation rel : rels){ + String update = updateTarRel.replace("##", entModif.toString()).replace("**", rel.rowId.toString()); + System.out.println(update); + int rows = stmt.executeUpdate( update ) ; + } + stmt.close(); + System.out.println("Problem solved!"); + } + + public static void solveSrcRels(Connection conn, List<Relation> rels, Long entId, Long entModif) throws SQLException{ + Statement stmt = conn.createStatement() ; + for(Relation rel : rels){ + String update = updateSrcRel.replace("##", entModif.toString()).replace("**", rel.rowId.toString()); + System.out.println(update); + int rows = stmt.executeUpdate( update ) ; + } + stmt.close(); + System.out.println("Problem solved!"); + }*/ + + public static ResultSet getTarRels(Connection conn, Long entId) + throws SQLException { + String query = queryTarRels.replace("##", entId.toString()); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); + return rs; + } + + public static ResultSet getSrcRels(Connection conn, Long entId) + throws SQLException { + String query = querySrcRels.replace("##", entId.toString()); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); + return rs; + } + + /** + * TODO reduces double entities + * @param conn + * @param objectClass + * @return + * @throws SQLException + */ + public static ResultSet getEntitiesByClass(Connection conn, + String objectClass) throws SQLException { + + String query = null; + + if(StringUtils.isEmpty(objectClass)){ + query = queryAllEntList; + }else{ + query = queryEntListByClass.replace("##", objectClass); + } + + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); + System.out.println("Entities had been loaded..."); + return rs; + } + + public static void execute(String mysqlUser, + String mysqlPass, String action, String objectClass, String relation) { + try { + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, mysqlUser, mysqlPass); + + if(StringUtils.equals("solve", action) && StringUtils.isEmpty(objectClass) && StringUtils.isEmpty(relation)){ + + for(ClassRelation classRel : classRelList){ + System.out.println("#######################################"); + System.out.println(classRel.toString()); + ResultSet ents = getEntitiesByClass(conn, classRel.objectClass); + int count = 0; + + try { + while (ents.next()) { + executeEntity(conn, ents.getLong("id"), ents.getLong("modification_time"), ents.getString("object_class"), classRel.relName, action); + count++; + + if(count % 50 == 0){ + System.out.print("*"); + } + if(count % 300 == 0){ + System.out.println(count); + } + } + } finally { + ents.close(); + } + System.out.println(); + } + + + }else{ + ResultSet ents = getEntitiesByClass(conn, objectClass); + int count = 0; + + try { + while (ents.next()) { + executeEntity(conn, ents.getLong("id"), ents.getLong("modification_time"), ents.getString("object_class"), relation, action); + count++; + + if(count % 50 == 0){ + System.out.print("*"); + } + if(count % 300 == 0){ + System.out.println(count); + } + } + } finally { + ents.close(); + } + } + + + System.out.println("#### Finished ####"); + + conn.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public static void test(){ + try { + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "ismi", "ismipw"); + + executeEntity(conn, Long.parseLong("297238"), Long.parseLong("1405366164815"), "WITNESS", "show", null); + + conn.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + /** + * + * @param args + * objectClass, mysqlUser, mysqlPassword, action + * + * action: + * - show: show all + * - showExemplarOf: for witnesses, it shows the relation "is_exemplar_of" with problems + * - solveExemplarOf: for witnesses, it solves the relation "is_exemplar_of" with problems + * - showPartOf + * - solvePartOf + */ + public static void main(String[] args) { + + //test(); + if (args.length == 3) { + execute(args[0], args[1], args[2], null, null); + }else if(args.length == 4){ + execute(args[0], args[1], args[2], args[3], null); + }else if(args.length == 5){ + execute(args[0], args[1], args[2], args[3], args[4]); + } else { + System.out + .println("Parameter/s no found: They should be: mode(SHOW/REDUCE), mysql_user, mysql_password"); + System.out.println("args.length= " + args.length); + System.out.println(Arrays.toString(args)); + + } + + System.exit(0); + } + + public static class ClassRelation{ + public String objectClass; + public String relName; + + public ClassRelation(String objectClass, String relName){ + this.objectClass = objectClass; + this.relName = relName; + } + + @Override + public String toString(){ + return this.objectClass + " [" + relName + "]"; + } + } + + public static class Relation{ + + public Long id; + public Long rowId; + public Long srcModif; + public Long tarModif; + public Long srcId; + public Long tarId; + public String label; + public Long modifTime; + + public Relation(ResultSet rs) throws SQLException{ + this.id = rs.getLong("id"); + this.rowId = rs.getLong("row_id"); + this.srcModif = rs.getLong("source_modif"); + this.tarModif = rs.getLong("target_modif"); + this.srcId = rs.getLong("source_id"); + this.tarId = rs.getLong("target_id"); + this.label = rs.getString("object_class"); + this.modifTime = rs.getLong("modification_time"); + } + + @Override + public String toString(){ + return this.rowId + "\t" + this.id + "\t" + this.label + "\t" + this.srcId + "\t" + this.tarId + "\t" + this.srcModif + "\t" + this.tarModif; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/WitnessCreatedByText.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,70 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class WitnessCreatedByText { + + private static String query = "select row_id, source_id, target_id, id from node " + + "where node_type = 'RELATION' " + + "and source_obj_class = 'WITNESS' " + + "and target_obj_class = 'PERSON' " + + "and own_value = 'was_created_by' " + + "and system_status = 'CURRENT_VERSION'"; + + public static void execute() { + try { + Connection conn; + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + conn = DriverManager.getConnection(url, "root", "e1nste1n"); + + ResultSet rs = select(conn); + + conn.close(); + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + } + + public static ResultSet select(Connection conn) { + ResultSet rs = null; + try { + System.out + .println("\tID\t[sourceId, targetId]"); + Statement st = conn.createStatement(); + rs = st.executeQuery(query); + int count = 0; + while (rs.next()) { + Statement st0 = conn.createStatement(); + String id = rs.getString("id"); + String source_id = rs.getString("source_id"); + String target_id = rs.getString("target_id"); + String row_id = rs.getString("row_id"); + st0.executeUpdate("DELETE FROM node WHERE row_id = '"+ row_id + "'"); + count++; + System.out.println(count + "\t" + id + "\t" + "[" + source_id + ", " + target_id + "]"); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return rs; + } + + public static void main(String[] args) { + + WitnessCreatedByText.execute(); + System.exit(0); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/WitnessOwnValueGenerator.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,92 @@ +package org.mpi.openmind.scripts; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.cache.WrapperService; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Node; +import org.mpi.openmind.repository.services.PersistenceService; +import org.mpi.openmind.repository.services.ServiceRegistry; + +public class WitnessOwnValueGenerator { + + + private WrapperService ot; + private PersistenceService ss; + + public WitnessOwnValueGenerator(WrapperService ot){ + this.ot = ot; + this.ss = ot.getPS(); + } + + + public void execute(){ + + try { + List<Entity> witnessList = ot.getLightweightAssertions("WITNESS", null, -1); + + List<Node> dirtyEntities = new ArrayList<Node>(); + int count = 0; + for(Entity witness : witnessList){ + + String ownValue = new String(); + Entity text = null; + + List<Entity> list0 = this.ss.getTargetsForSourceRelation(witness, "is_exemplar_of", "TEXT", 1); + if(list0.size() > 0){ + text = list0.get(0); + ownValue += text.getOwnValue(); + } + + + List<Entity> list = this.ss.getTargetsForSourceRelation(witness, "is_part_of", "CODEX", 1); + if(list.size() > 0){ + Entity codex = list.get(0); + //Attribute identifier = ot.getPSice().getAttributeByName(codex, "identifier"); + //String codexValue = (identifier == null) ? "" : "_" + identifier.getValue(); + list = this.ss.getTargetsForSourceRelation(codex, "is_part_of", "COLLECTION", 1); + if(list.size() > 0){ + Entity collection = list.get(0); + list = this.ss.getTargetsForSourceRelation(collection, "is_part_of", "REPOSITORY", 1); + if(list.size() > 0){ + Entity repository = list.get(0); + list = this.ss.getTargetsForSourceRelation(repository, "is_in", "PLACE", 1); + if(list.size() > 0){ + Entity city = list.get(0); + if(StringUtils.isNotEmpty(ownValue)) + ownValue += "_"; + ownValue += city.getOwnValue() + "_" + repository.getOwnValue() + "_" + /*collection.getOwnValue() + */codex.getOwnValue(); + System.out.println(ownValue); + witness.setOwnValue(ownValue); + dirtyEntities.add(witness); + } + } + } + } + + + count++; + if((count % 50) == 0){ + System.out.println("*"); + } + if((count % 200) == 0){ + System.out.println("\n["+ count + "/" + witnessList.size() + "]" + (double)(count/witnessList.size()) * 100 + "%"); + } + } + ot.saveNodeListOnlyForScripts(dirtyEntities); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + + public static void main(String[] args) { + ServiceRegistry services = new ServiceRegistry(); + WitnessOwnValueGenerator script = new WitnessOwnValueGenerator(services.getWrapper()); + script.execute(); + System.exit(0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/recovery/DomXmlRecovery.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,205 @@ +package org.mpi.openmind.scripts.recovery; + +import java.text.DecimalFormat; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +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.Relation; +import org.mpi.openmind.repository.services.ServiceRegistry; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class DomXmlRecovery { + + static XPathFactory xPathfactory = XPathFactory.newInstance(); + static XPath xpath = xPathfactory.newXPath(); + + public static void execute(String file){ + RecoveryCache cache = loadXMLFile(file); + ServiceRegistry services = new ServiceRegistry(); + WrapperService ws = services.getWrapper(); + + removingEntsFromDB(ws, cache); + createNewEnts(ws, cache); + } + + private static void removingEntsFromDB(WrapperService ws, RecoveryCache cache){ + print("################# Removing ################"); + for(Entity def : ws.getConcepts()){ + print(def.getOwnValue()); + for(Entity dbEnt : ws.getEntitiesByDef(def.getOwnValue())){ + if(!cache.existEnt(dbEnt.getId())){ + print("this must be removed from DB=" + dbEnt); + try { + ws.removeCurrentVersionEntity(dbEnt); + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + } + } + + private static void refreshEnts(WrapperService ws, RecoveryCache cache){ + + } + + private static void createNewEnts(WrapperService ws, RecoveryCache cache){ + for(Entity xmlEnt : cache.getEnts()){ + if(!ws.existEntity(xmlEnt.getId())){ + print("This entity must be created " + xmlEnt); + } + } + } + + + private static String percentage(int index, int total){ + double p = (100* (double)index) / (double)total; + DecimalFormat df = new DecimalFormat("#.#"); + return df.format(p); + } + + private static RecoveryCache loadXMLFile(String file){ + + + + RecoveryCache cache = new RecoveryCache(); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; + try { + + builder = factory.newDocumentBuilder(); + Document doc = builder.parse(file); + + + + NodeList nl = (NodeList) xpath.compile("/openmind-data/entities/entity").evaluate(doc, XPathConstants.NODESET); + print("Reading " + nl.getLength() + " entities."); + + for(int i=0; i< nl.getLength(); i++){ + try { + Entity ent = xml2Ent(nl.item(i)); + cache.addEnt(ent); + + if(i % 100 == 0){ + print(i + " entities loaded. " + percentage(i, nl.getLength()) + "%."); + } + + + } catch (Exception e) { + e.printStackTrace(); + } + } + + nl = (NodeList) xpath.compile("/openmind-data/relations/relation").evaluate(doc, XPathConstants.NODESET); + print("Reading " + nl.getLength() + " relations."); + for(int i=0; i< nl.getLength(); i++){ + try { + Relation rel = xml2Rel(nl.item(i)); + cache.addRel(rel); + + if(i % 100 == 0){ + print(i + " relations loaded. " + percentage(i, nl.getLength()) + "%."); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + } catch (Exception e) { + e.printStackTrace(); + } + return cache; + } + + private static Relation xml2Rel(Node node)throws Exception{ + Relation rel = new Relation(); + String ov = (String)xpath.compile("./@own-value").evaluate(node, XPathConstants.STRING); + Long id = Long.parseLong((String)xpath.compile("./@id").evaluate(node, XPathConstants.STRING)); + Long version = Long.parseLong((String)xpath.compile("./@version").evaluate(node, XPathConstants.STRING)); + Long modTime = Long.parseLong((String)xpath.compile("./@modification-time").evaluate(node, XPathConstants.STRING)); + + Long srcId = Long.parseLong((String)xpath.compile("./@source-id").evaluate(node, XPathConstants.STRING)); + Long tarId = Long.parseLong((String)xpath.compile("./@target-id").evaluate(node, XPathConstants.STRING)); + + rel.setId(id); + rel.setOwnValue(ov); + rel.setVersion(version); + rel.setModificationTime(modTime); + rel.setTargetId(tarId); + rel.setSourceId(srcId); + + return rel; + } + + private static Entity xml2Ent(Node node) throws Exception{ + + String oc = (String)xpath.compile("./@object-class").evaluate(node, XPathConstants.STRING); + Long id = Long.parseLong((String)xpath.compile("./@id").evaluate(node, XPathConstants.STRING)); + Long version = Long.parseLong((String)xpath.compile("./@version").evaluate(node, XPathConstants.STRING)); + Long modTime = Long.parseLong((String)xpath.compile("./@modification-time").evaluate(node, XPathConstants.STRING)); + String ov = (String)xpath.compile("./@own-value").evaluate(node, XPathConstants.STRING); + + Entity ent = new Entity(org.mpi.openmind.repository.bo.Node.TYPE_ABOX, oc, false); + ent.setId(id); + ent.setOwnValue(ov); + ent.setModificationTime(modTime); + ent.setVersion(version); + + NodeList nl = (NodeList) xpath.compile("./attributes/attribute").evaluate(node, XPathConstants.NODESET); + for(int i=0; i< nl.getLength(); i++){ + Attribute att = xml2Att(nl.item(i)); + ent.addAttribute(att); + //print(att); + } + + //print(ent); + + return ent; + } + + private static Attribute xml2Att(Node node) throws Exception{ + Attribute att = new Attribute(); + + Long id = Long.parseLong((String)xpath.compile("./@id").evaluate(node, XPathConstants.STRING)); + String name = (String)xpath.compile("./@name").evaluate(node, XPathConstants.STRING); + String value = (String)xpath.compile("./@value").evaluate(node, XPathConstants.STRING); + String contentType = (String)xpath.compile("./@content-type").evaluate(node, XPathConstants.STRING); + Long version = Long.parseLong((String)xpath.compile("./@version").evaluate(node, XPathConstants.STRING)); + Long modTime = Long.parseLong((String)xpath.compile("./@modification-time").evaluate(node, XPathConstants.STRING)); + + att.setId(id); + att.setName(name); + att.setValue(value); + att.setContentType(contentType); + att.setVersion(version); + att.setModificationTime(modTime); + + return att; + } + + private static void print(Object s){ + System.out.println(s); + } + + public static void main(String[] args) { + execute("/Users/jurzua/Projects/max-planck/db_backup/xml_backup/2013.07.12-ENT.xml"); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/recovery/EntitySAXReader.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,111 @@ +package org.mpi.openmind.scripts.recovery; + +import java.io.IOException; +import java.util.Iterator; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.mpi.openmind.repository.bo.Attribute; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Relation; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +public class EntitySAXReader extends DefaultHandler { + + private Entity ent; + private String temp; + private Attribute att; + private Relation rel; + private RecoveryCache cache; + + public RecoveryCache execute(String file) throws SAXException, IOException, + ParserConfigurationException { + cache = new RecoveryCache(); + SAXParserFactory spfac = SAXParserFactory.newInstance(); + SAXParser sp = spfac.newSAXParser(); + sp.parse(file, this); + return cache; + } + + @Override + public void characters(char[] buffer, int start, int length) { + temp = new String(buffer, start, length); + } + + @Override + public void startElement(String uri, String localName, String elementName, + Attributes attributes) throws SAXException { + + if (elementName.equalsIgnoreCase("entity")) { + + String oc = attributes.getValue("object-class"); + this.ent = new Entity(org.mpi.openmind.repository.bo.Node.TYPE_ABOX, oc, false); + + ent.setId(Long.parseLong(attributes.getValue("id"))); + ent.setVersion(Long.parseLong(attributes.getValue("version"))); + ent.setModificationTime(Long.parseLong(attributes.getValue("modification-time"))); + ent.setOwnValue(attributes.getValue("own-value")); + + } else if (elementName.equalsIgnoreCase("definition")) { + + String oc = attributes.getValue("object-class"); + this.ent = new Entity( + org.mpi.openmind.repository.bo.Node.TYPE_TBOX, oc, false); + + ent.setId(Long.parseLong(attributes.getValue("id"))); + ent.setVersion(Long.parseLong(attributes.getValue("version"))); + ent.setModificationTime(Long.parseLong(attributes.getValue("modification-time"))); + ent.setOwnValue(attributes.getValue("own-value")); + + } else if (elementName.equalsIgnoreCase("attribute")) { + this.att = new Attribute(); + + att.setId(Long.parseLong(attributes.getValue("id"))); + att.setVersion(Long.parseLong(attributes.getValue("version"))); + att.setModificationTime(Long.parseLong(attributes + .getValue("modification-time"))); + att.setContentType(attributes.getValue("content-type")); + att.setName(attributes.getValue("name")); + att.setValue(attributes.getValue("value")); + } else if (elementName.equalsIgnoreCase("relation")) { + this.rel = new Relation(); + + rel.setId(Long.parseLong(attributes.getValue("id"))); + rel.setOwnValue(attributes.getValue("own-value")); + //rel.setVersion(Long.parseLong(attributes.getValue("version"))); + //rel.setModificationTime(Long.parseLong(attributes.getValue("modification-time"))); + rel.setSourceId(Long.parseLong(attributes.getValue("source-id"))); + rel.setTargetId(Long.parseLong(attributes.getValue("target-id"))); + } + } + + @Override + public void endElement(String uri, String localName, String elementName) + throws SAXException { + + if (elementName.equalsIgnoreCase("entity")) { + cache.addEnt(ent); + } else if (elementName.equalsIgnoreCase("definition")) { + cache.addEnt(ent); + } else if (elementName.equalsIgnoreCase("attribute")) { + ent.addAttribute(att); + } else if (elementName.equalsIgnoreCase("relation")) { + cache.addRel(rel); + } + } + + public void readList() { + System.out.println("Entities '" + cache.getEnts().size() + "'."); + System.out.println("Relations '" + cache.getRels().size() + "'."); + + Iterator<Entity> it = cache.getEnts().iterator(); + while (it.hasNext()) { + System.out.println(it.next().toString()); + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/recovery/RecoveryCache.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,48 @@ +package org.mpi.openmind.scripts.recovery; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.bo.Relation; + +import cl.maps.triple.TripleKey; +import cl.maps.triple.TripleMap; + +public class RecoveryCache { + private TripleMap<Relation, Long, Long, Long> relMap = new TripleMap<Relation, Long, Long, Long>(); + private Map<Long, Entity> entMap = new HashMap<Long, Entity>(); + + public Collection<Entity> getEnts(){ + return entMap.values(); + } + + public Collection<Relation> getRels(){ + return relMap.values(); + } + + public void addEnt(Entity ent){ + if(!entMap.containsKey(ent.getId())){ + entMap.put(ent.getId(), ent); + } + } + + public void addRel(Relation rel){ + TripleKey<Long, Long, Long> key = new TripleKey<Long, Long, Long>(rel.getSourceId(), rel.getTargetId(), rel.getId()); + relMap.put(key, rel); + } + + public List<Relation> getRelBySrc(Long srcId){ + return this.relMap.getValuesByAKey(srcId); + } + + public List<Relation> getRelByTar(Long tarId){ + return this.relMap.getValuesByBKey(tarId); + } + + public boolean existEnt(Long id){ + return entMap.containsKey(id); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/recovery/SaxXmlRecovery.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,291 @@ +package org.mpi.openmind.scripts.recovery; + +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.FileAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; +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; +import org.mpi.openmind.repository.utils.OMUtils; + +public class SaxXmlRecovery { + + private static Logger logger = Logger.getLogger(SaxXmlRecovery.class); + + private static String user = "recovery_"; + + static { + + Date dNow = new Date(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + user = user + dateFormat.format(dNow); + + ConsoleAppender console = new ConsoleAppender(); // create appender + // configure the appender + String PATTERN = "%d [%p|%c{1}:%L] %m%n"; + console.setLayout(new PatternLayout(PATTERN)); + console.setThreshold(Level.INFO); + console.activateOptions(); + // add appender to any Logger (here is root) + Logger.getRootLogger().addAppender(console); + + /* + FileAppender fa = new FileAppender(); + fa.setName("FileLogger"); + fa.setFile("/Users/jurzua/test.log"); + + fa.setLayout(new PatternLayout(PATTERN)); + fa.setThreshold(Level.INFO); + fa.setAppend(true); + fa.activateOptions(); + + Logger.getRootLogger().addAppender(fa); + */ + + } + + public static void executeEnts(String file) { + long start = System.currentTimeMillis(); + try { + EntitySAXReader reader = new EntitySAXReader(); + RecoveryCache xmlCache = reader.execute(file); + ServiceRegistry services = new ServiceRegistry(); + WrapperService ws = services.getWrapper(); + + Map<Long, Long> idMap = new HashMap<Long, Long>(); + + idMap = saveEnts(ws, xmlCache, idMap); + + saveRels(ws, xmlCache, idMap); + + logger.info("\n" + + "**************************\n" + + "Summary:\n" + + "timeExecution=\t" + (System.currentTimeMillis() - start)); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void executeDefs(String file) { + long start = System.currentTimeMillis(); + try { + EntitySAXReader reader = new EntitySAXReader(); + RecoveryCache xmlCache = reader.execute(file); + ServiceRegistry services = new ServiceRegistry(); + WrapperService ws = services.getWrapper(); + + Map<Long, Long> idMap = new HashMap<Long, Long>(); + + saveDefs(ws, xmlCache, idMap); + saveDefRels(ws, xmlCache, idMap); + + System.out.println(idMap.size()); + + logger.info("\n" + + "**************************\n" + + "Summary:\n" + + "timeExecution=\t" + (System.currentTimeMillis() - start)); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void saveDefs(WrapperService ws, RecoveryCache xmlCache, Map<Long, Long> idMap) throws Exception { + + int i = 0; + int size = xmlCache.getEnts().size(); + + for(Entity xmlEnt : xmlCache.getEnts()){ + Long oldId = xmlEnt.getId(); + xmlEnt.resetId(); + xmlEnt.resetRowId(); + + //xmlEnt = ws.saveEntity(xmlEnt, user); + ws.saveLWDefinition(xmlEnt, user); + for(Attribute att : xmlEnt.getAttributes()){ + att.loadEntValue(xmlEnt); + ws.saveDefAttribute(att, user); + } + idMap.put(oldId, xmlEnt.getId()); + if (i % 100 == 0) { + logger.info(i + " entities loaded. " + OMUtils.percentage(i, size) + + "%."); + } + i++; + } + } + + private static Map<Long, Long> saveEnts(WrapperService ws, RecoveryCache xmlCache, Map<Long, Long> idMap) throws Exception { + //ws.getPS().setImportModus(true); no alternative method to generate the ownvalue + idMap = ws.saveEntityListAsNew(new ArrayList<Entity>(xmlCache.getEnts()), user, false); + return idMap; + } + + private static void saveDefRels(WrapperService ws, RecoveryCache xmlCache, Map<Long, Long> idMap) throws Exception { + + int i = 0; + int size = xmlCache.getRels().size(); + + for(Relation xmlRel : xmlCache.getRels()){ + + xmlRel.setSourceId(idMap.get(xmlRel.getSourceId())); + xmlRel.setTargetId(idMap.get(xmlRel.getTargetId())); + + Relation rel = Relation.defRelation(xmlRel, ws); + + rel.setId(null); + rel.setRowId(null); + + + ws.saveDefRelation(rel, user); + if (i % 100 == 0) { + logger.info(i + " relations loaded. " + OMUtils.percentage(i, size) + + "%."); + } + i++; + } + } + + private static void saveRels(WrapperService ws, RecoveryCache xmlCache, Map<Long, Long> idMap) throws Exception { + + List<Node> relList = new ArrayList<Node>(); + for(Relation xmlRel : xmlCache.getRels()){ + + xmlRel.setSourceId(idMap.get(xmlRel.getSourceId())); + xmlRel.setTargetId(idMap.get(xmlRel.getTargetId())); + + Relation rel = Relation.entRelation(xmlRel, ws); + + rel.setId(null); + rel.setRowId(null); + + relList.add(rel); + } + ws.saveNodeListOnlyForScripts(relList, user); + + } + + + /* + private static int removingEntsFromDB(WrapperService ws, + RecoveryCache xmlCache) throws Exception { + logger.info("1)################# Removing ################"); + + int counter = 0; + + for (Entity def : ws.getConcepts()) { + logger.info(def.getOwnValue()); + for (Entity dbEnt : ws.getEntitiesByDef(def.getOwnValue())) { + if (!xmlCache.existEnt(dbEnt.getId())) { + logger.info("\nthis must be removed from DB=" + dbEnt); + ws.removeCurrentVersionEntity(dbEnt); + counter++; + } + } + } + return counter; + } + + private static int createNewEnts(WrapperService ws, RecoveryCache xmlCache) + throws Exception { + logger.info("2)################# Creating ################"); + int i = 0; + Collection<Entity> xmlEnts = xmlCache.getEnts(); + int size = xmlEnts.size(); + int entsCreated = 0; + + List<Entity> tmpEnts = new ArrayList<Entity>(); + + for (Entity xmlEnt : xmlEnts) { + if (!ws.existEntity(xmlEnt.getId())) { + logger.info("\nThis entity must be created " + xmlEnt); + + // xmlEnt.setSourceRelations(xmlCache.getRelBySrc(xmlEnt.getId())); + // xmlEnt.setTargetRelations(xmlCache.getRelByTar(xmlEnt.getId())); + + //ws.saveEntity(xmlEnt, user); + tmpEnts.add(xmlEnt); + entsCreated++; + } + if (i % 100 == 0) { + logger.info(i + " entities loaded. " + percentage(i, size) + + "%."); + } + i++; + } + + ws.saveEntityList(tmpEnts, user); + + return entsCreated; + } + + private static int refreshEnts(WrapperService ws, RecoveryCache xmlCache) + throws Exception { + logger.info("3)################# Refresing ################"); + int counter = 0; + + List<Entity> tmpEnts = new ArrayList<Entity>(); + + for (Entity xmlEnt : xmlCache.getEnts()) { + + if(xmlEnt.getId().equals(Long.parseLong("423011"))){ + System.out.println(); + } + if(xmlEnt.getId().equals(Long.parseLong("449780"))){ + System.out.println(); + } + + xmlEnt.setSourceRelations(xmlCache.getRelBySrc(xmlEnt.getId())); + xmlEnt.setTargetRelations(xmlCache.getRelByTar(xmlEnt.getId())); + xmlEnt.setLightweight(false); + + Entity dbEnt = ws.getEntityByIdWithContent(xmlEnt.getId()); + + if (!dbEnt.equalsContent(xmlEnt)) { + logger.info("\nRefreshing " + dbEnt); + dbEnt.refreshEnt(xmlEnt, ws); + + //ws.saveEntity(dbEnt, user); + tmpEnts.add(dbEnt); + counter++; + } + } + + ws.saveEntityList(tmpEnts, user); + + return counter; + } + */ + + public static void main(String[] args) { + + String s = args[0]; + + if(StringUtils.equals(s, "ents")){ + executeEnts("/Users/jurzua/Projects/max-planck/db_backup/xml_backup/2013.07.12-ENT.xml"); + }else if(StringUtils.equals(s, "rels")){ + executeDefs("/Users/jurzua/Projects/max-planck/db_backup/xml_backup/2013.07.12-DEF.xml"); + } + + System.exit(0); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/AbstractSearchService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,583 @@ +package org.mpi.openmind.search; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +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.search.utils.ResultEntry; +import org.mpi.openmind.search.utils.SAttribute; +import org.mpi.openmind.search.utils.SAttributeMultipleName; +import org.mpi.openmind.search.utils.SAttributeMultipleValue; +import org.mpi.openmind.search.utils.SAttributeUniqueName; +import org.mpi.openmind.search.utils.SEntity; +import org.mpi.openmind.search.utils.SRelation; +import org.mpi.openmind.search.utils.SRelationMultipleName; +import org.mpi.openmind.search.utils.SRelationUniqueName; + +public class AbstractSearchService { + + private static Logger logger = Logger.getLogger(AbstractSearchService.class); + + private WrapperService om; + + protected List<ResultEntry> search0(List<SEntity> filters, List<SRelation> relFilters) throws Exception{ + List<ResultEntry> rs = new ArrayList<ResultEntry>(); + + //TODO sort filter by amount of SAttributes (should improve the performance) + + SEntity f0 = filters.get(0); + if(StringUtils.isEmpty(f0.getOc())){ + throw new Exception("ObjectClass is required for the filters."); + } + + for(Long entId : filterEntList(f0, om.getEntitiesByDef(f0.getOc()))){ + ResultEntry re = new ResultEntry(); + re.putEnt(f0.getIndex(), entId); + rs.add(re); + } + logger.info("*** RS generated by the first filter [" + f0.getOc() + "] = " + rs.size()); + + for(int i=1; i<filters.size(); i++){ + rs = getCandidatesBySRelations(filters.get(i), rs, relFilters); + } + return rs; + } + + public List<ResultEntry> getCandidatesBySRelations( + SEntity currentEntFilter, List<ResultEntry> approvedRS, List<SRelation> allRelFilters) throws Exception{ + + List<ResultEntry> rs = new ArrayList<ResultEntry>(); + + List<SRelation> srcSRelsForCurrentIndex = getSrcRelList(allRelFilters, currentEntFilter.getIndex()); + List<SRelation> tarSRelsForCurrentIndex = getTarRelList(allRelFilters, currentEntFilter.getIndex()); + List<SRelation> sRelsForCurrentIndex = new ArrayList<SRelation>(srcSRelsForCurrentIndex); + sRelsForCurrentIndex.addAll(tarSRelsForCurrentIndex); + + + //for each entry + for(ResultEntry approvedEntry : approvedRS){ + + List<Long> candidatesForCurrentEntFilter = new ArrayList<Long>(); + Map<Integer, Long> approvedEntMap = approvedEntry.getEntMap(); + + //for each item in this entry we will generate candidates for the other indices... + for(Entry<Integer, Long> itemEntry : approvedEntMap.entrySet()){ + + Integer itemEntryIndex = itemEntry.getKey(); + Long itemEntryEntId = itemEntry.getValue(); + + //expanding by src relations + for(SRelation sRel : srcSRelsForCurrentIndex){ + + if(sRel.getTarIndex().equals(itemEntryIndex)){ + //this if means: that the source relation references the itemEntryIndex + if(sRel instanceof SRelationUniqueName){ + SRelationUniqueName f = (SRelationUniqueName)sRel; + List<Entity> list = om.getSourcesForTargetRelation(itemEntryEntId, f.getRelName(), currentEntFilter.getOc(), -1); + for(Entity candidate : list){ + if(!candidatesForCurrentEntFilter.contains(candidate.getId())){ + if(isEntInSEnt(candidate.getId(), currentEntFilter)){ + candidatesForCurrentEntFilter.add(candidate.getId()); + //caching + sRel.addRelToCache(candidate.getId(), itemEntryEntId); + } + + } + } + }else{ + SRelationMultipleName f = (SRelationMultipleName)sRel; + for(String relName : f.getRelNameList()){ + List<Entity> list = om.getSourcesForTargetRelation(itemEntryEntId, relName, currentEntFilter.getOc(), -1); + for(Entity candidate : list){ + if(!candidatesForCurrentEntFilter.contains(candidate.getId())){ + if(isEntInSEnt(candidate.getId(), currentEntFilter)){ + candidatesForCurrentEntFilter.add(candidate.getId()); + //caching + sRel.addRelToCache(candidate.getId(), itemEntryEntId); + } + } + } + } + } + } + } + + //expanding by tar relations + for(SRelation sRel : tarSRelsForCurrentIndex){ + + if(sRel.getSrcIndex().equals(itemEntryIndex)){ + //this if means: that the source relation references the itemEntryIndex + if(sRel instanceof SRelationUniqueName){ + SRelationUniqueName f = (SRelationUniqueName)sRel; + List<Entity> list = om.getTargetsForSourceRelation(itemEntryEntId, f.getRelName(), currentEntFilter.getOc(), -1); + for(Entity candidate : list){ + if(!candidatesForCurrentEntFilter.contains(candidate.getId())){ + if(isEntInSEnt(candidate.getId(), currentEntFilter)){ + candidatesForCurrentEntFilter.add(candidate.getId()); + //caching + sRel.addRelToCache(itemEntryEntId, candidate.getId()); + } + } + } + }else{ + SRelationMultipleName f = (SRelationMultipleName)sRel; + for(String relName : f.getRelNameList()){ + List<Entity> list = om.getTargetsForSourceRelation(itemEntryEntId, relName, currentEntFilter.getOc(), -1); + for(Entity candidate : list){ + if(!candidatesForCurrentEntFilter.contains(candidate.getId())){ + if(isEntInSEnt(candidate.getId(), currentEntFilter)){ + candidatesForCurrentEntFilter.add(candidate.getId()); + //caching + sRel.addRelToCache(itemEntryEntId, candidate.getId()); + } + } + } + } + } + } + } + } + + //long start = System.currentTimeMillis(); + //logger.info("Found " + candidatesForCurrentEntFilter.size() + " candidates for index=" + currentEntFilter.getIndex() + " " + currentEntFilter.getOc()); + + //at this point the candidates have been generated for this entry (we have to apply producto punto) + for(Long candidateEntId : candidatesForCurrentEntFilter){ + if(isInsertionValidForSRels(currentEntFilter.getIndex(), candidateEntId, approvedEntry, sRelsForCurrentIndex)){ + //Map<Integer, Long> entryMap = new HashMap<Integer, Long>(approvedEntry); + //entryMap.put(currentEntFilter.getIndex(), candidateEntId); + //rs.add(entryMap); + ResultEntry re = new ResultEntry(approvedEntry); + re.putEnt(currentEntFilter.getIndex(), candidateEntId); + rs.add(re); + } + } + + //logger.info("After Filer rs.size=" + rs.size() + " - time=" + (System.currentTimeMillis() - start)); + } + return rs; + } + + private boolean isEntInSEnt(Long entId, SEntity sEnt){ + if(sEnt.containsAttFilters()){ + for(SAttribute sAtt : sEnt.getAttList()){ + try { + if(sAtt instanceof SAttributeUniqueName){ + SAttributeUniqueName sAtt0 = (SAttributeUniqueName)sAtt; + Attribute att = om.getAttributeByName(entId, sAtt0.getAttName()); + if(att == null || + StringUtils.isEmpty(att.getNormalizedOwnValue()) || + !att.getNormalizedOwnValue().contains(sAtt0.getNormalizedAttValue())){ + return false; + } + }else if(sAtt instanceof SAttributeMultipleName){ + SAttributeMultipleName sAtt0 = (SAttributeMultipleName)sAtt; + boolean valid = false; + for(String attName : sAtt0.getAttNameList()){ + Attribute att = om.getAttributeByName(entId, attName); + if(att != null && + StringUtils.isNotEmpty(att.getNormalizedOwnValue()) && + att.getNormalizedOwnValue().contains(sAtt0.getNormalizedAttValue())){ + valid = true; + break; + } + } + if(!valid){ + return false; + } + }else{ + SAttributeMultipleValue sAtt0 = (SAttributeMultipleValue)sAtt; + Attribute att = om.getAttributeByName(entId, sAtt0.getAttName()); + boolean valid = false; + if(att != null){ + for(String value : sAtt0.getNormalizedAttValueList()){ + if(att.getNormalizedOwnValue().contains(value)){ + valid = true; + break; + } + } + } + if(!valid){ + return false; + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + } + return true; + } + + private boolean isInsertionValidForSRels( + Integer candidateIndex, Long candidateEntId, ResultEntry entry, List<SRelation> sRelsForCadidate){ + + for(SRelation sRel : sRelsForCadidate){ + if(sRel instanceof SRelationUniqueName){ + SRelationUniqueName f = (SRelationUniqueName)sRel; + Long srcId = (candidateIndex.equals(f.getSrcIndex())) ? candidateEntId : entry.getEntMap().get(f.getSrcIndex()); + Long tarId = (candidateIndex.equals(f.getTarIndex())) ? candidateEntId : entry.getEntMap().get(f.getTarIndex()); + + if(srcId != null && tarId != null){ + if(f.isRelCached(srcId, tarId) || om.existRelation(srcId, tarId, f.getRelName())){ + entry.putRel(f.getSrcIndex(), f.getTarIndex(), f.getRelName()); + + }else{ + return false; + } + } + }else{ + SRelationMultipleName f = (SRelationMultipleName)sRel; + Long srcId = (candidateIndex.equals(f.getSrcIndex())) ? candidateEntId : entry.getEntMap().get(f.getSrcIndex()); + Long tarId = (candidateIndex.equals(f.getTarIndex())) ? candidateEntId : entry.getEntMap().get(f.getTarIndex()); + if(srcId != null && tarId != null){ + for(String relName : f.getRelNameList()){ + if(f.isRelCached(srcId, tarId) || om.existRelation(srcId, tarId, relName)){ + entry.putRel(f.getSrcIndex(), f.getTarIndex(), relName); + break; + } + } + } + } + } + + return true; + } + + + + public static List<SRelation> getSrcRelList(List<SRelation> relFilters, Integer srcIndex){ + List<SRelation> list = new ArrayList<SRelation>(); + + for(SRelation sRel : relFilters){ + if(sRel.isSrc(srcIndex)){ + list.add(sRel); + } + } + + return list; + } + + public static List<SRelation> getTarRelList(List<SRelation> relFilters, Integer srcIndex){ + List<SRelation> list = new ArrayList<SRelation>(); + + for(SRelation sRel : relFilters){ + if(sRel.isTar(srcIndex)){ + list.add(sRel); + } + } + + return list; + } + + private List<Long> filterEntList(SEntity filter, List<Entity> entList){ + List<Long> list = new ArrayList<Long>(); + + if(filter.containsAttFilters()){ + for(Entity ent : entList){ + if(isEntInSEnt(ent.getId(), filter)){ + list.add(ent.getId()); + } + } + }else{ + for(Entity ent : entList){ + list.add(ent.getId()); + } + } + return list; + } + + /* + * protected Map<Integer, List<Long>> generateCandidates(List<SEntity> filters) throws Exception{ + Map<Integer, List<Long>> candidates = new HashMap<Integer, List<Long>>(); + + //TODO sort filter by amount of SAttributes (sh) + + for(SEntity filter : filters){ + candidates.put(filter.getIndex(), new ArrayList<Long>()); + + if(StringUtils.isEmpty(filter.getOc())){ + throw new Exception("ObjectClass is required for the filters."); + } + + + List<Entity> entList = om.getEntitiesByDef(filter.getOc()); + if(filter.containsAttFilters()){ + for(Entity ent : entList){ + boolean entIsCandidate = true; + + for(SAttribute sAtt : filter.getAttList()){ + Attribute att = om.getAttributeByName(ent.getId(), sAtt.getAttName()); + if(att == null || !att.getNormalizedOwnValue().contains(sAtt.getNormalizedAttValue())){ + entIsCandidate = false; + break; + } + } + if(entIsCandidate){ + candidates.get(filter.getIndex()).add(ent.getId()); + } + + } + }else{ + for(Entity ent : entList){ + candidates.get(filter.getIndex()).add(ent.getId()); + } + } + } + StringBuilder sb = new StringBuilder("### GenerateCandidates Summary ###"); + for(SEntity sEnt : filters){ + sb.append("\t" + sEnt.toString() + "=\t" + candidates.get(sEnt.getIndex()).size() + "\n"); + } + sb.append("\n"); + logger.info(sb.toString()); + return candidates; + } + * */ + /* + protected List<Map<Integer, Long>> filterByRelations(Map<Integer, List<Long>> candidates, List<SRelation> relFilters) throws Exception{ + + List<Map<Integer, Long>> rs = null; + + if(candidates.size() == 1){ + rs = new ArrayList<Map<Integer,Long>>(); + for(Entry<Integer, List<Long>> entry : candidates.entrySet()){ + Map<Integer, Long> map = new HashMap<Integer, Long>(); + for(Long entId : entry.getValue()){ + map.put(entry.getKey(), entId); + } + rs.add(map); + } + }else{ + //generation possible combinations + List<Integer> keys = new ArrayList<Integer>(candidates.keySet()); + if(candidates.size() == 2){ + rs = generateCombinationsFor2Indexes( + keys.get(0), candidates.get(keys.get(0)), + keys.get(1), candidates.get(keys.get(1)), relFilters); + }else if(candidates.size() == 3){ + rs = generateCombinationsFor3Indexes( + keys.get(0), candidates.get(keys.get(0)), + keys.get(1), candidates.get(keys.get(1)), + keys.get(2), candidates.get(keys.get(2)), relFilters); + }else if(candidates.size() == 4){ + rs = generateCombinationsFor4Indexes( + keys.get(0), candidates.get(keys.get(0)), + keys.get(1), candidates.get(keys.get(1)), + keys.get(2), candidates.get(keys.get(2)), + keys.get(3), candidates.get(keys.get(3)), relFilters); + }else if(candidates.size() == 5){ + rs = generateCombinationsFor5Indexes( + keys.get(0), candidates.get(keys.get(0)), + keys.get(1), candidates.get(keys.get(1)), + keys.get(2), candidates.get(keys.get(2)), + keys.get(3), candidates.get(keys.get(3)), + keys.get(4), candidates.get(keys.get(4)), relFilters); + }else if(candidates.size() == 6){ + rs = generateCombinationsFor6Indexes( + keys.get(0), candidates.get(keys.get(0)), + keys.get(1), candidates.get(keys.get(1)), + keys.get(2), candidates.get(keys.get(2)), + keys.get(3), candidates.get(keys.get(3)), + keys.get(4), candidates.get(keys.get(4)), + keys.get(5), candidates.get(keys.get(5)), relFilters); + }else{ + throw new Exception("Indexes size must not exceed 6 items."); + } + + } + + return rs; + } + + + protected List<Map<Integer, Long>> generateCombinationsFor2Indexes( + Integer index1, List<Long> candList1, + Integer index2, List<Long> candList2, List<SRelation> relFilters){ + + List<Map<Integer, Long>> rs = new ArrayList<Map<Integer,Long>>(); + for(int i1=0; i1<candList1.size(); i1++){ + logger.info("i1=" + i1); + for(int i2=0; i2<candList2.size(); i2++){ + Map<Integer, Long> resultEntry = new HashMap<Integer, Long>(); + resultEntry.put(index1, candList1.get(i1)); + resultEntry.put(index2, candList2.get(i2)); + if(isCombinationsOfCandidatesValid(resultEntry, relFilters)){ + rs.add(resultEntry); + } + } + } + return rs; + } + + protected List<Map<Integer, Long>> generateCombinationsFor3Indexes( + Integer index1, List<Long> candList1, + Integer index2, List<Long> candList2, + Integer index3, List<Long> candList3, List<SRelation> relFilters){ + + List<Map<Integer, Long>> rs = new ArrayList<Map<Integer,Long>>(); + for(int i1=0; i1<candList1.size(); i1++){ + for(int i2=0; i2<candList2.size(); i2++){ + for(int i3=0; i3<candList3.size(); i3++){ + Map<Integer, Long> resultEntry = new HashMap<Integer, Long>(); + resultEntry.put(index1, candList1.get(i1)); + resultEntry.put(index2, candList2.get(i2)); + resultEntry.put(index3, candList3.get(i3)); + if(isCombinationsOfCandidatesValid(resultEntry, relFilters)){ + rs.add(resultEntry); + } + } + } + } + return rs; + } + + protected List<Map<Integer, Long>> generateCombinationsFor4Indexes( + Integer index1, List<Long> candList1, + Integer index2, List<Long> candList2, + Integer index3, List<Long> candList3, + Integer index4, List<Long> candList4, List<SRelation> relFilters){ + + List<Map<Integer, Long>> rs = new ArrayList<Map<Integer,Long>>(); + for(int i1=0; i1<candList1.size(); i1++){ + for(int i2=0; i2<candList2.size(); i2++){ + for(int i3=0; i3<candList3.size(); i3++){ + for(int i4=0; i4<candList4.size(); i4++){ + Map<Integer, Long> resultEntry = new HashMap<Integer, Long>(); + resultEntry.put(index1, candList1.get(i1)); + resultEntry.put(index2, candList2.get(i2)); + resultEntry.put(index3, candList3.get(i3)); + resultEntry.put(index4, candList4.get(i4)); + if(isCombinationsOfCandidatesValid(resultEntry, relFilters)){ + rs.add(resultEntry); + } + } + } + } + } + return rs; + } + + protected List<Map<Integer, Long>> generateCombinationsFor5Indexes( + Integer index1, List<Long> candList1, + Integer index2, List<Long> candList2, + Integer index3, List<Long> candList3, + Integer index4, List<Long> candList4, + Integer index5, List<Long> candList5, List<SRelation> relFilters){ + + List<Map<Integer, Long>> rs = new ArrayList<Map<Integer,Long>>(); + for(int i1=0; i1<candList1.size(); i1++){ + for(int i2=0; i2<candList2.size(); i2++){ + for(int i3=0; i3<candList3.size(); i3++){ + for(int i4=0; i4<candList4.size(); i4++){ + for(int i5=0; i5<candList5.size(); i5++){ + Map<Integer, Long> resultEntry = new HashMap<Integer, Long>(); + resultEntry.put(index1, candList1.get(i1)); + resultEntry.put(index2, candList2.get(i2)); + resultEntry.put(index3, candList3.get(i3)); + resultEntry.put(index4, candList4.get(i4)); + resultEntry.put(index5, candList5.get(i5)); + if(isCombinationsOfCandidatesValid(resultEntry, relFilters)){ + rs.add(resultEntry); + } + } + } + } + } + } + return rs; + } + + protected List<Map<Integer, Long>> generateCombinationsFor6Indexes( + Integer index1, List<Long> candList1, + Integer index2, List<Long> candList2, + Integer index3, List<Long> candList3, + Integer index4, List<Long> candList4, + Integer index5, List<Long> candList5, + Integer index6, List<Long> candList6, List<SRelation> relFilters){ + + List<Map<Integer, Long>> rs = new ArrayList<Map<Integer,Long>>(); + for(int i1=0; i1<candList1.size(); i1++){ + for(int i2=0; i2<candList2.size(); i2++){ + for(int i3=0; i3<candList3.size(); i3++){ + for(int i4=0; i4<candList4.size(); i4++){ + for(int i5=0; i5<candList5.size(); i5++){ + for(int i6=0; i6<candList6.size(); i6++){ + Map<Integer, Long> resultEntry = new HashMap<Integer, Long>(); + resultEntry.put(index1, candList1.get(i1)); + resultEntry.put(index2, candList2.get(i2)); + resultEntry.put(index3, candList3.get(i3)); + resultEntry.put(index4, candList4.get(i4)); + resultEntry.put(index5, candList5.get(i5)); + resultEntry.put(index6, candList6.get(i6)); + if(isCombinationsOfCandidatesValid(resultEntry, relFilters)){ + rs.add(resultEntry); + } + } + } + } + } + } + } + return rs; + } + + protected boolean isCombinationsOfCandidatesValid(Map<Integer, Long> possibleResultEntry, List<SRelation> relFilters){ + + for(SRelation filter : relFilters){ + Long srcId = possibleResultEntry.get(filter.getSrcIndex()); + Long tarId = possibleResultEntry.get(filter.getTarIndex()); + + Boolean cachedValue = filter.isRelValid(srcId, tarId); + + if(cachedValue == null){ + Boolean isValid = null; + if(filter instanceof SRelationUniqueName){ + SRelationUniqueName f = (SRelationUniqueName)filter; + isValid = om.existRelation( + srcId, + tarId, + f.getRelName()); + }else{ + SRelationMultipleName f = (SRelationMultipleName)filter; + isValid = false; + for(String relName : f.getRelNameList()){ + if(om.existRelation( + srcId, + tarId, + relName)){ + isValid = true; + break; + } + } + } + filter.addRelToCache(srcId, tarId, isValid); + if(!isValid){ + return false; + } + }else{ + System.out.print("*"); + if(!cachedValue){ + return false; + } + } + + } + + return true; + } + */ + public WrapperService getOm() { + return om; + } + + public void setOm(WrapperService om) { + this.om = om; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/SearchService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,38 @@ +package org.mpi.openmind.search; + +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.mpi.openmind.search.utils.ResultEntry; +import org.mpi.openmind.search.utils.SEntity; +import org.mpi.openmind.search.utils.SRelation; + +public class SearchService extends AbstractSearchService{ + + private static Logger logger = Logger.getLogger(SearchService.class); + + public List<ResultEntry> search(List<SEntity> entFilters, List<SRelation> relFilters){ + List<ResultEntry> rs = null; + try { + + long start = System.currentTimeMillis(); + rs = search0(entFilters, relFilters); + long endGenerateCandidates = System.currentTimeMillis(); + logger.info("search0 time=" + (endGenerateCandidates - start)); + + /* + long endGenerateCandidates = System.currentTimeMillis(); + logger.info("generateCandidates time=" + (endGenerateCandidates - start)); + rs = filterByRelations(candidates, relFilters); + logger.info("filterByRelations time=" + (System.currentTimeMillis() - endGenerateCandidates)); + */ + + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + + return rs; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/ResultEntry.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,53 @@ +package org.mpi.openmind.search.utils; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.HashMap; +import java.util.Map; + +public class ResultEntry implements Externalizable{ + + private Map<Integer, Long> entMap; + private Map<SRelIndexKey, String> relNameMap; + + public ResultEntry(){ + this.entMap = new HashMap<Integer, Long>(); + this.relNameMap = new HashMap<SRelIndexKey, String>(); + } + + public ResultEntry(ResultEntry re){ + this.entMap = new HashMap<Integer, Long>(re.entMap); + this.relNameMap = new HashMap<SRelIndexKey, String>(re.relNameMap); + } + + public void putEnt(Integer entIndex, Long entId){ + this.entMap.put(entIndex, entId); + } + + public void putRel(Integer srcIndex, Integer tarIndex, String relName){ + this.relNameMap.put(new SRelIndexKey(srcIndex, tarIndex), relName); + } + + public String getRel(Integer srcIndex, Integer tarIndex){ + return this.relNameMap.get(new SRelIndexKey(srcIndex, tarIndex)); + } + + public Map<Integer, Long> getEntMap(){ + return this.entMap; + } + + @Override + public void writeExternal(ObjectOutput out) throws IOException { + // TODO Auto-generated method stub + + } + + @Override + public void readExternal(ObjectInput in) throws IOException, + ClassNotFoundException { + // TODO Auto-generated method stub + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SAttribute.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,6 @@ +package org.mpi.openmind.search.utils; + + +public abstract class SAttribute { + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SAttributeMultipleName.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,32 @@ +package org.mpi.openmind.search.utils; + +import java.util.Arrays; +import java.util.List; + +public class SAttributeMultipleName extends SAttribute{ + + private List<String> attNameList; + private SAttributeValue sAttValue; + + public SAttributeMultipleName(String attValue, String ... attNameList) { + this.attNameList = Arrays.asList(attNameList); + this.sAttValue = new SAttributeValue(attValue); + } + + public List<String> getAttNameList() { + return attNameList; + } + + public String getNormalizedAttValue() { + return this.sAttValue.getNormalizedAttValue(); + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + for(String s : attNameList){ + sb.append(s + " "); + } + return sb.toString() + "=" + this.sAttValue.getAttValue(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SAttributeMultipleValue.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,35 @@ +package org.mpi.openmind.search.utils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class SAttributeMultipleValue extends SAttribute { + + private List<SAttributeValue> attValueList; + private String attName; + + public SAttributeMultipleValue(String attName, String ... attValueList) { + this.attValueList = new ArrayList<SAttributeValue>(); + for(String attValue : Arrays.asList(attValueList)){ + this.attValueList.add(new SAttributeValue(attValue)); + } + this.attName = attName; + } + + private List<String> normalizedAttValueList; + public List<String> getNormalizedAttValueList() { + if(normalizedAttValueList == null){ + normalizedAttValueList = new ArrayList<String>(); + for(SAttributeValue value : this.attValueList){ + normalizedAttValueList.add(value.getNormalizedAttValue()); + } + } + return normalizedAttValueList; + } + + public String getAttName() { + return attName; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SAttributeUniqueName.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,25 @@ +package org.mpi.openmind.search.utils; + +public class SAttributeUniqueName extends SAttribute{ + + private SAttributeValue sAttValue; + private String attName; + + public SAttributeUniqueName(String attName, String attValue) { + this.attName = attName; + this.sAttValue = new SAttributeValue(attValue); + } + + public String getAttName() { + return attName; + } + + public String getNormalizedAttValue() { + return this.sAttValue.getNormalizedAttValue(); + } + + @Override + public String toString(){ + return attName + "=" + this.sAttValue.getAttValue(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SAttributeValue.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,25 @@ +package org.mpi.openmind.search.utils; + +import org.apache.commons.lang.StringUtils; +import org.mpi.openmind.repository.utils.NormalizerUtils; + +public class SAttributeValue { + + protected String attValue; + protected String normalizedAttValue; + + public SAttributeValue(String attValue){ + this.attValue = attValue; + } + + public String getAttValue() { + return attValue; + } + + public String getNormalizedAttValue() { + if(StringUtils.isNotEmpty(attValue) || StringUtils.isEmpty(normalizedAttValue)){ + normalizedAttValue = NormalizerUtils.normalize(attValue); + } + return normalizedAttValue; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SEntity.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,55 @@ +package org.mpi.openmind.search.utils; + +import java.util.ArrayList; +import java.util.List; + +public class SEntity { + + private Integer index; + private String oc; + private List<SAttribute> attList; + + public SEntity(Integer index, String oc, List<SAttribute> attList){ + this.index = index; + this.oc = oc; + this.attList = attList; + } + + public SEntity(Integer index, String oc){ + this.index = index; + this.oc = oc; + } + + public void addAtt(SAttribute att){ + if(attList == null){ + this.attList = new ArrayList<SAttribute>(); + } + this.attList.add(att); + } + + public boolean containsAttFilters(){ + if(attList == null || attList.size() == 0){ + return false; + } + return true; + } + + public Integer getIndex() { + return index; + } + + public List<SAttribute> getAttList() { + return attList; + } + public void setAttList(List<SAttribute> attList) { + this.attList = attList; + } + public String getOc() { + return oc; + } + + @Override + public String toString(){ + return index + " " + oc; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SEquivalence.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,29 @@ +package org.mpi.openmind.search.utils; + +public class SEquivalence { + + private Short srcIndex; + private Short tarIndex; + private String attName; + + public Short getSrcIndex() { + return srcIndex; + } + public void setSrcIndex(Short srcIndex) { + this.srcIndex = srcIndex; + } + public Short getTarIndex() { + return tarIndex; + } + public void setTarIndex(Short tarIndex) { + this.tarIndex = tarIndex; + } + public String getAttName() { + return attName; + } + public void setAttName(String attName) { + this.attName = attName; + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SRelIndexKey.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,50 @@ +package org.mpi.openmind.search.utils; + + +public class SRelIndexKey { + private Integer srcIndex; + private Integer tarIndex; + + public SRelIndexKey(Integer srcIndex, Integer tarIndex){ + this.srcIndex = srcIndex; + this.tarIndex = tarIndex; + } + + public Integer getSrcIndex() { + return srcIndex; + } + public void setSrcIndex(Integer srcIndex) { + this.srcIndex = srcIndex; + } + public Integer getTarIndex() { + return tarIndex; + } + public void setTarIndex(Integer tarIndex) { + this.tarIndex = tarIndex; + } + + @Override + public int hashCode() { + String s = this.srcIndex + "_" + this.tarIndex; + return s.hashCode(); + } + + @Override + public boolean equals(Object o){ + if(o instanceof SRelIndexKey){ + SRelIndexKey other = (SRelIndexKey)o; + if(srcIndex.equals(other.srcIndex) && + tarIndex.equals(other.tarIndex)){ + return true; + } + } + return false; + } + + @Override + public String toString(){ + return this.srcIndex + "-" + this.tarIndex; + } + + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SRelLongKey.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,49 @@ +package org.mpi.openmind.search.utils; + +import java.io.Serializable; + + +public class SRelLongKey implements Serializable { + private static final long serialVersionUID = -3672734005519718115L; + + private Long srcId; + private Long tarId; + + public SRelLongKey(Long srcId, Long tarId){ + this.srcId = srcId; + this.tarId = tarId; + } + + public Long getSrcId() { + return srcId; + } + public void setSrcId(Long srcId) { + this.srcId = srcId; + } + public Long getTarId() { + return tarId; + } + public void setTarId(Long tarId) { + this.tarId = tarId; + } + + @Override + public int hashCode() { + String s = this.srcId + "_" + this.tarId; + return s.hashCode(); + } + + @Override + public boolean equals(Object o){ + if(o instanceof SRelLongKey){ + SRelLongKey other = (SRelLongKey)o; + if(srcId.equals(other.srcId) && + tarId.equals(other.tarId)){ + return true; + } + } + return false; + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SRelation.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,68 @@ +package org.mpi.openmind.search.utils; + +import java.util.ArrayList; +import java.util.List; + +public abstract class SRelation { + + protected Integer srcIndex; + protected Integer tarIndex; + + private List<SRelLongKey> cachedRels; + + public void addRelToCache(Long srcId, Long tarId){ + if(cachedRels == null){ + cachedRels = new ArrayList<SRelLongKey>(); + } + SRelLongKey key = new SRelLongKey(srcId, tarId); + if(!cachedRels.contains(key)){ + cachedRels.add(key); + } + + } + + public boolean isRelCached(Long srcId, Long tarId){ + if(cachedRels == null){ + return false; + }else{ + return cachedRels.contains(new SRelLongKey(srcId, tarId)); + } + } + + public boolean isSrc(Integer srcIndex){ + return srcIndex.equals(this.srcIndex); + } + + public boolean isTar(Integer tarIndex){ + return tarIndex.equals(this.tarIndex); + } + + /* + public boolean containsii(Integer srcIndex, Integer tarIndex){ + if( + (this.srcIndex.equals(srcIndex) && + this.tarIndex.equals(tarIndex)) + // || + //(this.srcIndex.equals(tarIndex) && + // this.tarIndex.equals(srcIndex)) + // + ){ + return true; + } + return false; + } +*/ + + public Integer getSrcIndex() { + return srcIndex; + } + public void setSrcIndex(Integer srcIndex) { + this.srcIndex = srcIndex; + } + public Integer getTarIndex() { + return tarIndex; + } + public void setTarIndex(Integer tarIndex) { + this.tarIndex = tarIndex; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SRelationMultipleName.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,35 @@ +package org.mpi.openmind.search.utils; + +import java.util.Arrays; +import java.util.List; + +public class SRelationMultipleName extends SRelation { + + private List<String> relNameList; + + public SRelationMultipleName(Integer srcIndex, Integer tarIndex, String ... relNameArray){ + this.srcIndex = srcIndex; + this.tarIndex = tarIndex; + this.relNameList = Arrays.asList(relNameArray); + } + + public SRelationMultipleName(SEntity src, SEntity tar, String ... relNameArray){ + this.srcIndex = src.getIndex(); + this.tarIndex = tar.getIndex(); + this.relNameList = Arrays.asList(relNameArray); + } + + + public List<String> getRelNameList() { + return relNameList; + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + for(String s : relNameList){ + sb.append(s + " "); + } + return srcIndex + " " + sb.toString() + " " + tarIndex; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/search/utils/SRelationUniqueName.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,29 @@ +package org.mpi.openmind.search.utils; + +public class SRelationUniqueName extends SRelation{ + private String relName; + + public SRelationUniqueName(Integer srcIndex, Integer tarIndex, String relName){ + this.srcIndex = srcIndex; + this.tarIndex = tarIndex; + this.relName = relName; + } + + public SRelationUniqueName(SEntity src, SEntity tar, String relName){ + this.srcIndex = src.getIndex(); + this.tarIndex = tar.getIndex(); + this.relName = relName; + } + + public String getRelName() { + return relName; + } + public void setRelName(String relName) { + this.relName = relName; + } + + @Override + public String toString(){ + return srcIndex + " " + relName + " " + tarIndex; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/SecurityService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,746 @@ +package org.mpi.openmind.security; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; +import org.hibernate.Query; +import org.hibernate.Session; +import org.mpi.openmind.repository.bo.Entity; +import org.mpi.openmind.repository.services.PersistenceService; +import org.mpi.openmind.repository.utils.HibernateUtil; +import org.mpi.openmind.security.bo.Group; +import org.mpi.openmind.security.bo.Permission; +import org.mpi.openmind.security.bo.Role; +import org.mpi.openmind.security.bo.User; +import org.mpi.openmind.security.bo.utils.GroupRole; +import org.mpi.openmind.security.bo.utils.RolePermission; +import org.mpi.openmind.security.bo.utils.UserGroup; +import org.mpi.openmind.security.bo.utils.UserRole; + +public class SecurityService { + + public static String ROLE_ADMINISTRATOR = "Administrator"; + public static String USER_ADMIN = "admin"; + + PersistenceService persistenceService; + private static Logger logger = Logger.getLogger(SecurityService.class); + + /** + * <p>Returns true if the user has directly or indirectly the given permission.</p> + * @param user + * @param perm + * @return + */ + public Boolean hasPermission(User user, String perm){ + for(Role role : getRoles(user)){ + if(getPerms(role).contains(perm)){ + return true; + } + } + return false; + } + + /** + * <p>Returns true if the group has directly or indirectly the given permission.</p> + * @param group + * @param perm + * @return + */ + public Boolean hasPermission(Group group, String perm){ + for(Role role : getRoles(group)){ + if(getPerms(role).contains(perm)){ + return true; + } + } + return false; + } + + /** + * <p>Returns all user, which belong to the given group.</p> + * @param group + * @return + */ + public List<User> getMembers(Group group){ + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from User as user, Group as group , UserGroup as usergroup " + + "where " + + "group.groupName = :groupName AND " + + "usergroup.emailUser = user.email AND " + + "usergroup.groupName = group.groupName " + + "group by user.email"; + + Query query = session.createQuery(hql); + query.setString("groupName", group.getGroupName()); + + List<User> members = new ArrayList<User>(); + List<Object> list = query.list(); + for (Object o : list) { + Object[] array = (Object[]) o; + Object item = (Object) array[0]; + if (item instanceof User) { + members.add((User)item); + } + } + session.getTransaction().commit(); + return members; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + + } + + /** + * <p>Returns all roles associated to the given user.</p> + * @param user + * @return + */ + public List<Role> getRoles(User user){ + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Role role, User as user, UserRole as userrole " + + "where " + + "user.email = :emailUser AND " + + "userrole.emailUser = :emailUser AND " + + "userrole.roleName = role.roleName " + + "group by role.roleName"; + + Query query = session.createQuery(hql); + query.setString("emailUser", user.getEmail()); + + List<Role> roles = new ArrayList<Role>(); + List<Object> list = query.list(); + for (Object o : list) { + Object[] array = (Object[]) o; + Object item = (Object) array[0]; + if (item instanceof Role) { + roles.add((Role)item); + } + } + session.getTransaction().commit(); + return roles; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p>Returns a list of roles, whose roles have been associated to the given group.</p> + * @param group + * @return + */ + public List<Role> getRoles(Group group){ + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Role role, Group as group, GroupRole as grouprole " + + "where " + + "group.groupName = :groupName AND " + + "grouprole.groupName = :groupName AND " + + "grouprole.roleName = role.roleName " + + "group by role.roleName"; + + Query query = session.createQuery(hql); + query.setString("groupName", group.getGroupName()); + + List<Role> roles = new ArrayList<Role>(); + List<Object> list = query.list(); + for (Object o : list) { + Object[] array = (Object[]) o; + Object item = (Object) array[0]; + if (item instanceof Role) { + roles.add((Role)item); + } + } + session.getTransaction().commit(); + return roles; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p>Returns a list of permission, whose permissions have been associated to the given role.</p> + * @param role + * @return + */ + public List<Permission> getPerms(Role role){ + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Permission perm, Role as role, RolePermission as rolepermission " + + "where " + + "role.roleName = :roleName AND " + + "rolepermission.roleName = :roleName AND " + + "rolepermission.permissionName = perm.permissionName " + + "group by perm.permissionName"; + + Query query = session.createQuery(hql); + query.setString("roleName", role.getRoleName()); + + List<Permission> perms = new ArrayList<Permission>(); + List<Object> list = query.list(); + for (Object o : list) { + Object[] array = (Object[]) o; + Object item = (Object) array[0]; + if (item instanceof Permission) { + perms.add((Permission)item); + } + } + session.getTransaction().commit(); + return perms; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + + } + + /** + * <p>Returns all groups, where the given user has been inserted.</p> + * @param user + * @return + */ + public List<Group> getGroups4User(User user) { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Group as group , User as user, UserGroup as usergroup " + + "where " + + "user.email = :email AND " + + "usergroup.emailUser = :email AND " + + "usergroup.groupName = group.groupName " + + "group by group.groupName"; + + Query query = session.createQuery(hql); + query.setString("emailUser", user.getEmail()); + + List<Group> groups = new ArrayList<Group>(); + List<Object> list = query.list(); + for (Object o : list) { + Object[] array = (Object[]) o; + Object item = (Object) array[0]; + if (item instanceof Group) { + groups.add((Group)item); + } + } + session.getTransaction().commit(); + return groups; + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * Returns a list of all user existing in the system. + * @return + */ + public List<User> getAllUsers() { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from User"; + Query query = session.createQuery(hql); + List<User> list = query.list(); + session.getTransaction().commit(); + return list; + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p>Returns a list of all groups existing in the system.</p> + * @return + */ + public List<Group> getAllGroups() { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Group"; + Query query = session.createQuery(hql); + List<Group> list = query.list(); + session.getTransaction().commit(); + return list; + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p>Returns a list of all roles existing in the system.</p> + * @return + */ + public List<Role> getAllRoles() { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Role"; + Query query = session.createQuery(hql); + List<Role> list = query.list(); + session.getTransaction().commit(); + return list; + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p>Returns a list of all permissions existing in the system.</p> + * @return + */ + public List<Permission> getAllPermissions() { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Permission"; + Query query = session.createQuery(hql); + List<Permission> list = query.list(); + session.getTransaction().commit(); + return list; + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return null; + } + + /** + * <p>Returns true if there exists a user allready in the system, which contains a email like the given input <b>key</b>.</p> + * @param key: email value + * @return + */ + public Boolean existUserEmail(String email){ + if(this.getUserByEmail(email) != null){ + return true; + } + return false; + } + + /** + * <p>Returns an user by email. Email is the key of the entity user.</p> + * @param key + * @return + */ + public User getUserByEmail(String key) { + User user = null; + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from User where email = :email"; + Query query = session.createQuery(hql); + query.setString("email", key); + List<User> list = query.list(); + if (list.size() > 0) { + user = list.get(0); + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return user; + } + + /** + * <p>Saves or updates the given user.</p> + * @param user + */ + public void saveUser(User user){ + this.persistenceService.saveOrUpdateObject(user); + } + + /** + * <p>Saves or updates the given group.</p> + * @param user + */ + public void saveGroup(Group group){ + this.persistenceService.saveOrUpdateObject(group); + } + + /** + * <p>Saves or updates the given role.</p> + * @param user + */ + public void saveRole(Role role){ + this.persistenceService.saveOrUpdateObject(role); + } + + /** + * <p>Saves or updates the given permission.</p> + * @param user + */ + public void savePermission(Permission perm){ + this.persistenceService.saveOrUpdateObject(perm); + } + + /** + * <p>Deletes the given user and its association to others elements.</p> + * @param user + */ + public Boolean deleteUser(User user) { + Boolean found = false; + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from User where email = :email"; + Query query = session.createQuery(hql); + query.setString("email", user.getEmail()); + List<User> list = query.list(); + if (list.size() > 0) { + this.deleteAssocsOfUser(session, user); + session.delete(list.get(0)); + found = true; + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return found; + } + + private void deleteAssocsOfUser(Session session, User user) { + String hql1 = "from UserGroup where emailUser = :emailUser"; + Query query1 = session.createQuery(hql1); + query1.setString("emailUser", user.getEmail()); + List<Object> list1 = query1.list(); + for (Object o : list1) { + session.delete(o); + } + String hql2 = "from UserRole where emailUser = :emailUser"; + Query query2 = session.createQuery(hql2); + query2.setString("emailUser", user.getEmail()); + List<Object> list2 = query2.list(); + for (Object o : list2) { + session.delete(o); + } + } + + /** + * <p>Deletes the given group and its association to others elements.</p> + * @param user + */ + public Boolean deleteGroup(Group group) { + Boolean found = false; + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Group where groupName = :groupName"; + Query query = session.createQuery(hql); + query.setString("groupName", group.getGroupName()); + List<Group> list = query.list(); + if (list.size() > 0) { + this.deleteAssocsOfGroup(session, group); + session.delete(list.get(0)); + found = true; + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return found; + } + + + private void deleteAssocsOfGroup(Session session, Group group) { + String hql1 = "from UserGroup where groupName = :groupName"; + Query query1 = session.createQuery(hql1); + query1.setString("groupName", group.getGroupName()); + List<Object> list1 = query1.list(); + for (Object o : list1) { + session.delete(o); + } + String hql2 = "from GroupRole where groupName = :groupName"; + Query query2 = session.createQuery(hql2); + query2.setString("groupName", group.getGroupName()); + List<Object> list2 = query2.list(); + for (Object o : list2) { + session.delete(o); + } + } + + /** + * <p>Deletes the given role and its association to others elements.</p> + * @param user + */ + public Boolean deleteRole(Role role) { + Boolean found = false; + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Role where roleName = :roleName"; + Query query = session.createQuery(hql); + query.setString("groupName", role.getRoleName()); + List<Role> list = query.list(); + if (list.size() > 0) { + this.deleteAssocsOfRole(session, role); + session.delete(list.get(0)); + found = true; + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return found; + } + + + private void deleteAssocsOfRole(Session session, Role role) { + String hql1 = "from RolePermission where roleName = :roleName"; + Query query1 = session.createQuery(hql1); + query1.setString("roleName", role.getRoleName()); + List<Object> list1 = query1.list(); + for (Object o : list1) { + session.delete(o); + } + String hql2 = "from GroupRole where roleName = :roleName"; + Query query2 = session.createQuery(hql2); + query2.setString("roleName", role.getRoleName()); + List<Object> list2 = query2.list(); + for (Object o : list2) { + session.delete(o); + } + String hql3 = "from UserRole where roleName = :roleName"; + Query query3 = session.createQuery(hql3); + query3.setString("roleName", role.getRoleName()); + List<Object> list3 = query3.list(); + for (Object o : list3) { + session.delete(o); + } + } + + /** + * <p>Deletes the given permission and its association to others elements.</p> + * @param user + */ + public Boolean deletePermission(Permission perm) { + Boolean found = false; + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from Permission where permissionName = :permissionName"; + Query query = session.createQuery(hql); + query.setString("permissionName", perm.getPermissionName()); + List<Permission> list = query.list(); + if (list.size() > 0) { + this.deleteAssocsOfPermission(session, perm); + session.delete(list.get(0)); + found = true; + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return found; + } + + private void deleteAssocsOfPermission(Session session, Permission perm) { + String hql = "from RolePermission where permissionName = :permissionName"; + Query query = session.createQuery(hql); + query.setString("permissionName", perm.getPermissionName()); + List<Object> list = query.list(); + for (Object o : list) { + session.delete(o); + } + } + + public void insertUserIntoGroup(User user, Group group) { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from UserGroup where groupName = :groupName AND emailUser = :emailUser"; + Query query = session.createQuery(hql); + query.setString("groupName", group.getGroupName()); + query.setString("emailUser", user.getEmail()); + List<Object> list = query.list(); + if (list.size() == 0) { + UserGroup assoc = new UserGroup(); + assoc.setEmailUser(user.getEmail()); + assoc.setGroupName(group.getGroupName()); + session.save(assoc); + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + } + + /** + * If there exists no association between the given user and role, + * it will be made. + * @param role + * @param user + */ + public void addRoleToUser(Role role, User user) { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from UserRole where roleName = :roleName AND emailUser = :emailUser"; + Query query = session.createQuery(hql); + query.setString("roleName", role.getRoleName()); + query.setString("emailUser", user.getEmail()); + List<Object> list = query.list(); + if (list.size() == 0) { + UserRole assoc = new UserRole(); + assoc.setEmailUser(user.getEmail()); + assoc.setRoleName(role.getRoleName()); + session.save(assoc); + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + } + + /** + * If there exists no association between the given group and role, + * it will be made. + * @param role + * @param group + */ + public void addRoleToGroup(Role role, Group group) { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from GroupRole where roleName = :roleName AND groupName = :groupName"; + Query query = session.createQuery(hql); + query.setString("roleName", role.getRoleName()); + query.setString("groupName", group.getGroupName()); + List<Object> list = query.list(); + if (list.size() == 0) { + GroupRole assoc = new GroupRole(); + assoc.setGroupName(group.getGroupName()); + assoc.setRoleName(role.getRoleName()); + session.save(assoc); + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + } + + /** + * If there exists no association between the permission user and role, + * it will be made. + * @param perm + * @param role + */ + public void addPermissionToRole(Permission perm, Role role) { + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from RolePermission where roleName = :roleName AND permissionName = :permissionName"; + Query query = session.createQuery(hql); + query.setString("roleName", role.getRoleName()); + query.setString("permissionName", perm.getPermissionName()); + List<Object> list = query.list(); + if (list.size() == 0) { + RolePermission assoc = new RolePermission(); + assoc.setPermissionName(perm.getPermissionName()); + assoc.setRoleName(role.getRoleName()); + session.save(assoc); + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + } + + /** + * <p>Searchs an user by key(email) and password.</p> + * <p>The primary key of the object is not + * the email to make easily possible the change of it. + * However the email must be unique.</p> + * @param key + * @param password + * @return + */ + public User getUserByPassword(String key, String password) { + User user = null; + try { + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.getTransaction().begin(); + + String hql = "from User where email = :email AND password = :password"; + Query query = session.createQuery(hql); + query.setString("email", key); + query.setString("password", password); + List<User> list = query.list(); + if (list.size() > 0) { + user = list.get(0); + } + session.getTransaction().commit(); + + } catch (Exception e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + return user; + } + + public PersistenceService getPersistenceService() { + return persistenceService; + } + + public void setPersistenceService(PersistenceService persistenceService) { + this.persistenceService = persistenceService; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/bo/Group.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,37 @@ +package org.mpi.openmind.security.bo; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; +import javax.persistence.Id; +import javax.persistence.Transient; + +@Entity +@Table(name="sys_group") +public class Group implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @Column(name="group_name") + private String groupName; + + @Column(name="description") + private String description; + + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public String getGroupName() { + return groupName; + } + public void setGroupName(String groupName) { + this.groupName = groupName; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/bo/Permission.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,43 @@ +package org.mpi.openmind.security.bo; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="sys_permission") +public class Permission implements Serializable { + private static final long serialVersionUID = 1L; + + public static final String CREATE_CONCEPT = "create_concept"; + public static final String CREATE_ASSERTION = "create_assertion"; + public static final String EDIT_CONCEPT = "edit_concept"; + public static final String EDIT_ASSERTION = "edit_assertion"; + public static final String PUBLISH_CONCEPT = "publish_concept"; + public static final String PUBLISH_ASSERTION = "publish_assertion"; + public static final String MANAGE_SYSTEM_USERS = "manage_system_user"; + public static final String MANAGE_USER_GROUPS = "manage_user_groups"; + + @Id + @Column(name="perm_name") + private String permissionName; + + @Column(name="description") + private String description; + + public String getPermissionName() { + return permissionName; + } + public void setPermissionName(String permissionName) { + this.permissionName = permissionName; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/bo/Role.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,51 @@ +package org.mpi.openmind.security.bo; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Transient; + +@Entity +@Table(name="sys_role") +public class Role implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @Column(name="role_name") + private String roleName; + + @Column(name="description") + private String description; + + @Transient + private List<Permission> permissions = new ArrayList<Permission>(); + + public List<Permission> getPermissions() { + return permissions; + } + + public void setPermissions(List<Permission> permissions) { + this.permissions = permissions; + } + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/bo/User.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,105 @@ +package org.mpi.openmind.security.bo; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Transient; + +@Entity +@Table(name="sys_user") +public class User implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name="id") + private Long id; + + @Column(name="email") + private String email; + + @Column(name="user_name") + private String userName; + + @Column(name="first_name") + private String firstName; + + @Column(name="last_name") + private String lastName; + + @Column(name="password") + private String password; + + @Column(name="role") + private String role; + + /** + * ALTER TABLE sys_user ADD COLUMN role VARCHAR(255) AFTER user_name; + * + */ + + public boolean isPersistent(){ + if(getId() == null) + return false; + return true; + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + public String getUserName() { + return userName; + } + public void setUserName(String userName) { + this.userName = userName; + } + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString(){ + return "User " + getUserName() + " " + getEmail(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/bo/utils/GroupRole.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,78 @@ +package org.mpi.openmind.security.bo.utils; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * + * @author Jorge Urzúa + */ +@Entity +@Table(name="join_group_role") +public class GroupRole implements Serializable { + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name="GROUP_NAME") + private String groupName; + + @Column(name="ROLE_NAME") + private String RoleName; + + public String getRoleName() { + return RoleName; + } + + public void setRoleName(String RoleName) { + this.RoleName = RoleName; + } + + public String getGroupName() { + return groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof GroupRole)) { + return false; + } + GroupRole other = (GroupRole) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "org.mpi.openmind.security.GroupRole[id=" + id + "]"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/bo/utils/RolePermission.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,78 @@ +package org.mpi.openmind.security.bo.utils; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * + * @author Jorge Urzúa + */ +@Entity +@Table(name="role_permission") +public class RolePermission implements Serializable { + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name="role_name") + private String roleName; + + @Column(name="permission_name") + private String permissionName; + + public String getPermissionName() { + return permissionName; + } + + public void setPermissionName(String permissionName) { + this.permissionName = permissionName; + } + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof RolePermission)) { + return false; + } + RolePermission other = (RolePermission) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "org.mpi.openmind.security.utils.RolePermission[id=" + id + "]"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/bo/utils/UserGroup.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,78 @@ +package org.mpi.openmind.security.bo.utils; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * + * @author Jorge Urzúa + */ +@Entity +@Table(name="join_user_group") +public class UserGroup implements Serializable { + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name="EMAIL_USER") + private String emailUser; + + @Column(name="GROUP_NAME") + private String groupName; + + public String getGroupName() { + return groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + public String getEmailUser() { + return emailUser; + } + + public void setEmailUser(String emailUser) { + this.emailUser = emailUser; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof UserGroup)) { + return false; + } + UserGroup other = (UserGroup) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "org.mpi.openmind.security.UserGroup[id=" + id + "]"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/bo/utils/UserRole.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,78 @@ +package org.mpi.openmind.security.bo.utils; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * + * @author Jorge Urzúa + */ +@Entity +@Table(name="join_user_role") +public class UserRole implements Serializable { + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name="EMAIL_USER") + private String emailUser; + + @Column(name="ROLE_NAME") + private String roleName; + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + public String getEmailUser() { + return emailUser; + } + + public void setEmailUser(String emailUser) { + this.emailUser = emailUser; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof UserRole)) { + return false; + } + UserRole other = (UserRole) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "org.mpi.openmind.security.UserRole[id=" + id + "]"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/security/utils/PasswordService.java Wed Oct 29 13:28:45 2014 +0000 @@ -0,0 +1,42 @@ +package org.mpi.openmind.security.utils; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import org.apache.commons.codec.binary.Base64; + +public final class PasswordService { + + public final static String SHA = "SHA"; + public final static String UTF8 = "UTF-8"; + + public static synchronized String encrypt(String plaintext) + throws Exception { + MessageDigest md = null; + try { + md = MessageDigest.getInstance(SHA); + } catch (NoSuchAlgorithmException e) { + throw new Exception(e.getMessage()); + } + try { + md.update(plaintext.getBytes(UTF8)); + } catch (UnsupportedEncodingException e) { + throw new Exception(e.getMessage()); + } + + byte raw[] = md.digest(); + String hash = (new Base64()).encodeToString(raw); + return hash; + } + + public static void main(String[] args) { + String s = "12345"; + System.out.println(s); + try{ + System.out.println(encrypt(s)); + }catch (Exception e) { + e.printStackTrace(); + } + + } +}