private Set<Ack> rowsToAcks(final List<Row> rows) { if (rows.isEmpty()) { return Collections.emptySet(); } final Set<Ack> unacks = new HashSet<Ack>(rows.size()); for (Row row : rows) { final JsonNode json = row.getValueAsNode().get(DOC_FIELD); unacks.add(new AckImpl(json.get(CHID_FIELD).asText(), json.get(VERSION_FIELD).asLong())); } return unacks; }
private Set<Ack> rowsToAcks(final List<Row> rows) { if (rows.isEmpty()) { return Collections.emptySet(); } final Set<Ack> unacks = new HashSet<Ack>(rows.size()); for (Row row : rows) { final JsonNode json = row.getValueAsNode().get(DOC_FIELD); unacks.add(new AckImpl(json.get(CHID_FIELD).asText(), json.get(VERSION_FIELD).asLong())); } return unacks; }
@Override public Set<String> getChannelIds(final String uaid) { final ViewResult viewResult = db.queryView(query(Views.UAID.viewName(), uaid)); final List<Row> rows = viewResult.getRows(); if (rows.isEmpty()) { return Collections.emptySet(); } final Set<String> channelIds = new HashSet<String> (rows.size()); for (Row row : rows) { channelIds.add(row.getValueAsNode().get(DOC_FIELD).get(CHID_FIELD).asText()); } return channelIds; }
@Override public Set<Ack> removeAcknowledged(final String uaid, final Set<Ack> acked) { final ViewResult viewResult = db.queryView(query(Views.UNACKS.viewName(), uaid)); final List<Row> rows = viewResult.getRows(); final Collection<BulkDeleteDocument> removals = new LinkedHashSet<BulkDeleteDocument>(); for (Iterator<Row> iter = rows.iterator(); iter.hasNext(); ) { final Row row = iter.next(); final JsonNode json = row.getValueAsNode(); final JsonNode doc = json.get(DOC_FIELD); final String channelId = doc.get(CHID_FIELD).asText(); for (Ack ack : acked) { if (ack.getChannelId().equals(channelId)) { removals.add(BulkDeleteDocument.of(doc)); iter.remove(); } } } db.executeBulk(removals); return rowsToAcks(rows); }
@Override public String updateVersion(final String endpointToken, final long version) throws VersionException, ChannelNotFoundException { final ViewResult viewResult = db.queryView(query(Views.TOKEN.viewName(), endpointToken)); final List<Row> rows = viewResult.getRows(); if (rows.isEmpty()) { throw new ChannelNotFoundException("Cound not find channel for endpointToken", endpointToken); } final ObjectNode node = (ObjectNode) rows.get(0).getValueAsNode().get(DOC_FIELD); final long currentVersion = node.get(VERSION_FIELD).asLong(); if (version <= currentVersion) { throw new VersionException("version [" + version + "] must be greater than the current version [" + currentVersion + "]"); } node.put(VERSION_FIELD, String.valueOf(version)); db.update(node); return node.get(CHID_FIELD).asText(); }
@Override public String updateVersion(final String endpointToken, final long version) throws VersionException, ChannelNotFoundException { final ViewResult viewResult = db.queryView(query(Views.TOKEN.viewName(), endpointToken)); final List<Row> rows = viewResult.getRows(); if (rows.isEmpty()) { throw new ChannelNotFoundException("Cound not find channel for endpointToken", endpointToken); } final ObjectNode node = (ObjectNode) rows.get(0).getValueAsNode().get(DOC_FIELD); final long currentVersion = node.get(VERSION_FIELD).asLong(); if (version <= currentVersion) { throw new VersionException("version [" + version + "] must be greater than the current version [" + currentVersion + "]"); } node.put(VERSION_FIELD, String.valueOf(version)); db.update(node); return node.get(CHID_FIELD).asText(); }
@Override @View(name = "find_by_observation_id", map = "function (doc) { if (doc.type === 'Encounter') for each(item in doc.observations) { emit(item.observationId, item); recursiveDescent(item); } function recursiveDescent(obsNode) { for each(item in obsNode.dependantObservations) { emit(item.observationId, item); recursiveDescent(item) }}}") public MRSObservation findByObservationId(String observationId) { ViewQuery viewQuery = createQuery("find_by_observation_id").key(observationId).includeDocs(false); ViewResult viewResult = db.queryView(viewQuery); MRSObservation obs = null; if (viewResult.getRows() != null && viewResult.getRows().size() > 0) { Row row = viewResult.getRows().get(0); JsonNode value = row.getValueAsNode(); String text = value.toString(); obs = (CouchObservation) CouchJsonUtils.readJson(text, CouchObservation.class); } return obs; }
public String getValue() { return nodeAsString(getValueAsNode()); }
@Override public Set<String> getChannelIds(final String uaid) { final ViewResult viewResult = db.queryView(query(Views.UAID.viewName(), uaid)); final List<Row> rows = viewResult.getRows(); if (rows.isEmpty()) { return Collections.emptySet(); } final Set<String> channelIds = new HashSet<String> (rows.size()); for (Row row : rows) { channelIds.add(row.getValueAsNode().get(DOC_FIELD).get(CHID_FIELD).asText()); } return channelIds; }
@Override public void removeChannels(final Set<String> channelIds) { final ViewResult viewResult = db.queryView(channelsQuery(channelIds)); final List<Row> rows = viewResult.getRows(); final Collection<BulkDeleteDocument> removals = new LinkedHashSet<BulkDeleteDocument>(); for (Row row : rows) { final JsonNode json = row.getValueAsNode(); removals.add(BulkDeleteDocument.of(json.get(DOC_FIELD))); } db.executeBulk(removals); }
private JsonNode getChannelJson(final String channelId) throws ChannelNotFoundException { final ViewResult viewResult = db.queryView(query(Views.CHANNEL.viewName(), channelId)); final List<Row> rows = viewResult.getRows(); if (rows.isEmpty()) { throw new ChannelNotFoundException("Cound not find channel", channelId); } if (rows.size() > 1) { throw new IllegalStateException("There should not be multiple channelId with the same id"); } return rows.get(0).getValueAsNode(); }
@Override @View(name = "find_observations_by_patient_id_and_concept_name", map = "function (doc) { if (doc.type === 'Encounter') for each(item in doc.observations) { emit([doc.encounterPatientId, item.conceptName], item); recursiveDescent(item); } function recursiveDescent(obsNode) { for each(item in obsNode.dependantObservations) { emit([doc.encounterPatientId, item.conceptName], item); recursiveDescent(item) }}}") public List<MRSObservation> findObservationsByPatientIdAndConceptName(String motechId, String conceptName) { ViewQuery viewQuery = createQuery("find_observations_by_patient_id_and_concept_name").key(ComplexKey.of(motechId, conceptName)).includeDocs(false); ViewResult viewResult = db.queryView(viewQuery); List<MRSObservation> obsList = new ArrayList<MRSObservation>(); for (Row row : viewResult.getRows()) { JsonNode value = row.getValueAsNode(); String text = value.toString(); MRSObservation obs = (CouchObservation) CouchJsonUtils.readJson(text, CouchObservation.class); obsList.add(obs); } return obsList; }
public int getValueAsInt() { return getValueAsNode().asInt(0); }
@Override public void removeChannels(final String uaid) { final ViewResult viewResult = db.queryView(query(Views.UAID.viewName(), uaid)); final List<Row> rows = viewResult.getRows(); final Set<String> channelIds = new HashSet<String>(rows.size()); for (Row row : rows) { final JsonNode json = row.getValueAsNode().get(DOC_FIELD); channelIds.add(json.get(CHID_FIELD).asText()); } removeChannels(channelIds); }
@Override @SuppressWarnings("unchecked") public Map<Object, Object> getTuple(ViewResult.Row value) throws IOException { return mapper.readValue(value.getValueAsNode(), HashMap.class); } }
@Override public void removeChannels(final String uaid) { final ViewResult viewResult = db.queryView(query(Views.UAID.viewName(), uaid)); final List<Row> rows = viewResult.getRows(); final Set<String> channelIds = new HashSet<String>(rows.size()); for (Row row : rows) { final JsonNode json = row.getValueAsNode().get(DOC_FIELD); channelIds.add(json.get(CHID_FIELD).asText()); } removeChannels(channelIds); }
@SuppressWarnings("unchecked") @Override public Map<Object, Object> getTuple(ViewResult.Row row) { Map<Object, Object> valueMap = Maps.newHashMap(); try { valueMap = mapper.readValue(row.getValueAsNode(), valueMap.getClass()); } catch (IOException e) { e.printStackTrace(); } return valueMap; } }
private JsonNode getChannelJson(final String channelId) throws ChannelNotFoundException { final ViewResult viewResult = db.queryView(query(Views.CHANNEL.viewName(), channelId)); final List<Row> rows = viewResult.getRows(); if (rows.isEmpty()) { throw new ChannelNotFoundException("Cound not find channel", channelId); } if (rows.size() > 1) { throw new IllegalStateException("There should not be multiple channelId with the same id"); } return rows.get(0).getValueAsNode(); }
@Override @SuppressWarnings("unchecked") public Map<Object, Object> getTuple(ViewResult.Row value) throws IOException { return mapper.readValue(value.getValueAsNode(), HashMap.class); } }
@Override public void removeChannels(final Set<String> channelIds) { final ViewResult viewResult = db.queryView(channelsQuery(channelIds)); final List<Row> rows = viewResult.getRows(); final Collection<BulkDeleteDocument> removals = new LinkedHashSet<BulkDeleteDocument>(); for (Row row : rows) { final JsonNode json = row.getValueAsNode(); removals.add(BulkDeleteDocument.of(json.get(DOC_FIELD))); } db.executeBulk(removals); }