/** * Accept a POST request. * * @param entity the representation of a new entry. * @throws ResourceException thrown when unable to accept representation. */ @Override public void acceptRepresentation(final Representation entity) throws ResourceException { String json; try { json = entity.getText(); log.debug("RecommendationsCollectionResource POST" + json); JSONObject jsonReco = JSONObject.fromObject(json); JSONObject jsonAuthor = jsonReco.getJSONObject(AUTHOR_KEY); JSONObject jsonSubject = jsonReco.getJSONObject(SUBJECT_KEY); Recommendation recommendation = new Recommendation( jsonSubject.getString(ID_KEY), jsonAuthor.getString(ID_KEY), jsonReco.getString(TEXT_KEY)); getRecommendationMapper().insert(recommendation); Map<String, Person> people = getPeopleInfoForRecommendations(recommendation); JSONObject recoJSON = convertRecoToJSON( recommendation, people.get(recommendation.getAuthorOpenSocialId()), people.get(recommendation.getSubjectOpenSocialId())); getAdaptedResponse().setEntity(recoJSON.toString(), MediaType.APPLICATION_JSON); } catch (IOException e) { log.error("POST to RecommendationsCollection failed", e); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST); } log.debug("RecommendationsCollectionResource POST " + entity.toString()); }
/** * Responds to HTTP Request. * * @param variant determines response. * @throws ResourceException thrown when unable to represent. * @return the representation. */ @Override public Representation represent(final Variant variant) throws ResourceException { log.debug("Getting recommendations for user with OS ID: " + openSocialId); List<Recommendation> recommendations; if (maxResults > 0) { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId, maxResults); } else { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId); } log.debug(recommendations.size() + " recommendations found"); JSONObject json = new JSONObject(); JSONArray recos = new JSONArray(); if (null != recommendations && recommendations.size() > 0) { Map<String, Person> people = getPeopleInfoForRecommendations(recommendations); log.debug("There are " + people.size() + " Person objects available for recommendations."); for (Recommendation reco : recommendations) { recos.add( convertRecoToJSON( reco, people.get(reco.getAuthorOpenSocialId()), people.get(reco.getSubjectOpenSocialId()))); } } json.put(RECOMMENDATIONS_KEY, recos); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }