From b97fac525d223dbed748d8816e47b6deca4702bf Mon Sep 17 00:00:00 2001 From: Ilia Naryzhny Date: Fri, 18 Mar 2022 21:31:49 -0700 Subject: [PATCH] Add utility methods for OEnum creations from resources --- .../org/orienteer/core/dao/dm/IOEnum.java | 26 ++++++++++++++++--- .../org/orienteer/core/util/CommonUtils.java | 4 +-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/orienteer-core/src/main/java/org/orienteer/core/dao/dm/IOEnum.java b/orienteer-core/src/main/java/org/orienteer/core/dao/dm/IOEnum.java index 881eaab0..1cbf857e 100644 --- a/orienteer-core/src/main/java/org/orienteer/core/dao/dm/IOEnum.java +++ b/orienteer-core/src/main/java/org/orienteer/core/dao/dm/IOEnum.java @@ -1,19 +1,22 @@ package org.orienteer.core.dao.dm; +import java.util.Arrays; +import java.util.List; import java.util.Map; +import java.util.stream.Stream; import org.orienteer.core.component.visualizer.UIVisualizersRegistry; +import org.orienteer.core.dao.DAO; import org.orienteer.core.dao.ODocumentWrapperProvider; import org.orienteer.core.dao.OrienteerOProperty; -import org.orienteer.transponder.annotation.EntityIndex; -import org.orienteer.transponder.annotation.EntityProperty; +import org.orienteer.core.util.CommonUtils; import org.orienteer.transponder.annotation.EntityPropertyIndex; import org.orienteer.transponder.annotation.EntityType; import org.orienteer.transponder.annotation.Lookup; import org.orienteer.transponder.orientdb.ODriver; import com.google.inject.ProvidedBy; -import com.orientechnologies.orient.core.metadata.schema.OClass.INDEX_TYPE; +import com.orientechnologies.common.util.OPair; /** * DAO interface for classes which represents enumerations @@ -34,4 +37,21 @@ public interface IOEnum { @Lookup("select from :targetType where alias = :alias") public boolean lookupByAlias(String alias); + + public static void upsertOEnum(Class daoClass, String prefix, String... values) { + upsertOEnum(daoClass, true, prefix, values); + } + + public static void upsertOEnum(Class daoClass, boolean updateIfPresent, String prefix, String... values) { + oEnumUpsertStream(Arrays.asList(values).stream(), daoClass, updateIfPresent, prefix) + .forEach(DAO::save); + } + + public static Stream oEnumUpsertStream(Stream values, Class daoClass, boolean updateIfPresent, String prefix) { + return values.map(n -> new OPair(n, DAO.create(daoClass))) + .filter(p -> !p.getValue().lookupByAlias(p.getKey()) || updateIfPresent) + .map(p -> (E)p.getValue() + .setName(CommonUtils.getLocalizedStrings(prefix+"."+p.getKey())) + .setAlias(p.getKey())); + } } diff --git a/orienteer-core/src/main/java/org/orienteer/core/util/CommonUtils.java b/orienteer-core/src/main/java/org/orienteer/core/util/CommonUtils.java index b672fec0..0ca9436e 100644 --- a/orienteer-core/src/main/java/org/orienteer/core/util/CommonUtils.java +++ b/orienteer-core/src/main/java/org/orienteer/core/util/CommonUtils.java @@ -33,7 +33,7 @@ */ public class CommonUtils { - public static final String[] DEFAULT_LANGUAGE_TAGS = {"en", "ru", "uk"}; + public static String[] defaultLanguageTags = {"en", "ru", "uk"}; private CommonUtils() { @@ -376,7 +376,7 @@ public static String decapitalize(final String s) } public static Map getLocalizedStrings(String key) { - return getLocalizedStrings(key, DEFAULT_LANGUAGE_TAGS); + return getLocalizedStrings(key, defaultLanguageTags); } public static Map getLocalizedStrings(String key, String... languageTags) {