Example usage for com.liferay.portal.kernel.util SortedArrayList SortedArrayList

List of usage examples for com.liferay.portal.kernel.util SortedArrayList SortedArrayList

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util SortedArrayList SortedArrayList.

Prototype

public SortedArrayList(Comparator<E> comparator) 

Source Link

Usage

From source file:com.liferay.events.global.mobile.Utils.java

License:Open Source License

public static String getJSONLikenessDescription(EventContact me, EventContact targetContact)
        throws JSONException {

    JSONArray result = JSONFactoryUtil.createJSONArray();

    Map<String, Double> desires1 = getJSONWordWeightsFromString(me.getDesires());
    Map<String, Double> desires2 = getJSONWordWeightsFromString(targetContact.getDesires());
    Map<String, Double> expertise1 = getJSONWordWeightsFromString(me.getExpertise());
    Map<String, Double> expertise2 = getJSONWordWeightsFromString(targetContact.getExpertise());

    // how many of my desires do they have expertise in?
    Set<String> common1 = new HashSet<String>(desires1.keySet());
    common1.retainAll(expertise2.keySet());

    // how many of my expertises do they desire?
    Set<String> common2 = new HashSet<String>(desires2.keySet());
    common2.retainAll(expertise1.keySet());

    if (common1.size() > 0) {
        JSONObject bit = JSONFactoryUtil.createJSONObject();

        List<String> myNeeds = new ArrayList<String>(common1);
        JSONArray args = JSONFactoryUtil.createJSONArray();
        args.put(targetContact.getGivenName());
        args.put(StringUtils.join(myNeeds.size() > 5 ? myNeeds.subList(0, 5) : myNeeds,
                " " + StringPool.SLASH + " "));

        bit.put("key", "HAS_EXPERTISE_IN_MY_AREAS");
        bit.put("args", args);
        result.put(bit);/*  ww  w.j  a  v  a2  s  . c  o m*/
    }

    if (common2.size() > 0) {
        JSONObject bit = JSONFactoryUtil.createJSONObject();
        JSONArray args = JSONFactoryUtil.createJSONArray();

        List<String> myExpertise = new ArrayList<String>(common2);
        args.put(targetContact.getGivenName());
        args.put(StringUtils.join(myExpertise.size() > 5 ? myExpertise.subList(0, 5) : myExpertise,
                " " + StringPool.SLASH + " "));

        bit.put("key", "HAS_NEEDS_IN_MY_AREAS");
        bit.put("args", args);
        result.put(bit);

    }

    double industrySimilarity = getJaroWinklerDistance(me.getIndustry(), targetContact.getIndustry());
    double jobTitleSimilarity = getJaroWinklerDistance(me.getJobTitle(), targetContact.getJobTitle());
    double locationDistance;

    if (me.getLat() == 0 || me.getLng() == 0 || targetContact.getLat() == 0 || targetContact.getLng() == 0) {
        locationDistance = 100000;
    } else {
        locationDistance = getDistanceBetween(me.getLat(), me.getLng(), targetContact.getLat(),
                targetContact.getLng());
    }

    double locationSimilarity = 1.0 - (locationDistance / 1000.0);
    if (locationSimilarity < 0)
        locationSimilarity = 0;

    if (locationSimilarity > .5 && me.getCountry().equals(targetContact.getCountry())) {

        JSONObject bit = JSONFactoryUtil.createJSONObject();
        JSONArray args = JSONFactoryUtil.createJSONArray();
        args.put(targetContact.getGivenName());
        args.put(targetContact.getCity());
        bit.put("key", "IS_NEARBY");
        bit.put("args", args);
        result.put(bit);

    } else if (me.getCountry().equals(targetContact.getCountry())) {
        JSONObject bit = JSONFactoryUtil.createJSONObject();
        JSONArray args = JSONFactoryUtil.createJSONArray();
        args.put(targetContact.getGivenName());
        bit.put("key", "LIVES_WORKS_IN_COUNTRY");
        bit.put("args", args);
        result.put(bit);

    }

    if (industrySimilarity > .7) {
        JSONObject bit = JSONFactoryUtil.createJSONObject();
        JSONArray args = JSONFactoryUtil.createJSONArray();
        args.put(targetContact.getGivenName());
        args.put(targetContact.getIndustry());
        bit.put("key", "SIMILAR_INDUSTRY");
        bit.put("args", args);
        result.put(bit);

    }
    if (jobTitleSimilarity > .7) {
        JSONObject bit = JSONFactoryUtil.createJSONObject();
        JSONArray args = JSONFactoryUtil.createJSONArray();
        args.put(targetContact.getGivenName());
        args.put(targetContact.getJobTitle());
        bit.put("key", "SIMILAR_JOB");
        bit.put("args", args);
        result.put(bit);

    }

    JSONArray words1o = JSONFactoryUtil.createJSONArray(me.getInterests());
    JSONArray words2o = JSONFactoryUtil.createJSONArray(targetContact.getInterests());

    List<String> words1 = new ArrayList<String>();
    List<String> words2 = new ArrayList<String>();
    final Map<String, Integer> count1 = new HashMap<String, Integer>();
    final Map<String, Integer> count2 = new HashMap<String, Integer>();
    final Map<String, Double> weight1 = new HashMap<String, Double>();
    final Map<String, Double> weight2 = new HashMap<String, Double>();

    for (int i = 0; i < words1o.length(); i++) {
        JSONObject o = words1o.getJSONObject(i);

        String word = o.getString("word");
        int count = o.getInt("count");
        double weight = o.getDouble("weight");

        words1.add(word);
        count1.put(word, count);
        weight1.put(word, weight);
    }

    for (int i = 0; i < words2o.length(); i++) {
        JSONObject o = words2o.getJSONObject(i);

        String word = o.getString("word");
        int count = o.getInt("count");
        double weight = o.getDouble("weight");

        words2.add(word);
        count2.put(word, count);
        weight2.put(word, weight);
    }

    Set<String> commonWords = new HashSet<String>(words1);
    commonWords.retainAll(words2);

    List<String> sortedCommon = new SortedArrayList<String>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return (int) Math.floor(
                    ((((double) count1.get(o2) * weight1.get(o2)) + ((double) count2.get(o2) * weight2.get(o2)))
                            - (((double) count1.get(o1) * weight1.get(o1))
                                    + ((double) count2.get(o1) * weight2.get(o1)))));

        }
    });

    sortedCommon.addAll(commonWords);

    if (!sortedCommon.isEmpty()) {
        JSONObject bit = JSONFactoryUtil.createJSONObject();
        JSONArray args = JSONFactoryUtil.createJSONArray();
        args.put(StringUtils.join(sortedCommon.size() > 5 ? sortedCommon.subList(0, 5) : sortedCommon, " / "));
        bit.put("key", "SIMILAR_SKILLS_INTERESTS");
        bit.put("args", args);
        result.put(bit);

    }

    if (result.length() <= 0) {
        List<String> sortedTargetWords = new SortedArrayList<String>(new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return (int) Math.floor(((weight2.get(b) * (double) count2.get(b))
                        - (weight2.get(a) * (double) count2.get(a))));
            }
        });
        sortedTargetWords.addAll(words2);

        if (!sortedTargetWords.isEmpty()) {
            JSONObject bit = JSONFactoryUtil.createJSONObject();
            JSONArray args = JSONFactoryUtil.createJSONArray();
            args.put(StringUtils.join(
                    sortedTargetWords.size() > 3 ? sortedTargetWords.subList(0, 3) : sortedTargetWords, " / "));
            bit.put("key", "MIGHT_BE_INTERESTED");
            bit.put("args", args);
            result.put(bit);

        }
    }
    return result.toString();
}

From source file:com.liferay.pluginsecuritymanager.util.PluginSecurityManagerUtil.java

License:Open Source License

public static List<JSONObject> getPACLPoliciesJSONObjects() throws Exception {

    Comparator<JSONObject> comparator = new Comparator<JSONObject>() {

        public int compare(JSONObject jsonObject1, JSONObject jsonObject2) {
            String servletContextName1 = jsonObject1.getString("servletContextName");
            String servletContextName2 = jsonObject2.getString("servletContextName");

            return servletContextName1.compareTo(servletContextName2);
        }/*from w  w  w.  j av  a 2  s.c  om*/

    };

    List<JSONObject> jsonObjects = new SortedArrayList<JSONObject>(comparator);

    Map<ClassLoader, Object> paclPolicies = _getPACLPolicies();

    for (Map.Entry<ClassLoader, Object> entry : paclPolicies.entrySet()) {
        Object value = entry.getValue();

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(JSONFactoryUtil.serialize(value));

        jsonObjects.add(jsonObject);
    }

    return jsonObjects;
}

From source file:com.liferay.pluginssecuritymanager.util.PluginsSecurityManagerUtil.java

License:Open Source License

public static List<JSONObject> getPACLPoliciesJSONObjects() throws Exception {

    Comparator<JSONObject> comparator = new Comparator<JSONObject>() {

        public int compare(JSONObject jsonObject1, JSONObject jsonObject2) {
            String servletContextName1 = jsonObject1.getString("servletContextName");
            String servletContextName2 = jsonObject2.getString("servletContextName");

            return servletContextName1.compareTo(servletContextName2);
        }/*from  w  ww. j  ava 2s. c o  m*/

    };

    List<JSONObject> jsonObjects = new SortedArrayList<JSONObject>(comparator);

    Map<ClassLoader, Object> paclPolicies = _getPACLPolicies();

    if (paclPolicies.isEmpty()) {
        paclPolicies = _cachedPaclPolicies;
    }

    for (Map.Entry<ClassLoader, Object> entry : paclPolicies.entrySet()) {
        Object value = entry.getValue();

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(JSONFactoryUtil.serialize(value));

        jsonObjects.add(jsonObject);
    }

    return jsonObjects;
}