// Copyright 2020 Google LLC
//
// 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
//
//     https://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.google.graphgeckos.dashboard.datatypes;

import com.google.cloud.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;

/**
 * Contains the information retrieved from a single build bot. It is used as a member
 * of {@link BuildInfo}
 */
@Entity(name = "builder")
public class BuildBotData {

  /**
   * The commit hash, it is fetched from the BuildBot API.
   * This field is transient because it is only needed to
   * determine which commit this data belongs to and put
   * it in the right {@link BuildInfo} in the storage.
   * Used in {@link com.google.graphgeckos.dashboard.storage.DatastoreRepository}.
   */
  private String commitHash;

  /* The timestamp of the build. */
  private Timestamp timestamp;

  /* Name of the buildbot. */
  private String name;

  /**
   * The logs of each compilation stage, stored as described by {@link Log}.
   */
  private final List<Log> logs = new ArrayList<>();

  /**
   * Builder compilation status, as described by {@link BuilderStatus}.
   */
  private BuilderStatus status;

  public BuildBotData(String commitHash, String name, List<Log> logs, BuilderStatus status) {
    this.commitHash = commitHash;
    this.name = name;
    this.logs.addAll(logs);
    this.status = status;
  }

  public BuildBotData() {}

  /**
   * Returns the name of the builder bot. Cannot be null.
   */
  public String getName() {
    return name;
  }

  /**
   * Returns the list of logs for each compilation stage. Cannot be null.
   */
  public List<Log> getLogs() {
    return logs;
  }

  /**
   * Returns the compilation status of the builder. Cannot be null.
   */
  public BuilderStatus getStatus() {
    return status;
  }

  /**
   * Returns the commit hash of the commit tested by the current buildbot.
   */
  public String getCommitHash() {
    return commitHash;
  }

  /**
   * Returns the timestamp of the build.
   */
  public Timestamp getTimestamp() {
    return timestamp;
  }

}

@@ -793,16 +793,142 @@
 .Entity;
+%0Aimport org.springframework.cloud.gcp.data.datastore.core.mapping.Field;%0Aimport org.springframework.data.annotation.Transient;
 %0A%0A/**%0A *
@@ -1410,32 +1410,75 @@
 ository%7D.%0A   */%0A
+  @Transient%0A  @Field(name = %22commitHash%22)%0A
   private String
@@ -1495,16 +1495,23 @@
 h;%0A%0A  /*
+* %0A   *
  The tim
@@ -1531,19 +1531,52 @@
  build. 
+%0A   
 */%0A
+  @Field(name = %22timestamp%22)%0A
   privat
@@ -1603,16 +1603,22 @@
 p;%0A%0A  /*
+*%0A   *
  Name of
@@ -1631,19 +1631,46 @@
 uildbot.
- */
+%0A   */%0A  @Field(name = %22name%22)
 %0A  priva
@@ -1767,32 +1767,56 @@
 ink Log%7D.%0A   */%0A
+  @Field(name = %22logs%22)%0A
   private final 
@@ -1936,16 +1936,42 @@
 .%0A   */%0A
+  @Field(name = %22status%22)%0A
   privat
@@ -1995,16 +1995,243 @@
 tatus;%0A%0A
+  /**%0A   * Sets all fields to null. Only to be used by Spring GCP.%0A   */%0A  public BuildBotData() %7B%0A    this.commitHash = null;%0A    this.timestamp = null;%0A    this.name = null;%0A    this.logs = null;%0A    this.status = null;%0A  %7D%0A%0A
   public
@@ -2435,36 +2435,8 @@
  %7D%0A%0A
-  public BuildBotData() %7B%7D%0A%0A
   /*
@@ -3096,10 +3096,790 @@
 p;%0A  %7D%0A%0A
+    /**%0A   * Returns the name of the builder bot. Cannot be null.%0A   */%0A  public void setName(String name) %7B%0A    this.name = name;%0A  %7D%0A%0A  /**%0A   * Returns the list of logs for each compilation stage. Cannot be null.%0A   */%0A  public void getLogs(List%3CLog%3E logs) %7B%0A    this.logs = new ArrayList%3C%3E(logs);%0A  %7D%0A%0A  /**%0A   * Returns the compilation status of the builder. Cannot be null.%0A   */%0A  public void getStatus(BuilderStatus status) %7B%0A    this.status = status;%0A  %7D%0A%0A  /**%0A   * Returns the commit hash of the commit tested by the current buildbot.%0A   */%0A  public void getCommitHash(String commitHash) %7B%0A    this.commitHash = commitHash;%0A  %7D%0A%0A  /**%0A   * Returns the timestamp of the build.%0A   */%0A  public void getTimestamp(Timestamp timestamp) %7B%0A    this.timestamp = timestamp;%0A  %7D%0A%0A
 %7D%0A

update buildbotdata