public Prediction createPrediction(SecurityContext sec, Prediction pred) { User user = securityCheck(sec, Roles.CREATE_PREDICTION); pred.setTags(TextUtils.tag(pred.getTags())); int id; pred = InputSanitizer.sanitize(pred); if (StringUtils.isBlank(pred.getSourceAuthor())) { pred.setType(Prediction.PredictionType.quote.name()); } else { pred.setType(Prediction.PredictionType.prediction.name()); } if (pred.getTitle() == null || pred.getTitle().trim().isEmpty()) { pred.setTitle(StringUtils.abbreviate(pred.getText(), 64)); } else { pred.setTitle(StringUtils.abbreviate(pred.getTitle(), 64)); } pred.setText(StringUtils.abbreviate(pred.getText(), MAX_PREDICTION_LENGTH)); if (sec != getAdminSecurityContext()) { pred.setCreatedByUserId(user.getId()); pred.setCreatedByUser(user.getFullName()); } pred.setTime(TextUtils.getProbablePredictionTime(pred.getText())); id = db.createPrediction(pred); pred.setId(id); return db.getPrediction(id); }
public CountedContainer<Prediction> search(SecurityContext sec, String query, int resultSize) { securityCheck(sec, Roles.SEARCH); SearchResults results = searchService.search(query, resultSize); if (results.predictions.isEmpty()) { return CountedContainer.wrap(Collections.emptyList(), results.hits, resultSize); } List<Prediction> list = InputSanitizer.desanitize(db.getPredictions(results.predictions)); return CountedContainer.wrap(list, results.hits, resultSize); }
public int createComment(SecurityContext sec, int predictionId, Comment comment) { User user = securityCheck(sec, Roles.COMMENT); Prediction prediction = getPrediction(predictionId); comment = InputSanitizer.sanitize(comment); comment.setCreatedByUserId(user.getId()); if (StringUtils.isBlank(comment.getAuthor())) { comment.setAuthor(user.getEmail()); } int cid = db.createComment(prediction.getId(), comment); comment.setId(cid); comment.setPredictionId(predictionId); return cid; }
public Prediction getPrediction(int id) { return InputSanitizer.desanitize(db.getPrediction(id)); }
public List<Prediction> getPredictions(SecurityContext sec, int page, int pageSize) { boolean includedApproved = isAdmin(sec); return InputSanitizer.desanitize(db.getPredictions(includedApproved, page, pageSize)); }