Skip to content

Instantly share code, notes, and snippets.

@ethanbustad
Last active August 29, 2015 14:07
Show Gist options
  • Save ethanbustad/4c5bea1a40e2770cc96b to your computer and use it in GitHub Desktop.
Save ethanbustad/4c5bea1a40e2770cc96b to your computer and use it in GitHub Desktop.
Matrix API stuff
<%@ 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