Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2370a05e114af21f8af09e1f70a9e1ccecef55e7 (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
/*******************************************************************************
 * Copyright (c) 2014 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.ats.rest.internal.config;

import java.net.URI;
import java.util.List;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import org.eclipse.osee.ats.api.IAtsObject;
import org.eclipse.osee.ats.api.config.BaseConfigEndpointApi;
import org.eclipse.osee.ats.api.config.JaxAtsObject;
import org.eclipse.osee.ats.api.data.AtsArtifactToken;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.util.IAtsChangeSet;
import org.eclipse.osee.ats.core.users.AtsCoreUsers;
import org.eclipse.osee.ats.rest.IAtsServer;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.IArtifactToken;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.data.ArtifactReadable;

/**
 * @author Donald G. Dunne
 */
public abstract class BaseConfigEndpointImpl<T extends JaxAtsObject> implements BaseConfigEndpointApi<T> {

   protected final IAtsServer atsServer;
   protected final IArtifactType artifactType;
   protected final IArtifactToken typeFolder;

   public BaseConfigEndpointImpl(IArtifactType artifactType, IArtifactToken typeFolder, IAtsServer atsServer) {
      this.artifactType = artifactType;
      this.typeFolder = typeFolder;
      this.atsServer = atsServer;
   }

   @Override
   @GET
   public List<T> get() throws Exception {
      return getObjects();
   }

   @Override
   @GET
   @Path("{uuid}")
   public T get(@PathParam("uuid") long uuid) throws Exception {
      return getObject(uuid);
   }

   @Override
   @POST
   public Response create(T jaxAtsObject) throws Exception {
      if (jaxAtsObject.getUuid() <= 0L) {
         throw new OseeStateException("Invalid uuid %d");
      } else if (!Strings.isValid(jaxAtsObject.getName())) {
         throw new OseeStateException("Invalid name [%d]");
      }
      ArtifactReadable artifact = atsServer.getArtifact(jaxAtsObject.getUuid());
      if (artifact != null) {
         throw new OseeStateException("Artifact with uuid %d already exists", jaxAtsObject.getUuid());
      }
      IAtsChangeSet changes =
         atsServer.getStoreService().createAtsChangeSet("Create " + artifactType.getName(), AtsCoreUsers.SYSTEM_USER);
      ArtifactId newArtifact =
         changes.createArtifact(artifactType, jaxAtsObject.getName(), GUID.create(), jaxAtsObject.getUuid());
      IAtsObject newAtsObject = atsServer.getConfigItemFactory().getConfigObject(newArtifact);
      if (typeFolder != null) {
         ArtifactReadable typeFolderArtifact = atsServer.getArtifact(typeFolder);
         if (typeFolderArtifact == null) {
            typeFolderArtifact = (ArtifactReadable) changes.createArtifact(AtsArtifactToken.CountryFolder);
         }
         if (typeFolderArtifact.getParent() == null) {
            ArtifactReadable headingFolder = atsServer.getArtifact(AtsArtifactToken.HeadingFolder);
            changes.relate(headingFolder, CoreRelationTypes.Default_Hierarchical__Child, typeFolderArtifact);
         }
         changes.relate(typeFolderArtifact, CoreRelationTypes.Default_Hierarchical__Child, newArtifact);
      }
      if (Strings.isValid(jaxAtsObject.getDescription())) {
         changes.setSoleAttributeValue(newAtsObject, AtsAttributeTypes.Description, jaxAtsObject.getDescription());
      } else {
         changes.deleteAttributes(newAtsObject, AtsAttributeTypes.Description);
      }
      changes.setSoleAttributeValue(newAtsObject, AtsAttributeTypes.Active, jaxAtsObject.isActive());
      create(jaxAtsObject, newArtifact, changes);
      changes.execute();
      return Response.created(new URI("/" + jaxAtsObject.getUuid())).build();
   }

   /**
    * Implement by subclass to perform other checks and sets during artifact creation
    */
   protected void create(T jaxAtsObject, ArtifactId newArtifact, IAtsChangeSet changes) {
      // provided for subclass implementation
   }

   @Override
   @DELETE
   public Response delete(@PathParam("uuid") long uuid) throws Exception {
      ArtifactReadable artifact = atsServer.getArtifact(uuid);
      if (artifact == null) {
         throw new OseeStateException("Artifact with uuid %d not found", uuid);
      }
      IAtsChangeSet changes =
         atsServer.getStoreService().createAtsChangeSet("Create " + artifactType.getName(), AtsCoreUsers.SYSTEM_USER);
      changes.deleteArtifact(artifact);
      changes.execute();
      return Response.ok().build();
   }

   public abstract T getConfigObject(ArtifactId artifact);

   protected T getObject(long uuid) {
      ArtifactReadable configArt = atsServer.getQuery().andUuid(uuid).getResults().getExactlyOne();
      return getConfigObject(configArt);
   }

   public abstract List<T> getObjects();

}

Back to the top