Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / json / TTopologyTemplateSerializer.java
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.json;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.winery.model.tosca.TEntityTemplate;
19 import org.eclipse.winery.model.tosca.TNodeTemplate;
20 import org.eclipse.winery.model.tosca.TRelationshipTemplate;
21 import org.eclipse.winery.model.tosca.TTopologyTemplate;
22
23 import com.fasterxml.jackson.core.JsonGenerator;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonSerializer;
26 import com.fasterxml.jackson.databind.SerializerProvider;
27
28 public class TTopologyTemplateSerializer extends JsonSerializer<TTopologyTemplate> {
29         
30         /**
31          * Does NOT wrap the result into an object. Assumes that the current
32          * position at jgen is in an object
33          * 
34          * @param value the list of entity templates to serialize
35          */
36         public void serialize(List<TEntityTemplate> value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
37                 List<TRelationshipTemplate> relationshipTemplates = new ArrayList<TRelationshipTemplate>();
38                 
39                 jgen.writeFieldName("nodeTemplates");
40                 jgen.writeStartObject();
41                 for (TEntityTemplate template : value) {
42                         if (template instanceof TNodeTemplate) {
43                                 // write out as <id> : <default serialization>
44                                 jgen.writeFieldName(template.getId());
45                                 provider.defaultSerializeValue(template, jgen);
46                                 
47                         } else {
48                                 assert (template instanceof TRelationshipTemplate);
49                                 relationshipTemplates.add((TRelationshipTemplate) template);
50                         }
51                 }
52                 jgen.writeEndObject();
53                 
54                 jgen.writeFieldName("relationshipTemplates");
55                 jgen.writeStartObject();
56                 for (TRelationshipTemplate template : relationshipTemplates) {
57                         // write out as <id> : <default serialization>
58                         jgen.writeFieldName(template.getId());
59                         provider.defaultSerializeValue(template, jgen);
60                 }
61                 jgen.writeEndObject();
62         }
63         
64         @Override
65         public void serialize(TTopologyTemplate topologyTemplate, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
66                 jgen.writeStartObject();
67                 
68                 // write out the other fields unmodified
69                 jgen.writeFieldName("documentation");
70                 provider.defaultSerializeValue(topologyTemplate.getDocumentation(), jgen);
71                 jgen.writeFieldName("any");
72                 provider.defaultSerializeValue(topologyTemplate.getAny(), jgen);
73                 jgen.writeFieldName("otherAttributes");
74                 provider.defaultSerializeValue(topologyTemplate.getOtherAttributes(), jgen);
75                 
76                 // finally, write the topology template
77                 this.serialize(topologyTemplate.getNodeTemplateOrRelationshipTemplate(), jgen, provider);
78                 
79                 jgen.writeEndObject();
80         }
81 }