1 /*
2  * Copyright (c) 2017 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.entity;
18 
19 import static com.googlecode.objectify.ObjectifyService.ofy;
20 
21 import com.google.appengine.api.datastore.Entity;
22 import com.googlecode.objectify.Key;
23 import com.googlecode.objectify.annotation.Cache;
24 import com.googlecode.objectify.annotation.Id;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import lombok.Data;
28 import lombok.NoArgsConstructor;
29 
30 @com.googlecode.objectify.annotation.Entity(name = "TestPlan")
31 @Cache
32 @Data
33 @NoArgsConstructor
34 /** Entity describing test plan metadata. */
35 public class TestPlanEntity implements DashboardEntity {
36     protected static final Logger logger = Logger.getLogger(TestPlanEntity.class.getName());
37 
38     public static final String KIND = "TestPlan";
39 
40     // Property keys
41     public static final String TEST_PLAN_NAME = "testPlanName";
42 
43     @Id
44     public String testPlanName;
45 
46     /**
47      * Create a TestPlanEntity object.
48      *
49      * @param testPlanName The name of the test plan.
50      */
TestPlanEntity(String testPlanName)51     public TestPlanEntity(String testPlanName) {
52         this.testPlanName = testPlanName;
53     }
54 
toEntity()55     public Entity toEntity() {
56         Entity planEntity = new Entity(KIND, this.testPlanName);
57         return planEntity;
58     }
59 
getKey()60     public Key getKey() {
61         Key key = Key.create(TestPlanEntity.class, this.testPlanName);
62         return key;
63     }
64 
65     /**
66      * Convert an Entity object to a TestEntity.
67      *
68      * @param e The entity to process.
69      * @return TestEntity object with the properties from e processed, or null if incompatible.
70      */
71     @SuppressWarnings("unchecked")
fromEntity(Entity e)72     public static TestPlanEntity fromEntity(Entity e) {
73         if (!e.getKind().equals(KIND) || e.getKey().getName() == null
74                 || !e.hasProperty(TEST_PLAN_NAME)) {
75             logger.log(Level.WARNING, "Missing test plan attributes in entity: " + e.toString());
76             return null;
77         }
78         String testPlanName = e.getKey().getName();
79         return new TestPlanEntity(testPlanName);
80     }
81 
82     /** Saving function for the instance of this class */
83     @Override
save()84     public com.googlecode.objectify.Key<TestPlanEntity> save() {
85         return ofy().save().entity(this).now();
86     }
87 }
88