Skip to content

Commit

Permalink
Add utility methods for OEnum creations from resources
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomYdn committed Mar 19, 2022
1 parent 212eec4 commit b97fac5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
26 changes: 23 additions & 3 deletions orienteer-core/src/main/java/org/orienteer/core/dao/dm/IOEnum.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -34,4 +37,21 @@ public interface IOEnum {

@Lookup("select from :targetType where alias = :alias")
public boolean lookupByAlias(String alias);

public static void upsertOEnum(Class<? extends IOEnum> daoClass, String prefix, String... values) {
upsertOEnum(daoClass, true, prefix, values);
}

public static void upsertOEnum(Class<? extends IOEnum> daoClass, boolean updateIfPresent, String prefix, String... values) {
oEnumUpsertStream(Arrays.asList(values).stream(), daoClass, updateIfPresent, prefix)
.forEach(DAO::save);
}

public static <E extends IOEnum> Stream<E> oEnumUpsertStream(Stream<String> values, Class<E> daoClass, boolean updateIfPresent, String prefix) {
return values.map(n -> new OPair<String, E>(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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -376,7 +376,7 @@ public static String decapitalize(final String s)
}

public static Map<String, String> getLocalizedStrings(String key) {
return getLocalizedStrings(key, DEFAULT_LANGUAGE_TAGS);
return getLocalizedStrings(key, defaultLanguageTags);
}

public static Map<String, String> getLocalizedStrings(String key, String... languageTags) {
Expand Down

0 comments on commit b97fac5

Please sign in to comment.