1+ /*
2+ * Copyright 2019 wuriyanto.com
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com .telkomdev .consumer .model ;
18+
19+ import com .google .protobuf .InvalidProtocolBufferException ;
20+ import com .telkomdev .consumer .avrojava .ProductAvro ;
21+ import com .telkomdev .consumer .protojava .ProductProto ;
22+ import com .telkomdev .consumer .deserializer .JsonParser ;
23+ import org .apache .avro .io .*;
24+ import org .apache .avro .specific .SpecificDatumReader ;
25+ import org .apache .avro .specific .SpecificDatumWriter ;
26+
27+ import java .io .ByteArrayOutputStream ;
28+ import java .io .IOException ;
29+ import java .io .InputStream ;
30+ import java .util .ArrayList ;
31+ import java .util .List ;
32+
33+ public class Product {
34+
35+ private String id ;
36+ private String name ;
37+ private Integer quantity ;
38+ private List <String >images ;
39+
40+ public Product () {
41+
42+ }
43+
44+ public Product (String id ,String name ,Integer quantity ,List <String >images ) {
45+ this .id =id ;
46+ this .name =name ;
47+ this .quantity =quantity ;
48+ this .images =images ;
49+ }
50+
51+ public String getId () {
52+ return id ;
53+ }
54+
55+ public void setId (String id ) {
56+ this .id =id ;
57+ }
58+
59+ public String getName () {
60+ return name ;
61+ }
62+
63+ public void setName (String name ) {
64+ this .name =name ;
65+ }
66+
67+ public Integer getQuantity () {
68+ return quantity ;
69+ }
70+
71+ public void setQuantity (Integer quantity ) {
72+ this .quantity =quantity ;
73+ }
74+
75+ public List <String >getImages () {
76+ return images ;
77+ }
78+
79+ public void setImages (List <String >images ) {
80+ this .images =images ;
81+ }
82+
83+ @ Override
84+ public String toString () {
85+ return "Product{" +
86+ "id='" +id +'\'' +
87+ ", name='" +name +'\'' +
88+ ", quantity=" +quantity +
89+ ", images=" +images +
90+ '}' ;
91+ }
92+
93+ public byte []toAvro ()throws IOException {
94+ DatumWriter <ProductAvro >datumWriter =new SpecificDatumWriter <>(ProductAvro .class );
95+ ProductAvro productAvro =new ProductAvro ();
96+ productAvro .setId (this .id );
97+ productAvro .setName (this .name );
98+ productAvro .setQuantity (this .quantity );
99+
100+ List <CharSequence >imagesAvro =new ArrayList <>();
101+ for (String image :this .images ) {
102+ CharSequence im =new StringBuilder (image );
103+ imagesAvro .add (im );
104+ }
105+ productAvro .setImages (imagesAvro );
106+
107+ ByteArrayOutputStream outputStream =new ByteArrayOutputStream ();
108+ Encoder encoder =EncoderFactory .get ().binaryEncoder (outputStream ,null );
109+ try {
110+ datumWriter .write (productAvro ,encoder );
111+ encoder .flush ();
112+ outputStream .close ();
113+ }catch (IOException ex ) {
114+ throw ex ;
115+ }
116+
117+ return outputStream .toByteArray ();
118+ }
119+
120+ public static Product fromAvro (byte []in )throws IOException {
121+ ProductAvro productAvro =null ;
122+ Product product =new Product ();
123+ DatumReader <ProductAvro >datumReader =new SpecificDatumReader <>(ProductAvro .class );
124+ Decoder decoder =DecoderFactory .get ().binaryDecoder (in ,null );
125+
126+ try {
127+ productAvro =datumReader .read (null ,decoder );
128+ }catch (IOException ex ) {
129+ throw ex ;
130+ }
131+
132+ product .setId (productAvro .getId ().toString ());
133+ product .setName (productAvro .getName ().toString ());
134+ product .setQuantity (productAvro .getQuantity ());
135+
136+ List <String >images =new ArrayList <>();
137+ for (CharSequence sb :productAvro .getImages ()) {
138+ images .add (sb .toString ());
139+ }
140+
141+ product .setImages (images );
142+
143+ return product ;
144+ }
145+
146+ public byte []toJson () {
147+ JsonParser <Product >jp =new JsonParser <>(this ,Product .class );
148+ return jp .serialize ();
149+ }
150+
151+ public static Product fromJson (byte []in ) {
152+ JsonParser <Product >jp =new JsonParser <>(Product .class );
153+ return jp .deserialize (in );
154+ }
155+
156+ public ProductProto .Product toProto () {
157+ ProductProto .Product productOut =ProductProto .Product .newBuilder ()
158+ .setID (this .id )
159+ .setName (this .name )
160+ .setQuantity (this .quantity )
161+ .addAllImages (this .images )
162+ .build ();
163+ return productOut ;
164+ }
165+
166+ public static Product fromProto (byte []in )throws InvalidProtocolBufferException {
167+ ProductProto .Product productIn =ProductProto .Product .parseFrom (in );
168+ Product product =new Product ();
169+ product .setId (productIn .getID ());
170+ product .setName (productIn .getName ());
171+ product .setQuantity ((int )productIn .getQuantity ());
172+ List <String >images =new ArrayList <>();
173+ for (String image :productIn .getImagesList ()) {
174+ images .add (image );
175+ }
176+
177+ product .setImages (images );
178+ return product ;
179+ }
180+
181+ public static Product fromProto (InputStream in )throws IOException {
182+ ProductProto .Product productIn =ProductProto .Product .parseFrom (in );
183+ Product product =new Product ();
184+ product .setId (productIn .getID ());
185+ product .setName (productIn .getName ());
186+ product .setQuantity ((int )productIn .getQuantity ());
187+ List <String >images =new ArrayList <>();
188+ for (String image :productIn .getImagesList ()) {
189+ images .add (image );
190+ }
191+
192+ product .setImages (images );
193+ return product ;
194+ }
195+ }