1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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.TestAcknowledgmentEntity;
20 import com.android.vts.entity.TestEntity;
21 import com.android.vts.util.DatastoreHelper;
22 import com.google.appengine.api.datastore.DatastoreService;
23 import com.google.appengine.api.datastore.DatastoreServiceFactory;
24 import com.google.appengine.api.datastore.Entity;
25 import com.google.appengine.api.datastore.Query;
26 import com.google.appengine.api.users.UserServiceFactory;
27 import com.google.gson.Gson;
28 import com.google.gson.JsonObject;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.logging.Level;
33 import javax.servlet.RequestDispatcher;
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 
38 public class ShowTestAcknowledgmentServlet extends BaseServlet {
39     private static final String TEST_ACK_JSP = "WEB-INF/jsp/show_test_acknowledgments.jsp";
40 
41     @Override
getNavParentType()42     public PageType getNavParentType() {
43         return PageType.TOT;
44     }
45 
46     @Override
getBreadcrumbLinks(HttpServletRequest request)47     public List<Page> getBreadcrumbLinks(HttpServletRequest request) {
48         List<Page> links = new ArrayList<>();
49         return links;
50     }
51 
52     @Override
doGetHandler(HttpServletRequest request, HttpServletResponse response)53     public void doGetHandler(HttpServletRequest request, HttpServletResponse response)
54             throws IOException {
55         DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
56         Query ackQuery = new Query(TestAcknowledgmentEntity.KIND);
57         List<JsonObject> testAcks = new ArrayList<>();
58         for (Entity ackEntity :
59                 datastore.prepare(ackQuery).asIterable(DatastoreHelper.getLargeBatchOptions())) {
60             TestAcknowledgmentEntity ack = TestAcknowledgmentEntity.fromEntity(ackEntity);
61             if (ack == null) continue;
62             testAcks.add(ack.toJson());
63         }
64 
65         List<String> allTestNames = TestEntity.getAllTestNames();
66 
67         request.setAttribute("testAcknowledgments", new Gson().toJson(testAcks));
68         request.setAttribute("allTests", new Gson().toJson(allTestNames));
69         request.setAttribute("branches", new Gson().toJson(DatastoreHelper.getAllBranches()));
70         request.setAttribute("devices", new Gson().toJson(DatastoreHelper.getAllBuildFlavors()));
71         request.setAttribute("readOnly", new Gson().toJson(!UserServiceFactory.getUserService().isUserAdmin()));
72 
73         RequestDispatcher dispatcher = request.getRequestDispatcher(TEST_ACK_JSP);
74         try {
75             dispatcher.forward(request, response);
76         } catch (ServletException e) {
77             logger.log(Level.SEVERE, "Servlet Exception caught : ", e);
78         }
79     }
80 }
81