summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/entity/UserFavoriteEntity.java
blob: 254804298fc0aed5b1933fb13d509c8eb6dca7ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (c) 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.entity;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Ignore;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.logging.Level;
import java.util.logging.Logger;

import static com.googlecode.objectify.ObjectifyService.ofy;

@Cache
@com.googlecode.objectify.annotation.Entity(name = "UserFavorite")
@EqualsAndHashCode(of = "email")
@NoArgsConstructor
/** Entity describing subscriptions between a user and a test. */
public class UserFavoriteEntity implements DashboardEntity {
    protected static final Logger logger = Logger.getLogger(UserFavoriteEntity.class.getName());

    public static final String KIND = "UserFavorite";

    // Property keys
    public static final String USER = "user";
    public static final String TEST_KEY = "testKey";
    public static final String MUTE_NOTIFICATIONS = "muteNotifications";

    @Ignore private Key key = null;

    @Ignore public User user = null;

    @Ignore public Key testKey = null;

    @Getter @Setter private com.googlecode.objectify.Key<TestEntity> test;

    @Getter @Setter private String userEmail;

    @Getter @Setter public boolean muteNotifications;

    /**
     * Create a user favorite relationship.
     *
     * @param key The key of the entity in the database.
     * @param user The User object for the subscribing user.
     * @param testKey The key of the TestEntity object describing the test.
     * @param muteNotifications True if the subscriber has muted notifications, false otherwise.
     */
    private UserFavoriteEntity(Key key, User user, Key testKey, boolean muteNotifications) {
        this.key = key;
        this.user = user;
        this.testKey = testKey;
        this.muteNotifications = muteNotifications;
    }

    /**
     * Create a user favorite relationship.
     *
     * @param user The User object for the subscribing user.
     * @param testKey The key of the TestEntity object describing the test.
     * @param muteNotifications True if the subscriber has muted notifications, false otherwise.
     */
    public UserFavoriteEntity(User user, Key testKey, boolean muteNotifications) {
        this(null, user, testKey, muteNotifications);
    }

    /** Saving function for the instance of this class */
    @Override
    public com.googlecode.objectify.Key<UserFavoriteEntity> save() {
        return ofy().save().entity(this).now();
    }

    public Entity toEntity() {
        Entity favoriteEntity;
        if (this.key != null) {
            favoriteEntity = new Entity(key);
        } else {
            favoriteEntity = new Entity(KIND);
        }
        favoriteEntity.setProperty(USER, this.user);
        favoriteEntity.setProperty(TEST_KEY, this.testKey);
        favoriteEntity.setProperty(MUTE_NOTIFICATIONS, this.muteNotifications);
        return favoriteEntity;
    }

    /**
     * Convert an Entity object to a UserFavoriteEntity.
     *
     * @param e The entity to process.
     * @return UserFavoriteEntity object with the properties from e, or null if incompatible.
     */
    public static UserFavoriteEntity fromEntity(Entity e) {
        if (!e.getKind().equals(KIND) || !e.hasProperty(USER) || !e.hasProperty(TEST_KEY)) {
            logger.log(
                    Level.WARNING, "Missing user favorite attributes in entity: " + e.toString());
            return null;
        }
        try {
            User user = (User) e.getProperty(USER);
            Key testKey = (Key) e.getProperty(TEST_KEY);
            boolean muteNotifications = false;
            if (e.hasProperty(MUTE_NOTIFICATIONS)) {
                muteNotifications = (boolean) e.getProperty(MUTE_NOTIFICATIONS);
            }
            return new UserFavoriteEntity(e.getKey(), user, testKey, muteNotifications);
        } catch (ClassCastException exception) {
            // Invalid cast
            logger.log(Level.WARNING, "Error parsing user favorite entity.", exception);
        }
        return null;
    }
}