Example usage for com.mongodb DBObject put

List of usage examples for com.mongodb DBObject put

Introduction

In this page you can find the example usage for com.mongodb DBObject put.

Prototype

Object put(String key, Object v);

Source Link

Document

Sets a name/value pair in this object.

Usage

From source file:com.mobileman.kuravis.core.domain.util.EntityUtils.java

License:Apache License

/**
 * @param entity/*  w  w  w  . j a  v a  2s. c  o  m*/
 * @return entity
 */
public static DBObject setBasePropertiesOnCreate(DBObject entity) {
    entity.put(CREATED_ON, new Date());
    entity.put(MODIFIED_ON, entity.get(CREATED_ON));
    if (!entity.containsField(ID)) {
        entity.put(ID, newId());
    }

    return entity;
}

From source file:com.mobileman.kuravis.core.domain.util.EntityUtils.java

License:Apache License

public static void copyAttribute(String propertyName, DBObject source, DBObject target) {
    if (source.containsField(propertyName)) {
        target.put(propertyName, source.get(propertyName));
    }/*from  w ww. j av  a 2 s  . c  o  m*/
}

From source file:com.mobileman.kuravis.core.domain.util.EntityUtils.java

License:Apache License

/**
 * @param replaceProperty TreatmentReview property name to be replaced
 * @param replaceObj property to be replaced
 * @param oldReview//from  ww w  . j a  va 2  s  .  c  o m
 * @param newReview
 */
public static void copyTreatmentReview(String replaceProperty, DBObject replaceObj, DBObject oldReview,
        DBObject newReview) {
    switch (replaceProperty) {
    case TreatmentReview.DISEASE:
        newReview.put(TreatmentReview.DISEASE, replaceObj);
        EntityUtils.copyAttribute(TreatmentReview.TREATMENT, oldReview, newReview);
        break;
    case TreatmentReview.TREATMENT:
        newReview.put(TreatmentReview.TREATMENT, replaceObj);
        EntityUtils.copyAttribute(TreatmentReview.DISEASE, oldReview, newReview);
        break;
    default:
        break;
    }
    EntityUtils.copyAttribute(TreatmentReview.TEXT, oldReview, newReview);
    EntityUtils.copyAttribute(CREATED_ON, oldReview, newReview);
    EntityUtils.copyAttribute(TreatmentReview.RATING, oldReview, newReview);
    EntityUtils.copyAttribute(TreatmentReview.SIDE_EFFECTS, oldReview, newReview);
    EntityUtils.copyAttribute(TreatmentReview.AUTHOR, oldReview, newReview);
    EntityUtils.copyAttribute(TreatmentReview.VOTES_COUNT, oldReview, newReview);
    EntityUtils.copyAttribute(TreatmentReview.LAST_VOTED, oldReview, newReview);
    EntityUtils.copyAttribute(TreatmentReview.COMMENTS_COUNT, oldReview, newReview);
    EntityUtils.copyAttribute(TreatmentReview.LAST_COMMENTED, oldReview, newReview);
    EntityUtils.copyAttribute(TreatmentReview.RECENT_EVENT, oldReview, newReview);
}

From source file:com.mobileman.kuravis.core.domain.util.TreatmentReviewSummaryUtil.java

License:Apache License

/**
 * @param treatmentCosts// w  ww .  java 2s  .com
 * @return cost statictics
 */
public static List<DBObject> computeCostStatistics(List<TreatmentCost> treatmentCosts) {
    List<DBObject> statisticsData = new ArrayList<DBObject>(5);

    for (int i = 0; i < TreatmentCostStatistics.getCategoriesBounds().size(); i++) {
        DBObject stat = new BasicDBObject();
        stat.put(TreatmentReviewSummary.CATEGORY, Integer.valueOf(i));
        stat.put(TreatmentReviewSummary.NAME, "");
        stat.put(TreatmentReviewSummary.COUNT, 0);
        statisticsData.add(stat);
    }

    return computeCostStatistics(statisticsData, treatmentCosts);
}

From source file:com.mobileman.kuravis.core.domain.util.TreatmentReviewSummaryUtil.java

License:Apache License

/**
 * @param statisticsData /*  w ww. ja  v a  2  s .  com*/
 * @param treatmentCosts
 * @return cost statictics
 */
public static List<DBObject> computeCostStatistics(List<DBObject> statisticsData,
        List<TreatmentCost> treatmentCosts) {
    Map<Integer, DBObject> statsByGroup = new HashMap<Integer, DBObject>();
    for (DBObject statData : statisticsData) {
        statsByGroup.put((Integer) statData.get(TreatmentReviewSummary.CATEGORY), statData);
    }

    if (treatmentCosts == null) {
        return statisticsData;
    }

    for (TreatmentCost cost : treatmentCosts) {
        int category = TreatmentCostStatistics.computeCategoryId(cost.costOfMedication());
        DBObject statData = statsByGroup.get(Integer.valueOf(category));
        if (statData != null) {
            Integer count = (Integer) statData.get(TreatmentReviewSummary.COUNT);
            if (count == null) {
                count = Integer.valueOf(1);
            } else {
                count = count.intValue() + 1;
            }

            statData.put(TreatmentReviewSummary.COUNT, count);
        }
    }

    return statisticsData;
}

From source file:com.mobileman.kuravis.core.domain.util.TreatmentReviewSummaryUtil.java

License:Apache License

/**
 * @param treatmentEvents/*from   w  w w .  j a  va2 s  .  c  o  m*/
 * @return treatment duration statictics
 */
public static List<DBObject> computeTreatmentDurationStatistics(List<TreatmentEvent> treatmentEvents) {
    List<DBObject> statisticsData = new ArrayList<DBObject>(5);
    Map<Integer, DBObject> statsByGroup = new HashMap<Integer, DBObject>();
    for (int i = 0; i < TreatmentDurationStatistics.getCategoriesBounds().size(); i++) {
        DBObject stat = new BasicDBObject();
        stat.put(TreatmentReviewSummary.CATEGORY, Integer.valueOf(i));
        stat.put(TreatmentReviewSummary.NAME, "");
        stat.put(TreatmentReviewSummary.COUNT, 0);
        statisticsData.add(stat);
        statsByGroup.put((Integer) stat.get(TreatmentReviewSummary.CATEGORY), stat);
    }

    if (treatmentEvents == null || treatmentEvents.isEmpty()) {
        return statisticsData;
    }

    for (TreatmentEvent event : treatmentEvents) {
        int category = TreatmentDurationStatistics.computeCategoryId(event);
        DBObject statData = statsByGroup.get(Integer.valueOf(category));
        if (statData != null) {
            Integer count = (Integer) statData.get(TreatmentReviewSummary.COUNT);
            if (count == null) {
                count = Integer.valueOf(1);
            } else {
                count = count.intValue() + 1;
            }

            statData.put(TreatmentReviewSummary.COUNT, count);
        }
    }

    return statisticsData;
}

From source file:com.mobileman.kuravis.core.domain.util.TreatmentReviewSummaryUtil.java

License:Apache License

/**
 * @param trSideEffects/* www.  j  av  a2 s  .  c  om*/
 * @param trsSideEffects
 * @return summary side efects
 */
@SuppressWarnings("unchecked")
public static Map<String, DBObject> createTreatmentReviewSummarySideEffects(List<DBObject> trSideEffects,
        Map<String, DBObject> trsSideEffects) {
    if (trsSideEffects == null) {
        trsSideEffects = new HashMap<>();
    }

    if (trSideEffects.isEmpty()) {
        DBObject trSideEffect = new BasicDBObject("severity", new Double(0));
        Map<String, Object> sideEffect = new HashMap<String, Object>();
        sideEffect.put(EntityUtils.NAME, TreatmentSideEffect.NO_SIDE_EFFECT_NAME);
        sideEffect.put("noSideEffect", Boolean.TRUE);
        trSideEffect.put("sideEffect", sideEffect);
        trSideEffects.add(trSideEffect);
    }

    for (DBObject trSideEffect : trSideEffects) {
        Number severity = (Number) trSideEffect.get("severity");
        Map<String, Object> sideEffect = DBObject.class.isInstance(trSideEffect.get("sideEffect"))
                ? DBObject.class.cast(trSideEffect.get("sideEffect")).toMap()
                : (Map<String, Object>) trSideEffect.get("sideEffect");
        if (sideEffect.get(EntityUtils.NAME) != null && severity != null) {
            String sideEffectName = (String) sideEffect.get(EntityUtils.NAME);
            Boolean noSideEffect = (Boolean) sideEffect.get("noSideEffect");
            if (!trsSideEffects.containsKey(sideEffectName)) {
                // any group exists, so create complete new structure
                trsSideEffects.put(sideEffectName, new BasicDBObject(EntityUtils.NAME, sideEffectName)
                        .append("noSideEffect", noSideEffect).append("counts", new ArrayList<>(Arrays
                                .asList(new BasicDBObject(EntityUtils.NAME, severity).append("count", 1)))));
            } else {
                // some count group already exists, try to find appropriate group by severity value and increment count or create new group otherwise
                List<DBObject> counts = (List<DBObject>) DBObject.class.cast(trsSideEffects.get(sideEffectName))
                        .get("counts");
                boolean updated = false;
                for (DBObject count : counts) {
                    Number value = (Number) count.get(EntityUtils.NAME);
                    if (value != null) {
                        if (value.equals(severity)) {
                            int newCount = Number.class.cast(count.get("count")).intValue() + 1;
                            count.put("count", newCount);
                            updated = true;
                            break;
                        }
                    }
                }

                if (updated == false) {
                    counts.add(new BasicDBObject(EntityUtils.NAME, severity).append("count", 1));
                }
            }
        }
    }

    return trsSideEffects;
}

From source file:com.mobileman.kuravis.core.domain.util.UserUtils.java

License:Apache License

/**
 * @param source/*  www . j  av  a2 s.  c  o  m*/
 * @param target
 */
public static void copyUser(DBObject source, DBObject target) {
    for (String property : new String[] { "email", "name", "gender", "yearOfBirth", "state", "settings",
            EntityUtils.ID }) {
        if (source.containsField(property)) {
            if (Map.class.isInstance(source.get(property))) {
                target.put(property, new BasicDBObject(Map.class.cast(source.get(property))));
            } else {
                target.put(property, source.get(property));
            }

        }
    }
}

From source file:com.mobileman.kuravis.core.services.disease.impl.DiseaseServiceImpl.java

License:Apache License

/** 
 * {@inheritDoc}/*from  w  w  w .j  a  v a 2s  . co m*/
 * @see com.mobileman.kuravis.core.services.disease.DiseaseService#findAllDiseasesWithTreatmentsForSuggestionAdmin(org.springframework.data.domain.Pageable)
 */
@Override
public List<DBObject> findAllDiseasesWithTreatmentsForSuggestionAdmin(Pageable page) {

    List<DBObject> diseases = findAll(page, Disease.NAME + "," + Disease.ID);
    List<String> diseaseIds = new ArrayList<String>(diseases.size());
    Map<String, DBObject> diseaseId2Disease = new HashMap<String, DBObject>();
    for (DBObject disease : diseases) {
        String diseaseId = EntityUtils.getEntityId(disease);
        diseaseIds.add(diseaseId);
        diseaseId2Disease.put(diseaseId, disease);
        disease.put("summariesCount", new Integer(0));
    }

    Query query = Query.query(
            Criteria.where(TreatmentReviewSummary.DISEASE + "." + TreatmentReviewSummary.ID).in(diseaseIds));
    List<DBObject> summaries = findAllByQuery(TreatmentReviewSummary.ENTITY_NAME, query.getQueryObject(),
            new BasicDBObject(TreatmentReviewSummary.DISEASE, 1).append(TreatmentReviewSummary.TREATMENT, 1)
                    .append(TreatmentReviewSummary.SUGGESTION, 1));
    Map<String, List<DBObject>> disease2Tretaments = new HashMap<String, List<DBObject>>();
    for (DBObject summary : summaries) {

        DBObject disease = (DBObject) summary.get(TreatmentReviewSummary.DISEASE);
        DBObject treatment = (DBObject) summary.get(TreatmentReviewSummary.TREATMENT);
        Boolean suggestion = (Boolean) summary.get(TreatmentReviewSummary.SUGGESTION);
        String summaryId = (String) summary.get(TreatmentReviewSummary.ID);
        String diseaseId = EntityUtils.getEntityId(disease);
        if (suggestion == null || Boolean.FALSE.equals(suggestion)) {
            // it is not a suggestion
            Integer summariesCount = (Integer) diseaseId2Disease.get(diseaseId).get("summariesCount");
            if (summariesCount == null) {
                summariesCount = Integer.valueOf(1);
            } else {
                summariesCount = Integer.valueOf(summariesCount.intValue() + 1);
            }

            diseaseId2Disease.get(diseaseId).put("summariesCount", summariesCount);

        } else {
            // it is a suggestion - get suggested treatments
            List<DBObject> treatments = disease2Tretaments.get(EntityUtils.getEntityId(disease));
            if (treatments == null) {
                treatments = new ArrayList<DBObject>();
                disease2Tretaments.put(EntityUtils.getEntityId(disease), treatments);
            }

            treatment.put("treatmentReviewSummaryId", summaryId);
            treatments.add(treatment);
        }
    }

    for (DBObject disease : diseases) {
        disease.put("suggestedTreatments", disease2Tretaments.get(EntityUtils.getEntityId(disease)));
    }

    return diseases;
}