001    package de.deepamehta.core.util;
002    
003    import de.deepamehta.core.Identifiable;
004    import de.deepamehta.core.JSONEnabled;
005    import de.deepamehta.core.Topic;
006    import de.deepamehta.core.model.TopicModel;
007    
008    import org.codehaus.jettison.json.JSONArray;
009    import org.codehaus.jettison.json.JSONObject;
010    
011    import java.net.URL;
012    import java.util.ArrayList;
013    import java.util.Collection;
014    import java.util.HashMap;
015    import java.util.Iterator;
016    import java.util.List;
017    import java.util.Map;
018    import java.util.logging.Logger;
019    
020    
021    
022    public class DeepaMehtaUtils {
023    
024        private static Logger logger = Logger.getLogger("de.deepamehta.core.util.DeepaMehtaUtils");
025    
026        private static final String DM4_HOST_URL = System.getProperty("dm4.host.url");  // ### TODO: default value (#734)
027        static {
028            logger.info("Host setting:\ndm4.host.url=\"" + DM4_HOST_URL + "\"");
029        }
030    
031    
032    
033        // ************
034        // *** URLs ***
035        // ************
036    
037    
038    
039        /**
040         * Checks if an URL refers to this DeepaMehta installation.
041         * The check relies on the "dm4.host.url" system property.
042         */
043        public static boolean isDeepaMehtaURL(URL url) {
044            try {
045                return url.toString().startsWith(DM4_HOST_URL);
046            } catch (Exception e) {
047                throw new RuntimeException("Checking for DeepaMehta URL failed (url=\"" + url + "\")", e);
048            }
049        }
050    
051    
052    
053        // *******************
054        // *** Collections ***
055        // *******************
056    
057    
058    
059        public static List<Long> idList(Iterable<? extends Identifiable> items) {
060            List<Long> ids = new ArrayList();
061            for (Identifiable item : items) {
062                ids.add(item.getId());
063            }
064            return ids;
065        }
066    
067        public static List<TopicModel> toTopicModels(Iterable<? extends Topic> topics) {
068            List<TopicModel> topicModels = new ArrayList();
069            for (Topic topic : topics) {
070                topicModels.add(topic.getModel());
071            }
072            return topicModels;
073        }
074    
075        public static String topicNames(Iterable<? extends Topic> topics) {
076            StringBuilder names = new StringBuilder();
077            Iterator<? extends Topic> i = topics.iterator();
078            while (i.hasNext()) {
079                Topic topic = i.next();
080                names.append('"').append(topic.getSimpleValue()).append('"');
081                if (i.hasNext()) {
082                    names.append(", ");
083                }
084            }
085            return names.toString();
086        }
087    
088    
089    
090        // ************
091        // *** JSON ***
092        // ************
093    
094    
095    
096        // === Generic ===
097    
098        public static Map toMap(JSONObject o) {
099            return toMap(o, new HashMap());
100        }
101    
102        public static Map toMap(JSONObject o, Map map) {
103            try {
104                Iterator<String> i = o.keys();
105                while (i.hasNext()) {
106                    String key = i.next();
107                    map.put(key, o.get(key));   // throws JSONException
108                }
109                return map;
110            } catch (Exception e) {
111                throw new RuntimeException("Converting JSONObject to Map failed", e);
112            }
113        }
114    
115        // ---
116    
117        public static List toList(JSONArray o) {
118            try {
119                List list = new ArrayList();
120                for (int i = 0; i < o.length(); i++) {
121                    list.add(o.get(i));         // throws JSONException
122                }
123                return list;
124            } catch (Exception e) {
125                throw new RuntimeException("Converting JSONArray to List failed", e);
126            }
127        }
128    
129        // ---
130    
131        // ### TODO: drop this method? There is a JSONArray(Collection) constructor.
132        public static JSONArray stringsToJson(Collection<String> strings) {
133            JSONArray array = new JSONArray();
134            for (String string : strings) {
135                array.put(string);
136            }
137            return array;
138        }
139    
140        // === DeepaMehta specific ===
141    
142        // ### TODO: rename to toJSONArray()
143        public static JSONArray objectsToJSON(Iterable<? extends JSONEnabled> items) {
144            JSONArray array = new JSONArray();
145            for (JSONEnabled item : items) {
146                array.put(item.toJSON());
147            }
148            return array;
149        }
150    }