Skip to main content
summaryrefslogtreecommitdiffstats
blob: 900dbb907471fbfb90540b90b89d395a471f6236 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.manager.servlet.ats;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.manager.servlet.DataServlet;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.orcs.OrcsApi;
import org.w3c.dom.Node;

/**
 * @author Roberto E. Escobar
 */
public class AtsService {

   private static enum DataTypeEnum {
      PROGRAM,
      BUILD,
      WORKFLOW;

      public String asFileName() {
         return "ats." + this.name().toLowerCase() + ".data.xml";
      }
   }

   public static interface IResourceProvider {
      IResource getResource(String typeName) throws OseeCoreException;
   }

   private final IResourceProvider resourceProvider;
   private final AtsXmlSearch xmlSearch;
   private final AtsXmlMessages messages;
   private final IResourceManager resourceManager;
   private final OrcsApi orcsApi;

   public AtsService(IResourceProvider resourceProvider, AtsXmlSearch xmlSearch, AtsXmlMessages messages, IResourceManager resourceManager, OrcsApi orcsApi) {
      this.xmlSearch = xmlSearch;
      this.messages = messages;
      this.resourceProvider = resourceProvider;
      this.resourceManager = resourceManager;
      this.orcsApi = orcsApi;
   }

   public void performOperation(IResource resource, HttpServletResponse response) {
      try {
         Collection<OperationData> requests = OperationData.fromResource(resource);
         for (OperationData data : requests) {
            switch (data.getOperationType()) {
               case GET_BUILDS_BY_PROGRAM_ID:
                  getBuilds(response, data.getProgramId());
                  break;
               case GET_CHANGE_REPORTS_BY_IDS:
                  getChangeReport(response, data.getItemIds());
                  break;
               case GET_PROGRAMS:
                  getPrograms(response);
                  break;
               case GET_WORKFLOWS_BY_IDS:
                  getWorkflowsById(response, data.getItemIds());
                  break;
               case GET_WORKFLOWS_BY_PROGRAM_AND_BUILD_ID:
                  getWorkflowsByProgramAndBuild(response, data.getProgramId(), data.getBuildId());
                  break;
               default:
                  throw new UnsupportedOperationException();
            }
         }
      } catch (Exception ex) {
         messages.sendError(response, ex);
      }
   }

   private IResource getResource(DataTypeEnum fileType) throws OseeCoreException {
      return getResource(fileType.asFileName());
   }

   private IResource getResource(String resource) throws OseeCoreException {
      String urlPath = String.format("%s://%s", AtsResourceLocatorProvider.PROTOCOL, resource);
      return resourceProvider.getResource(urlPath);
   }

   public void getPrograms(HttpServletResponse response) throws OseeCoreException {
      IResource resource = getResource(DataTypeEnum.PROGRAM);
      Collection<Node> nodes = xmlSearch.findPrograms(resource);
      messages.sendPrograms(response, nodes);
   }

   public void getBuilds(HttpServletResponse response, String programId) throws OseeCoreException {
      IResource resource = getResource(DataTypeEnum.BUILD);
      Collection<Node> nodes = xmlSearch.findBuildsByProgramId(resource, programId);
      messages.sendBuilds(response, nodes);
   }

   public void getWorkflowsById(HttpServletResponse response, String idSearch) throws OseeCoreException {
      IResource resource = getResource(DataTypeEnum.WORKFLOW);
      Collection<Node> nodes = xmlSearch.findWorkflowsById(resource, idSearch);
      messages.sendWorkflows(response, nodes);
   }

   public void getWorkflowsByProgramAndBuild(HttpServletResponse response, String programId, String buildId) throws OseeCoreException {
      IResource resource = getResource(DataTypeEnum.WORKFLOW);
      Collection<Node> nodes = xmlSearch.findWorkflowsByProgramAndBuild(resource, programId, buildId);
      messages.sendWorkflows(response, nodes);
   }

   public void getChangeReport(HttpServletResponse response, String idSearch) throws OseeCoreException {
      IResource resource = getResource(DataTypeEnum.WORKFLOW);
      Collection<Node> nodes = xmlSearch.findWorkflowsById(resource, idSearch);
      messages.sendChangeReports(response, nodes);
   }

   public void sendClient(HttpServletRequest request, HttpServletResponse response) {
      String urlPath = request.getParameter("url");
      boolean wasErrorSent = false;
      IResource resource = null;
      try {
         if (Strings.isValid(urlPath)) {
            resource = resourceProvider.getResource(urlPath);
         } else {
            String servletPath = request.getServletPath();
            urlPath = request.getRequestURI().replace(servletPath, "");

            if (urlPath.contains("osee/data")) {
               DataServlet.handleUriRequest(resourceManager, urlPath, response, orcsApi);
               return;
            } else {
               resource = getResource(urlPath);
            }
         }
      } catch (OseeCoreException ex) {
         messages.sendError(response, ex);
         wasErrorSent = true;
      }

      if (resource == null && !wasErrorSent) {
         messages.sendError(response, new Exception(String.format("Resource not found - [%s]", urlPath)));
      } else {
         messages.sendResource(response, resource.getName(), resource);
      }
   }
}

Back to the top