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.users.User;
22 import com.googlecode.objectify.annotation.Cache;
23 import com.googlecode.objectify.annotation.Ignore;
24 import lombok.EqualsAndHashCode;
25 import lombok.Getter;
26 import lombok.NoArgsConstructor;
27 import lombok.Setter;
28 
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 
32 import static com.googlecode.objectify.ObjectifyService.ofy;
33 
34 @Cache
35 @com.googlecode.objectify.annotation.Entity(name = "UserFavorite")
36 @EqualsAndHashCode(of = "email")
37 @NoArgsConstructor
38 /** Entity describing subscriptions between a user and a test. */
39 public class UserFavoriteEntity implements DashboardEntity {
40     protected static final Logger logger = Logger.getLogger(UserFavoriteEntity.class.getName());
41 
42     public static final String KIND = "UserFavorite";
43 
44     // Property keys
45     public static final String USER = "user";
46     public static final String TEST_KEY = "testKey";
47     public static final String MUTE_NOTIFICATIONS = "muteNotifications";
48 
49     @Ignore private Key key = null;
50 
51     @Ignore public User user = null;
52 
53     @Ignore public Key testKey = null;
54 
55     @Getter @Setter private com.googlecode.objectify.Key<TestEntity> test;
56 
57     @Getter @Setter private String userEmail;
58 
59     @Getter @Setter public boolean muteNotifications;
60 
61     /**
62      * Create a user favorite relationship.
63      *
64      * @param key The key of the entity in the database.
65      * @param user The User object for the subscribing user.
66      * @param testKey The key of the TestEntity object describing the test.
67      * @param muteNotifications True if the subscriber has muted notifications, false otherwise.
68      */
UserFavoriteEntity(Key key, User user, Key testKey, boolean muteNotifications)69     private UserFavoriteEntity(Key key, User user, Key testKey, boolean muteNotifications) {
70         this.key = key;
71         this.user = user;
72         this.testKey = testKey;
73         this.muteNotifications = muteNotifications;
74     }
75 
76     /**
77      * Create a user favorite relationship.
78      *
79      * @param user The User object for the subscribing user.
80      * @param testKey The key of the TestEntity object describing the test.
81      * @param muteNotifications True if the subscriber has muted notifications, false otherwise.
82      */
UserFavoriteEntity(User user, Key testKey, boolean muteNotifications)83     public UserFavoriteEntity(User user, Key testKey, boolean muteNotifications) {
84         this(null, user, testKey, muteNotifications);
85     }
86 
87     /** Saving function for the instance of this class */
88     @Override
save()89     public com.googlecode.objectify.Key<UserFavoriteEntity> save() {
90         return ofy().save().entity(this).now();
91     }
92 
toEntity()93     public Entity toEntity() {
94         Entity favoriteEntity;
95         if (this.key != null) {
96             favoriteEntity = new Entity(key);
97         } else {
98             favoriteEntity = new Entity(KIND);
99         }
100         favoriteEntity.setProperty(USER, this.user);
101         favoriteEntity.setProperty(TEST_KEY, this.testKey);
102         favoriteEntity.setProperty(MUTE_NOTIFICATIONS, this.muteNotifications);
103         return favoriteEntity;
104     }
105 
106     /**
107      * Convert an Entity object to a UserFavoriteEntity.
108      *
109      * @param e The entity to process.
110      * @return UserFavoriteEntity object with the properties from e, or null if incompatible.
111      */
fromEntity(Entity e)112     public static UserFavoriteEntity fromEntity(Entity e) {
113         if (!e.getKind().equals(KIND) || !e.hasProperty(USER) || !e.hasProperty(TEST_KEY)) {
114             logger.log(
115                     Level.WARNING, "Missing user favorite attributes in entity: " + e.toString());
116             return null;
117         }
118         try {
119             User user = (User) e.getProperty(USER);
120             Key testKey = (Key) e.getProperty(TEST_KEY);
121             boolean muteNotifications = false;
122             if (e.hasProperty(MUTE_NOTIFICATIONS)) {
123                 muteNotifications = (boolean) e.getProperty(MUTE_NOTIFICATIONS);
124             }
125             return new UserFavoriteEntity(e.getKey(), user, testKey, muteNotifications);
126         } catch (ClassCastException exception) {
127             // Invalid cast
128             logger.log(Level.WARNING, "Error parsing user favorite entity.", exception);
129         }
130         return null;
131     }
132 }
133