summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/servlet/ShowReleaseServlet.java
blob: ae175ae7602554e215201d51a118e775a3b79dcc (plain)
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
/*
 * Copyright (c) 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.servlet;

import com.android.vts.entity.TestPlanEntity;
import com.android.vts.entity.TestSuiteResultEntity;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.UserServiceFactory;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;

import static com.googlecode.objectify.ObjectifyService.ofy;

/**
 * Represents the servlet that is invoked on loading the release page.
 */
public class ShowReleaseServlet extends BaseServlet {

  @Override
  public PageType getNavParentType() {
    return PageType.RELEASE;
  }

  @Override
  public List<Page> getBreadcrumbLinks(HttpServletRequest request) {
    return null;
  }

  @Override
  public void doGetHandler(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    String testType =
        request.getParameter("type") == null ? "plan" : request.getParameter("type");

    RequestDispatcher dispatcher;
    if (testType.equalsIgnoreCase("plan")) {
      dispatcher = this.getTestPlanDispatcher(request, response);
    } else {
      dispatcher = this.getTestSuiteDispatcher(request, response);
    }

    try {
      request.setAttribute("testType", testType);
      response.setStatus(HttpServletResponse.SC_OK);
      dispatcher.forward(request, response);
    } catch (ServletException e) {
      logger.log(Level.SEVERE, "Servlet Excpetion caught : ", e);
    }
  }

  private RequestDispatcher getTestPlanDispatcher(
      HttpServletRequest request, HttpServletResponse response) {
    String RELEASE_JSP = "WEB-INF/jsp/show_release.jsp";

    List<TestPlanEntity> testPlanEntityList = ofy().load().type(TestPlanEntity.class).list();

    List<String> plans = testPlanEntityList.stream()
        .sorted(Comparator.comparing(TestPlanEntity::getTestPlanName))
        .map(te -> te.getTestPlanName()).collect(Collectors.toList());

    request.setAttribute("isAdmin", UserServiceFactory.getUserService().isUserAdmin());
    request.setAttribute("planNames", plans);
    RequestDispatcher dispatcher = request.getRequestDispatcher(RELEASE_JSP);
    return dispatcher;
  }

  private RequestDispatcher getTestSuiteDispatcher(
      HttpServletRequest request, HttpServletResponse response) {
    String RELEASE_JSP = "WEB-INF/jsp/show_release.jsp";

    List<TestSuiteResultEntity> suiteResultEntityList = TestSuiteResultEntity.getTestSuitePlans();

    List<String> plans =
        suiteResultEntityList
            .stream()
            .map(suiteEntity -> suiteEntity.getSuitePlan())
            .collect(Collectors.toList());
    request.setAttribute("isAdmin", UserServiceFactory.getUserService().isUserAdmin());
    request.setAttribute("planNames", plans);
    RequestDispatcher dispatcher = request.getRequestDispatcher(RELEASE_JSP);
    return dispatcher;
  }
}