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 Folder(Entity entity) {
        this.folderName = (String) entity.getProperty("folderName");
        this.folderDefaultLanguage = (String) entity.getProperty("folderDefaultLanguage");
        this.folderKey = (String) entity.getProperty("folderKey");
    }

    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;
    }

}
@@ -1523,269 +1523,8 @@
 );%0A%0A
-        // Store initial folder entity without properties to generate auto ID for entity%0A        // This is necessary to obtain a %22complete%22 key%0A        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A        datastore.put(folder);%0A%0A
     
@@ -1662,90 +1662,8 @@
 ge);
-%0A        folder.setProperty(%22folderKey%22, KeyFactory.keyToString(folder.getKey()));
 %0A%0A  

Remove side effect of createEntity()