Skip to content

Commit

Permalink
MOSIP-29472 Create API dynamicfields/all. (#897)
Browse files Browse the repository at this point in the history
* MOSIP-29472 Create API dynamicfields/all.

* MOSIP-29472 Create API dynamicfields/all.

* MOSIP-29472 Create API dynamicfields/all.

* MOSIP-29472 Covered Junit for Dynamic field all API.

* MOSIP-29472 Covered Junit for Dynamic field all API.

* MOSIP-29472 Changed logic as per review comment.

* MOSIP-29472 Changed logic as per review comment.

* MOSIP-29472 Changed logic as per review comment.
  • Loading branch information
kameshsr authored Sep 21, 2023
1 parent cc90a4b commit 300d300
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;

import javax.validation.Valid;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
Expand Down Expand Up @@ -39,7 +38,6 @@
import io.mosip.kernel.masterdata.dto.response.FilterResponseCodeDto;
import io.mosip.kernel.masterdata.dto.response.FilterResponseDto;
import io.mosip.kernel.masterdata.dto.response.PageResponseDto;
import io.mosip.kernel.masterdata.entity.DynamicField;
import io.mosip.kernel.masterdata.service.DynamicFieldService;
import io.mosip.kernel.masterdata.service.GenericService;
import io.mosip.kernel.masterdata.utils.AuditUtil;
Expand Down Expand Up @@ -93,6 +91,16 @@ public ResponseWrapper<DynamicFieldConsolidateResponseDto> getDynamicFieldByName
responseWrapper.setResponse(dynamicFieldService.getDynamicFieldByNameAndLangcode(fieldName,langCode,withValue));
return responseWrapper;
}

@ResponseFilter
@GetMapping("/all/{fieldName}")
@ApiOperation(value = " Service to fetch one dynamic field in all the languages")
public ResponseWrapper<List<DynamicFieldExtnDto>> getAllDynamicFieldByName(
@PathVariable("fieldName") String fieldName){
ResponseWrapper<List<DynamicFieldExtnDto>> responseWrapper = new ResponseWrapper<>();
responseWrapper.setResponse(dynamicFieldService.getAllDynamicFieldByName(fieldName));
return responseWrapper;
}


@ResponseFilter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import org.json.JSONArray;

import java.time.LocalDateTime;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ int updateDynamicField(String id, String description, String langCode, String da

/**
* update isDeleted as true
* @param id
* @param updatedDateTime
* @param updatedBy
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.mosip.kernel.masterdata.dto.request.SearchDto;
import io.mosip.kernel.masterdata.dto.response.FilterResponseCodeDto;
import io.mosip.kernel.masterdata.dto.response.PageResponseDto;
import io.mosip.kernel.masterdata.entity.DynamicField;

/**
* Methods to create / update / inactivate / addValues dynamic field
Expand Down Expand Up @@ -83,4 +82,5 @@ public PageDto<DynamicFieldExtnDto> getAllDynamicField(int pageNumber, int pageS
public DynamicFieldConsolidateResponseDto getDynamicFieldByNameAndLangcode(String fieldName,String langCode,boolean withValue);


List<DynamicFieldExtnDto> getAllDynamicFieldByName(String fieldName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import javax.transaction.Transactional;

import org.json.JSONArray;
import org.json.JSONException;
import io.mosip.kernel.masterdata.dto.response.FilterResult;

Expand All @@ -31,10 +30,7 @@
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down Expand Up @@ -75,17 +71,17 @@

@Service
public class DynamicFieldServiceImpl implements DynamicFieldService {

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

private final ObjectMapper objectMapper = new ObjectMapper();

@Autowired
private DynamicFieldRepository dynamicFieldRepository;

@Autowired
private MasterdataCreationUtil masterdataCreationUtil;

@Autowired
private MasterdataSearchHelper masterdataSearchHelper;

Expand All @@ -103,11 +99,11 @@ public class DynamicFieldServiceImpl implements DynamicFieldService {

@Autowired
AuditUtil auditUtil;


/*
* (non-Javadoc)
*
*
* @see
* io.mosip.kernel.masterdata.service.DynamicFieldService#getAllDynamicField()
*/
Expand All @@ -116,18 +112,18 @@ public class DynamicFieldServiceImpl implements DynamicFieldService {
condition="#langCode != null")
@Override
public PageDto<DynamicFieldExtnDto> getAllDynamicField(int pageNumber, int pageSize, String sortBy, String orderBy, String langCode,
LocalDateTime lastUpdated, LocalDateTime currentTimestamp) {
LocalDateTime lastUpdated, LocalDateTime currentTimestamp) {
Page<Object[]> pagedResult = null;

if (lastUpdated == null) {
lastUpdated = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC);
}
try {
PageRequest pageRequest = PageRequest.of(pageNumber, pageSize, Sort.by(Direction.fromString(orderBy), sortBy));

PageRequest pageRequest = PageRequest.of(pageNumber, pageSize, Sort.by(Direction.fromString(orderBy), sortBy));
pagedResult = langCode == null ? dynamicFieldRepository.findAllLatestDynamicFieldNames(lastUpdated, currentTimestamp, pageRequest) :
dynamicFieldRepository.findAllLatestDynamicFieldNamesByLangCode(langCode,lastUpdated, currentTimestamp, pageRequest);
dynamicFieldRepository.findAllLatestDynamicFieldNamesByLangCode(langCode,lastUpdated, currentTimestamp, pageRequest);

} catch (DataAccessException | DataAccessLayerException e) {
throw new MasterDataServiceException(SchemaErrorCode.DYNAMIC_FIELD_FETCH_EXCEPTION.getErrorCode(),
SchemaErrorCode.DYNAMIC_FIELD_FETCH_EXCEPTION.getErrorMessage() + " "
Expand All @@ -153,11 +149,11 @@ public PageDto<DynamicFieldExtnDto> getAllDynamicField(int pageNumber, int pageS
list.add(getDynamicFieldDto(groupedValues.get(lang)));
}
});

pagedFields.setPageNo(pagedResult.getNumber());
pagedFields.setTotalPages(pagedResult.getTotalPages());
pagedFields.setTotalItems(pagedResult.getTotalElements());
}
}
return pagedFields;
}

Expand Down Expand Up @@ -208,7 +204,7 @@ public List<DynamicFieldDefDto> getDistinctDynamicFields(String langCode) {

/*
* (non-Javadoc)
*
*
* @see
* io.mosip.kernel.masterdata.service.DynamicFieldService#createDynamicField()
*/
Expand Down Expand Up @@ -245,7 +241,7 @@ public DynamicFieldResponseDto createDynamicField(DynamicFieldDto dto) {

/*
* (non-Javadoc)
*
*
* @see
* io.mosip.kernel.masterdata.service.DynamicFieldService#updateDynamicField()
*/
Expand All @@ -258,10 +254,10 @@ public DynamicFieldResponseDto updateDynamicField(String id, DynamicFieldPutDto

String valueJson = getValidatedFieldValue(dto.getFieldVal());

int updatedRows = dynamicFieldRepository.updateDynamicField(id, dto.getDescription(), dto.getLangCode(),
int updatedRows = dynamicFieldRepository.updateDynamicField(id, dto.getDescription(), dto.getLangCode(),
dto.getDataType(), MetaDataUtils.getCurrentDateTime(), MetaDataUtils.getContextUser(),
valueJson);

if (updatedRows < 1) {
throw new DataNotFoundException(SchemaErrorCode.DYNAMIC_FIELD_NOT_FOUND_EXCEPTION.getErrorCode(),
SchemaErrorCode.DYNAMIC_FIELD_NOT_FOUND_EXCEPTION.getErrorMessage());
Expand All @@ -285,14 +281,14 @@ public StatusResponseDto deleteDynamicFieldValue(String id) {
if(dynamicField == null)
throw new DataNotFoundException(SchemaErrorCode.DYNAMIC_FIELD_NOT_FOUND_EXCEPTION.getErrorCode(),
SchemaErrorCode.DYNAMIC_FIELD_NOT_FOUND_EXCEPTION.getErrorMessage());
if(dynamicField.getValueJson()==null)
throw new DataNotFoundException(SchemaErrorCode.DYNAMIC_FIELD_VALUE_NOT_FOUND_EXCEPTION.getErrorCode(),
if(dynamicField.getValueJson()==null)
throw new DataNotFoundException(SchemaErrorCode.DYNAMIC_FIELD_VALUE_NOT_FOUND_EXCEPTION.getErrorCode(),
SchemaErrorCode.DYNAMIC_FIELD_VALUE_NOT_FOUND_EXCEPTION.getErrorMessage());
JsonNode valueJson =objectMapper.readTree(dynamicField.getValueJson());
String code = valueJson.get("code").toString();



int deletedRows = dynamicFieldRepository.deleteDynamicField(dynamicField.getName(), "%"+code+"%",
MetaDataUtils.getCurrentDateTime(), MetaDataUtils.getContextUser());

Expand Down Expand Up @@ -539,4 +535,29 @@ public DynamicFieldConsolidateResponseDto getDynamicFieldByNameAndLangcode(Strin
}


}
@Override
@Cacheable(value = "dynamic-field", key = "'dynamicfield'.concat('-').concat(#fieldName)")
public List<DynamicFieldExtnDto> getAllDynamicFieldByName(String fieldName) {
List<DynamicField> fields = null;
try {
fields = dynamicFieldRepository.findAllDynamicFieldByName(fieldName);
} catch (DataAccessException | DataAccessLayerException e) {
throw new MasterDataServiceException(SchemaErrorCode.DYNAMIC_FIELD_FETCH_EXCEPTION.getErrorCode(),
SchemaErrorCode.DYNAMIC_FIELD_FETCH_EXCEPTION.getErrorMessage() + " "
+ ExceptionUtils.parseException(e));
}
List<DynamicFieldExtnDto> list = new ArrayList<>();
if(fields != null && !fields.isEmpty()) {
Map<String, List<DynamicField>> groupedValues = fields
.stream()
.collect(Collectors.groupingBy(DynamicField::getLangCode));
list = groupedValues.keySet()
.stream()
.map(lang -> getDynamicFieldDto(groupedValues.get(lang)))
.collect(Collectors.toList());

}
return list;
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package io.mosip.kernel.masterdata.test.controller;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.mosip.kernel.masterdata.dto.request.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.mosip.kernel.core.http.RequestWrapper;
import io.mosip.kernel.core.websub.model.EventModel;
import io.mosip.kernel.core.websub.spi.PublisherClient;
import io.mosip.kernel.masterdata.dto.DynamicFieldDto;
import io.mosip.kernel.masterdata.dto.DynamicFieldPutDto;
import io.mosip.kernel.masterdata.dto.request.FilterDto;
import io.mosip.kernel.masterdata.dto.request.FilterValueDto;
import io.mosip.kernel.masterdata.dto.request.Pagination;
import io.mosip.kernel.masterdata.dto.request.SearchDto;
import io.mosip.kernel.masterdata.dto.request.SearchSort;
import io.mosip.kernel.masterdata.test.TestBootApplication;
import io.mosip.kernel.masterdata.test.utils.MasterDataTest;
import io.mosip.kernel.masterdata.utils.AuditUtil;
import io.mosip.kernel.masterdata.validator.FilterColumnEnum;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
Expand All @@ -24,18 +33,8 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import io.mosip.kernel.core.http.RequestWrapper;
import io.mosip.kernel.core.websub.model.EventModel;
import io.mosip.kernel.core.websub.spi.PublisherClient;
import io.mosip.kernel.masterdata.dto.DynamicFieldDto;
import io.mosip.kernel.masterdata.dto.DynamicFieldPutDto;
import io.mosip.kernel.masterdata.test.TestBootApplication;
import io.mosip.kernel.masterdata.test.utils.MasterDataTest;
import io.mosip.kernel.masterdata.utils.AuditUtil;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestBootApplication.class)
Expand Down Expand Up @@ -418,4 +417,12 @@ public void t022getDynamicFieldByNameTest2() throws Exception {
"KER-SCH-003");
}

@Test
@WithUserDetails("global-admin")
public void t000getAllDynamicFieldByNameTest() throws Exception {
MasterDataTest.checkResponse(
mockMvc.perform(MockMvcRequestBuilders.get("/dynamicfields/all/gender")).andReturn(),
null);
}

}
Loading

0 comments on commit 300d300

Please sign in to comment.