+ private RecipientId resolveRecipientLocked(
+ Connection connection, RecipientAddress address
+ ) throws SQLException {
+ final var byServiceId = address.serviceId().isEmpty()
+ ? Optional.<RecipientWithAddress>empty()
+ : findByServiceId(connection, address.serviceId().get());
+
+ if (byServiceId.isPresent()) {
+ return byServiceId.get().id();
+ }
+
+ final var byPni = address.pni().isEmpty()
+ ? Optional.<RecipientWithAddress>empty()
+ : findByServiceId(connection, address.pni().get());
+
+ if (byPni.isPresent()) {
+ return byPni.get().id();
+ }
+
+ final var byNumber = address.number().isEmpty()
+ ? Optional.<RecipientWithAddress>empty()
+ : findByNumber(connection, address.number().get());
+
+ if (byNumber.isPresent()) {
+ return byNumber.get().id();
+ }
+
+ logger.debug("Got new recipient, both serviceId and number are unknown");
+
+ if (address.serviceId().isEmpty()) {
+ return addNewRecipient(connection, address);
+ }
+
+ return addNewRecipient(connection, new RecipientAddress(address.serviceId().get()));
+ }
+
+ private RecipientId resolveRecipientLocked(Connection connection, ServiceId serviceId) throws SQLException {
+ final var recipient = findByServiceId(connection, serviceId);
+
+ if (recipient.isEmpty()) {
+ logger.debug("Got new recipient, serviceId is unknown");
+ return addNewRecipient(connection, new RecipientAddress(serviceId));
+ }
+
+ return recipient.get().id();
+ }
+
+ private RecipientId resolveRecipientLocked(Connection connection, String number) throws SQLException {
+ final var recipient = findByNumber(connection, number);
+
+ if (recipient.isEmpty()) {
+ logger.debug("Got new recipient, number is unknown");
+ return addNewRecipient(connection, new RecipientAddress(null, number));
+ }
+
+ return recipient.get().id();
+ }
+