Example usage for com.liferay.portal.kernel.json JSONObject toString

List of usage examples for com.liferay.portal.kernel.json JSONObject toString

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.json JSONObject toString.

Prototype

public String toString(int indentFactor) throws JSONException;

Source Link

Usage

From source file:com.liferay.samplerestservice.rest.SampleRestService.java

License:Open Source License

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@POST//from w w  w .j  a v a  2  s .c  o  m
@Path("blogs")
@Produces(MediaType.APPLICATION_JSON)
public Response addBlogsEntry(@Context HttpServletRequest request, @Context UriInfo uriInfo,
        @FormParam("title") String title, @FormParam("content") String content) {

    ResponseBuilder responseBuilder = null;

    try {
        long companyId = PortalUtil.getCompanyId(request);

        Calendar calendar = CalendarFactoryUtil.getCalendar();

        calendar.setTime(new Date());

        long userId = UserLocalServiceUtil.getDefaultUserId(companyId);
        String description = null;
        int displayDateMonth = calendar.get(Calendar.MONTH);
        int displayDateDay = calendar.get(Calendar.DATE);
        int displayDateYear = calendar.get(Calendar.YEAR);
        int displayDateHour = calendar.get(Calendar.HOUR_OF_DAY);
        int displayDateMinute = calendar.get(Calendar.MINUTE);
        boolean allowPingbacks = false;
        boolean allowTrackbacks = false;
        String[] trackbacks = null;
        boolean smallImage = false;
        String smallImageURL = null;
        String smallImageFileName = null;
        InputStream smallImageInputStream = null;

        ServiceContext serviceContext = new ServiceContext();

        Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);

        serviceContext.setScopeGroupId(group.getGroupId());

        BlogsEntry blogsEntry = BlogsEntryLocalServiceUtil.addEntry(userId, title, description, content,
                displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
                allowPingbacks, allowTrackbacks, trackbacks, smallImage, smallImageURL, smallImageFileName,
                smallImageInputStream, serviceContext);

        UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();

        uriBuilder = uriBuilder.path("blogs");

        uriBuilder = uriBuilder.path(String.valueOf(blogsEntry.getEntryId()));

        responseBuilder = Response.created(uriBuilder.build());

        JSONObject jsonObject = getBlogsEntryJSONObject(blogsEntry);

        responseBuilder = responseBuilder.entity(jsonObject.toString(4));

        return responseBuilder.build();
    } catch (Exception e) {
        responseBuilder = Response.status(Response.Status.INTERNAL_SERVER_ERROR);

        responseBuilder.entity(JSONFactoryUtil.serializeThrowable(e));

        return responseBuilder.build();
    }
}

From source file:com.liferay.samplerestservice.rest.SampleRestService.java

License:Open Source License

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@PUT// w w w. java2s  .c  o  m
@Path("blogs/{entryId}")
@Produces(MediaType.APPLICATION_JSON)
public Response updateBlogsEntry(@Context HttpServletRequest request, @PathParam("entryId") Long entryId,
        @FormParam("title") String title, @FormParam("content") String content) {

    ResponseBuilder responseBuilder = null;

    try {
        long companyId = PortalUtil.getCompanyId(request);
        long userId = UserLocalServiceUtil.getDefaultUserId(companyId);
        Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);

        Calendar calendar = CalendarFactoryUtil.getCalendar();

        calendar.setTime(new Date());

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setScopeGroupId(group.getGroupId());

        BlogsEntry blogsEntry = BlogsEntryLocalServiceUtil.updateEntry(userId, entryId, title, null, content,
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), calendar.get(Calendar.YEAR),
                calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false, false, null, false,
                null, null, null, serviceContext);

        JSONObject jsonObject = getBlogsEntryJSONObject(blogsEntry);

        responseBuilder = Response.ok(jsonObject.toString(4), MediaType.APPLICATION_JSON);

        return responseBuilder.build();
    } catch (Exception e) {
        responseBuilder = Response.status(Response.Status.INTERNAL_SERVER_ERROR);

        responseBuilder.entity(JSONFactoryUtil.serializeThrowable(e));

        return responseBuilder.build();
    }
}