001 package de.deepamehta.core.model; 002 003 import de.deepamehta.core.RelatedTopic; 004 import de.deepamehta.core.Topic; 005 006 import org.codehaus.jettison.json.JSONArray; 007 import org.codehaus.jettison.json.JSONObject; 008 009 import java.util.ArrayList; 010 import java.util.List; 011 import java.util.logging.Logger; 012 013 014 015 public enum IndexMode { 016 017 OFF, KEY, FULLTEXT, FULLTEXT_KEY; 018 019 private static final String INDEX_MODES_NAMESPACE = "dm4.core."; 020 021 // -------------------------------------------------------------------------------------------------- Public Methods 022 023 public static List<IndexMode> fromTopics(List<RelatedTopicModel> topics) { 024 List<IndexMode> indexModes = new ArrayList(); 025 for (TopicModel topic : topics) { 026 indexModes.add(fromUri(topic.getUri())); 027 } 028 return indexModes; 029 } 030 031 public String toUri() { 032 return INDEX_MODES_NAMESPACE + name().toLowerCase(); 033 } 034 035 // ----------------------------------------------------------------------------------------- Package Private Methods 036 037 static List<IndexMode> parse(JSONObject topicTypeModel) { 038 try { 039 List<IndexMode> indexModes = new ArrayList(); 040 JSONArray indexModeUris = topicTypeModel.optJSONArray("index_mode_uris"); 041 if (indexModeUris != null) { 042 for (int i = 0; i < indexModeUris.length(); i++) { 043 indexModes.add(fromUri(indexModeUris.getString(i))); 044 } 045 } 046 return indexModes; 047 } catch (Exception e) { 048 throw new RuntimeException("Parsing index modes failed (topicTypeModel=" + topicTypeModel + ")", e); 049 } 050 } 051 052 static void toJSON(List<IndexMode> indexModes, JSONObject o) throws Exception { 053 List indexModeUris = new ArrayList(); 054 for (IndexMode indexMode : indexModes) { 055 indexModeUris.add(indexMode.toUri()); 056 } 057 o.put("index_mode_uris", indexModeUris); 058 } 059 060 // ------------------------------------------------------------------------------------------------- Private Methods 061 062 private static IndexMode fromUri(String uri) { 063 if (!uri.startsWith(INDEX_MODES_NAMESPACE)) { 064 throw new RuntimeException("\"" + uri + "\" is not a valid index mode URI"); 065 } 066 String name = uri.substring(INDEX_MODES_NAMESPACE.length()).toUpperCase(); 067 return IndexMode.valueOf(name); 068 } 069 }