Skip to main content
summaryrefslogtreecommitdiffstats
blob: e459346ab18d828a7cf9cccfdc69affba081fc95 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*******************************************************************************
 * Copyright (c) 2015 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.rest.internal;

import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON;
import static org.eclipse.osee.orcs.rest.internal.OrcsRestUtil.executeCallable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.AttributeId;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.OrcsTypeSheet;
import org.eclipse.osee.framework.core.data.OrcsTypesConfig;
import org.eclipse.osee.framework.core.data.OrcsTypesData;
import org.eclipse.osee.framework.core.data.OrcsTypesSheet;
import org.eclipse.osee.framework.core.data.OrcsTypesVersion;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.CoreTupleTypes;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.core.enums.TxChange;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.jaxrs.OseeWebApplicationException;
import org.eclipse.osee.jdbc.JdbcService;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.OrcsTypes;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.AttributeReadable;
import org.eclipse.osee.orcs.data.AttributeTypes;
import org.eclipse.osee.orcs.data.EnumEntry;
import org.eclipse.osee.orcs.data.EnumType;
import org.eclipse.osee.orcs.data.JaxEnumAttribute;
import org.eclipse.osee.orcs.data.JaxEnumEntry;
import org.eclipse.osee.orcs.data.OrcsTopicEvents;
import org.eclipse.osee.orcs.rest.model.TypesEndpoint;
import org.eclipse.osee.orcs.transaction.TransactionBuilder;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

/**
 * @author Roberto E. Escobar
 */
public class TypesEndpointImpl implements TypesEndpoint {

   private final OrcsApi orcsApi;
   private final EventAdmin eventAdmin;
   private final JdbcService jdbcService;

   public TypesEndpointImpl(OrcsApi orcsApi, EventAdmin eventAdmin, JdbcService jdbcService) {
      this.orcsApi = orcsApi;
      this.eventAdmin = eventAdmin;
      this.jdbcService = jdbcService;
   }

   private OrcsTypes getOrcsTypes() {
      return orcsApi.getOrcsTypes();
   }

   @Override
   public Response getTypes() {
      return Response.ok().entity(new StreamingOutput() {

         @Override
         public void write(OutputStream output) throws WebApplicationException {
            Callable<Void> op = getOrcsTypes().writeTypes(output);
            executeCallable(op);
         }
      }).build();
   }

   @Override
   public Response setTypes(final InputStream inputStream) {
      IResource resource = asResource("http.osee.model", inputStream);
      Callable<Void> op = getOrcsTypes().loadTypes(resource);
      executeCallable(op);
      getOrcsTypes().invalidateAll();
      return Response.ok().build();
   }

   @Override
   public Response invalidateCaches() {
      getOrcsTypes().invalidateAll();
      return Response.ok().build();
   }

   private IResource asResource(final String fileName, final InputStream inputStream) {
      byte[] bytes;
      try {
         String types = Lib.inputStreamToString(inputStream);
         bytes = types.getBytes("UTF-8");
      } catch (IOException ex1) {
         throw new OseeWebApplicationException(Status.BAD_REQUEST, "Error parsing data");
      }
      return new ByteResource(fileName, bytes);
   }

   private static final class ByteResource implements IResource {

      private final String filename;
      private final byte[] bytes;

      public ByteResource(String filename, byte[] bytes) {
         super();
         this.filename = filename;
         this.bytes = bytes;
      }

      @Override
      public InputStream getContent() throws OseeCoreException {
         return new ByteArrayInputStream(bytes);
      }

      @Override
      public URI getLocation() {
         String modelName = filename;
         if (!modelName.endsWith(".osee")) {
            modelName += ".osee";
         }
         try {
            return new URI("osee:/" + modelName);
         } catch (URISyntaxException ex) {
            throw new OseeCoreException(ex, "Error creating URI for [%s]", modelName);
         }
      }

      @Override
      public String getName() {
         return filename;
      }

      @Override
      public boolean isCompressed() {
         return false;
      }
   }

   @Override
   public Response getEnums() {
      List<JaxEnumAttribute> attributes = new ArrayList<>();
      AttributeTypes attributeTypes = orcsApi.getOrcsTypes().getAttributeTypes();
      for (IAttributeType type : attributeTypes.getAll()) {
         if (attributeTypes.isEnumerated(type)) {
            JaxEnumAttribute enumAttr = createJaxEnumAttribute(attributeTypes, type);
            attributes.add(enumAttr);
         }
      }
      return Response.ok(attributes).build();

   }

   private JaxEnumAttribute createJaxEnumAttribute(AttributeTypes attributeTypes, IAttributeType type) {
      JaxEnumAttribute enumAttr = new JaxEnumAttribute();
      enumAttr.setName(type.getName());
      enumAttr.setDescription(type.getDescription());
      enumAttr.setUuid(type.getIdString());
      enumAttr.setDataProvider(attributeTypes.getAttributeProviderId(type));
      enumAttr.setDefaultValue(attributeTypes.getDefaultValue(type));
      enumAttr.setMax(attributeTypes.getMaxOccurrences(type));
      enumAttr.setMin(attributeTypes.getMinOccurrences(type));
      enumAttr.setMediaType(attributeTypes.getMediaType(type));
      EnumType enumType = attributeTypes.getEnumType(type);
      enumAttr.setEnumTypeName(enumType.getName());
      enumAttr.setEnumTypeUuid(enumType.getId().toString());
      for (EnumEntry enumEntry : enumType.values()) {
         JaxEnumEntry entry = new JaxEnumEntry();
         entry.setName(enumEntry.getName());
         String guid = enumEntry.getGuid();
         Long uuid = null;
         if (Strings.isNumeric(guid)) {
            uuid = Long.valueOf(guid);
         }
         if (uuid != null) {
            entry.setUuid(uuid);
         }
         enumAttr.getEntries().add(entry);
      }
      return enumAttr;
   }

   @Override
   public Response getEnums(Long uuid) {
      IAttributeType attrType = orcsApi.getOrcsTypes().getAttributeTypes().get(uuid);
      JaxEnumAttribute jaxEnumAttribute = createJaxEnumAttribute(orcsApi.getOrcsTypes().getAttributeTypes(), attrType);
      return Response.ok().entity(jaxEnumAttribute).build();
   }

   @Override
   public Response getEnumEntries(Long uuid) {
      IAttributeType attrType = orcsApi.getOrcsTypes().getAttributeTypes().get(uuid);
      JaxEnumAttribute jaxEnumAttribute = createJaxEnumAttribute(orcsApi.getOrcsTypes().getAttributeTypes(), attrType);
      return Response.ok().entity(jaxEnumAttribute.getEntries()).build();
   }

   @Override
   public Response importOrcsTypes(OrcsTypesData typesData) {
      TransactionBuilder tx =
         orcsApi.getTransactionFactory().createTransaction(COMMON, SystemUser.OseeSystem, "Add Types to Common Branch");
      for (OrcsTypeSheet sheet : typesData.getSheets()) {
         String guid = GUID.create();
         if (Strings.isValid(sheet.getGuid())) {
            guid = sheet.getGuid();
         }
         Long id = Lib.generateArtifactIdAsInt();
         if (Strings.isNumeric(sheet.getId())) {
            id = Long.valueOf(sheet.getId());
         }
         ArtifactId artifact = tx.createArtifact(CoreArtifactTypes.OseeTypeDefinition, sheet.getName(), guid, id);
         tx.setSoleAttributeValue(artifact, CoreAttributeTypes.Active, true);
         tx.setSoleAttributeFromString(artifact, CoreAttributeTypes.UriGeneralStringData, sheet.getTypesSheet());
      }
      tx.commit();

      tx = orcsApi.getTransactionFactory().createTransaction(COMMON, SystemUser.OseeSystem,
         "Add OseeTypeDef Tuples to Common Branch");
      for (ArtifactReadable artifact : orcsApi.getQueryFactory().fromBranch(COMMON).andIsOfType(
         CoreArtifactTypes.OseeTypeDefinition).getResults()) {
         tx.addTuple2(CoreTupleTypes.OseeTypeDef, OrcsTypesData.OSEE_TYPE_VERSION,
            artifact.getAttributes(CoreAttributeTypes.UriGeneralStringData).iterator().next());
      }
      tx.commit();
      return Response.ok().entity("Success").build();
   }

   @Override
   public Response dbInit() {
      Event event = new Event(OrcsTopicEvents.DBINIT_IMPORT_TYPES, (Map<String, ?>) null);
      eventAdmin.postEvent(event);
      return Response.ok().entity("Success").build();
   }

   public static final String LOAD_OSEE_TYPE_DEF_NAME_AND_ID =
      "select attr.value, attr.art_id, attr.attr_id, attr.attr_type_id from osee_attribute attr, osee_txs txs where txs.BRANCH_ID = ? " + //
         "and attr.gamma_id = txs.gamma_id and txs.TX_CURRENT = 1 and attr.art_id " + //
         "in (select distinct art_id from osee_attribute where attr_id in (ATTR_IDS)) order by attr_type_id desc";

   @Override
   public Response getConfig() {

      OrcsTypesConfig config = new OrcsTypesConfig();
      config.setCurrentVersion(OrcsTypesData.OSEE_TYPE_VERSION.intValue());
      List<Integer> attrIds = new LinkedList<>();

      jdbcService.getClient().runQuery(stmt1 -> {
         int version = stmt1.getInt("e1");
         final OrcsTypesVersion typeVersion = new OrcsTypesVersion();
         config.getVersions().add(typeVersion);
         typeVersion.setVersionNum(version);

         jdbcService.getClient().runQuery(stmt2 -> {
            OrcsTypesSheet sheet = new OrcsTypesSheet();
            sheet.setAttrId(stmt2.getInt("attr_id"));
            attrIds.add(new Long(sheet.getAttrId()).intValue());
            typeVersion.getSheets().add(sheet);
         }, OrcsTypes.LOAD_OSEE_TYPE_DEF_URIS, CoreTupleTypes.OseeTypeDef, CoreBranches.COMMON,
            TxChange.CURRENT.getValue(), typeVersion.getVersionNum(), TxChange.CURRENT.getValue());

      }, OrcsTypes.LOAD_OSEE_TYPE_VERSIONS, CoreTupleTypes.OseeTypeDef.getId());

      String query = LOAD_OSEE_TYPE_DEF_NAME_AND_ID.replace("ATTR_IDS", Collections.toString(",", attrIds));
      jdbcService.getClient().runQuery(stmt -> {
         long attrId = stmt.getLong("attr_id");
         long attrTypeId = stmt.getLong("attr_type_id");
         long artId = stmt.getLong("art_id");
         if (CoreAttributeTypes.UriGeneralStringData.getId().equals(attrTypeId)) {
            for (OrcsTypesSheet sheet : getSheetsFromAttrId(attrId, config)) {
               sheet.setArtifactId(artId);
            }
         } else if (CoreAttributeTypes.Name.getId().equals(attrTypeId)) {
            for (OrcsTypesSheet sheet : getSheetsFromArtId(artId, config)) {
               sheet.setName(stmt.getString("value"));
            }
         }
      }, query, CoreBranches.COMMON.getId());
      return Response.ok(config).build();
   }

   private Collection<OrcsTypesSheet> getSheetsFromArtId(Long artId, OrcsTypesConfig config) {
      List<OrcsTypesSheet> sheets = new LinkedList<>();
      for (OrcsTypesVersion version : config.getVersions()) {
         for (OrcsTypesSheet sheet : version.getSheets()) {
            if (artId.equals(sheet.getArtifactId())) {
               sheets.add(sheet);
            }
         }
      }
      return sheets;
   }

   private Collection<OrcsTypesSheet> getSheetsFromAttrId(Long attrId, OrcsTypesConfig config) {
      List<OrcsTypesSheet> sheets = new LinkedList<>();
      for (OrcsTypesVersion version : config.getVersions()) {
         for (OrcsTypesSheet sheet : version.getSheets()) {
            if (attrId.equals(sheet.getAttrId())) {
               sheets.add(sheet);
            }
         }
      }
      return sheets;
   }

   @Override
   public Response getConfigSheets() {
      List<OrcsTypesSheet> sheets = new LinkedList<>();
      for (ArtifactReadable art : orcsApi.getQueryFactory().fromBranch(CoreBranches.COMMON).andIsOfType(
         CoreArtifactTypes.OseeTypeDefinition, CoreArtifactTypes.OseeTypeDefinitionTemp).getResults()) {
         OrcsTypesSheet sheet = new OrcsTypesSheet();
         sheet.setArtifactId(art.getId());
         sheet.setName(art.getName());
         ResultSet<? extends AttributeReadable<Object>> attributes =
            art.getAttributes(CoreAttributeTypes.UriGeneralStringData);
         if (!attributes.isEmpty()) {
            sheet.setAttrId(attributes.iterator().next().getId());
         }
         sheets.add(sheet);
      }
      return Response.ok(sheets).build();
   }

   @SuppressWarnings("unused")
   @Override
   public Response setConfigSheets(OrcsTypesVersion version) {
      // clear out existing config, if any
      TransactionBuilder tx = orcsApi.getTransactionFactory().createTransaction(COMMON, SystemUser.OseeSystem,
         "Set OSEE Types Configuration");
      long verNum = version.getVersionNum();

      Iterable<Long> attrIds = orcsApi.getQueryFactory().tupleQuery().getTuple2Raw(CoreTupleTypes.OseeTypeDef, COMMON,
         Long.valueOf(version.getVersionNum()));
      for (Long attrId : attrIds) {
         throw new OseeStateException("Configuration already exist for version %s; these need to be manually removed",
            version);
      }

      // add type configuration
      for (OrcsTypesSheet sheet : version.getSheets()) {
         tx.addTuple2(CoreTupleTypes.OseeTypeDef, verNum, AttributeId.valueOf(sheet.getAttrId()));
      }

      tx.commit();
      return Response.ok(version).build();
   }

}

Back to the top