1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may 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 implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.adservices.data.adselection;
18 
19 import static androidx.room.ForeignKey.CASCADE;
20 
21 import android.adservices.common.AdTechIdentifier;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.net.Uri;
25 
26 import androidx.room.ColumnInfo;
27 import androidx.room.Embedded;
28 import androidx.room.Entity;
29 import androidx.room.ForeignKey;
30 import androidx.room.Index;
31 import androidx.room.TypeConverters;
32 
33 import com.android.adservices.data.common.FledgeRoomConverters;
34 
35 import com.google.auto.value.AutoValue;
36 
37 /** Table for records related to result of an ad selection run. */
38 @AutoValue
39 @AutoValue.CopyAnnotations
40 @Entity(
41         tableName = "ad_selection_result",
42         indices = {@Index(value = {"ad_selection_id", "winning_buyer", "winning_ad_render_uri"})},
43         foreignKeys =
44                 @ForeignKey(
45                         entity = DBAdSelectionInitialization.class,
46                         parentColumns = "ad_selection_id",
47                         childColumns = "ad_selection_id",
48                         onDelete = CASCADE),
49         primaryKeys = {"ad_selection_id"})
50 @TypeConverters({FledgeRoomConverters.class})
51 public abstract class DBAdSelectionResult {
52 
53     /** The id associated with this auction. */
54     @AutoValue.CopyAnnotations
55     @ColumnInfo(name = "ad_selection_id")
getAdSelectionId()56     public abstract long getAdSelectionId();
57 
58     /** The buyer whose ad won this ad selection run. */
59     @AutoValue.CopyAnnotations
60     @ColumnInfo(name = "winning_buyer")
61     @Nullable
getWinningBuyer()62     public abstract AdTechIdentifier getWinningBuyer();
63 
64     /** The winning ad bid for this ad selection run. */
65     @AutoValue.CopyAnnotations
66     @ColumnInfo(name = "winning_ad_bid")
getWinningAdBid()67     public abstract double getWinningAdBid();
68 
69     /** The winning ad render uri for this ad selection run. */
70     @AutoValue.CopyAnnotations
71     @ColumnInfo(name = "winning_ad_render_uri")
72     @Nullable
getWinningAdRenderUri()73     public abstract Uri getWinningAdRenderUri();
74 
75     /** The winning custom audience associated with this ad selection run. */
76     @AutoValue.CopyAnnotations
77     @Nullable
78     @Embedded(prefix = "winning_custom_audience_")
getWinningCustomAudience()79     public abstract DBWinningCustomAudience getWinningCustomAudience();
80 
81     /** Returns an AutoValue builder for a {@link DBAdSelectionResult} entity. */
82     @NonNull
builder()83     public static DBAdSelectionResult.Builder builder() {
84         return new AutoValue_DBAdSelectionResult.Builder();
85     }
86 
87     /**
88      * Creates a {@link DBAdSelectionResult} object using the builder.
89      *
90      * <p>Required for Room SQLite integration.
91      */
92     @NonNull
create( long adSelectionId, @Nullable AdTechIdentifier winningBuyer, double winningAdBid, @Nullable Uri winningAdRenderUri, @Nullable DBWinningCustomAudience winningCustomAudience)93     public static DBAdSelectionResult create(
94             long adSelectionId,
95             @Nullable AdTechIdentifier winningBuyer,
96             double winningAdBid,
97             @Nullable Uri winningAdRenderUri,
98             @Nullable DBWinningCustomAudience winningCustomAudience) {
99 
100         return builder()
101                 .setAdSelectionId(adSelectionId)
102                 .setWinningBuyer(winningBuyer)
103                 .setWinningAdBid(winningAdBid)
104                 .setWinningAdRenderUri(winningAdRenderUri)
105                 .setWinningCustomAudience(winningCustomAudience)
106                 .build();
107     }
108 
109     /** Builder class for a {@link DBAdSelectionResult}. */
110     @AutoValue.Builder
111     public abstract static class Builder {
112         /** Sets the id associated with this auction. */
setAdSelectionId(long adSelectionId)113         public abstract Builder setAdSelectionId(long adSelectionId);
114 
115         /** Sets the buyer whose ad won this ad selection run. */
setWinningBuyer(@ullable AdTechIdentifier winningBuyer)116         public abstract Builder setWinningBuyer(@Nullable AdTechIdentifier winningBuyer);
117 
118         /** Sets the winning ad bid for this ad selection run. */
setWinningAdBid(double winningAdBid)119         public abstract Builder setWinningAdBid(double winningAdBid);
120 
121         /** Sets the winning ad render uri for this ad selection run. */
setWinningAdRenderUri(@ullable Uri winningAdRenderUri)122         public abstract Builder setWinningAdRenderUri(@Nullable Uri winningAdRenderUri);
123 
124         /** Sets the winning custom audience associated with this ad selection run */
setWinningCustomAudience( @ullable DBWinningCustomAudience winningCustomAudience)125         public abstract Builder setWinningCustomAudience(
126                 @Nullable DBWinningCustomAudience winningCustomAudience);
127 
128         /** Builds a {@link DBAdSelectionResult} object. */
build()129         public abstract DBAdSelectionResult build();
130     }
131 }
132