Search in sources :

Example 1 with ThriftJsonSerializer

use of org.eclipse.sw360.portal.common.ThriftJsonSerializer in project sw360portal by sw360.

the class ProjectPortletTest method testJsonOfClearing.

@Test
public void testJsonOfClearing() throws Exception {
    ReleaseClearingStateSummary releaseClearingStateSummary = new ReleaseClearingStateSummary().setNewRelease(1).setReportAvailable(5).setUnderClearing(6).setUnderClearingByProjectTeam(17).setApproved(4);
    ThriftJsonSerializer thriftJsonSerializer = new ThriftJsonSerializer();
    String json = thriftJsonSerializer.toJson(releaseClearingStateSummary);
    assertThat(json, containsString("{\"newRelease\":1,\"underClearing\":6,\"underClearingByProjectTeam\":17,\"reportAvailable\":5,\"approved\":4}"));
    ObjectMapper objectMapper = new ObjectMapper();
    @SuppressWarnings("unchecked") Map<String, Object> map = objectMapper.readValue(json, Map.class);
    assertThat(map, hasEntry("newRelease", (Object) 1));
}
Also used : ThriftJsonSerializer(org.eclipse.sw360.portal.common.ThriftJsonSerializer) Matchers.containsString(org.hamcrest.Matchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReleaseClearingStateSummary(org.eclipse.sw360.datahandler.thrift.components.ReleaseClearingStateSummary) Test(org.junit.Test)

Example 2 with ThriftJsonSerializer

use of org.eclipse.sw360.portal.common.ThriftJsonSerializer in project sw360portal by sw360.

the class ProjectPortlet method serveGetClearingStateSummaries.

private void serveGetClearingStateSummaries(ResourceRequest request, ResourceResponse response) throws IOException, PortletException {
    User user = UserCacheHolder.getUserFromRequest(request);
    List<Project> projects;
    String[] ids = request.getParameterValues(Project._Fields.ID.toString() + "[]");
    if (ids == null || ids.length == 0) {
        JSONArray jsonResponse = createJSONArray();
        writeJSON(request, response, jsonResponse);
    } else {
        try {
            ProjectService.Iface client = thriftClients.makeProjectClient();
            projects = client.getProjectsById(Arrays.asList(ids), user);
        } catch (TException e) {
            log.error("Could not fetch project summary from backend!", e);
            projects = Collections.emptyList();
        }
        projects = getWithFilledClearingStateSummaryIncludingSubprojects(projects, user);
        JSONArray jsonResponse = createJSONArray();
        ThriftJsonSerializer thriftJsonSerializer = new ThriftJsonSerializer();
        for (Project project : projects) {
            try {
                JSONObject row = createJSONObject();
                row.put("id", project.getId());
                row.put("clearing", JsonHelpers.toJson(project.getReleaseClearingStateSummary(), thriftJsonSerializer));
                ProjectClearingState clearingState = project.getClearingState();
                if (clearingState == null) {
                    row.put("clearingstate", "Unknown");
                } else {
                    row.put("clearingstate", ThriftEnumUtils.enumToString(clearingState));
                }
                jsonResponse.put(row);
            } catch (JSONException e) {
                log.error("cannot serialize json", e);
            }
        }
        writeJSON(request, response, jsonResponse);
    }
}
Also used : WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) TException(org.apache.thrift.TException) User(org.eclipse.sw360.datahandler.thrift.users.User) JSONFactoryUtil.createJSONArray(com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONArray) JSONArray(com.liferay.portal.kernel.json.JSONArray) JSONException(com.liferay.portal.kernel.json.JSONException) JSONFactoryUtil.createJSONObject(com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONObject) JSONObject(com.liferay.portal.kernel.json.JSONObject)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JSONArray (com.liferay.portal.kernel.json.JSONArray)1 JSONException (com.liferay.portal.kernel.json.JSONException)1 JSONFactoryUtil.createJSONArray (com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONArray)1 JSONFactoryUtil.createJSONObject (com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONObject)1 JSONObject (com.liferay.portal.kernel.json.JSONObject)1 TException (org.apache.thrift.TException)1 WrappedTException (org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException)1 ReleaseClearingStateSummary (org.eclipse.sw360.datahandler.thrift.components.ReleaseClearingStateSummary)1 User (org.eclipse.sw360.datahandler.thrift.users.User)1 ThriftJsonSerializer (org.eclipse.sw360.portal.common.ThriftJsonSerializer)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1