Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5df1f7d1f34b8bd5f0392e24cca226d453e9cfab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package org.eclipse.osee.ats.rest.internal.config;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.eclipse.osee.ats.api.IAtsConfigObject;
import org.eclipse.osee.ats.api.IAtsObject;
import org.eclipse.osee.ats.api.agile.IAgileFeatureGroup;
import org.eclipse.osee.ats.api.agile.IAgileSprint;
import org.eclipse.osee.ats.api.agile.IAgileTeam;
import org.eclipse.osee.ats.api.country.IAtsCountry;
import org.eclipse.osee.ats.api.data.AtsRelationTypes;
import org.eclipse.osee.ats.api.insertion.IAtsInsertion;
import org.eclipse.osee.ats.api.insertion.IAtsInsertionActivity;
import org.eclipse.osee.ats.api.program.IAtsProgram;
import org.eclipse.osee.ats.api.team.IAtsTeamDefinition;
import org.eclipse.osee.ats.api.version.IAtsVersion;
import org.eclipse.osee.ats.impl.IAtsServer;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.jaxrs.mvc.IdentityView;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.AttributeReadable;
import org.eclipse.osee.orcs.data.AttributeTypes;

/**
 * @author Donald G. Dunne
 */
@Provider
public class ConfigJsonWriter implements MessageBodyWriter<IAtsConfigObject> {

   private JsonFactory jsonFactory;
   private IAtsServer atsServer;

   public void setAtsServer(IAtsServer atsServer) {
      this.atsServer = atsServer;
   }

   public void start() {
      jsonFactory = org.eclipse.osee.ats.impl.config.JsonFactory.create();
   }

   public void stop() {
      jsonFactory = null;
   }

   @Override
   public long getSize(IAtsConfigObject data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
      return -1;
   }

   @Override
   public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
      boolean assignableFrom = IAtsConfigObject.class.isAssignableFrom(type);
      return assignableFrom && MediaType.APPLICATION_JSON_TYPE.equals(mediaType);
   }

   private boolean matches(Class<? extends Annotation> toMatch, Annotation[] annotations) {
      for (Annotation annotation : annotations) {
         if (annotation.annotationType().isAssignableFrom(toMatch)) {
            return true;
         }
      }
      return false;
   }

   private AttributeTypes getAttributeTypes() {
      return atsServer.getOrcsApi().getOrcsTypes().getAttributeTypes();
   }

   @Override
   public void writeTo(IAtsConfigObject config, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
      JsonGenerator writer = null;
      try {
         writer = jsonFactory.createJsonGenerator(entityStream);
         writer.writeStartArray();
         addProgramObject(atsServer, config, annotations, writer, matches(IdentityView.class, annotations),
            getAttributeTypes());
         writer.writeEndArray();
      } finally {
         if (writer != null) {
            writer.flush();
         }
      }
   }

   public static void addProgramObject(IAtsServer atsServer, IAtsObject atsObject, Annotation[] annotations, JsonGenerator writer, boolean identityView, AttributeTypes attributeTypes) throws IOException, JsonGenerationException, JsonProcessingException {
      ArtifactReadable artifact = (ArtifactReadable) atsObject.getStoreObject();
      writer.writeStartObject();
      writer.writeNumberField("uuid", getUuid(atsObject));
      writer.writeStringField("Name", atsObject.getName());
      writer.writeStringField("Description", atsObject.getDescription());

      if (atsObject instanceof IAtsTeamDefinition) {
         if (!identityView) {
            writer.writeArrayFieldStart("version");
            for (ArtifactReadable verArt : artifact.getRelated(AtsRelationTypes.TeamDefinitionToVersion_Version)) {
               IAtsVersion version = atsServer.getConfigItemFactory().getVersion(verArt);
               addProgramObject(atsServer, version, annotations, writer, true, attributeTypes);
            }
            writer.writeEndArray();
         }
      }
      if (atsObject instanceof IAtsVersion) {
         if (!identityView) {
            writer.writeArrayFieldStart("workflow");
            for (ArtifactReadable workArt : artifact.getRelated(AtsRelationTypes.TeamWorkflowTargetedForVersion_Workflow)) {
               addArtifactIdentity(writer, workArt);
            }
            writer.writeEndArray();
         }
      } else if (atsObject instanceof IAtsInsertionActivity) {
         IAtsInsertionActivity activity = (IAtsInsertionActivity) atsObject;
         writer.writeBooleanField("Active", activity.isActive());
         if (!identityView) {
            writer.writeArrayFieldStart("insertion");
            for (ArtifactReadable insertion : artifact.getRelated(AtsRelationTypes.InsertionToInsertionActivity_Insertion)) {
               addArtifactIdentity(writer, insertion);
            }
            writer.writeEndArray();
            writer.writeArrayFieldStart("workpackage");
            for (ArtifactReadable workPackage : artifact.getRelated(AtsRelationTypes.InsertionActivityToWorkPackage_WorkPackage)) {
               addArtifactIdentity(writer, workPackage);
            }
            writer.writeEndArray();
         }
      } else if (atsObject instanceof IAtsInsertion) {
         IAtsInsertion insertion = (IAtsInsertion) atsObject;
         writer.writeBooleanField("Active", insertion.isActive());
         if (!identityView) {
            writer.writeArrayFieldStart("program");
            for (ArtifactReadable program : artifact.getRelated(AtsRelationTypes.ProgramToInsertion_Program)) {
               addArtifactIdentity(writer, program);
            }
            writer.writeEndArray();
            writer.writeArrayFieldStart("insertionactivity");
            for (ArtifactReadable activity : artifact.getRelated(AtsRelationTypes.InsertionToInsertionActivity_InsertionActivity)) {
               addArtifactIdentity(writer, activity);
            }
            writer.writeEndArray();
         }
      } else if (atsObject instanceof IAtsProgram) {
         IAtsProgram program = (IAtsProgram) atsObject;
         writer.writeStringField("Namespace", program.getNamespace());
         writer.writeBooleanField("Active", program.isActive());
         if (!identityView) {
            writer.writeArrayFieldStart("country");
            for (ArtifactReadable country : artifact.getRelated(AtsRelationTypes.CountryToProgram_Country)) {
               addArtifactIdentity(writer, country);
            }
            writer.writeEndArray();
            writer.writeArrayFieldStart("insertion");
            for (ArtifactReadable insertion : artifact.getRelated(AtsRelationTypes.ProgramToInsertion_Insertion)) {
               addArtifactIdentity(writer, insertion);
            }
            writer.writeEndArray();
         }
      } else if (atsObject instanceof IAtsCountry) {
         IAtsCountry country = (IAtsCountry) atsObject;
         if (!identityView) {
            writer.writeArrayFieldStart("programs");
            Collection<IAtsProgram> programs = atsServer.getProgramService().getPrograms(country);
            for (IAtsProgram program : programs) {
               writer.writeStartObject();
               writer.writeNumberField("uuid", program.getUuid());
               writer.writeStringField("Name", program.getName());
               writer.writeBooleanField("active", program.isActive());
               writer.writeEndObject();
            }
            writer.writeEndArray();
         }
      } else if (atsObject instanceof IAgileTeam) {
         IAgileTeam team = (IAgileTeam) atsObject;
         writer.writeBooleanField("Active", team.isActive());
         writer.writeStringField("Description", team.getDescription());
         writer.writeArrayFieldStart("featureGroups");
         Collection<IAgileFeatureGroup> featureGroups = atsServer.getAgileService().getAgileFeatureGroups(team);
         for (IAgileFeatureGroup group : featureGroups) {
            writer.writeStartObject();
            writer.writeNumberField("uuid", group.getUuid());
            writer.writeStringField("Name", group.getName());
            writer.writeBooleanField("active", group.isActive());
            writer.writeEndObject();
         }
         writer.writeEndArray();
         writer.writeArrayFieldStart("sprints");
         Collection<IAgileSprint> agileSprints = atsServer.getAgileService().getAgileSprints(team);
         for (IAgileSprint sprint : agileSprints) {
            writer.writeStartObject();
            writer.writeNumberField("uuid", sprint.getUuid());
            writer.writeStringField("Name", sprint.getName());
            writer.writeBooleanField("active", sprint.isActive());
            writer.writeEndObject();
         }
         writer.writeEndArray();
         ArtifactReadable teamArt = atsServer.getArtifact(team.getUuid());
         ArtifactReadable backlogArt =
            teamArt.getRelated(AtsRelationTypes.AgileTeamToBacklog_Backlog).getAtMostOneOrNull();
         writer.writeStringField("Backlog Uuid", (backlogArt != null ? String.valueOf(backlogArt.getUuid()) : ""));
         writer.writeStringField("Backlog", (backlogArt != null ? String.valueOf(backlogArt.getName()) : ""));
      }
      if (!identityView) {
         addAttributeData(writer, attributeTypes, artifact);
      }
      writer.writeEndObject();
   }

   public static void addAttributeData(JsonGenerator writer, AttributeTypes attributeTypes, ArtifactReadable artifact) throws IOException, JsonGenerationException, JsonProcessingException {
      Collection<? extends IAttributeType> attrTypes = attributeTypes.getAll();
      ResultSet<? extends AttributeReadable<Object>> attributes = artifact.getAttributes();
      if (!attributes.isEmpty()) {
         for (IAttributeType attrType : attrTypes) {
            if (artifact.isAttributeTypeValid(attrType)) {
               List<Object> attributeValues = artifact.getAttributeValues(attrType);
               if (!attributeValues.isEmpty()) {

                  if (attributeValues.size() > 1) {
                     writer.writeArrayFieldStart(attrType.getName());
                     for (Object value : attributeValues) {
                        writer.writeObject(value);
                     }
                     writer.writeEndArray();
                  } else if (attributeValues.size() == 1) {
                     Object value = attributeValues.iterator().next();
                     writer.writeObjectField(attrType.getName(), value);
                  }

               }
            }
         }
      }
   }

   private static void addArtifactIdentity(JsonGenerator writer, ArtifactReadable workArt) throws IOException, JsonGenerationException, JsonProcessingException {
      writer.writeStartObject();
      writer.writeNumberField("uuid", workArt.getUuid());
      writer.writeStringField("Name", workArt.getName());
      writer.writeEndObject();
   }

   public static Long getUuid(IAtsObject atsObject) {
      long uuid = atsObject.getUuid();
      if (uuid <= 0L) {
         uuid = ((ArtifactReadable) atsObject.getStoreObject()).getUuid();
      }
      return uuid;
   }
}

Back to the top