Skip to content
Snippets Groups Projects
Commit fe2e5ba5 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

Fix compilation

parent 30f03309
No related branches found
No related tags found
No related merge requests found
Showing
with 210 additions and 108 deletions
......@@ -284,6 +284,7 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> {
/* protected methods */
protected void initI18n() throws IOException {
if (I18n.getDefaultLocale() != null) return; // already init
// --------------------------------------------------------------------//
// init i18n
......@@ -307,6 +308,7 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> {
logger.info(String.format("Starts i18n with locale [%s] at [%s]",
i18nLocale, i18nDirectory));
}
I18n.init(new UserI18nInitializer(
i18nDirectory, new DefaultI18nInitializer(getI18nBundleName())),
i18nLocale);
......
......@@ -25,6 +25,7 @@ package org.duniter.elasticsearch.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonSyntaxException;
......@@ -45,6 +46,10 @@ import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRespon
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.bytes.BytesArray;
......@@ -52,9 +57,13 @@ import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.nuiton.i18n.I18n;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
......@@ -226,6 +235,54 @@ public abstract class AbstractService implements Bean {
return value;
}
/**
* Retrieve some field from a document id
* @param docId
* @return
*/
protected Map<String, Object> getFieldsById(String index, String type, String docId, String... fieldNames) {
// Prepare request
SearchRequestBuilder searchRequest = client
.prepareSearch(index)
.setTypes(type)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
searchRequest.setQuery(QueryBuilders.matchQuery("_id", docId));
searchRequest.addFields(fieldNames);
// Execute query
try {
SearchResponse response = searchRequest.execute().actionGet();
Map<String, Object> result = new HashMap<>();
// Read query result
SearchHit[] searchHits = response.getHits().getHits();
for (SearchHit searchHit : searchHits) {
if (searchHit.source() != null) {
JsonNode source = objectMapper.readTree(searchHit.source());
for(String fieldName: fieldNames) {
result.put(fieldName, getMandatoryField(source, fieldName));
}
}
else {
for(String fieldName: fieldNames) {
result.put(fieldName, searchHit.getFields().get(fieldName).getValue());
}
}
break;
}
return result;
}
catch(SearchPhaseExecutionException | JsonSyntaxException | IOException e) {
// Failed or no item on index
throw new TechnicalException(String.format("[%s/%s] Unable to retrieve fields [%s] for document [%s]",
index, type,
Joiner.on(',').join(fieldNames).toString(),
docId), e);
}
}
protected void bulkFromClasspathFile(String classpathFile, String indexName, String indexType) {
bulkFromClasspathFile(classpathFile, indexName, indexType, null);
}
......
package org.duniter.elasticsearch.gchange.model;
import org.duniter.core.client.model.elasticsearch.Record;
/**
* Created by blavenie on 01/12/16.
*/
public class MarketRecord extends Record{
public static final String PROPERTY_TITLE="title";
public static final String PROPERTY_DESCRIPTION="description";
private String title;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package org.duniter.elasticsearch.gchange.model.event;
/**
* Created by blavenie on 01/12/16.
*/
public enum GchangeEventCodes {
NEW_COMMENT
}
......@@ -25,19 +25,19 @@ package org.duniter.elasticsearch.gchange.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Joiner;
import com.google.gson.JsonSyntaxException;
import org.duniter.core.client.model.elasticsearch.Currency;
import org.duniter.core.client.model.elasticsearch.Record;
import org.duniter.core.client.model.elasticsearch.RecordComment;
import org.duniter.core.client.service.bma.WotRemoteService;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.service.CryptoService;
import org.duniter.core.util.StringUtils;
import org.duniter.elasticsearch.exception.InvalidFormatException;
import org.duniter.elasticsearch.gchange.PluginSettings;
import org.duniter.elasticsearch.gchange.model.MarketRecord;
import org.duniter.elasticsearch.gchange.model.event.GchangeEventCodes;
import org.duniter.elasticsearch.service.AbstractService;
import org.duniter.elasticsearch.user.service.event.UserEvent;
import org.duniter.elasticsearch.user.service.event.UserEventCodes;
import org.duniter.elasticsearch.user.service.event.UserEventLink;
import org.duniter.elasticsearch.user.service.event.UserEventService;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
......@@ -54,9 +54,10 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.nuiton.i18n.I18n;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* Created by Benoit on 30/03/2015.
......@@ -171,21 +172,34 @@ public class MarketService extends AbstractService {
public String indexCommentFromJson(String json) {
JsonNode actualObj = readAndVerifyIssuerSignature(json);
String issuer = getMandatoryField(actualObj, RecordComment.PROPERTY_ISSUER).asText();
String recordId = getMandatoryField(actualObj, RecordComment.PROPERTY_RECORD).asText();
String recordIssuer = getRecordIssuerById(recordId);
if (logger.isDebugEnabled()) {
logger.debug(String.format("Indexing a %s from issuer [%s]", RECORD_COMMENT_TYPE, issuer.substring(0, 8)));
}
String commentId = indexDocumentFromJson(INDEX, RECORD_COMMENT_TYPE, json);
// Notify record issuer
// Notification
{
// Notify issuer of record (is not same as comment writer)
String recordId = getMandatoryField(actualObj, RecordComment.PROPERTY_RECORD).asText();
Map<String, Object> recordFields = getRecordFieldsById(recordId, MarketRecord.PROPERTY_TITLE, MarketRecord.PROPERTY_ISSUER);
String recordIssuer = recordFields.get(MarketRecord.PROPERTY_ISSUER).toString();
String recordTitle = recordFields.get(MarketRecord.PROPERTY_TITLE).toString();
if (!issuer.equals(recordIssuer)) {
userEventService.notifyUser(recordIssuer,
new UserEvent(UserEvent.EventType.INFO, /*TODO*/ "NEW_COMMENT"));
new UserEvent(UserEvent.EventType.INFO,
GchangeEventCodes.NEW_COMMENT.name(),
new UserEventLink(INDEX, RECORD_TYPE, recordId),
I18n.n("duniter.market.event.newComment"),
issuer, recordTitle
)
);
}
}
return commentId;
}
public void updateCommentFromJson(String json, String id) {
checkIssuerAndUpdateDocumentFromJson(INDEX, RECORD_COMMENT_TYPE, json, id);
}
......@@ -410,44 +424,15 @@ public class MarketService extends AbstractService {
}
/**
* Retrieve a blockchain from its name
* Retrieve record field's values
* @param recordId
* @param fieldNames
* @return
*/
protected String getRecordIssuerById(String recordId) {
// Prepare request
SearchRequestBuilder searchRequest = client
.prepareSearch(INDEX)
.setTypes(RECORD_TYPE)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
protected Map<String, Object> getRecordFieldsById(String recordId, String... fieldNames) {
searchRequest.setQuery(QueryBuilders.matchQuery("_id", recordId));
searchRequest.addFields(Record.PROPERTY_ISSUER);
// Execute query
try {
SearchResponse response = searchRequest.execute().actionGet();
// Read query result
SearchHit[] searchHits = response.getHits().getHits();
for (SearchHit searchHit : searchHits) {
if (searchHit.source() != null) {
JsonNode source = objectMapper.readTree(searchHit.source());
return source.get(Record.PROPERTY_ISSUER).asText();
}
else {
SearchHitField field = searchHit.getFields().get(Record.PROPERTY_ISSUER);
return field.getValue().toString();
}
}
}
catch(SearchPhaseExecutionException | JsonSyntaxException | IOException | UnsupportedEncodingException e) {
// Failed or no item on index
if (logger.isDebugEnabled()) {
logger.error(String.format("Unable to retrieve issuer of record [%s]", recordId), e);
}
return getFieldsById(INDEX, RECORD_TYPE, recordId, fieldNames);
}
return null;
}
}
......@@ -23,9 +23,8 @@ package org.duniter.elasticsearch.user;
*/
import com.google.common.collect.Lists;
import org.duniter.elasticsearch.PluginInit;
import org.duniter.elasticsearch.user.service.ServiceModule;
import org.duniter.elasticsearch.user.rest.RestModule;
import org.duniter.elasticsearch.user.service.ServiceModule;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module;
......@@ -47,7 +46,7 @@ public class Plugin extends org.elasticsearch.plugins.Plugin {
@Override
public String name() {
return "duniter.user";
return "duniter4j-es-user";
}
@Override
......
......@@ -38,6 +38,7 @@ import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.nuiton.i18n.I18n;
/**
* Created by blavenie on 17/06/16.
......@@ -77,7 +78,8 @@ public class PluginInit extends AbstractLifecycleComponent<org.duniter.elasticse
.notifyAdmin(new UserEvent(
UserEvent.EventType.INFO,
UserEventCodes.NODE_STARTED.name(),
new String[]{clusterName}));
I18n.n("duniter.event.NODE_STARTED"),
clusterName));
});
}
......
......@@ -31,27 +31,32 @@ import java.util.Locale;
*/
public class UserEvent {
private EventType type;
private final EventType type;
private String code;
private final String code;
private long time;
private final long time;
private String message;
private final String message;
private final String[] params;
private String[] params;
private final UserEventLink link;
public UserEvent(EventType type, String code) {
this(type, code, null);
this(type, code, null, "duniter.event." + code, null);
}
public UserEvent(EventType type, String code, String[] params) {
public UserEvent(EventType type, String code, String message, String... params) {
this(type, code, null, message, params);
}
public UserEvent(EventType type, String code, UserEventLink link, String message, String... params) {
this.type = type;
this.code = code;
this.params = params;
// default
this.message = I18n.t("duniter4j.event." + code, params);
this.link = link;
this.message = message;
this.time = Math.round(1d * System.currentTimeMillis() / 1000);
}
......@@ -59,38 +64,22 @@ public class UserEvent {
return type;
}
public void setType(EventType type) {
this.type = type;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public String getLocalizedMessage(Locale locale) {
return I18n.l(locale, "duniter4j.event." + code, params);
}
public void setMessage(String message) {
this.message = message;
return I18n.l(locale, message, params);
}
public String[] getParams() {
return params;
}
public void setParams(String[] params) {
this.params = params;
}
public long getTime() {
return time;
}
......
......@@ -28,7 +28,6 @@ package org.duniter.elasticsearch.user.service.event;
public enum UserEventCodes {
NODE_STARTED,
CREATE_DOC,
UPDATE_DOC,
COMMENT_DOC
CREATE_DOC
}
package org.duniter.elasticsearch.action.registry;
package org.duniter.elasticsearch.user.service.event;
/*
* #%L
* duniter4j-elasticsearch-plugin
* Duniter4j :: ElasticSearch Plugin
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
......@@ -22,17 +22,36 @@ package org.duniter.elasticsearch.action.registry;
* #L%
*/
import org.duniter.elasticsearch.rest.security.RestSecurityController;
import org.duniter.elasticsearch.service.RegistryService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.rest.RestRequest;
import org.nuiton.i18n.I18n;
public class RestRegistryCategoryAction {
import java.util.Locale;
@Inject
public RestRegistryCategoryAction(RestSecurityController securityController) {
// Add security rule for category
securityController.allowIndexType(RestRequest.Method.GET, RegistryService.INDEX, RegistryService.RECORD_CATEGORY_TYPE);
/**
* Created by blavenie on 29/11/16.
*/
public class UserEventLink {
private final String index;
private final String type;
private final String id;
public UserEventLink(String index, String type, String id) {
this.index = index;
this.type = type;
this.id = id;
}
public String getIndex() {
return index;
}
public String getType() {
return type;
}
public String getId() {
return id;
}
}
......@@ -89,7 +89,7 @@ public class UserEventService extends AbstractService implements ChangeListener
if (!this.mailEnable && logger.isTraceEnabled()) {
logger.trace("Mail disable");
}
ChangeService.registerListener(this);
//ChangeService.registerListener(this);
}
/**
......@@ -118,17 +118,6 @@ public class UserEventService extends AbstractService implements ChangeListener
}
}
/**
* Notify a new document
*/
public void notifyNewDocument(String index, String type, String id, String issuer) {
String docId = String.format("%s/%s/%s", index, type, id);
logger.info(String.format("Detected new document at: %s", docId));
notifyUser(issuer, new UserEvent(UserEvent.EventType.INFO, UserEventCodes.CREATE_DOC.name(), new String[]{docId}));
}
/**
* Notify a user
*/
......@@ -142,7 +131,7 @@ public class UserEventService extends AbstractService implements ChangeListener
@Override
public void onChanges(String json) {
// TODO get doc issuer
String issuer = nodePubkey;
/* String issuer = nodePubkey;
ChangeEvent event = ChangeUtils.fromJson(objectMapper, json);
......@@ -153,7 +142,7 @@ public class UserEventService extends AbstractService implements ChangeListener
if (event.getOperation() == ChangeEvent.Operation.CREATE) {
notifyNewDocument(event.getIndex(), event.getType(), event.getId(), issuer);
}
}*/
}
......@@ -291,15 +280,34 @@ public class UserEventService extends AbstractService implements ChangeListener
.field("index", "not_analyzed")
.endObject()
// params
.startObject("params")
// link
.startObject("link")
.field("type", "nested")
.field("dynamic", "false")
.startObject("properties")
.startObject("index")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
.startObject("type")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
.startObject("id")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
.endObject()
.endObject()
// message
.startObject("message")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
// params
.startObject("params")
.field("type", "string")
.endObject()
.endObject()
......
duniter.event.NODE_STARTED=
duniter4j.event.NODE_STARTED=Node started on cluster Duniter4j ES [%s]
duniter4j.event.subject.ERROR=[%s] Error message
duniter4j.event.subject.INFO=[%s] Information message
......
duniter.event.NODE_STARTED=
duniter4j.event.NODE_STARTED=Noeud démarré sur le cluster Duniter4j ES [%s]
duniter4j.event.subject.ERROR=%s Message d'erreur
duniter4j.event.subject.INFO=%s Message d'information
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment