Skip to content

Commit

Permalink
ARC-1462: Builders for clients (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrichner-oviva authored Mar 18, 2024
1 parent 86c8de5 commit 23df414
Show file tree
Hide file tree
Showing 26 changed files with 338 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -1,97 +1,21 @@
package com.oviva.epa.client;

import com.oviva.epa.client.konn.KonnektorConnection;
import com.oviva.epa.client.svc.CardServiceClient;
import com.oviva.epa.client.svc.CertificateServiceClient;
import com.oviva.epa.client.svc.EventServiceClient;
import com.oviva.epa.client.svc.PhrManagementServiceClient;
import com.oviva.epa.client.svc.model.KonnektorContext;
import com.oviva.epa.client.svc.phr.PhrServiceClient;
import com.oviva.epa.client.svc.phr.SmbInformationServiceClient;
import com.oviva.epa.client.svc.phr.model.RecordIdentifier;
import com.oviva.epa.client.svc.phr.model.RecordIdentifierAdapter;
import de.gematik.epa.LibIheXdsMain;
import com.oviva.epa.client.model.Card;
import com.oviva.epa.client.model.PinStatus;
import com.oviva.epa.client.model.RecordIdentifier;
import com.oviva.epa.client.model.WriteDocumentResponse;
import de.gematik.epa.ihe.model.document.Document;
import de.gematik.epa.ihe.model.request.DocumentSubmissionRequest;
import de.gematik.epa.ihe.model.simple.AuthorInstitution;
import de.gematik.epa.ihe.model.simple.SubmissionSetMetadata;
import java.time.LocalDateTime;
import java.util.List;
import oasis.names.tc.ebxml_regrep.xsd.rs._3.RegistryResponseType;
import telematik.ws.conn.cardservice.xsd.v8_1.CardInfoType;
import telematik.ws.conn.cardservice.xsd.v8_1.PinStatusEnum;

public class KonnektorService {
public interface KonnektorService {
List<AuthorInstitution> getAuthorInstitutions();

private final EventServiceClient eventServiceClient;
private final CardServiceClient cardServiceClient;
private final PhrManagementServiceClient phrManagementServiceClient;
private final PhrServiceClient phrServiceClient;
List<Card> getCardsInfo();

private final SmbInformationServiceClient smbInformationServiceClient;
PinStatus verifySmcPin(String cardHandle);

public KonnektorService(KonnektorConnection connection, KonnektorContext konnektorContext) {
WriteDocumentResponse writeDocument(RecordIdentifier recordIdentifier, Document document);

eventServiceClient = new EventServiceClient(connection.eventService(), konnektorContext);

cardServiceClient =
new CardServiceClient(connection.cardService(), konnektorContext, eventServiceClient);

phrServiceClient = new PhrServiceClient(connection.phrService(), konnektorContext);

phrManagementServiceClient =
new PhrManagementServiceClient(connection.phrManagementService(), konnektorContext);

var certificateServiceClient =
new CertificateServiceClient(connection.certificateService(), konnektorContext);

smbInformationServiceClient =
new SmbInformationServiceClient(eventServiceClient, certificateServiceClient);
}

public List<AuthorInstitution> getAuthorInstitutions() {
return smbInformationServiceClient.getAuthorInstitutions();
}

// TODO: map CardInfoType to a VO
public List<CardInfoType> getCardsInfo() {
// don't be fooled by the name, this returns a list of cards...
return eventServiceClient.getSmbInfo().getCards().getCard();
}

// TODO: map PinStatusEnum to a VO
public PinStatusEnum verifyPin(String cardHandle) {
var response = cardServiceClient.getPinStatusResponse(cardHandle, "PIN.SMC");
return response.getPinStatus();
}

// TODO: map RegistryResponseType to a VO
public RegistryResponseType writeDocument(RecordIdentifier recordIdentifier, Document document) {

var docSubmissionRequest =
new DocumentSubmissionRequest(
new RecordIdentifierAdapter(recordIdentifier),
List.of(document),
getSubmissionSetMetadata(document));

var provideAndRegisterRequest =
LibIheXdsMain.convertDocumentSubmissionRequest(docSubmissionRequest);

return phrServiceClient.documentRepositoryProvideAndRegisterDocumentSetB(
recordIdentifier, provideAndRegisterRequest);
}

public String getHomeCommunityID(String kvnr) {
return phrManagementServiceClient.getHomeCommunityID(kvnr);
}

private SubmissionSetMetadata getSubmissionSetMetadata(Document document) {

var author =
document.documentMetadata().author().stream()
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("no author"));

return new SubmissionSetMetadata(List.of(author), null, LocalDateTime.now(), null, null, null);
}
String getHomeCommunityID(String kvnr);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.oviva.epa.client;

import com.oviva.epa.client.internal.KonnektorServiceImpl;
import com.oviva.epa.client.internal.svc.model.KonnektorContext;
import com.oviva.epa.client.konn.KonnektorConnection;

public class KonnektorServiceBuilder {

private KonnektorConnection connection;
private String workplaceId = "a";
private String mandantId = "m";
private String clientSystemId = "c";
private String userId = "admin";

private KonnektorServiceBuilder() {}

public static KonnektorServiceBuilder newBuilder() {
return new KonnektorServiceBuilder();
}

public KonnektorServiceBuilder connection(KonnektorConnection connection) {
this.connection = connection;
return this;
}

public KonnektorServiceBuilder workplaceId(String workplace) {
this.workplaceId = workplace;
return this;
}

public KonnektorServiceBuilder mandantId(String mandantId) {
this.mandantId = mandantId;
return this;
}

public KonnektorServiceBuilder clientSystemId(String clientSystemId) {
this.clientSystemId = clientSystemId;
return this;
}

public KonnektorServiceBuilder userId(String userId) {
this.userId = userId;
return this;
}

public KonnektorService build() {

if (connection == null) {
throw new IllegalArgumentException("konnektor connection required");
}

if (workplaceId == null) {
throw new IllegalArgumentException("workplaceId required");
}

if (mandantId == null) {
throw new IllegalArgumentException("mandantId required");
}

if (clientSystemId == null) {
throw new IllegalArgumentException("clientSystemId required");
}

var ctx = new KonnektorContext(mandantId, clientSystemId, workplaceId, userId);
return new KonnektorServiceImpl(connection, ctx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.oviva.epa.client.internal;

import com.oviva.epa.client.*;
import com.oviva.epa.client.internal.svc.CardServiceClient;
import com.oviva.epa.client.internal.svc.CertificateServiceClient;
import com.oviva.epa.client.internal.svc.EventServiceClient;
import com.oviva.epa.client.internal.svc.PhrManagementServiceClient;
import com.oviva.epa.client.internal.svc.model.KonnektorContext;
import com.oviva.epa.client.internal.svc.phr.PhrServiceClient;
import com.oviva.epa.client.internal.svc.phr.SmbInformationServiceClient;
import com.oviva.epa.client.internal.svc.phr.model.RecordIdentifierAdapter;
import com.oviva.epa.client.konn.KonnektorConnection;
import com.oviva.epa.client.model.*;
import de.gematik.epa.LibIheXdsMain;
import de.gematik.epa.ihe.model.document.Document;
import de.gematik.epa.ihe.model.request.DocumentSubmissionRequest;
import de.gematik.epa.ihe.model.simple.AuthorInstitution;
import de.gematik.epa.ihe.model.simple.SubmissionSetMetadata;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import oasis.names.tc.ebxml_regrep.xsd.rs._3.RegistryErrorList;
import oasis.names.tc.ebxml_regrep.xsd.rs._3.RegistryResponseType;
import telematik.ws.conn.cardservicecommon.xsd.v2_0.CardTypeType;

public class KonnektorServiceImpl implements KonnektorService {

private static final String STATUS_SUCCESS =
"urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success";
private final EventServiceClient eventServiceClient;
private final CardServiceClient cardServiceClient;
private final PhrManagementServiceClient phrManagementServiceClient;
private final PhrServiceClient phrServiceClient;

private final SmbInformationServiceClient smbInformationServiceClient;

public KonnektorServiceImpl(KonnektorConnection connection, KonnektorContext konnektorContext) {

eventServiceClient = new EventServiceClient(connection.eventService(), konnektorContext);

cardServiceClient =
new CardServiceClient(connection.cardService(), konnektorContext, eventServiceClient);

phrServiceClient = new PhrServiceClient(connection.phrService(), konnektorContext);

phrManagementServiceClient =
new PhrManagementServiceClient(connection.phrManagementService(), konnektorContext);

var certificateServiceClient =
new CertificateServiceClient(connection.certificateService(), konnektorContext);

smbInformationServiceClient =
new SmbInformationServiceClient(eventServiceClient, certificateServiceClient);
}

@Override
public List<AuthorInstitution> getAuthorInstitutions() {
return smbInformationServiceClient.getAuthorInstitutions();
}

@Override
public List<Card> getCardsInfo() {
// don't be fooled by the name, this returns a list of cards...
return eventServiceClient.getSmbInfo().getCards().getCard().stream()
.map(c -> new Card(c.getCardHandle(), c.getCardHolderName(), mapCardType(c.getCardType())))
.toList();
}

private Card.CardType mapCardType(CardTypeType t) {
return switch (t) {
case SMC_B -> Card.CardType.SMC_B;
case SMC_KT -> Card.CardType.SMC_KT;
default -> Card.CardType.UNKNOWN;
};
}

@Override
public PinStatus verifySmcPin(String cardHandle) {
var response = cardServiceClient.getPinStatusResponse(cardHandle, "PIN.SMC");
return PinStatus.valueOf(response.getPinStatus().name());
}

@Override
public WriteDocumentResponse writeDocument(RecordIdentifier recordIdentifier, Document document) {

var phrRecordIdentifier =
new com.oviva.epa.client.internal.svc.phr.model.RecordIdentifier(
recordIdentifier.kvnr(), recordIdentifier.homeCommunityId());
var docSubmissionRequest =
new DocumentSubmissionRequest(
new RecordIdentifierAdapter(phrRecordIdentifier),
List.of(document),
getSubmissionSetMetadata(document));

var provideAndRegisterRequest =
LibIheXdsMain.convertDocumentSubmissionRequest(docSubmissionRequest);

var res =
phrServiceClient.documentRepositoryProvideAndRegisterDocumentSetB(
phrRecordIdentifier, provideAndRegisterRequest);

validateResponse(res);

return new WriteDocumentResponse(res.getRequestId());
}

private void validateResponse(RegistryResponseType res) {

if (STATUS_SUCCESS.equals(res.getStatus())) {
return;
}
var errors =
Optional.ofNullable(res.getRegistryErrorList())
.map(RegistryErrorList::getRegistryError)
.stream()
.flatMap(Collection::stream)
.map(
e ->
new WriteDocumentException.Error(
e.getValue(),
e.getCodeContext(),
e.getErrorCode(),
e.getSeverity(),
e.getLocation()))
.toList();
throw new WriteDocumentException(
"writing document failed, status='%s'".formatted(res.getStatus()), errors);
}

@Override
public String getHomeCommunityID(String kvnr) {
return phrManagementServiceClient.getHomeCommunityID(kvnr);
}

private SubmissionSetMetadata getSubmissionSetMetadata(Document document) {

var author =
document.documentMetadata().author().stream()
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("no author"));

return new SubmissionSetMetadata(List.of(author), null, LocalDateTime.now(), null, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

package com.oviva.epa.client.svc;
package com.oviva.epa.client.internal.svc;

import com.oviva.epa.client.internal.svc.model.KonnektorContext;
import com.oviva.epa.client.konn.internal.util.KonnektorUtils;
import com.oviva.epa.client.svc.model.KonnektorContext;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.math.BigInteger;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

package com.oviva.epa.client.svc;
package com.oviva.epa.client.internal.svc;

import com.oviva.epa.client.svc.model.KonnektorContext;
import com.oviva.epa.client.svc.utils.CertificateUtils;
import com.oviva.epa.client.internal.svc.model.KonnektorContext;
import com.oviva.epa.client.internal.svc.utils.CertificateUtils;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.security.cert.X509Certificate;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

package com.oviva.epa.client.svc;
package com.oviva.epa.client.internal.svc;

import com.oviva.epa.client.svc.model.KonnektorContext;
import com.oviva.epa.client.internal.svc.model.KonnektorContext;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.NoSuchElementException;
import telematik.ws.conn.cardservicecommon.xsd.v2_0.CardTypeType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.oviva.epa.client.svc;
package com.oviva.epa.client.internal.svc;

import com.oviva.epa.client.svc.model.KonnektorContext;
import com.oviva.epa.client.internal.svc.model.KonnektorContext;
import telematik.ws.conn.connectorcontext.xsd.v2_0.ContextType;
import telematik.ws.conn.phrs.phrmanagementservice.wsdl.v2_5.PHRManagementServicePortType;
import telematik.ws.conn.phrs.phrmanagementservice.xsd.v2_5.ObjectFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.oviva.epa.client.svc.model;
package com.oviva.epa.client.internal.svc.model;

import telematik.ws.conn.connectorcontext.xsd.v2_0.ContextType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.oviva.epa.client.svc.phr;
package com.oviva.epa.client.internal.svc.phr;

import com.oviva.epa.client.svc.model.KonnektorContext;
import com.oviva.epa.client.svc.phr.model.RecordIdentifier;
import com.oviva.epa.client.internal.svc.model.KonnektorContext;
import com.oviva.epa.client.internal.svc.phr.model.RecordIdentifier;
import telematik.ws.conn.connectorcontext.xsd.v2_0.ContextType;
import telematik.ws.conn.phrs.phrservice.xsd.v2_0.ContextHeader;
import telematik.ws.fd.phr.phrcommon.xsd.v1_1.InsurantIdType;
Expand Down
Loading

0 comments on commit 23df414

Please sign in to comment.