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