[UI] Support data registration
[validation.git] / ui / src / main / java / org / akraino / validation / ui / entity / BlueprintInstance.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * 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
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package org.akraino.validation.ui.entity;
17
18 import java.io.IOException;
19 import java.io.Serializable;
20 import java.util.HashSet;
21 import java.util.Set;
22
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.FetchType;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.JoinColumn;
31 import javax.persistence.JoinTable;
32 import javax.persistence.ManyToMany;
33 import javax.persistence.ManyToOne;
34 import javax.persistence.Table;
35
36 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.onap.portalsdk.core.web.support.UserUtils;
38
39 import com.fasterxml.jackson.databind.SerializerProvider;
40 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
41 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
42
43 @Entity
44 @Table(name = "blueprint_instance")
45 public class BlueprintInstance implements Serializable {
46
47     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(BlueprintInstance.class);
48
49     /**
50      *
51      */
52     private static final long serialVersionUID = 1L;
53
54     @Id
55     @GeneratedValue(strategy = GenerationType.IDENTITY)
56     @Column(name = "id")
57     private int blueprintInstId;
58
59     @ManyToOne
60     @JoinColumn(name = "blueprint_id")
61     private Blueprint blueprint;
62
63     @Column(name = "version")
64     private String version;
65
66     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
67     @JoinTable(name = "blueprint_instance_blueprint_layer", joinColumns = {
68             @JoinColumn(name = "blueprint_instance_id") }, inverseJoinColumns = {
69                     @JoinColumn(name = "blueprint_layer_id") })
70     private Set<BlueprintLayer> blueprintLayers = new HashSet<>();
71
72     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
73     @JoinTable(name = "blueprint_instance_timeslot", joinColumns = {
74             @JoinColumn(name = "blueprint_instance_id") }, inverseJoinColumns = { @JoinColumn(name = "timeslot_id") })
75     @JsonSerialize(using = TimeslotsSerializer.class)
76     private Set<Timeslot> timeslots = new HashSet<>();
77
78     public int getBlueprintInstanceId() {
79         return blueprintInstId;
80     }
81
82     public void setBlueprintInstanceId(int blueprintInstId) {
83         this.blueprintInstId = blueprintInstId;
84     }
85
86     public Blueprint getBlueprint() {
87         return blueprint;
88     }
89
90     public void setBlueprint(Blueprint blueprint) {
91         this.blueprint = blueprint;
92     }
93
94     public void setVersion(String version) {
95         this.version = version;
96     }
97
98     public String getVersion() {
99         return version;
100     }
101
102     public Set<BlueprintLayer> getBlueprintLayers() {
103         return blueprintLayers;
104     }
105
106     public void setBlueprintLayers(Set<BlueprintLayer> blueprintLayers) {
107         this.blueprintLayers = blueprintLayers;
108     }
109
110     public Set<Timeslot> getTimeslots() {
111         return timeslots;
112     }
113
114     public void setTimeslots(Set<Timeslot> timeslots) {
115         this.timeslots = timeslots;
116     }
117
118     static class TimeslotsSerializer extends StdSerializer<Set<Timeslot>> {
119
120         public TimeslotsSerializer() {
121             this(null);
122         }
123
124         public TimeslotsSerializer(Class<Set<Timeslot>> tclass) {
125             super(tclass);
126         }
127
128         @Override
129         public void serialize(Set<Timeslot> timeslots, com.fasterxml.jackson.core.JsonGenerator gen,
130                 SerializerProvider provider) throws IOException {
131             Set<Timeslot> results = new HashSet<>();
132             for (Timeslot timeslot : timeslots) {
133                 try {
134                     Timeslot result = new Timeslot();
135                     result.setDuration(timeslot.getDuration());
136                     result.setLabInfo(timeslot.getLabInfo());
137                     result.setStartDateTime(timeslot.getStartDateTime());
138                     result.setTimeslotId(timeslot.getTimeslotId());
139                     results.add(result);
140                 } catch (Exception ex) {
141                     LOGGER.error(EELFLoggerDelegate.errorLogger,
142                             "Error when serializing." + UserUtils.getStackTrace(ex));
143                 }
144             }
145             gen.writeObject(results);
146         }
147     }
148
149 }