product sync
This commit is contained in:
parent
29ae19cbba
commit
e32e84ac4a
|
|
@ -0,0 +1,43 @@
|
|||
package com.teterialosjuanjos.tfg.sync_service.integration.prestashop.dto;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Handles PrestaShop's multilingual fields, which the webservice returns as either:
|
||||
* <ul>
|
||||
* <li>A plain string: {@code "Product Name"}</li>
|
||||
* <li>A language array: {@code [{"id":"1","value":"Product Name"},...]}</li>
|
||||
* </ul>
|
||||
* Extracts the {@code value} of the first language entry, or returns the string as-is.
|
||||
*/
|
||||
public class MultilangStringDeserializer extends JsonDeserializer<String> {
|
||||
|
||||
@Override
|
||||
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
if (p.currentToken() == JsonToken.VALUE_STRING) {
|
||||
return p.getText();
|
||||
}
|
||||
if (p.currentToken() == JsonToken.START_ARRAY) {
|
||||
String first = null;
|
||||
while (p.nextToken() != JsonToken.END_ARRAY) {
|
||||
if (p.currentToken() == JsonToken.START_OBJECT && first == null) {
|
||||
JsonNode node = p.readValueAsTree();
|
||||
if (node.has("value")) {
|
||||
first = node.get("value").asText(null);
|
||||
}
|
||||
} else {
|
||||
p.skipChildren();
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
p.skipChildren();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.teterialosjuanjos.tfg.sync_service.integration.prestashop.dto;
|
|||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -27,9 +28,10 @@ public record PrestashopProductDto(
|
|||
/** "1" = active, "0" = inactive */
|
||||
String active,
|
||||
@JsonProperty("id_category_default") String idCategoryDefault,
|
||||
String name,
|
||||
String description,
|
||||
@JsonProperty("description_short") String descriptionShort
|
||||
@JsonDeserialize(using = MultilangStringDeserializer.class) String name,
|
||||
@JsonDeserialize(using = MultilangStringDeserializer.class) String description,
|
||||
@JsonProperty("description_short")
|
||||
@JsonDeserialize(using = MultilangStringDeserializer.class) String descriptionShort
|
||||
) {
|
||||
|
||||
/** Envelope for {@code GET /api/products?display=full} */
|
||||
|
|
|
|||
Loading…
Reference in New Issue