package com.google.sps.data;

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

public final class Folder {

    private String folderName;
    private String folderDefaultLanguage;
    private String folderKey;

    public Folder(
        String folderName,
        String folderDefaultLanguage) {
        
        this.folderName = folderName;
        this.folderDefaultLanguage = folderDefaultLanguage;
        this.folderKey = "null";
    }

    public String getFolderName() {
        return this.folderName;
    }

    public String getFolderDefaultLanguage() {
        return this.folderDefaultLanguage;
    }

    public String getFolderKey() {
        return this.folderKey;
    }

    public void setFolderName(String newFolderName) {
        this.folderName = newFolderName;
    }

    public void setFolderKey(String folderKey) {
        this.folderKey = folderKey;
    }

    public Entity createEntity(Key userKey) {
        
        // Set owner of folder 
        Entity folder = new Entity("Folder", userKey);

        // Store initial folder entity without properties to generate auto ID for entity
        // This is necessary to obtain a "complete" key
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(folder);

        folder.setProperty("folderName", this.folderName);
        folder.setProperty("folderDefaultLanguage", this.folderDefaultLanguage);
        folder.setProperty("folderKey", KeyFactory.keyToString(folder.getKey()));

        return folder;
    }

    // Returns a folder instance from a given Entity
    public static Folder EntityToFolder(Entity entity) {
        
        String folderName = (String) entity.getProperty("folderName");
        String folderDefaultLanguage = (String) entity.getProperty("folderDefaultLanguage");
        String folderKey = (String) entity.getProperty("folderKey");

        Folder folder = new Folder(folderName, folderDefaultLanguage);
        folder.setFolderKey(folderKey);

        return folder;
    }
}
@@ -660,32 +660,301 @@
  %22null%22;%0A    %7D%0A%0A
+    public Folder(Entity entity) %7B%0A        this.folderName = (String) entity.getProperty(%22folderName%22);%0A        this.folderDefaultLanguage = (String) entity.getProperty(%22folderDefaultLanguage%22);%0A        this.folderKey = (String) entity.getProperty(%22folderKey%22);%0A    %7D%0A%0A
     public Strin
@@ -2037,499 +2037,5 @@
  %7D%0A%0A
-    // Returns a folder instance from a given Entity%0A    public static Folder EntityToFolder(Entity entity) %7B%0A        %0A        String folderName = (String) entity.getProperty(%22folderName%22);%0A        String folderDefaultLanguage = (String) entity.getProperty(%22folderDefaultLanguage%22);%0A        String folderKey = (String) entity.getProperty(%22folderKey%22);%0A%0A        Folder folder = new Folder(folderName, folderDefaultLanguage);%0A        folder.setFolderKey(folderKey);%0A%0A        return folder;%0A    %7D%0A
 %7D

Add Folder constructor with Entity