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 com.google.appengine.api.datastore.Entity;
20 import com.google.appengine.api.datastore.Key;
21 import com.google.appengine.api.datastore.KeyFactory;
22 import com.googlecode.objectify.annotation.Cache;
23 import com.googlecode.objectify.annotation.Id;
24 import lombok.Data;
25 import lombok.NoArgsConstructor;
26 
27 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import java.util.stream.Collectors;
31 
32 import static com.googlecode.objectify.ObjectifyService.ofy;
33 
34 @com.googlecode.objectify.annotation.Entity(name = "Branch")
35 @Cache
36 @Data
37 @NoArgsConstructor
38 /** Entity describing a branch. */
39 public class BranchEntity implements DashboardEntity {
40     protected static final Logger logger = Logger.getLogger(BranchEntity.class.getName());
41 
42     public static final String KIND = "Branch"; // The entity kind.
43 
44     @Id private String name;
45 
46     /**
47      * Create a BranchEntity object.
48      *
49      * @param branchName The name of the branch.
50      */
BranchEntity(String branchName)51     public BranchEntity(String branchName) {
52         this.name = branchName;
53     }
54 
getKey()55     public Key getKey() {
56         return KeyFactory.createKey(KIND, this.name);
57     }
58 
59     /** find by branch name */
getByBranch(String branchName)60     public static List<String> getByBranch(String branchName) {
61         if (branchName.equals("*")) {
62             return ofy().load()
63                     .type(BranchEntity.class)
64                     .limit(100)
65                     .list()
66                     .stream()
67                     .map(b -> b.name)
68                     .collect(Collectors.toList());
69         } else {
70             com.googlecode.objectify.Key startKey =
71                     com.googlecode.objectify.Key.create(BranchEntity.class, branchName);
72 
73             int lastPosition = branchName.length() - 1;
74             int lastCharValue = branchName.charAt(lastPosition);
75             String nextChar = String.valueOf((char) (lastCharValue + 1));
76 
77             String nextBranchName = branchName.substring(0, lastPosition) + nextChar;
78             com.googlecode.objectify.Key endKey =
79                     com.googlecode.objectify.Key.create(BranchEntity.class, nextBranchName);
80             return ofy().load()
81                     .type(BranchEntity.class)
82                     .filterKey(">=", startKey)
83                     .filterKey("<", endKey)
84                     .list()
85                     .stream()
86                     .map(b -> b.name)
87                     .collect(Collectors.toList());
88         }
89     }
90 
91     /** Saving function for the instance of this class */
92     @Override
save()93     public com.googlecode.objectify.Key<BranchEntity> save() {
94         return ofy().save().entity(this).now();
95     }
96 
97     /**
98      * Convert an Entity object to a BranchEntity.
99      *
100      * @param e The entity to process.
101      * @return BranchEntity object with the properties from e, or null if incompatible.
102      */
fromEntity(Entity e)103     public static BranchEntity fromEntity(Entity e) {
104         if (!e.getKind().equals(KIND) || e.getKey().getName() == null) {
105             logger.log(Level.WARNING, "Missing branch attributes in entity: " + e.toString());
106             return null;
107         }
108         String branchName = e.getKey().getName();
109         return new BranchEntity(branchName);
110     }
111 }
112