Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

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

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:com.epam.ta.reportportal.database.OverallStatisticsDocumentHandler.java

License:Open Source License

@SuppressWarnings("unchecked")
private String getValue(DBObject dbObject, String field) {
    String[] innerKeys = field.split("\\.");
    DBObject innerObject = dbObject;
    int currentIndex = 0;
    int itterationCount = innerKeys.length - 1;
    while (currentIndex < itterationCount) {
        innerObject = (DBObject) innerObject.get(innerKeys[currentIndex]);
        if (innerObject == null) {
            break;
        }/*from  w  w w .j  a v  a  2  s.co  m*/
        currentIndex++;
    }

    if ((innerObject != null) && (innerObject.get(innerKeys[currentIndex]) instanceof Date)) {
        return String.valueOf(((Date) innerObject.get(innerKeys[currentIndex])).getTime());
    }

    // TODO Should be refactored after new UI implementation for issue
    // statistics!
    if (innerObject == null)
        return null;
    else {
        if (innerObject.get(innerKeys[currentIndex]) instanceof Integer) {
            return String.valueOf(innerObject.get(innerKeys[currentIndex]));
        } else {
            Map<String, Integer> seria = (Map<String, Integer>) innerObject.get(innerKeys[currentIndex]);
            String key = innerKeys.length == 4 ? innerKeys[3] : IssueCounter.GROUP_TOTAL;
            return seria != null && seria.keySet().size() > 0 ? String.valueOf(seria.get(key)) : "0";
        }
    }
}

From source file:com.epam.ta.reportportal.database.StatisticsDocumentHandler.java

License:Open Source License

@Override
public void processDocument(DBObject dbObject) throws MongoException, DataAccessException {
    if (fieldsForChart == null) {
        return;/*from  w  w w  .  j ava  2  s. com*/
    }

    ChartObject chartObject = new ChartObject();
    if (fieldXAxis.contains(NAME))
        chartObject.setName(getValue(dbObject, fieldXAxis.get(fieldXAxis.indexOf(NAME))));
    if (fieldXAxis.contains(NUMBER))
        chartObject.setNumber(getValue(dbObject, fieldXAxis.get(fieldXAxis.indexOf(NUMBER))));
    if (fieldXAxis.contains(START_TIME))
        chartObject.setStartTime(getValue(dbObject, fieldXAxis.get(fieldXAxis.indexOf(START_TIME))));
    chartObject.setId(dbObject.get("_id").toString());

    Map<String, String> values = new HashMap<>();
    fieldsForChart.stream().forEach(field -> values.put(field, getValue(dbObject, field)));
    chartObject.setValues(values);
    result.add(chartObject);
}

From source file:com.epam.ta.reportportal.database.StatisticsDocumentHandler.java

License:Open Source License

/**
 * Iterate thru inner DBObjects and return field value, throw
 * ReportPortalException if value isn't integer.
 * //www  .j  av a 2s . com
 * @param dbObject
 * @param field
 * @return String
 */
@SuppressWarnings("unchecked")
private String getValue(DBObject dbObject, String field) {
    String[] innerKeys = field.split("\\.");
    DBObject innerObject = dbObject;
    int currentIndex = 0;
    int itterationCount = innerKeys.length - 1;
    while (currentIndex < itterationCount) {
        innerObject = (DBObject) innerObject.get(innerKeys[currentIndex]);
        if (innerObject == null) {
            break;
        }
        currentIndex++;
    }
    if ((innerObject != null) && (innerObject.get(innerKeys[currentIndex]) instanceof Date)) {
        return String.valueOf(((Date) innerObject.get(innerKeys[currentIndex])).getTime());
    }

    // TODO Should be refactored after new UI implementation for issue
    // statistics!
    if (innerObject == null)
        return null;
    else {
        if ((innerObject.get(innerKeys[currentIndex]) instanceof Integer)
                || (innerObject.get(innerKeys[currentIndex]) instanceof String)
                || (innerObject.get(innerKeys[currentIndex]) instanceof Long)) {
            return String.valueOf(innerObject.get(innerKeys[currentIndex]));
        } else {
            Map<String, Integer> seria = (Map<String, Integer>) innerObject.get(innerKeys[currentIndex]);
            String key = innerKeys.length == 4 ? innerKeys[3] : IssueCounter.GROUP_TOTAL;
            return seria != null && seria.keySet().size() > 0 ? String.valueOf(seria.get(key)) : "0";
        }
    }
}

From source file:com.epam.ta.reportportal.database.UniqueBugDocumentHandler.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void processDocument(DBObject dbObject) throws MongoException, DataAccessException {
    if (dbObject.containsField(ISSUE)
            && ((DBObject) dbObject.get(ISSUE)).containsField(EXTERNAL_SYSTEM_ISSUES)) {
        List<DBObject> innerDbObjects = (List<DBObject>) ((DBObject) dbObject.get(ISSUE))
                .get(EXTERNAL_SYSTEM_ISSUES);
        for (DBObject innerDbObject : innerDbObjects) {
            ChartObject axisObject = new ChartObject();
            Map<String, String> objectValues = new HashMap<>();
            String ticketId;/*w  w  w .j a v  a  2  s  . c  om*/
            if (dbObject.containsField(LAUNCH_REF)) {
                objectValues.put(LAUNCH_REF, dbObject.get(LAUNCH_REF).toString());
                axisObject.setValues(objectValues);
            }
            if (dbObject.containsField(ID)) {
                /* ID of test-item containing external system issue */
                axisObject.setId(dbObject.get(ID).toString());
            }
            if (innerDbObject.containsField(SUBMIT_DATE)) {
                axisObject.setStartTime(innerDbObject.get(SUBMIT_DATE).toString());
            }
            if (innerDbObject.containsField(SUBMITTER)) {
                axisObject.setName(innerDbObject.get(SUBMITTER).toString());
            }
            if (innerDbObject.containsField(TICKET_ID)) {
                ticketId = innerDbObject.get(TICKET_ID).toString();
                if (result.containsKey(ticketId)) {
                    List<ChartObject> items = result.get(ticketId);
                    items.add(axisObject);
                } else
                    result.put(ticketId, Lists.newArrayList(axisObject));
            }
        }
    }
}

From source file:com.epam.ta.reportportal.triggers.DeleteItemsListener.java

License:Open Source License

@Override
public void onBeforeDelete(BeforeDeleteEvent<TestItem> event) {
    DBObject dbqo = queryMapper.getMappedObject(event.getDBObject(),
            mappingContext.getPersistentEntity(TestItem.class));
    for (DBObject dbObject : mongoTemplate.getCollection(event.getCollectionName()).find(dbqo)) {
        final String id = dbObject.get("_id").toString();
        final BasicDBObject itemDescendantsQuery = new BasicDBObject("path",
                new BasicDBObject("$in", singletonList(id)));
        final List<String> itemIds = stream(
                mongoTemplate.getCollection(event.getCollectionName()).find(itemDescendantsQuery).spliterator(),
                false).map(it -> it.get("_id").toString()).collect(toList());
        mongoTemplate.getCollection(event.getCollectionName()).remove(itemDescendantsQuery);
        itemIds.add(id);//from   www .j  a  va2s.c  o  m
        logRepository.deleteByItemRef(itemIds);
    }
}

From source file:com.epam.ta.reportportal.triggers.DeleteLogsListener.java

License:Open Source License

@Override
public void onBeforeDelete(BeforeDeleteEvent<Log> event) {
    List<String> ids = new ArrayList<>();
    for (DBObject dbObject : mongoTemplate.getCollection(event.getCollectionName()).find(queryMapper
            .getMappedObject(event.getDBObject(), mongoMappingContext.getPersistentEntity(Log.class)))) {
        if (dbObject.containsField("binary_content")) {
            @SuppressWarnings("unchecked")
            Map<String, Object> binaries = (Map<String, Object>) dbObject.get("binary_content");
            if (binaries.containsKey("id")) {
                ids.add(binaries.get("id").toString());
            }//w  w  w  .  j  a  va2  s  . co m
            if (binaries.containsKey("thumbnail_id")) {
                ids.add(binaries.get("thumbnail_id").toString());
            }
        }
    }
    dataStorage.delete(ids);
}

From source file:com.epam.ta.reportportal.triggers.DeleteProjectListener.java

License:Open Source License

@Override
public void onBeforeDelete(BeforeDeleteEvent<Project> event) {
    for (DBObject dbObject : mongoTemplate.getCollection(event.getCollectionName()).find(
            queryMapper.getMappedObject(event.getDBObject(), context.getPersistentEntity(Project.class)))) {
        final String id = dbObject.get("_id").toString();
        updateDefaultProject(id);/*  w w  w. j  a va2 s .  com*/
        removeProjectShareable(id);
        externalSystemRepository.deleteByProjectRef(id);
        activityRepository.deleteByProjectRef(id);
        launchRepository.deleteByProjectRef(id);
    }
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Deletes an item from the db//from  w w w. j  a  va2 s  . com
 *
 * @param entity
 *            The item to delete
 * @throws IOException
 */
public void delete(UXBEntity entity) throws IOException {
    if (entity == null) {
        logger.warn("no UXBEntity given, no action");
        return;
    }

    // delete item
    DBObject item = getNewsById(Long.parseLong(entity.getUuid()), entity.getLanguage());
    if (item != null) {
        // delete file from filesystem
        URL url = new URL((String) item.get("url"));
        File file = new File(webpath + url.getPath());
        if (file.exists()) {
            // Try acquiring the lock without blocking. This method returns
            // null or throws an exception if the file is already locked.
            try {
                FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
                // Try to lock the file
                FileLock lock = channel.tryLock();
                // Delete the file
                boolean deleteSuccess = file.delete();
                if (!deleteSuccess) {
                    logger.debug("Couldn't delete file, file not found. Maybe someone else has deleted it.");
                }
                // Release the lock
                lock.release();
                lock.channel().close();
            } catch (OverlappingFileLockException e) {
                logger.info("File is already locked in this thread or virtual machine");
            }
        }
        // delete the article
        DBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        query.put("fs_id", item.get("fs_id"));
        query.put("language", item.get("language"));
        dbNews.remove(query);
    }

}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Deletes every item older than the creationTime of the UXBEntity.
 *
 * @param entity/*from   w  w  w  .ja  va2s . c o  m*/
 *            Entity containing the expireDate (= createTime of the entity)
 */
public void cleanup(UXBEntity entity) {

    if (entity == null) {
        logger.warn("no UXBEntity given, no action");
        return;
    }

    // delete newsdrilldown
    DBCursor items = getNewsByLastmodified(entity.getCreateTime());
    for (DBObject item : items) {
        DBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        query.put("fs_id", item.get("fs_id"));
        query.put("language", item.get("language"));
        dbNews.remove(query);
    }

    // delete categories
    items = getCategoriesByLastmodified(entity.getCreateTime());
    for (DBObject item : items) {
        DBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        query.put("fs_id", item.get("fs_id"));
        query.put("language", item.get("language"));
        dbCategories.remove(query);
    }

    // delete metaCategories
    items = getMetaCategoriesByLastmodified(entity.getCreateTime());
    for (DBObject item : items) {
        DBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        query.put("fs_id", item.get("fs_id"));
        query.put("language", item.get("language"));
        dbMetaCategories.remove(query);
    }

}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * handle the update and save process for the news.
 *
 * @param news/*from  w w w .  jav  a  2  s . c o  m*/
 *            the News object
 * @throws Exception
 *             the exception
 */
private void handleNews(News news) throws Exception {

    // 1. save the product
    DBObject item = getNewsById(news.getFs_id(), news.getLanguage());
    if (item != null) {
        item.put("fs_id", news.getFs_id());
        item.put("language", news.getLanguage());
        item.put("headline", news.getHeadline());
        item.put("subHeadline", news.getSubheadline());
        item.put("headline", news.getHeadline());
        item.put("teaser", news.getTeaser());
        item.put("content", news.getContent());
        item.put("date", news.getDate());
        item.put("url", news.getUrl());
        item.put("version", news.getVersion());
        item.put("lastmodified", news.getLastmodified());

        BasicDBObject query = new BasicDBObject();
        news.setId((Long) item.get("_id"));
        query.put("_id", news.getId());

        dbNews.update(query, item);
    } else {
        item = new BasicDBObject();

        item.put("fs_id", news.getFs_id());
        item.put("language", news.getLanguage());
        item.put("headline", news.getHeadline());
        item.put("subHeadline", news.getSubheadline());
        item.put("headline", news.getHeadline());
        item.put("teaser", news.getTeaser());
        item.put("content", news.getContent());
        item.put("date", news.getDate());
        item.put("url", news.getUrl());
        item.put("version", news.getVersion());
        item.put("lastmodified", news.getLastmodified());

        news.setId(generateIdentifier(NEWS_COLLECTION_NAME, mdb));
        item.put("_id", news.getId());

        dbNews.insert(item);
    }

    // 2. save categories
    // add categories to news
    BasicDBList catList = new BasicDBList();
    if (news.getCategories() != null) {
        for (NewsCategory cat : news.getCategories()) {
            handleCategory(cat, news);

            DBObject tmpCat = getCategoryById(cat.getFs_id(), cat.getLanguage());
            //catList.add(new DBRef(this.mdb, this.CATEGORY_COLLECTION_NAME, tmpCat.get("_id")));
            catList.add(tmpCat.get("_id"));
        }
    }

    // add list of categories to the news
    //      item.put("categories", catList);
    item.put("categories_$$manyToManyIds", catList);

    BasicDBObject query = new BasicDBObject();
    query.put("_id", news.getId());

    dbNews.update(query, item);
}