Skip to content

Commit 69829bf

Browse files
authored
Merge pull request #118 from connyduck/custom_elements_support
add support for custom elements
2 parents cfa45cb + c69abce commit 69829bf

File tree

11 files changed

+379
-3
lines changed

11 files changed

+379
-3
lines changed

kontent-delivery-generators/src/main/java/kentico/kontent/delivery/generators/CodeGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public JavaFile generateSource(ContentType type) {
104104
case "text" :
105105
case "rich_text" :
106106
case "url_slug" :
107+
case "custom" :
107108
typeName = ClassName.get(String.class);
108109
break;
109110
case "number" :

kontent-delivery-generators/src/test/java/kentico/kontent/delivery/generators/CodeGeneratorTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
import java.io.File;
1616
import java.io.IOException;
17+
import java.net.URISyntaxException;
18+
import java.nio.charset.StandardCharsets;
19+
import java.nio.file.Files;
20+
import java.nio.file.Paths;
1721
import java.util.Arrays;
1822
import java.util.List;
1923

@@ -62,7 +66,15 @@ public void testGenerator() throws Exception {
6266
Assert.assertEquals(2, files.size());
6367
Assert.assertTrue(files.contains("Article.java"));
6468
Assert.assertTrue(files.contains("Brewer.java"));
65-
// TODO extend check for files content
69+
70+
Assert.assertEquals(
71+
readResource("Article.java"),
72+
readFile(new File(outputDir, "com/dancinggoat/models/Article.java"))
73+
);
74+
Assert.assertEquals(
75+
readResource("Brewer.java"),
76+
readFile(new File(outputDir, "com/dancinggoat/models/Brewer.java"))
77+
);
6678
}
6779

6880
@Test
@@ -92,4 +104,14 @@ public void testExceptionWhenOutputDirectoryIsFile() throws IOException {
92104
}
93105

94106
}
107+
108+
String readFile(File file) throws IOException {
109+
byte[] encoded = Files.readAllBytes(file.toPath());
110+
return new String(encoded, StandardCharsets.UTF_8);
111+
}
112+
113+
String readResource(String resourceName) throws IOException, URISyntaxException {
114+
byte[] encoded = Files.readAllBytes(Paths.get(this.getClass().getResource(resourceName).toURI()));
115+
return new String(encoded, StandardCharsets.UTF_8);
116+
}
95117
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.dancinggoat.models;
2+
3+
import java.lang.String;
4+
import java.time.ZonedDateTime;
5+
import java.util.List;
6+
import kentico.kontent.delivery.Asset;
7+
import kentico.kontent.delivery.ContentItem;
8+
import kentico.kontent.delivery.ContentItemMapping;
9+
import kentico.kontent.delivery.ElementMapping;
10+
import kentico.kontent.delivery.System;
11+
import kentico.kontent.delivery.Taxonomy;
12+
13+
/**
14+
* This code was generated by a <a href="https://github.com/Kentico/cloud-generators-java">cloud-generators-java tool</a>
15+
*
16+
* Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
17+
* For further modifications of the class, create a separate file and extend this class.
18+
*/
19+
@ContentItemMapping("article")
20+
public class Article {
21+
@ElementMapping("personas")
22+
List<Taxonomy> personas;
23+
24+
@ElementMapping("title")
25+
String title;
26+
27+
@ElementMapping("teaser_image")
28+
List<Asset> teaserImage;
29+
30+
@ElementMapping("post_date")
31+
ZonedDateTime postDate;
32+
33+
@ElementMapping("summary")
34+
String summary;
35+
36+
@ElementMapping("body_copy")
37+
String bodyCopy;
38+
39+
@ContentItemMapping("related_articles")
40+
List<ContentItem> relatedArticles;
41+
42+
@ElementMapping("meta_keywords")
43+
String metaKeywords;
44+
45+
@ElementMapping("meta_description")
46+
String metaDescription;
47+
48+
@ElementMapping("color")
49+
String color;
50+
51+
System system;
52+
53+
public List<Taxonomy> getPersonas() {
54+
return personas;
55+
}
56+
57+
public void setPersonas(List<Taxonomy> personas) {
58+
this.personas = personas;
59+
}
60+
61+
public String getTitle() {
62+
return title;
63+
}
64+
65+
public void setTitle(String title) {
66+
this.title = title;
67+
}
68+
69+
public List<Asset> getTeaserImage() {
70+
return teaserImage;
71+
}
72+
73+
public void setTeaserImage(List<Asset> teaserImage) {
74+
this.teaserImage = teaserImage;
75+
}
76+
77+
public ZonedDateTime getPostDate() {
78+
return postDate;
79+
}
80+
81+
public void setPostDate(ZonedDateTime postDate) {
82+
this.postDate = postDate;
83+
}
84+
85+
public String getSummary() {
86+
return summary;
87+
}
88+
89+
public void setSummary(String summary) {
90+
this.summary = summary;
91+
}
92+
93+
public String getBodyCopy() {
94+
return bodyCopy;
95+
}
96+
97+
public void setBodyCopy(String bodyCopy) {
98+
this.bodyCopy = bodyCopy;
99+
}
100+
101+
public List<ContentItem> getRelatedArticles() {
102+
return relatedArticles;
103+
}
104+
105+
public void setRelatedArticles(List<ContentItem> relatedArticles) {
106+
this.relatedArticles = relatedArticles;
107+
}
108+
109+
public String getMetaKeywords() {
110+
return metaKeywords;
111+
}
112+
113+
public void setMetaKeywords(String metaKeywords) {
114+
this.metaKeywords = metaKeywords;
115+
}
116+
117+
public String getMetaDescription() {
118+
return metaDescription;
119+
}
120+
121+
public void setMetaDescription(String metaDescription) {
122+
this.metaDescription = metaDescription;
123+
}
124+
125+
public String getColor() {
126+
return color;
127+
}
128+
129+
public void setColor(String color) {
130+
this.color = color;
131+
}
132+
133+
public System getSystem() {
134+
return system;
135+
}
136+
137+
public void setSystem(System system) {
138+
this.system = system;
139+
}
140+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.dancinggoat.models;
2+
3+
import java.lang.Double;
4+
import java.lang.String;
5+
import java.util.List;
6+
import kentico.kontent.delivery.Asset;
7+
import kentico.kontent.delivery.ContentItemMapping;
8+
import kentico.kontent.delivery.ElementMapping;
9+
import kentico.kontent.delivery.Option;
10+
import kentico.kontent.delivery.System;
11+
import kentico.kontent.delivery.Taxonomy;
12+
13+
/**
14+
* This code was generated by a <a href="https://github.com/Kentico/cloud-generators-java">cloud-generators-java tool</a>
15+
*
16+
* Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
17+
* For further modifications of the class, create a separate file and extend this class.
18+
*/
19+
@ContentItemMapping("brewer")
20+
public class Brewer {
21+
@ElementMapping("product_name")
22+
String productName;
23+
24+
@ElementMapping("price")
25+
Double price;
26+
27+
@ElementMapping("image")
28+
List<Asset> image;
29+
30+
@ElementMapping("product_status")
31+
List<Taxonomy> productStatus;
32+
33+
@ElementMapping("manufacturer")
34+
String manufacturer;
35+
36+
@ElementMapping("in_stock")
37+
List<Option> inStock;
38+
39+
@ElementMapping("short_description")
40+
String shortDescription;
41+
42+
@ElementMapping("long_description")
43+
String longDescription;
44+
45+
System system;
46+
47+
public String getProductName() {
48+
return productName;
49+
}
50+
51+
public void setProductName(String productName) {
52+
this.productName = productName;
53+
}
54+
55+
public Double getPrice() {
56+
return price;
57+
}
58+
59+
public void setPrice(Double price) {
60+
this.price = price;
61+
}
62+
63+
public List<Asset> getImage() {
64+
return image;
65+
}
66+
67+
public void setImage(List<Asset> image) {
68+
this.image = image;
69+
}
70+
71+
public List<Taxonomy> getProductStatus() {
72+
return productStatus;
73+
}
74+
75+
public void setProductStatus(List<Taxonomy> productStatus) {
76+
this.productStatus = productStatus;
77+
}
78+
79+
public String getManufacturer() {
80+
return manufacturer;
81+
}
82+
83+
public void setManufacturer(String manufacturer) {
84+
this.manufacturer = manufacturer;
85+
}
86+
87+
public List<Option> getInStock() {
88+
return inStock;
89+
}
90+
91+
public void setInStock(List<Option> inStock) {
92+
this.inStock = inStock;
93+
}
94+
95+
public String getShortDescription() {
96+
return shortDescription;
97+
}
98+
99+
public void setShortDescription(String shortDescription) {
100+
this.shortDescription = shortDescription;
101+
}
102+
103+
public String getLongDescription() {
104+
return longDescription;
105+
}
106+
107+
public void setLongDescription(String longDescription) {
108+
this.longDescription = longDescription;
109+
}
110+
111+
public System getSystem() {
112+
return system;
113+
}
114+
115+
public void setSystem(System system) {
116+
this.system = system;
117+
}
118+
}

kontent-delivery-generators/src/test/resources/kentico/kontent/delivery/generators/SampleContentTypeList.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
"meta_description": {
4545
"type": "text",
4646
"name": "Meta description"
47+
},
48+
"color": {
49+
"type": "custom",
50+
"name": "Color"
4751
}
4852
}
4953
},

kontent-delivery/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ for (Option option : element.getOptions()) {
248248
articleItem.getLinkedItems("related_articles")
249249
```
250250

251+
### Custom items
252+
253+
```java
254+
// Retrieves the value of the custom element 'color'
255+
String customElementValue = ((CustomElement) articleItem.getElements().get("color")).getValue();
256+
```
257+
251258
## Further information
252259

253260
For more developer resources, visit the Kentico Kontent Docs at <https://docs.kontent.ai>.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package kentico.kontent.delivery;
26+
27+
import com.fasterxml.jackson.annotation.JsonProperty;
28+
29+
/**
30+
* Object model for custom elements
31+
*/
32+
@lombok.Getter
33+
@lombok.Setter
34+
@lombok.ToString(callSuper = true)
35+
@lombok.EqualsAndHashCode(callSuper = true)
36+
public class CustomElement extends Element<String> {
37+
38+
static final String TYPE_VALUE = "custom";
39+
40+
/**
41+
* The value of a custom element is a string.
42+
*
43+
* @param value Sets the value of this.
44+
* @return The element value of this.
45+
*/
46+
@JsonProperty("value")
47+
String value;
48+
49+
public CustomElement() {
50+
setType(TYPE_VALUE);
51+
}
52+
}

kontent-delivery/src/main/java/kentico/kontent/delivery/Element.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
@JsonSubTypes.Type(value = AssetsElement.class, name = AssetsElement.TYPE_VALUE),
4747
@JsonSubTypes.Type(value = LinkedItem.class, name = LinkedItem.TYPE_VALUE),
4848
@JsonSubTypes.Type(value = TaxonomyElement.class, name = TaxonomyElement.TYPE_VALUE),
49-
@JsonSubTypes.Type(value = UrlSlugElement.class, name = UrlSlugElement.TYPE_VALUE)
49+
@JsonSubTypes.Type(value = UrlSlugElement.class, name = UrlSlugElement.TYPE_VALUE),
50+
@JsonSubTypes.Type(value = CustomElement.class, name = CustomElement.TYPE_VALUE)
5051
})
5152
@lombok.Getter
5253
@lombok.Setter

0 commit comments

Comments
 (0)