I have a mutation which accepts WorkspaceDTO(POJO):
public WorkspaceDTO createWorkspace(WorkspaceDTO workspaceDTO) {
Workspace workspace = workspaceMapper.toEntity(workspaceDTO);
Workspace newWorkspace = workspaceService.save(workspace);
return workspaceMapper.toDTO(newWorkspace);
}
WorkspaceDTO
public class WorkspaceDTO implements Serializable {
private String key;
private Map<DomainDTO, Map<String, Map<String, EntityDTO>>> entities;
}
public enum DomainDTO {
DOMAIN_BUILDER
}
public class EntityDTO {
private String key;
private String id;
private EntityNumberDTO entityNumber;
private String defPackageKey;
}
public enum EntityNumberDTO {
Entity(243L),
Entity_VERSION(244L);
private final Long id;
}
GraphQLSchema
createWorkspace(newWorkspace: WorkspaceInput!): Workspace!
input WorkspaceInput{
key: String
### How to add Map<Domain, Map<String, Map<String, Entity>>>
}
How to generate graphql schema (Map<DomainDTO, Map<String, Map<String, EntityDTO>>> entities) in Workspace Input so that client can make the request.
Related
The browser sends the following object to the backend:
Now I would like to store the data in my database. So I have an entity that looks like this:
#Entity
public class NewQuote {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String description;
#ElementCollection(targetClass = Details.class)
private List<Details> details = new ArrayList<>();
public NewQuote(String description, ArrayList<Details> details) {
this.description = description;
this.details = details;
}
#Embeddable
public class Details {
private String description;
private String label;
public Details(String description, String label) {
this.description = description;
this.label = label;
}
public Details() {}
}
Controller
#PostMapping(value = "/save-quote", produces = MediaType.APPLICATION_JSON_VALUE)
public void saveQuote(#RequestBody Map<String, String> data) {
newQuoteService.saveQuote(data);
}
Service
public void saveQuote(Map<String, String> data) {
JSONObject json = new JSONObject(data);
NewQuote newQuote = new NewQuote(
json.getAsString("description"),
json.getAsString("details")
);
newQuoteRepository.save(newQuote);
}
I am getting an error because json.getAsString("details") should not be a string of course. So how can I turn it to ArrayList<Details>?
Add a DTO to manage your json response. You don't need to explicitly use JSONObject because spring already manage the process of mapping under the wood with Jackson.
Also, it is not a good practice to pass your Entities directly into the controller methods.
NewQuoteDto.java
public class NewQuoteDto {
private Long id;
private String description;
private List<Details> details = new ArrayList<>();
public NewQuoteDto() {
}
// getter and setter or you can use lombok
}
DetailDto.java
public class DetailDto {
private String description;
private String label;
public DetailDto() {}
// getter and setter or you can use lombok
}
Controller
#PostMapping(value = "/save-quote", produces = MediaType.APPLICATION_JSON_VALUE)
public void saveQuote(#RequestBody NewQuoteDto dataDto) {
// here you map dataDto to your model before you call saveQuote
NewQuote data = mapper.map(dataDto, NewQuote.class); // if you use ModelMapper library.
newQuoteService.saveQuote(data);
}
For custom mapping take look here.
I have a structure that looks like this for RechargeResponse Model:
public class RechargeResponse {
private String code;
private String status;
private Set<OperatorWiseCircles> payload;
// setter and getters
}
here is the OperatorWiseCircles Model
public class OperatorWiseCircles {
private String operatorName;
private String operatorId;
private List<CircleWisePlan> circleWisePlanLists;
//setter and getters
}
CircleWisePlan Model class
public class CircleWisePlan {
private String circleName;
private String circleId;
}
Below is the sample json which we need to flattern.
{
"code": 200,
"status": "SUCCESS",
"payload": [
{
"operatorName": "VODAFONE",
"operatorId": "VF",
"circleWisePlanLists": [
{
"circleName": "C1",
"circleId": "1"
},
{
"circleName": "C2",
"circleId": "2"
}
]
}
]
}
I am expecting this to be flattern and map it to Entity object, so that I can add all these iteration to Hashset and save them all to DB, I want to do it using java8 stream. I how can I do it efficiently. I didnt get the right example, to parse nested json values and create entities for it using map/ flatmap.
Result should be like
Eg: ["VODAFONE","VF","C1", "1"]--->
ENTRY1
["VODAFONE","VF","C2", "2"] ---> ENTRY2
#Entity
public class RechargePlanEntity extends Audit<String>{
#Id
#Column(name="id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name="operator_name")
private String operatorName;
#Column(name="operator_id")
private String operatorId;
#Column(name="circle_name")
private String circleName;
#Column(name="circle_id")
private String circleId;
}
Truth is I'm sure is there any easy way to do this, Yet you can follow something like this,
Here in this example I have created utility class to map OperatorWiseCircles class to List<RechargePlanEntity>.
public class Main {
public static void main(String[] args) throws IOException {
String s = "{\"code\":200,\"status\":\"SUCCESS\",\"payload\":[{\"operatorName\":\"VODAFONE\",\"operatorId\":\"VF\",\"circleWisePlanLists\":[{\"circleName\":\"C1\",\"circleId\":\"1\"},{\"circleName\":\"C2\",\"circleId\":\"2\"}]}]}";
ObjectMapper om = new ObjectMapper();
RechargeResponse response = om.readValue(s, RechargeResponse.class);
List<RechargePlanEntity> collection = response.getPayload()
.stream()
.map(MapUtil::toEntity)
.flatMap(Collection::stream)
.collect(Collectors.toList());
System.out.println(collection);
}
}
#Getter
#Setter
#ToString
class RechargePlanEntity {
private Long id;
private String operatorName;
private String operatorId;
private String circleName;
private String circleId;
}
#Getter
#Setter
class RechargeResponse {
private String code;
private String status;
private Set<OperatorWiseCircles> payload;
}
#Getter
#Setter
class OperatorWiseCircles {
private String operatorName;
private String operatorId;
private List<CircleWisePlan> circleWisePlanLists;
}
#Getter
#Setter
class CircleWisePlan {
private String circleName;
private String circleId;
}
final class MapUtil {
public static List<RechargePlanEntity> toEntity(OperatorWiseCircles in) {
return in.getCircleWisePlanLists()
.stream()
.map(MapUtil::map)
.peek(out -> map(in, out))
.collect(Collectors.toList());
}
private static RechargePlanEntity map(CircleWisePlan in) {
RechargePlanEntity out = new RechargePlanEntity();
out.setCircleId(in.getCircleId());
out.setCircleName(in.getCircleName());
return out;
}
private static void map(OperatorWiseCircles in, RechargePlanEntity out) {
out.setOperatorId(in.getOperatorId());
out.setOperatorName(in.getOperatorName());
}
}
The entities without ids may be created from RechargeResponse model providing that the entity has all-args constructor:
RechargeResponse modelFromJson = ... //
List<RechargePlanEntity> entities = modelFromJson.getPayload()
.stream() // Stream<OperatorWiseCircles>
.flatMap(ows -> ows.getCircleWisePlanLists()
.stream() // Stream<CircleWisePlan>
.map(cwp -> new RechargePlanEntity(
null, // instead of id
ows.getOperatorName(),
ows.getOperatorId(),
cwp.getCircleName(),
cwp.getCircleId()
)) // Stream<RechargePlanEntity>
) // Stream<RechargePlanEntity>
.collect(Collectors.toList());
or, if a builder is implemented in the entity class (e.g. using Lombok's #Builder annotation), this conversion may look as follows:
List<RechargePlanEntity> entities = modelFromJson.getPayload()
.stream() // Stream<OperatorWiseCircles>
.flatMap(ows -> ows.getCircleWisePlanLists()
.stream() // Stream<CircleWisePlan>
.map(cwp -> RechargePlanEntity.builder()
.operatorName(ows.getOperatorName())
.operatorId(ows.getOperatorId())
.circleName(cwp.getCircleName())
.circleId(cwp.getCircleId())
.build()
) // Stream<RechargePlanEntity>
) // Stream<RechargePlanEntity>
.collect(Collectors.toList());
I have the below enum with two values, and i have a search api with many fields, One of these fields is a list of StatusEnum. So i created a dto that contains this field.
The problem when i send the data the list status is always empty
json exp: {"status":["En activité"],"startDate":null,"endDate":null}
public enum StatusEnum {
INACTIVITY, ENDACTIVITY;
private static Map<String, StatusEnum > namesMap = new HashMap<>(2);
static {
namesMap.put("En activité", INACTIVITY);
namesMap.put("En fin d'activité", ENDACTIVITY);
}
#JsonCreator
public static StatusEnum forValue(String value) {
return namesMap.get(StringUtils.lowerCase(value));
}
#JsonValue
public String toValue() {
for (Entry<String, StatusEnum > entry : namesMap.entrySet()) {
if (entry.getValue() == this)
return entry.getKey();
}
return null;
}
}
#PostMapping
public ResponseEntity<List<Object>> search(#RequestBody SearchDTO search) { }
public class SearchDTO {
private Date startDate;
private Date endDate
private List<StatusEnum> status;
//getter and setter
}
#JsonCreator
public static StatusEnum forValue(String value) {
return namesMap.get(StringUtils.lowerCase(value));
}
Problem is the usage of #lowerCase in forValue!
Your keys in your map aren't lower-cased. That's why namesMap.get can't find anything.
i'm working in a rest API using Spring boot.
when i wanted to return my entity from an End Point i realized that the Properties are different from what i need on my response so i tried to use Model Mapper to return a DTO.
My entity is like this:
public class RuleEntity {
private String ruleId;
private String bankDecision;
private String aggregatorFunctionType;
private String limitOperatorType;
private double limitRule;
private Integer windowMinutes;
private Integer layer;
private String expressionRule;
private String status;
private List<GroupingKeyName> groupingKeyNames;
private List<RuleFilter> ruleFilters;
}
And the DTO that i need Must Be Like this:
public class RuleDTO {
private String ruleId;
private String bankDecision;
private String aggregatorFunctionType;
private String limitOperatorType;
private double limitRule;
private Integer windowMinutes;
private Integer layer;
private String expressionRule;
private String status;
private List<String> groupingKeyNames;
private List<String> ruleFilters;
}
The only change is that the last two lists are of String instead of The Object
The Objects groupingKeyNames and ruleFilters have a Name and an ID, and i only need the name on the list of DTO so it is a List of Strings
I tried using Model Mapper like this:
ModelMapper modelMapper = new ModelMapper();
RuleSetModel ruleSetModel = modelMapper.map(ruleSetEntity, RuleSetModel.class);
it works, with all the properties but in the Lists it is returning something like:
groupingKeyNames=[GroupingKeyName(groupingKeyId=1, name=cardHash)], ruleFilters=[RuleFilter(ruleFilterId=1, name=status)]
What could i do so i get a result like this:
groupingKeyNames=[cardHash], ruleFilters=[status]
Thanks in advance!
Create a method into your RuleEntity to do it
public RuleDTO dto() {
// config to skip
PropertyMap<RuleEntity, RuleDTO> propertyMap = new PropertyMap<RuleEntity, RuleDTO>() {
#Override
protected void configure() {
skip(destination.getGroupingKeyNames());
skip(destination.getRuleFilters());
}
};
RuleDTO ruleDTO = new RuleDTO();
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
modelMapper.addMappings(propertyMap);
modelMapper.map(this,ruleDTO);
if (!this.groupingKeyNames.isEmpty()) {
ruleDTO.getGroupingKeyNames().clear();
List<String> tmpGroupingKeyNames = new ArrayList<>();
this.getGroupingKeyNames().forEach(itemDTO -> {
tmpGroupingKeyNames.add(itemDTO.name);
});
ruleDTO.getGroupingKeyNames().addAll(tmpGroupingKeyNames);
}
if (!this.ruleFilters.isEmpty()) {
ruleDTO.getRuleFilters().clear();
List<String> tmpRuleFilters = new ArrayList<>();
this.getRuleFilters().forEach(itemDTO -> {
tmpRuleFilters.add(itemDTO.name);
});
ruleDTO.getRuleFilters().addAll(tmpRuleFilters);
}
return ruleDTO;
}
application.properties file contains properties that have sub properties:
status.available=00, STATUS.ALLOWED
status.forbidden=01, STATUS.FORBIDDEN
status.authdenied=05, STATUS.AUTH_DENIED
The idea was to get those properties into the application like this:
#Configuration
#ConfigurationProperties(prefix = "status")
#JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
public class StatusProperties {
private Map <String, List <String>> statusMapping;
public Map <String, List <String>> getStatusMapping () {
return statusMapping;
}
public void setStatusMapping (Map <String, List <String>> statusMapping) {
this.statusMapping = statusMapping;
}
}
The problem is that this Map is returned empty. I must be doing something wrong. Maybe this is not even possible in Spring to do like this?
I'm not sure about your choice regarding the data type and its assignment. I'd suggest you to rethink this design.
To your main question:
Spring can't know, that status.* should be mapped to private Map <String, List <String>> statusMapping;. Also as your class is named *properties, It seems that you don't want it to be a #Configuration class. Consider the following pattern:
First, create a properties class to hold the properties:
#ConfigurationProperties(prefix = "status")
public class StatusProperties {
private Map.Entry<Integer, String> available;
private Map.Entry<Integer, String> forbidden;
private Map.Entry<Integer, String> authdenied;
public Map.Entry<Integer, String> getAvailable() {
return available;
}
public void setAvailable(Map.Entry<Integer, String> available) {
this.available = available;
}
public Map.Entry<Integer, String> getForbidden() {
return forbidden;
}
public void setForbidden(Map.Entry<Integer, String> forbidden) {
this.forbidden = forbidden;
}
public Map.Entry<Integer, String> getAuthdenied() {
return authdenied;
}
public void setAuthdenied(Map.Entry<Integer, String> authdenied) {
this.authdenied = authdenied;
}
}
Now, your IDE should be able to read the docs from the setters while editing application.properties and check the validity. Spring can autowire the fields and automatically create the correct data types for you.
Consider mapping the Entries to a Map (Or, as I already told, change the design)
Now, you can use this properties class in your configuration:
#Configuration
#EnableConfigurationProperties(StatusProperties.class)
public class StatusConfiguration {
#Bean
public MyBean myBean(StatusProperties properties) {
return new MyBean(properties);
}
}
I found the solution:
application.properties:
app.statuses[0].id=00
app.statuses[0].title=STATUS.ALLOWED
app.statuses[1].id=01
app.statuses[1].title=STATUS.FORBIDDEN
app.statuses[2].id=02
app.statuses[2].title=STATUS.CONTRACT_ENDED
Properties.java
#Component
#ConfigurationProperties(prefix = "app")
#JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
public class StatusProperties {
private List<Status> statuses = new ArrayList<>();
public List <Status> getStatuses () {
return statuses;
}
public void setStatuses (List <Status> statuses) {
this.statuses = statuses;
}
public static class Status {
private String id;
private String title;
public String getId () {
return id;
}
public void setId (String id) {
this.id = id;
}
public String getTitle () {
return title;
}
public void setTitle (String title) {
this.title = title;
}
}
}