Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop' into OP-1189
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudFonzam committed Aug 7, 2024
2 parents 548aac6 + 0a5e423 commit e0d6808
Show file tree
Hide file tree
Showing 11 changed files with 774 additions and 177 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/isf/sms/manager/SmsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public List<Sms> getAll(LocalDateTime from, LocalDateTime to) throws OHServiceEx

/**
* Save or Update a {@link Sms}. If the sms's text length is greater than
* {@code MAX_LENGTH} it will throw a {@code testMaxLenghtError} error if
* {@code MAX_LENGTH} it will throw a {@code testMaxLengthError} error if
* {@code split} parameter is set to {@code false}
*
* @param smsToSend - the {@link Sms} to save or update
Expand All @@ -87,12 +87,12 @@ public void saveOrUpdate(Sms smsToSend, boolean split) throws OHServiceException

List<Sms> smsList = new ArrayList<>();
String text = smsToSend.getSmsText();
int textLenght = text.length();
if (textLenght > MAX_LENGTH && !split) {
int textLength = text.length();
if (textLength > MAX_LENGTH && !split) {
throw new OHDataValidationException(
new OHExceptionMessage(MessageBundle.formatMessage("angal.sms.themessageislongerthencharacters.fmt.msg", MAX_LENGTH)));
}
else if (textLenght > MAX_LENGTH && split) {
if (textLength > MAX_LENGTH) {

String[] parts = split(text);
String number = smsToSend.getSmsNumber();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public class SkebbyGatewayService implements SmsSenderInterface {

private static final String SERVICE_NAME = "skebby-gateway-service";
private static final String RESPONSE_SUCCESS = "OK";
private static final String KEY_PASSWORD = "skebby-gateway-service.password";
private static final String KEY_USERNAME = "skebby-gateway-service.username";
private static final String KEY_MESSAGE_TYPE = "skebby-gateway-service.message-type";
protected static final String KEY_PASSWORD = "skebby-gateway-service.password";
protected static final String KEY_USERNAME = "skebby-gateway-service.username";
protected static final String KEY_MESSAGE_TYPE = "skebby-gateway-service.message-type";

private static final String KEY_USER_KEY = "skebby-gateway-service.userKey";
private static final String KEY_ACCESS_TOKEN = "skebby-gateway-service.accessToken";
protected static final String KEY_USER_KEY = "skebby-gateway-service.userKey";
protected static final String KEY_ACCESS_TOKEN = "skebby-gateway-service.accessToken";

private static final String KEY_SENDER = "skebby-gateway-service.sender";

Expand Down Expand Up @@ -82,8 +82,8 @@ public boolean sendSMS(Sms sms) {

SkebbySmsRequest smsSendingRequest = this.skebbyGatewayConverter.toServiceDTO(sms, messageType, sender);

SkebbyGatewayRemoteService httpClient = buildHttlClient();
System.out.println("Sending...");
SkebbyGatewayRemoteService httpClient = buildHttpClient();
LOGGER.info("Sending...");
SkebbySmsResponse result;
try {
if (this.isAccessTokenAuthentication()) {
Expand All @@ -100,7 +100,7 @@ public boolean sendSMS(Sms sms) {
return result != null && RESPONSE_SUCCESS.equals(result.getResult());
}

private SkebbyGatewayRemoteService buildHttlClient() {
private SkebbyGatewayRemoteService buildHttpClient() {
String baseUrl = this.smsProperties.getProperty(this.getRootKey() + ".ribbon.base-url");
// For debug remember to update log level to: feign.Logger.Level.FULL. Happy debugging!
return Feign.builder().encoder(new CustomCommonEncoder()).decoder(new CustomCommonDecoder()).logger(new Slf4jLogger(SkebbyGatewayRemoteService.class))
Expand All @@ -117,15 +117,15 @@ private String loginUserKeySessionKey() {

final String username = this.smsProperties.getProperty(KEY_USERNAME);
final String password = this.smsProperties.getProperty(KEY_PASSWORD);
SkebbyGatewayRemoteService httpClient = buildHttlClient();
SkebbyGatewayRemoteService httpClient = buildHttpClient();
return httpClient.loginUserKeySessionKey(username, password).getBody();
}

private boolean isAccessTokenAuthentication() {
// if user defined these properties, then it means that we will retrieve data with ACCESS_TOKEN (which does not expire -> SESSION_KEY instead expires)
final String userKey = this.smsProperties.getProperty(KEY_USER_KEY);
final String token = this.smsProperties.getProperty(KEY_ACCESS_TOKEN);
return (userKey != null && !userKey.trim().isEmpty() && token != null && !token.trim().isEmpty());
return userKey != null && !userKey.trim().isEmpty() && token != null && !token.trim().isEmpty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void setReturnCredits(Boolean returnCredits) {

@Override
public String toString() {
return "SckebbySmsRequest [messageType=" + messageType + ", message=***" + ", recipient=***" + ", sender=***" + ", scheduledDeliveryTime="
return "SkebbySmsRequest [messageType=" + messageType + ", message=***" + ", recipient=***" + ", sender=***" + ", scheduledDeliveryTime="
+ scheduledDeliveryTime + ", orderId=" + orderId + ", returnCredits=" + returnCredits + ']';
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/isf/sms/service/SmsSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ public SmsSender() {

@Override
public void run() {
SmsOperations smsOp = Context.getApplicationContext().getBean(SmsOperations.class);
SmsSenderOperations sender = Context.getApplicationContext().getBean(SmsSenderOperations.class);
while (running) {
LOGGER.info("SMS Sender running...");
SmsOperations smsOp = Context.getApplicationContext().getBean(SmsOperations.class);
List<Sms> smsList = null;
try {
smsList = smsOp.getList();
} catch (OHServiceException e1) {
LOGGER.error("Error list loading");
}
if (!smsList.isEmpty()) {
if (smsList != null && !smsList.isEmpty()) {
LOGGER.info("Found {} SMS to send", smsList.size());
SmsSenderOperations sender = Context.getApplicationContext().getBean(SmsSenderOperations.class);
if (sender.initialize()) {
for (Sms sms : smsList) {
if (sms.getSmsDateSched().isBefore(TimeTools.getNow())) {
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/org/isf/sms/service/SmsSenderOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ public SmsSenderOperations(Environment smsProperties, List<SmsSenderInterface> s
}

public boolean initialize() {
SmsParameters.initialize();
String gateway = this.smsProperties.getProperty(KEY_SMS_GATEWAY);
if (gateway == null || gateway.isEmpty()) {
LOGGER.error("No HTTP Gateway has been set. Please check sms.properties file (v.2 - init)");
return false;
}
Optional<SmsSenderInterface> smsGatewayOpt = findSmsGatewayService(gateway);
if (smsGatewayOpt.isPresent()) {
return smsGatewayOpt.get().initialize();
}
return false;
return smsGatewayOpt.map(SmsSenderInterface::initialize).orElse(false);
}

public void preSMSSending(Sms sms) {
Expand Down Expand Up @@ -95,10 +93,7 @@ public boolean terminate() {
return false;
}
Optional<SmsSenderInterface> smsGatewayOpt = findSmsGatewayService(gateway);
if (smsGatewayOpt.isPresent()) {
return smsGatewayOpt.get().terminate();
}
return false;
return smsGatewayOpt.map(SmsSenderInterface::terminate).orElse(false);
}

private Optional<SmsSenderInterface> findSmsGatewayService(String gateway) {
Expand Down
147 changes: 4 additions & 143 deletions src/main/java/org/isf/utils/db/DbJpaUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Open Hospital (www.open-hospital.org)
* Copyright © 2006-2023 Informatici Senza Frontiere ([email protected])
* Copyright © 2006-2024 Informatici Senza Frontiere ([email protected])
*
* Open Hospital is a free and open source software for healthcare data management.
*
Expand All @@ -21,18 +21,8 @@
*/
package org.isf.utils.db;

import java.util.List;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.LockTimeoutException;
import jakarta.persistence.NoResultException;
import jakarta.persistence.NonUniqueResultException;
import jakarta.persistence.PersistenceException;
import jakarta.persistence.PessimisticLockException;
import jakarta.persistence.Query;
import jakarta.persistence.QueryTimeoutException;
import jakarta.persistence.TransactionRequiredException;

import org.isf.generaldata.MessageBundle;
import org.isf.menu.manager.Context;
Expand All @@ -41,15 +31,14 @@
import org.slf4j.LoggerFactory;

/**
* Class that executes a query using JPA
* Class that is used to create a DB connection.
*/
public class DbJpaUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(DbJpaUtil.class);

private static EntityManagerFactory entityManagerFactory = Context.getApplicationContext().getBean("entityManagerFactory", EntityManagerFactory.class);
private static EntityManager entityManager;
private static Query query;

/**
* Constructor that initialize the entity Manager
Expand All @@ -58,7 +47,7 @@ public DbJpaUtil() {}

/**
* Constructor that initialize the entity Manager
* @throws OHException
* @throws OHException
*/
public void open() throws OHException {
try {
Expand All @@ -68,140 +57,12 @@ public void open() throws OHException {
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), illegalStateException);
}
}

/**
* @return the entityManager
*/
public EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}


/**
* @return the entityManager
*/
public EntityManager getEntityManager() {
return entityManager;
}

/**
* Method to remove an object
* @throws OHException
*/
public void remove(Object entity) throws OHException {
try {
LOGGER.debug("Remove: {}", entity);
entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
} catch (IllegalArgumentException illegalArgumentException) {
LOGGER.error("IllegalArgumentException", illegalArgumentException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), illegalArgumentException);
} catch (TransactionRequiredException transactionRequiredException) {
LOGGER.error("TransactionRequiredException", transactionRequiredException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), transactionRequiredException);
}
}

/**
* @param parameters
* @param jpql
* @throws OHException
*/
public void setParameters(List<?> parameters, boolean jpql) throws OHException {
try {
for (int i = 0; i < parameters.size(); i++) {
query.setParameter((i + 1), parameters.get(i));
}
} catch (IllegalArgumentException illegalArgumentException) {
LOGGER.error("IllegalArgumentException", illegalArgumentException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), illegalArgumentException);
}
}

/**
* Method that executes a query and returns a list
* @return List of objects
* @throws OHException
*/
public List<?> getList() throws OHException {
List<?> list;

try {
list = query.getResultList();
} catch (IllegalStateException illegalStateException) {
LOGGER.error("IllegalStateException", illegalStateException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), illegalStateException);
} catch (QueryTimeoutException queryTimeoutException) {
LOGGER.error("QueryTimeoutException", queryTimeoutException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), queryTimeoutException);
} catch (TransactionRequiredException transactionRequiredException) {
LOGGER.error("TransactionRequiredException", transactionRequiredException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), transactionRequiredException);
} catch (PessimisticLockException pessimisticLockException) {
LOGGER.error("PessimisticLockException", pessimisticLockException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), pessimisticLockException);
} catch (LockTimeoutException lockTimeoutException) {
LOGGER.error("LockTimeoutException", lockTimeoutException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), lockTimeoutException);
} catch (PersistenceException persistenceException) {
LOGGER.error("PersistenceException", persistenceException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), persistenceException);
} catch (StringIndexOutOfBoundsException stringIndexOutOfBoundsException) {
LOGGER.error("StringIndexOutOfBoundsException", stringIndexOutOfBoundsException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), stringIndexOutOfBoundsException);
}
return list;
}

/**
* Method that executes a query and return an object
* @return Object
* @throws OHException
*/
public Object getResult() throws OHException {
Object result = null;

try {
result = query.getSingleResult();
} catch (NoResultException noResultException) {
LOGGER.error("NoResultException", noResultException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), noResultException);
} catch (NonUniqueResultException nonUniqueResultException) {
LOGGER.error("NonUniqueResultException", nonUniqueResultException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), nonUniqueResultException);
} catch (IllegalStateException illegalStateException) {
LOGGER.error("IllegalStateException", illegalStateException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), illegalStateException);
} catch (QueryTimeoutException queryTimeoutException) {
LOGGER.error("QueryTimeoutException", queryTimeoutException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), queryTimeoutException);
} catch (TransactionRequiredException transactionRequiredException) {
LOGGER.error("TransactionRequiredException", transactionRequiredException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), transactionRequiredException);
} catch (PessimisticLockException pessimisticLockException) {
LOGGER.error("PessimisticLockException", pessimisticLockException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), pessimisticLockException);
} catch (LockTimeoutException lockTimeoutException) {
LOGGER.error("LockTimeoutException", lockTimeoutException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), lockTimeoutException);
} catch (PersistenceException persistenceException) {
LOGGER.error("PersistenceException", persistenceException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), persistenceException);
} catch (Exception exception) {
LOGGER.error("UnknownException", exception);
}
return result;
}

/**
* Method to close the JPA entity manager
* @throws OHException
*/
public void close() throws OHException {
try {
entityManager.close();
} catch (IllegalStateException illegalStateException) {
LOGGER.error("IllegalStateException", illegalStateException);
throw new OHException(MessageBundle.getMessage("angal.sql.problemsoccurredwiththesqlinstruction.msg"), illegalStateException);
}
}

}
Loading

0 comments on commit e0d6808

Please sign in to comment.