package com.google.sps.workspace;

import java.util.Objects;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Provides an interface to access and modify workspaces in the datastore.
 * The contents of this object will only reflect the current state of the 
 * entities. It will not update with the datastore.
 */
public class Workspace {
  private static final String KIND = "Workspace";

  private final transient DatastoreService datastore;
  private String studentUID;
  private String TaUID;
  private String workspaceID;
  
  private Workspace() {
    datastore = DatastoreServiceFactory.getDatastoreService();
  }

  public Workspace(String studentUID, String TaUID) {
    this();
    this.studentUID = Objects.requireNonNull(studentUID);
    this.TaUID = Objects.requireNonNull(TaUID);

    Entity entity = new Entity(KIND);
    entity.setProperty("studentUID", studentUID);
    entity.setProperty("taUID", TaUID);
    
    workspaceID = KeyFactory.keyToString(datastore.put(entity));
  }

  public Workspace(String workspaceID) throws EntityNotFoundException {
    this();
    this.workspaceID = Objects.requireNonNull(workspaceID);

    Entity entity = datastore.get(KeyFactory.stringToKey(workspaceID));
    studentUID = (String) entity.getProperty("studentUID");
    TaUID = (String) entity.getProperty("taUID");
  }

  /**
   * @return the studentUID
   */
  public String getStudentUID() {
    return studentUID;
  }

  /**
   * @return the taUID
   */
  public String getTaUID() {
    return TaUID;
  }

  /**
   * @return the workspaceID
   */
  public String getWorkspaceID() {
    return workspaceID;
  }

  public void update(String studentID, String TaUID) throws EntityNotFoundException {
    Entity entity = datastore.get(KeyFactory.stringToKey(workspaceID));
    entity.setProperty("studentUID", studentID);
    entity.setProperty("taUID", TaUID);
    datastore.put(entity);

    this.studentUID = studentID;
    this.TaUID = TaUID;
  }
}
@@ -32,35 +32,8 @@
 e;%0A%0A
-import java.util.Objects;%0A%0A
 impo
@@ -325,16 +325,42 @@
 Factory;
+%0Aimport java.util.Objects;
 %0A%0A/**%0A *
@@ -431,19 +431,16 @@
 tastore.
-%0A *
  The con
@@ -452,16 +452,19 @@
  of this
+%0A *
  object 
@@ -506,20 +506,16 @@
  of the 
-%0A * 
 entities
@@ -539,24 +539,27 @@
 ate with the
+%0A *
  datastore.%0A
@@ -775,18 +775,16 @@
 paceID;%0A
-  
 %0A  priva
@@ -1169,20 +1169,16 @@
 TaUID);%0A
-    
 %0A    wor
@@ -1569,37 +1569,32 @@
 ID%22);%0A  %7D%0A%0A  /**
-%0A   *
  @return the stu
@@ -1600,19 +1600,16 @@
 udentUID
-%0A  
  */%0A  pu
@@ -1663,37 +1663,32 @@
 tUID;%0A  %7D%0A%0A  /**
-%0A   *
  @return the taU
@@ -1689,19 +1689,16 @@
 he taUID
-%0A  
  */%0A  pu
@@ -1750,21 +1750,16 @@
 %7D%0A%0A  /**
-%0A   *
  @return
@@ -1774,19 +1774,16 @@
 kspaceID
-%0A  
  */%0A  pu
@@ -2033,24 +2033,47 @@
 tudentUID%22, 
+Objects.requireNonNull(
 studentID);%0A
@@ -2070,16 +2070,17 @@
 udentID)
+)
 ;%0A    en
@@ -2105,22 +2105,46 @@
 taUID%22, 
+Objects.requireNonNull(
 TaUID)
+)
 ;%0A    da
@@ -2227,8 +2227,9 @@
 D;%0A  %7D%0A%7D
+%0A

Update workspace update to check for null student and TA.