From e9400a0888007157687d574fe8e95a843bed6cd4 Mon Sep 17 00:00:00 2001 From: Kaveesha Date: Sun, 30 Aug 2020 22:44:24 +0530 Subject: [PATCH] Fix QR generating issue in Cloud App --- .../core/otp/mgt/dao/OTPManagementDAO.java | 7 ++-- .../dao/impl/GenericOTPManagementDAOImpl.java | 37 +++++++++---------- .../mgt/service/OTPManagementServiceImpl.java | 33 +++++++++++++---- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/dao/OTPManagementDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/dao/OTPManagementDAO.java index 4c3690f5d0..44bb2a3064 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/dao/OTPManagementDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/dao/OTPManagementDAO.java @@ -20,15 +20,16 @@ package org.wso2.carbon.device.mgt.core.otp.mgt.dao; import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO; import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException; +import java.util.List; + public interface OTPManagementDAO { /** * Save OTP token data and tenant details of registering user - * @param oneTimePinDTO OTPMailDTO - * @return Primary key of the newly adding data raw + * @param oneTimePinDTOList OTPMailDTO * @throws OTPManagementDAOException if error occurred whule storing data */ - int addOTPData(OneTimePinDTO oneTimePinDTO) throws OTPManagementDAOException; + void addOTPData(List oneTimePinDTOList) throws OTPManagementDAOException; /** * Get OTP data for requesting One Time Token diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/dao/impl/GenericOTPManagementDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/dao/impl/GenericOTPManagementDAOImpl.java index 273cf6c1a5..58b55197a0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/dao/impl/GenericOTPManagementDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/dao/impl/GenericOTPManagementDAOImpl.java @@ -32,17 +32,20 @@ import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.util.Calendar; +import java.util.List; public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPManagementDAO { private static final Log log = LogFactory.getLog(GenericOTPManagementDAOImpl.class); @Override - public int addOTPData(OneTimePinDTO oneTimePinDTO) throws OTPManagementDAOException { + public void addOTPData(List oneTimePinDTOList) throws OTPManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to create an OTP data entry"); log.debug("OTP Details : "); - log.debug("OTP key : " + oneTimePinDTO.getOtpToken() + " Email : " + oneTimePinDTO.getEmail()); + for(OneTimePinDTO oneTimePinDTO: oneTimePinDTOList){ + log.debug("OTP key : " + oneTimePinDTO.getOtpToken() + " Email : " + oneTimePinDTO.getEmail()); + } } String sql = "INSERT INTO DM_OTP_DATA " @@ -57,29 +60,25 @@ public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPM Connection conn = this.getDBConnection(); Calendar calendar = Calendar.getInstance(); Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); - try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { - stmt.setString(1, oneTimePinDTO.getOtpToken()); - stmt.setString(2, oneTimePinDTO.getEmail()); - stmt.setString(3, oneTimePinDTO.getEmailType()); - stmt.setString(4, oneTimePinDTO.getMetaInfo()); - stmt.setTimestamp(5, timestamp); - stmt.setInt(6, oneTimePinDTO.getTenantId()); - stmt.setString(7, oneTimePinDTO.getUsername()); - stmt.executeUpdate(); - try (ResultSet rs = stmt.getGeneratedKeys()) { - if (rs.next()) { - return rs.getInt(1); - } - return -1; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (OneTimePinDTO oneTimePinDTO : oneTimePinDTOList) { + stmt.setString(1, oneTimePinDTO.getOtpToken()); + stmt.setString(2, oneTimePinDTO.getEmail()); + stmt.setString(3, oneTimePinDTO.getEmailType()); + stmt.setString(4, oneTimePinDTO.getMetaInfo()); + stmt.setTimestamp(5, timestamp); + stmt.setInt(6, oneTimePinDTO.getTenantId()); + stmt.setString(7, oneTimePinDTO.getUsername()); + stmt.addBatch(); } + stmt.executeBatch(); } } catch (DBConnectionException e) { - String msg = "Error occurred while obtaining the DB connection to create an opt entry for email " - + oneTimePinDTO.getEmail(); + String msg = "Error occurred while obtaining the DB connection to create an opt entry."; log.error(msg, e); throw new OTPManagementDAOException(msg, e); } catch (SQLException e) { - String msg = "Error occurred while executing SQL to create an otp entry for email " + oneTimePinDTO.getEmail(); + String msg = "Error occurred while executing SQL to create an otp entry"; log.error(msg, e); throw new OTPManagementDAOException(msg, e); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/service/OTPManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/service/OTPManagementServiceImpl.java index 9b25987e84..c80502ec4f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/service/OTPManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/otp/mgt/service/OTPManagementServiceImpl.java @@ -61,6 +61,8 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.UUID; +import java.util.ArrayList; +import java.util.Collections; public class OTPManagementServiceImpl implements OTPManagementService { @@ -82,12 +84,7 @@ public class OTPManagementServiceImpl implements OTPManagementService { otpWrapper.getUsername(), tenant, -1234); try { ConnectionManagerUtil.beginDBTransaction(); - if (this.otpManagementDAO.addOTPData(oneTimePinDTO) == -1) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "OTP data saving failed. Please, contact Administrator"; - log.error(msg); - throw new OTPManagementException(msg); - } + this.otpManagementDAO.addOTPData(Collections.singletonList(oneTimePinDTO)); Properties props = new Properties(); props.setProperty("first-name", tenant.getAdminFirstName()); props.setProperty("otp-token", oneTimePinDTO.getOtpToken()); @@ -98,12 +95,12 @@ public class OTPManagementServiceImpl implements OTPManagementService { log.error(msg, e); throw new OTPManagementException(msg, e); } catch (DBConnectionException e) { - String msg = "Error occurred while getting database connection."; + String msg = "Error occurred while getting database connection to add OPT data."; log.error(msg, e); throw new OTPManagementException(msg, e); } catch (OTPManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while saving the OTP data. Email address: " + oneTimePinDTO.getEmail(); + String msg = "Error occurred while saving the OTP data for given email" ; log.error(msg, e); throw new OTPManagementException(msg, e); } finally { @@ -219,24 +216,44 @@ public class OTPManagementServiceImpl implements OTPManagementService { } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); OneTimePinDTO oneTimePinDTO; + List oneTimePinDTOList = new ArrayList<>(); Properties props = new Properties(); props.setProperty("enrollment-steps", enrollmentSteps.toString()); try { + ConnectionManagerUtil.beginDBTransaction(); for (String username : deviceEnrollmentInvitation.getUsernames()) { String emailAddress = DeviceManagerUtil.getUserClaimValue( username, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS); oneTimePinDTO = createOneTimePin(emailAddress, OTPEmailTypes.DEVICE_ENROLLMENT.toString(), username, null, tenantId); + oneTimePinDTOList.add(oneTimePinDTO); props.setProperty("first-name", DeviceManagerUtil. getUserClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME)); props.setProperty("username", username); props.setProperty("otp-token", oneTimePinDTO.getOtpToken()); sendMail(props, emailAddress, DeviceManagementConstants.EmailAttributes.USER_ENROLLMENT_TEMPLATE); } + this.otpManagementDAO.addOTPData(oneTimePinDTOList); + ConnectionManagerUtil.commitDBTransaction(); } catch (UserStoreException e) { String msg = "Error occurred while getting claim values to invite user"; log.error(msg, e); throw new OTPManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while getting database connection to add OPT data."; + log.error(msg, e); + throw new OTPManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "SQL Error occurred when adding OPT data to send device enrollment Invitation."; + log.error(msg, e); + throw new OTPManagementException(msg, e); + } catch (OTPManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while saving the OTP data."; + log.error(msg, e); + throw new OTPManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } }