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.cobalt.data; 18 19 import androidx.annotation.NonNull; 20 import androidx.annotation.Nullable; 21 import androidx.room.ColumnInfo; 22 import androidx.room.Embedded; 23 import androidx.room.Entity; 24 import androidx.room.Ignore; 25 26 import com.google.auto.value.AutoValue; 27 import com.google.auto.value.AutoValue.CopyAnnotations; 28 29 import java.util.Optional; 30 31 /** Stores when reports were last sent. */ 32 @AutoValue 33 @CopyAnnotations 34 @Entity( 35 tableName = "Reports", 36 primaryKeys = {"customer_id", "project_id", "metric_id", "report_id"}) 37 abstract class ReportEntity { 38 /** Values uniquely identifying the report. */ 39 @CopyAnnotations 40 @Embedded 41 @NonNull reportKey()42 abstract ReportKey reportKey(); 43 44 /** Day the report was last sent, can be empty if not yet sent. */ 45 @CopyAnnotations 46 @ColumnInfo(name = "last_sent_day_index") 47 @Nullable lastSentDayIndex()48 abstract Optional<Integer> lastSentDayIndex(); 49 50 /** 51 * Creates a {@link ReportEntity}. 52 * 53 * <p>Used by Room to instantiate objects. 54 */ 55 @NonNull create(ReportKey reportKey, Optional<Integer> lastSentDayIndex)56 static ReportEntity create(ReportKey reportKey, Optional<Integer> lastSentDayIndex) { 57 return new AutoValue_ReportEntity(reportKey, lastSentDayIndex); 58 } 59 60 /** 61 * Creates a {@link ReportEntity} without a last sent day index. 62 * 63 * <p>Ignored by Room. 64 */ 65 @Ignore 66 @NonNull create(ReportKey reportKey)67 static ReportEntity create(ReportKey reportKey) { 68 return new AutoValue_ReportEntity(reportKey, Optional.empty()); 69 } 70 71 /** 72 * Creates a {@link ReportEntity} with a last sent day index. 73 * 74 * <p>Ignored by Room. 75 */ 76 @Ignore 77 @NonNull create(ReportKey reportKey, int lastSentDayIndex)78 static ReportEntity create(ReportKey reportKey, int lastSentDayIndex) { 79 return new AutoValue_ReportEntity(reportKey, Optional.of(lastSentDayIndex)); 80 } 81 } 82