![JAR search and dependency download from the Maven repository](/logo.png)
net.intelie.liverig.witsml.etp.ETPClientFacade Maven / Gradle / Ivy
The newest version!
package net.intelie.liverig.witsml.etp;
import Energistics.Datatypes.ChannelData.ChannelMetadataRecord;
import Energistics.Datatypes.ChannelData.ChannelStreamingInfo;
import Energistics.Datatypes.ChannelData.StreamingStartIndex;
import Energistics.Protocol.ChannelStreaming.ChannelData;
import Energistics.Protocol.ChannelStreaming.ChannelMetadata;
import Energistics.Protocol.Discovery.GetResourcesResponse;
import Energistics.Protocol.Store.Object;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import net.intelie.liverig.parser.ParseException;
import net.intelie.liverig.witsml.WITSMLException;
import net.intelie.liverig.witsml.WITSMLResult;
import net.intelie.liverig.witsml.WitsmlFilters;
import net.intelie.liverig.witsml.objects.*;
import net.intelie.liverig.witsml.query.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.websocket.Session;
import javax.xml.ws.WebServiceException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ETPClientFacade {
private static final Logger LOGGER = LoggerFactory.getLogger(ETPClientFacade.class);
private final ETPClient etpClient;
private final QueryFactory queryFactory;
private static final Pattern END_LOG_TEMPLATE = Pattern.compile("\\s*$");
@VisibleForTesting
public ETPClientFacade(ETPClient etpClient) {
this.etpClient = etpClient;
this.queryFactory = queryFactoryFor(ApiVers.WITSML_20);
}
public ETPClientFacade(URI uri, String application, String version, boolean jsonEnconding, String username, String password) {
this.etpClient = new ETPClient(uri, application, version, username, password, jsonEnconding);
this.queryFactory = queryFactoryFor(ApiVers.WITSML_20);
}
public Session testConnection() {
return etpClient.getSession();
}
public List listWells() {
try {
List responses = etpClient.discoveryQuery(new EML.Builder().addPath("Well").build().getURI());
List wells = new ArrayList<>();
for (GetResourcesResponse response : responses.stream().distinct().collect(Collectors.toList())) {
if (response.getResource() != null) {
WellUidName wellUidName = new WellUidName();
wellUidName.setUid(response.getResource().getUuid().toString());
wellUidName.setName(response.getResource().getName().toString());
LOGGER.info("well {} {} found", wellUidName.getUid(), wellUidName.getName());
wells.add(wellUidName);
}
}
return wells;
} catch (Exception e) {
LOGGER.error("FAIL", e);
throw new RuntimeException(e);
}
}
public List listWellbores(String uidWell) {
try {
List responses = etpClient.discoveryQuery(new EML.Builder().addPath("Well", uidWell).addPath("Wellbore").build().getURI());
List wellbores = new ArrayList<>();
for (GetResourcesResponse response : responses.stream().distinct().collect(Collectors.toList())) {
WellboreUidName wellboreUidName = new WellboreUidName();
wellboreUidName.setUid(response.getResource().getUuid().toString());
wellboreUidName.setName(response.getResource().getName().toString());
LOGGER.info("wellbore {} {} found", wellboreUidName.getUid(), wellboreUidName.getName());
wellbores.add(wellboreUidName);
}
return wellbores;
} catch (Exception e) {
LOGGER.error("FAIL", e);
throw new RuntimeException(e);
}
}
public List listLogs(String uidWell, String uidWellbore, WitsmlFilters filters) {
try {
List responses = etpClient.discoveryQuery(new EML.Builder().addPath("Well", uidWell).addPath("Wellbore", uidWellbore).addPath("Log").build().getURI());
List logs = new ArrayList<>();
for (GetResourcesResponse response : responses.stream().distinct().collect(Collectors.toList())) {
LogData logData = getLogHeader(uidWell, uidWellbore, response.getResource().getUuid().toString(), filters, false);
logs.add(logData.getHeader());
}
return logs;
} catch (Exception e) {
LOGGER.error("FAIL", e);
throw new RuntimeException(e);
}
}
public List listLogs() {
try {
List responses = etpClient.discoveryQuery(new EML.Builder().addPath("Log").build().getURI());
List logs = new ArrayList<>();
for (GetResourcesResponse response : responses.stream().distinct().collect(Collectors.toList())) {
LogHeader logHeader = new LogHeader();
logHeader.setUid(response.getResource().getUuid().toString());
logHeader.setName((response == null ? logHeader.getUid() : response.getResource().getName().toString()));
logs.add(logHeader);
}
return logs;
} catch (Exception e) {
LOGGER.error("FAIL", e);
throw new RuntimeException(e);
}
}
public Object getObject(String uri) {
Session session = null;
try {
session = etpClient.getSession();
// open session
etpClient.requestSession(session);
// getObject
return etpClient.getObject(session, uri);
} catch (Exception e) {
LOGGER.error("FAIL", e);
throw new RuntimeException(e);
} finally {
if (session != null && session.isOpen())
etpClient.closeSession(session);
}
}
public List discoveryQuery(String uri) {
return etpClient.discoveryQuery(uri).stream().distinct().collect(Collectors.toList());
}
public LogData getLogHeader(String uidWell, String uidWellbore, String uid, WitsmlFilters filters, boolean raw)
throws WebServiceException, WITSMLException, ParseException {
LogQuery query = queryFactory.getLogHeader(uidWell, uidWellbore, uid, filters, raw);
return query.parse(getFromStore(query));
}
public LogData getLogData(String uidWell, String uidWellbore, String uid, WitsmlFilters filters, LogRange range)
throws WebServiceException, WITSMLException, ParseException {
return getLogData(uidWell, uidWellbore, uid, filters, range, false);
}
public LogData getLogData(String uidWell, String uidWellbore, String uid, WitsmlFilters filters, LogRange range, boolean raw)
throws WebServiceException, WITSMLException, ParseException {
LogQuery query = queryFactory.getLogData(uidWell, uidWellbore, uid, filters, range, raw);
return query.parse(getFromStore(query));
}
public RawData getLogDataOrRaw(String uidWell, String uidWellbore, String uid, WitsmlFilters filters, @NotNull LogRange range) {
Session session = null;
ChannelData channelData = null;
try {
LogQuery query = queryFactory.getLogData(uidWell, uidWellbore, uid, filters, range, true);
session = etpClient.getSession();
// open session
etpClient.requestSession(session);
// start message
etpClient.start(session);
// discovery channelsets
List responses = etpClient.discovery(session, new EML
.Builder()
.addWell(uidWell)
.addWellbore(uidWellbore)
.addLog(uid)
.addPath("ChannelSet")
.build()
.getURI());
// describe
ChannelMetadata channelMetadata = etpClient.describe(session, responses
.stream()
.map(uri -> new EML
.Builder()
.addPath("ChannelSet",
uri.getResource().getUuid().toString())
.build()
.getURI()
)
.collect(Collectors.toList()));
if (channelMetadata != null && !channelMetadata.getChannels().isEmpty()) {
if (range instanceof LogIndex) {
LogIndex logIndex = (LogIndex) range;
if (logIndex.getStartIndex() != null && logIndex.getEndIndex() != null) {
// range request
channelData = etpClient.requestRange(session, range, channelMetadata);
} else {
// start streamming
List channels = channelMetadata.getChannels().stream()
.map(ChannelMetadataRecord::getChannelId)
.map(channelId -> ChannelStreamingInfo
.newBuilder()
.setStartIndex(StreamingStartIndex.newBuilder()
.setItem(logIndex.getStartIndex() == null ? null :
logIndex.getStartIndex().longValue())
.build())
.setChannelId(channelId)
.setReceiveChangeNotification(true)
.build())
.collect(Collectors.toList());
channelData = etpClient.startStreaming(session, channels);
// stop streamming
etpClient.stop(session, channelMetadata.getChannels().stream()
.map(ChannelMetadataRecord::getChannelId).collect(Collectors.toList()));
}
}
}
return mergeData(query, channelMetadata, channelData);
} catch (Exception e) {
LOGGER.error("FAIL", e);
throw new RuntimeException(e);
} finally {
if (session != null && session.isOpen())
etpClient.closeSession(session);
}
}
private RawData mergeData(RawDataQuery query, @NotNull ChannelMetadata channelMetadata, @Nullable ChannelData channelData) {
RawData rawData = getFromStoreOrRaw(query);
String channelMetaDataXml = org.json.XML.toString(
new JSONObject((channelMetadata == null ? "{}" : channelMetadata.toString())), "channelMetadata");
String channelDataXml = org.json.XML.toString(
new JSONObject((channelData == null ? "{}" : channelData.toString())), "channelData");
StringBuilder xml = new StringBuilder();
xml.append("");
if (!Strings.isNullOrEmpty(channelMetaDataXml)) {
// remove unecessary data
channelMetaDataXml = channelMetaDataXml.replaceAll("(?<=)(.*?)(?= )", "");
xml.append(channelMetaDataXml);
}
if (!Strings.isNullOrEmpty(channelDataXml)) {
xml.append(channelDataXml);
}
xml.append("");
xml.append("");
String log = END_LOG_TEMPLATE.matcher(rawData.getRaw()).replaceFirst(xml.toString());
rawData.setRaw(log);
return rawData;
}
private RawData getFromStoreOrRaw(RawDataQuery query) {
WITSMLResult result;
try {
result = getFromStore(query);
} catch (Exception e) {
// TODO resolve this method
return new RawData("");
}
if (Strings.isNullOrEmpty(result.getXml()))
return result.toRawData();
try {
return query.parse(result);
} catch (ParseException e) {
LOGGER.info("Could not parse response", e);
return result.toRawData();
}
}
private WITSMLResult getFromStore(String uri) {
try {
LOGGER.debug("getFromStore {}", uri);
Object object = getObject(uri);
if (object == null) {
return new WITSMLResult((short) -1, "");
} else {
return new WITSMLResult(WITSMLResult.SUCCESS, StandardCharsets.UTF_8.decode(object.getDataObject().getData()).toString());
}
} catch (Exception e) {
throw new RuntimeException("FAIL:", e);
}
}
public WITSMLResult getFromStore(Query query) {
return getFromStore(query.query());
}
private static QueryFactory queryFactoryFor(ApiVers apiVers) {
switch (apiVers) {
case WITSML_131:
return new QueryFactory131();
case WITSML_141:
return new QueryFactory141();
case WITSML_20:
return new QueryFactory20();
default:
throw new IllegalArgumentException();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy