Mercurial > hg > openmind
view src/main/java/org/mpi/openmind/repository/utils/OMUtils.java @ 89:8adfa8679991
new implementation of translit-to-romanization rules in RomanizationLoc with test(!).
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Mon, 26 Feb 2018 14:39:49 +0100 |
parents | cb25e343e317 |
children |
line wrap: on
line source
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; } /** * Returns if the two Longs are equal or both null. * * @param one * @param two * @return */ 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); } }