Skip to main content
summaryrefslogtreecommitdiffstats
blob: 434aeb16e83fd5d4f9df3cac510befc9378f2007 (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
/*******************************************************************************
 * Copyright (c) 2013 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.resources;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.eclipse.osee.ats.api.IAtsWorkItem;
import org.eclipse.osee.ats.api.ai.IAtsActionableItem;
import org.eclipse.osee.ats.api.data.AtsArtifactTypes;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.util.IAtsChangeSet;
import org.eclipse.osee.ats.api.workflow.ChangeType;
import org.eclipse.osee.ats.api.workflow.IAtsAction;
import org.eclipse.osee.ats.core.util.AtsUtilCore;
import org.eclipse.osee.ats.impl.IAtsServer;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.jdk.core.type.IResourceRegistry;
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.AHTML;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;

/**
 * @author Donald G. Dunne
 */
@Path("action")
public final class ActionResource {

   private final IAtsServer atsServer;
   private final OrcsApi orcsApi;
   private final IResourceRegistry resourceRegistry;

   public ActionResource(IAtsServer atsServer, OrcsApi orcsApi, IResourceRegistry resourceRegistry) {
      this.resourceRegistry = resourceRegistry;
      this.atsServer = atsServer;
      this.orcsApi = orcsApi;
   }

   @GET
   @Produces(MediaType.TEXT_HTML)
   public String get() throws Exception {
      return AHTML.simplePage("Action Resource");
   }

   /**
    * @param id (guid, atsId) of action to display
    * @return html representation of the action
    */
   @Path("{id}")
   @GET
   @Produces(MediaType.TEXT_HTML)
   public String getAction(@PathParam("id") String id) throws Exception {
      ArtifactReadable action = atsServer.getActionById(id);
      if (action == null) {
         return AHTML.simplePage(String.format("Action with id [%s] can not be found", id));
      }
      return atsServer.getWorkItemPage().getHtml(action, "Action - " + id, resourceRegistry, false);
   }

   /**
    * @param id (guid, atsId) of action to display
    * @return html representation of the action
    */
   @Path("{id}/details")
   @GET
   @Produces(MediaType.TEXT_HTML)
   public String getActionWithDetails(@PathParam("id") String id) throws Exception {
      ArtifactReadable action = atsServer.getActionById(id);
      if (action == null) {
         return AHTML.simplePage(String.format("Action with id [%s] can not be found", id));
      }
      return atsServer.getWorkItemPage().getHtml(action, "Action - " + id, resourceRegistry, true);
   }

   /**
    * @param form containing information to create a new action
    * @param form.ats_title - title of new action
    * @param form.desc - description of the action
    * @param form.actionableItems - actionable item name
    * @param form.changeType - Improvement, Refinement, Problem, Support
    * @param form.priority - 1-5
    * @param uriInfo
    * @return html representation of action created
    * @throws Exception
    */
   @POST
   @Consumes("application/x-www-form-urlencoded")
   public Response createAction(MultivaluedMap<String, String> form, @Context UriInfo uriInfo) throws Exception {
      // get parameters
      String htmlStr = "";
      String query = uriInfo.getPath();
      System.out.println("query [" + query + "]");
      String searchId = form.getFirst("searchId");

      if (Strings.isValid(searchId)) {
         htmlStr = getSearchResults(searchId);
      } else {
         String title = form.getFirst("ats_title");
         String description = form.getFirst("desc");
         String actionableItemName = form.getFirst("actionableItems");
         String changeTypeStr = form.getFirst("changeType");
         String priority = form.getFirst("priority");
         String userId = form.getFirst("userId");

         Conditions.checkNotNullOrEmpty(userId, "userId");
         IAtsUser atsUser = atsServer.getUserService().getUserById(userId);
         if (atsUser == null) {
            throw new OseeStateException("User by id [%s] does not exist", userId);
         }

         IAtsChangeSet changes = atsServer.getStoreFactory().createAtsChangeSet("Create Action - Server", atsUser);
         orcsApi.getTransactionFactory(null).createTransaction(AtsUtilCore.getAtsBranch(),
            ((ArtifactReadable) atsUser.getStoreObject()), "Create Action - Server");

         List<IAtsActionableItem> aias = new ArrayList<IAtsActionableItem>();

         ArtifactReadable aiArt =
            atsServer.getQuery().andTypeEquals(AtsArtifactTypes.ActionableItem).andNameEquals(actionableItemName).getResults().getExactlyOne();
         IAtsActionableItem aia = (IAtsActionableItem) atsServer.getConfig().getSoleByGuid(aiArt.getGuid());
         aias.add(aia);

         ChangeType changeType = ChangeType.valueOf(changeTypeStr);

         IAtsAction action =
            atsServer.getActionFactory().createAction(atsUser, title, description, changeType, priority, false, null,
               aias, new Date(), atsUser, null, changes).getFirst();
         changes.execute();

         htmlStr =
            atsServer.getWorkItemPage().getHtml(
               ((ArtifactReadable) action.getTeamWorkflows().iterator().next().getStoreObject()),
               "Action Created - " + action.getGuid(), resourceRegistry, false);
      }

      return Response.status(200).entity(htmlStr).build();
   }

   private String getSearchResults(String searchId) throws Exception {
      String results = null;
      ArtifactReadable action = null;
      if (GUID.isValid(searchId)) {
         action = atsServer.getArtifactByGuid(searchId);
      }
      if (action != null) {
         IAtsWorkItem workItem = atsServer.getWorkItemFactory().getWorkItem(action);
         if (workItem != null) {
            results = atsServer.getWorkItemPage().getHtml(action, "Action - " + searchId, resourceRegistry, false);
         } else {
            results = AHTML.simplePage(String.format("Undisplayable %s", action));
         }
      }
      if (!Strings.isValid(results)) {
         for (IAttributeType attrType : Arrays.asList(AtsAttributeTypes.AtsId, AtsAttributeTypes.LegacyPcrId)) {
            ResultSet<ArtifactReadable> legacyQueryResults =
               atsServer.getQuery().and(attrType, org.eclipse.osee.framework.core.enums.Operator.EQUAL, searchId).getResults();
            if (legacyQueryResults.size() == 1) {
               results =
                  atsServer.getWorkItemPage().getHtml(legacyQueryResults.getExactlyOne(), "Action - " + searchId,
                     resourceRegistry, false);
               break;
            } else if (legacyQueryResults.size() > 1) {
               results = getGuidListHtml(legacyQueryResults);
               break;
            }
         }
      }
      if (!Strings.isValid(results)) {
         results = AHTML.simplePage(String.format("Unknown Id [%s]", searchId));
      }
      return results;
   }

   private String getGuidListHtml(ResultSet<ArtifactReadable> results2) {
      StringBuilder sb = new StringBuilder("Returned ");
      sb.append(results2.size());
      sb.append(" results.");
      sb.append(AHTML.newline(2));
      for (ArtifactReadable art : results2) {
         sb.append(AHTML.getHyperlink("/ats/action/" + art.getGuid(), art.toString()));
         sb.append(AHTML.newline());
      }
      return AHTML.simplePage(sb.toString());
   }
}

Back to the top