1 /*
2  * Copyright (c) 2016 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you
5  * may not use this file except in compliance with the License. You may
6  * obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 package com.android.vts.servlet;
18 
19 import com.android.vts.entity.TestPlanEntity;
20 import com.android.vts.entity.TestSuiteResultEntity;
21 import com.google.appengine.api.datastore.DatastoreService;
22 import com.google.appengine.api.datastore.DatastoreServiceFactory;
23 import com.google.appengine.api.datastore.Entity;
24 import com.google.appengine.api.datastore.Query;
25 import com.google.appengine.api.users.UserServiceFactory;
26 
27 import javax.servlet.RequestDispatcher;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Comparator;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.logging.Level;
38 import java.util.stream.Collectors;
39 
40 import static com.googlecode.objectify.ObjectifyService.ofy;
41 
42 /**
43  * Represents the servlet that is invoked on loading the release page.
44  */
45 public class ShowReleaseServlet extends BaseServlet {
46 
47   @Override
getNavParentType()48   public PageType getNavParentType() {
49     return PageType.RELEASE;
50   }
51 
52   @Override
getBreadcrumbLinks(HttpServletRequest request)53   public List<Page> getBreadcrumbLinks(HttpServletRequest request) {
54     return null;
55   }
56 
57   @Override
doGetHandler(HttpServletRequest request, HttpServletResponse response)58   public void doGetHandler(HttpServletRequest request, HttpServletResponse response)
59       throws IOException {
60     String testType =
61         request.getParameter("type") == null ? "plan" : request.getParameter("type");
62 
63     RequestDispatcher dispatcher;
64     if (testType.equalsIgnoreCase("plan")) {
65       dispatcher = this.getTestPlanDispatcher(request, response);
66     } else {
67       dispatcher = this.getTestSuiteDispatcher(request, response);
68     }
69 
70     try {
71       request.setAttribute("testType", testType);
72       response.setStatus(HttpServletResponse.SC_OK);
73       dispatcher.forward(request, response);
74     } catch (ServletException e) {
75       logger.log(Level.SEVERE, "Servlet Excpetion caught : ", e);
76     }
77   }
78 
getTestPlanDispatcher( HttpServletRequest request, HttpServletResponse response)79   private RequestDispatcher getTestPlanDispatcher(
80       HttpServletRequest request, HttpServletResponse response) {
81     String RELEASE_JSP = "WEB-INF/jsp/show_release.jsp";
82 
83     List<TestPlanEntity> testPlanEntityList = ofy().load().type(TestPlanEntity.class).list();
84 
85     List<String> plans = testPlanEntityList.stream()
86         .sorted(Comparator.comparing(TestPlanEntity::getTestPlanName))
87         .map(te -> te.getTestPlanName()).collect(Collectors.toList());
88 
89     request.setAttribute("isAdmin", UserServiceFactory.getUserService().isUserAdmin());
90     request.setAttribute("planNames", plans);
91     RequestDispatcher dispatcher = request.getRequestDispatcher(RELEASE_JSP);
92     return dispatcher;
93   }
94 
getTestSuiteDispatcher( HttpServletRequest request, HttpServletResponse response)95   private RequestDispatcher getTestSuiteDispatcher(
96       HttpServletRequest request, HttpServletResponse response) {
97     String RELEASE_JSP = "WEB-INF/jsp/show_release.jsp";
98 
99     List<TestSuiteResultEntity> suiteResultEntityList = TestSuiteResultEntity.getTestSuitePlans();
100 
101     List<String> plans =
102         suiteResultEntityList
103             .stream()
104             .map(suiteEntity -> suiteEntity.getSuitePlan())
105             .collect(Collectors.toList());
106     request.setAttribute("isAdmin", UserServiceFactory.getUserService().isUserAdmin());
107     request.setAttribute("planNames", plans);
108     RequestDispatcher dispatcher = request.getRequestDispatcher(RELEASE_JSP);
109     return dispatcher;
110   }
111 }
112