Last active
August 29, 2015 14:07
-
-
Save ethanbustad/4c5bea1a40e2770cc96b to your computer and use it in GitHub Desktop.
Matrix API stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page import="com.liferay.portal.kernel.json.JSONFactoryUtil" %> | |
<%@ page import="com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil" %> | |
<%@ page import="com.liferay.portal.kernel.util.BigDecimalUtil" %> | |
<%@ page import="java.math.RoundingMode" %> | |
<%@ page import="java.util.ArrayList" %> | |
<%@ page import="java.util.List" %> | |
<%@ page import="java.util.Map" %> | |
<%! | |
public static class MatrixUtil { | |
public static double getCompositeVisibilityValue(String jsonString) { | |
List<Map<String, Number>> roleMaps = null; | |
try { | |
roleMaps = JSONFactoryUtil.looseDeserialize(jsonString, ArrayList.class); | |
} | |
catch (Exception e) { | |
throw new RuntimeException("Bad json string. Must be an array.", e); | |
} | |
return getCompositeVisibilityValue(roleMaps); | |
} | |
public static double getCompositeVisibilityValue(List<Map<String, Number>> roleMaps) { | |
double userWeightedDNO = 0; | |
for (Map<String, Number> roleMap : roleMaps) { | |
double roleUserWeightedDNO = BigDecimalUtil.multiply(roleMap.get("usersInRole"), roleMap.get("roleDNO")); | |
userWeightedDNO = BigDecimalUtil.add(userWeightedDNO, roleUserWeightedDNO); | |
} | |
double numerator = 0; | |
for (Map<String, Number> roleMap : roleMaps) { | |
double roleValue = BigDecimalUtil.multiply(roleMap.get("roleWeight"), userWeightedDNO); | |
numerator = BigDecimalUtil.add(numerator, roleValue); | |
} | |
double totalUsers = sum(roleMaps, "usersInRole"); | |
double roleWeightSum = sum(roleMaps, "roleWeight"); | |
double denominator = BigDecimalUtil.multiply(totalUsers, roleWeightSum); | |
return BigDecimalUtil.divide(numerator, denominator, 6, RoundingMode.HALF_UP); | |
} | |
protected static double sum(List<Map<String, Number>> maps, String field) { | |
double sum = 0; | |
for (Map<String, Number> map : maps) { | |
sum = BigDecimalUtil.add(sum, map.get(field)); | |
} | |
return sum; | |
} | |
} | |
%> | |
<% | |
JSONWebServiceActionsManagerUtil.registerJSONWebServiceAction("/matrix", MatrixUtil.class, MatrixUtil.class.getDeclaredMethod("getCompositeVisibilityValue", String.class), "/util/getCompositeVisibilityValue", "GET"); | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment