From 0d786d45556110f9128e39aa5d107419b1fb3eed Mon Sep 17 00:00:00 2001 From: Mahesh Maney R <mahesh-maney@users.noreply.github.com> Date: Thu, 14 Sep 2023 18:16:03 +0530 Subject: [PATCH] moving this feature sunbird-rc <ManeyMR>. --- .../controller/UserController.java | 5 +- .../userManagement/handler/UserHandler.java | 9 +- .../model/UserAttributeModel.java | 25 ----- .../repository/UserAttributeRepository.java | 16 ---- .../userManagement/services/UserService.java | 93 ------------------- 5 files changed, 2 insertions(+), 146 deletions(-) delete mode 100644 src/main/java/com/tarento/upsmf/userManagement/model/UserAttributeModel.java delete mode 100644 src/main/java/com/tarento/upsmf/userManagement/repository/UserAttributeRepository.java diff --git a/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java b/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java index 50729c7..e7b0781 100644 --- a/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java +++ b/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java @@ -104,8 +104,5 @@ public class UserController { return userHandler.getTransactionByUniqueRefNumber(uniqueRefNumber); } - @PostMapping(value = "/attribute", produces = "application/json") - public List getUserByAttribute(@RequestBody JsonNode body) throws SQLException { - return userHandler.getUserByAttribute(body); - } + } diff --git a/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java b/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java index f46cbf2..fa96839 100644 --- a/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java +++ b/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java @@ -167,12 +167,5 @@ public class UserHandler { return userService.getTransactionByUniqueRefNumber(uniqueRefNumber); } - public List getUserByAttribute(JsonNode body) throws SQLException { - String fieldName = body.get("fieldName").asText(); - String fieldValue = body.get("fieldValue").asText(); - int offset = body.get("offset").asInt(); - int limit = body.get("limit").asInt(); - logger.info("Fetching user info by field {} and value {} with offset {} and limit {}",fieldName, fieldValue, offset, limit); - return userService.getUserListByAttribute(fieldName,fieldValue, offset, limit); - } + } diff --git a/src/main/java/com/tarento/upsmf/userManagement/model/UserAttributeModel.java b/src/main/java/com/tarento/upsmf/userManagement/model/UserAttributeModel.java deleted file mode 100644 index 5a4e38e..0000000 --- a/src/main/java/com/tarento/upsmf/userManagement/model/UserAttributeModel.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.tarento.upsmf.userManagement.model; - -import lombok.*; - -import javax.persistence.*; - -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder -@ToString -@Entity -@Table(name = "user_attribute") -public class UserAttributeModel { - - @Column(name="name") - private String name; - @Column(name="value") - private String value; - @Column(name="user_id") - private String userId; - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private String id; -} diff --git a/src/main/java/com/tarento/upsmf/userManagement/repository/UserAttributeRepository.java b/src/main/java/com/tarento/upsmf/userManagement/repository/UserAttributeRepository.java deleted file mode 100644 index 4bddafd..0000000 --- a/src/main/java/com/tarento/upsmf/userManagement/repository/UserAttributeRepository.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.tarento.upsmf.userManagement.repository; - -import com.tarento.upsmf.userManagement.model.UserAttributeModel; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface UserAttributeRepository extends JpaRepository<UserAttributeModel,String> { - @Query(value = "SELECT * FROM user_attribute WHERE name=:fieldName and value=:fieldValue OFFSET :offset LIMIT :limit",nativeQuery = true) - List<UserAttributeModel> findUserByAttribute(@Param("fieldName") String fieldName, @Param("fieldValue") String fieldValue, - @Param("offset") int offset, @Param("limit") int limit); -} diff --git a/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java b/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java index 60f89ef..87fdfa5 100644 --- a/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java +++ b/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java @@ -2,16 +2,13 @@ package com.tarento.upsmf.userManagement.services; import com.fasterxml.jackson.databind.JsonNode; import com.tarento.upsmf.userManagement.model.Transaction; -import com.tarento.upsmf.userManagement.model.UserAttributeModel; import com.tarento.upsmf.userManagement.repository.TransactionRepository; -import com.tarento.upsmf.userManagement.repository.UserAttributeRepository; import com.tarento.upsmf.userManagement.utility.KeycloakTokenRetriever; import com.tarento.upsmf.userManagement.utility.KeycloakUserCount; import com.tarento.upsmf.userManagement.utility.KeycloakUserCredentialPersister; import com.tarento.upsmf.userManagement.utility.SunbirdRCKeycloakTokenRetriever; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.HttpClientBuilder; -import org.keycloak.representations.idm.UserRepresentation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -19,7 +16,6 @@ import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.http.*; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -29,11 +25,7 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.*; -import java.util.stream.Collectors; @Component @PropertySource({ "classpath:application.properties" }) @@ -59,12 +51,6 @@ public class UserService { @Autowired private TransactionRepository transactionRepository; - @Autowired - private UserAttributeRepository userAttributeRepository; - - @Autowired - private JdbcTemplate jdbcTemplate; - @Autowired private KeycloakUserCount keycloakUserCount; @@ -243,85 +229,6 @@ public class UserService { return new ResponseEntity<>(transaction, HttpStatus.OK); } - public List<UserAttributeModel> getUserByAttribute(String fieldName, String fieldValue, int offset, int limit) { - return userAttributeRepository.findUserByAttribute(fieldName, fieldValue,offset,limit); - } - - public List getUserListByAttribute(String fieldName, String fieldValue, int offset, int limit) throws SQLException { - - List<UserAttributeModel> userByAttribute = getUserByAttribute(fieldName, fieldValue,offset,limit); - if(userByAttribute == null || userByAttribute.isEmpty()){ - logger.info("No records found."); - return Collections.EMPTY_LIST; - } - logger.info("Records found {}",userByAttribute); - List<String> collect = userByAttribute.stream().map(UserAttributeModel::getUserId).collect(Collectors.toList()); - Map<String, UserRepresentation> userRepresentationMap = getStringUserRepresentationMap(collect); - if(userRepresentationMap.isEmpty()){ - logger.info("No UserRepresentation records found for {}",collect); - return Collections.EMPTY_LIST; - } - return new ArrayList<>(userRepresentationMap.values()); - } - - private Map<String, UserRepresentation> getStringUserRepresentationMap(List<String> collect) throws SQLException { - Connection connection = Objects.requireNonNull(jdbcTemplate.getDataSource()).getConnection(); - String formattedString = getFormattedStringFromCollection(collect); - Map<String, UserRepresentation> userRepresentationMap = new HashMap<>(); - ResultSet resultSet = null; - PreparedStatement preparedStatement = null; - try { - preparedStatement = connection.prepareStatement(formattedString); - resultSet = preparedStatement.executeQuery(); - if (resultSet != null) { - while (resultSet.next()) { - String id = resultSet.getString("id"); - UserRepresentation userRepresentation = null; - if (userRepresentationMap.containsKey(id)) { - userRepresentation = userRepresentationMap.get(id); - userRepresentation.singleAttribute(resultSet.getString("name"), resultSet.getString("value")); - } else { - userRepresentation = new UserRepresentation(); - userRepresentation.setId(id); - userRepresentation.setUsername(resultSet.getString("username")); - userRepresentation.setEnabled(resultSet.getBoolean("enabled")); - userRepresentation.setEmail(resultSet.getString("email")); - userRepresentation.setFirstName(resultSet.getString("first_name")); - userRepresentation.setLastName(resultSet.getString("last_name")); - userRepresentation.singleAttribute(resultSet.getString("name"), resultSet.getString("value")); - userRepresentationMap.put(id, userRepresentation); - } - } - } - } catch (Exception exception){ - logger.error("Exception while processing data from DB.",exception); - } finally { - if(resultSet != null){ - resultSet.close(); - } - if(preparedStatement != null){ - preparedStatement.close(); - } - if(connection != null){ - connection.close(); - } - } - logger.info("userRepresentationMap {}",userRepresentationMap); - return userRepresentationMap; - } - - private String getFormattedStringFromCollection(List<String> collect) { - StringBuffer sbf = new StringBuffer(); - sbf.append("select ue.*,ua.name,ua.value from user_entity ue join user_attribute ua on ua.user_id = ue.id WHERE ue.id IN ("); - collect.stream().forEach(item -> { - sbf.append("'" + item + "'"); - sbf.append(","); - }); - String substring = sbf.substring(0, sbf.lastIndexOf(",")); - substring = substring + (" )"); - logger.info("Query to be Executed {}",substring); - return substring; - } } \ No newline at end of file -- GitLab